getch 0.0.2 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ require 'fileutils'
2
+
3
+ module Getch
4
+ module Gentoo
5
+ class Boot
6
+ def initialize(opts)
7
+ @disk = opts.disk
8
+ @user = opts.username
9
+ @config = OPTIONS_FS[DEFAULT_OPTIONS[:fs]]::Config.new()
10
+ end
11
+
12
+ def start
13
+ @config.fstab
14
+ OPTIONS_FS[DEFAULT_OPTIONS[:fs]]::Deps.new()
15
+ bootloader
16
+ password
17
+ umount
18
+ end
19
+
20
+ def bootloader
21
+ if Helpers::efi?
22
+ bootctl
23
+ else
24
+ grub
25
+ end
26
+ end
27
+
28
+ # bootctl is alrealy installed with the stage3-amd64-systemd
29
+ def bootctl
30
+ bootctl_dep
31
+ puts "Configuring systemd-boot."
32
+ # ref: https://forums.gentoo.org/viewtopic-p-8118822.html
33
+ esp = '/boot/efi'
34
+ exec_chroot("bootctl --path #{esp} install")
35
+ datas_loader = [
36
+ 'default gentoo',
37
+ 'timeout 3',
38
+ 'editor 0'
39
+ ]
40
+ @config.systemd_boot
41
+ File.write("#{MOUNTPOINT}/#{esp}/loader/loader.conf", datas_loader.join("\n"))
42
+
43
+ FileUtils.cp("#{MOUNTPOINT}/usr/src/linux/arch/x86/boot/bzImage", "#{MOUNTPOINT}/#{esp}/vmlinuz", preserve: true)
44
+ exec_chroot("bootctl --path #{esp} update")
45
+ end
46
+
47
+ def bootctl_dep
48
+ puts 'Installing systemd-boot...'
49
+ exec_chroot("euse -p sys-apps/systemd -E gnuefi")
50
+ Helpers::emerge("sys-apps/systemd efivar", MOUNTPOINT)
51
+ end
52
+
53
+ def grub
54
+ puts 'Installing GRUB...'
55
+ Helpers::emerge("sys-boot/grub:2", MOUNTPOINT)
56
+ @config.grub
57
+ exec_chroot("grub-install /dev/#{@disk}")
58
+ exec_chroot('grub-mkconfig -o /boot/grub/grub.cfg')
59
+ end
60
+
61
+ def password
62
+ puts 'Password for root'
63
+ cmd = "chroot #{MOUNTPOINT} /bin/bash -c \"source /etc/profile && passwd\""
64
+ system(cmd)
65
+ if @user != nil
66
+ puts "Creating user #{@user}"
67
+ exec_chroot("useradd -m -G users,wheel,audio,video #{@user}")
68
+ puts "Password for your user #{@user}"
69
+ cmd = "chroot #{MOUNTPOINT} /bin/bash -c \"source /etc/profile && passwd #{@user}\""
70
+ system(cmd)
71
+ end
72
+ end
73
+
74
+ def umount
75
+ Helpers::exec_or_die("umount -l /mnt/gentoo/dev{/shm,/pts,}")
76
+ Helpers::exec_or_die("umount -R #{MOUNTPOINT}")
77
+ puts "Reboot when you have done"
78
+ end
79
+
80
+ private
81
+
82
+ def exec_chroot(cmd)
83
+ script = "chroot #{MOUNTPOINT} /bin/bash -c \"
84
+ source /etc/profile
85
+ #{cmd}
86
+ \""
87
+ Getch::Command.new(script).run!
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,90 @@
1
+ module Getch
2
+ module Gentoo
3
+ class Chroot
4
+ def initialize
5
+ @state = Getch::States.new()
6
+ @pkgs = []
7
+ mount
8
+ end
9
+
10
+ def update
11
+ return if STATES[:gentoo_update]
12
+ puts "Downloading the last ebuilds for Gentoo..."
13
+ cmd = "emerge-webrsync"
14
+ exec_chroot(cmd)
15
+ end
16
+
17
+ def world
18
+ return if STATES[:gentoo_update]
19
+ puts "Update Gentoo"
20
+ cmd = "emerge --update --deep --newuse @world"
21
+ exec_chroot(cmd)
22
+ @state.update
23
+ end
24
+
25
+ def systemd
26
+ puts "Updating locale, keymap..."
27
+ cmd = "locale-gen; emerge --config sys-libs/timezone-data"
28
+ exec_chroot(cmd)
29
+ end
30
+
31
+ def kernel
32
+ return if Dir.exist? "#{MOUNTPOINT}/usr/src/linux"
33
+ puts "Installing kernel gentoo-sources..."
34
+ license = "#{MOUNTPOINT}/etc/portage/package.license"
35
+ File.write(license, "sys-kernel/linux-firmware linux-fw-redistributable no-source-code\n")
36
+ @pkgs << "sys-kernel/gentoo-sources"
37
+ @pkgs << "dev-util/dwarves"
38
+ end
39
+
40
+ def kernel_deps
41
+ puts "Installing Garden..."
42
+ get_garden
43
+ garden_dep
44
+ end
45
+
46
+ def install_pkgs
47
+ @pkgs << "app-admin/sudo"
48
+ @pkgs << "app-editors/vim"
49
+ Helpers::emerge(@pkgs.join(" "), MOUNTPOINT)
50
+ end
51
+
52
+ private
53
+
54
+ def get_garden
55
+ return if Dir.exist? "#{MOUNTPOINT}/root/garden-master"
56
+ url = 'https://github.com/szorfein/garden/archive/master.tar.gz'
57
+ file = 'garden-master.tar.gz'
58
+
59
+ Dir.chdir("#{MOUNTPOINT}/root")
60
+ Helpers::get_file_online(url, file)
61
+ Getch::Command.new("tar xzf #{file}").run! if ! Dir.exist? 'garden-master'
62
+ end
63
+
64
+ def garden_dep
65
+ Helpers::emerge("gentoolkit", MOUNTPOINT)
66
+ exec_chroot("euse -p sys-apps/kmod -E lzma")
67
+ @pkgs << "sys-apps/kmod"
68
+ end
69
+
70
+ def mount
71
+ puts "Populate /proc, /sys and /dev."
72
+ Helpers::exec_or_die("mount --types proc /proc \"#{MOUNTPOINT}/proc\"")
73
+ Helpers::exec_or_die("mount --rbind /sys \"#{MOUNTPOINT}/sys\"")
74
+ Helpers::exec_or_die("mount --make-rslave \"#{MOUNTPOINT}/sys\"")
75
+ Helpers::exec_or_die("mount --rbind /dev \"#{MOUNTPOINT}/dev\"")
76
+ Helpers::exec_or_die("mount --make-rslave \"#{MOUNTPOINT}/dev\"")
77
+ # Maybe add /dev/shm like describe here:
78
+ # https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Base
79
+ end
80
+
81
+ def exec_chroot(cmd)
82
+ script = "chroot #{MOUNTPOINT} /bin/bash -c \"
83
+ source /etc/profile
84
+ #{cmd}
85
+ \""
86
+ Getch::Command.new(script).run!
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,102 @@
1
+ require 'fileutils'
2
+ require 'tempfile'
3
+ require 'securerandom'
4
+
5
+ module Getch
6
+ module Gentoo
7
+ class Config
8
+ def initialize
9
+ @make = "#{MOUNTPOINT}/etc/portage/make.conf"
10
+ end
11
+
12
+ def portage
13
+ nproc = `nproc`.chomp()
14
+ grub_pc = Helpers::efi? ? '' : 'GRUB_PLATFORMS="pc"'
15
+ data = [
16
+ '',
17
+ 'ACCEPT_KEYWORD="amd64 ~amd64"',
18
+ "MAKEOPTS=\"-j#{nproc} -l#{nproc}\"",
19
+ 'INPUT_DEVICES="libinput"',
20
+ grub_pc
21
+ ]
22
+ File.write(@make, data.join("\n"), mode: "a")
23
+ end
24
+
25
+ def repo
26
+ src = "#{MOUNTPOINT}/usr/share/portage/config/repos.conf"
27
+ dest = "#{MOUNTPOINT}/etc/portage/repos.conf"
28
+ FileUtils.mkdir dest, mode: 0644 if ! Dir.exist?(dest)
29
+ line_count = 0
30
+ tmp_file = Tempfile.new('gentoo.conf')
31
+ File.open(src).each { |l|
32
+ File.write(tmp_file, "sync-allow-hardlinks = yes\n", mode: 'a') if line_count == 2
33
+ File.write(tmp_file, l, mode: 'a')
34
+ line_count += 1
35
+ }
36
+ FileUtils.copy_file(tmp_file, "#{dest}/gentoo.conf", preserve = false)
37
+ end
38
+
39
+ def network
40
+ src = '/etc/resolv.conf'
41
+ dest = "#{MOUNTPOINT}/etc/resolv.conf"
42
+ FileUtils.copy_file(src, dest, preserve = false)
43
+ end
44
+
45
+ def systemd(options)
46
+ control_options(options)
47
+ File.write("#{MOUNTPOINT}/etc/locale.gen", @utf8)
48
+ File.write("#{MOUNTPOINT}/etc/locale.conf", "LANG=#{@lang}\n")
49
+ File.write("#{MOUNTPOINT}/etc/locale.conf", 'LC_COLLATE=C', mode: 'a')
50
+ File.write("#{MOUNTPOINT}/etc/timezone", "#{options.zoneinfo}")
51
+ File.write("#{MOUNTPOINT}/etc/vconsole.conf", "KEYMAP=#{options.keyboard}")
52
+ end
53
+
54
+ def hostname
55
+ id = SecureRandom.hex(2)
56
+ File.write("#{MOUNTPOINT}/etc/hostname", "gentoo-hatch-#{id}")
57
+ end
58
+
59
+ def portage_fs
60
+ portage = "#{MOUNTPOINT}/etc/portage"
61
+ Helpers::create_dir("#{portage}/package.use")
62
+ Helpers::create_dir("#{portage}/package.accept_keywords")
63
+ Helpers::create_dir("#{portage}/package.unmask")
64
+
65
+ Helpers::add_file("#{portage}/package.use/zzz_via_autounmask")
66
+ Helpers::add_file("#{portage}/package.accept_keywords/zzz_via_autounmask")
67
+ Helpers::add_file("#{portage}/package.unmask/zzz_via_autounmask")
68
+ end
69
+
70
+ private
71
+
72
+ def control_options(options)
73
+ search_zone(options.zoneinfo)
74
+ search_utf8(options.language)
75
+ search_key(options.keyboard)
76
+ end
77
+
78
+ def search_key(keys)
79
+ @keymap = nil
80
+ Dir.glob("#{MOUNTPOINT}/usr/share/keymaps/**/#{keys}.map.gz") { |f|
81
+ @keymap = f
82
+ }
83
+ raise ArgumentError, "No keymap #{@keymap} found" if ! @keymap
84
+ end
85
+
86
+ def search_zone(zone)
87
+ if ! File.exist?("#{MOUNTPOINT}/usr/share/zoneinfo/#{zone}")
88
+ raise ArgumentError, "Zoneinfo #{zone} doesn\'t exist."
89
+ end
90
+ end
91
+
92
+ def search_utf8(lang)
93
+ @utf8, @lang = nil, nil
94
+ File.open("#{MOUNTPOINT}/usr/share/i18n/SUPPORTED").each { |l|
95
+ @utf8 = $~[0] if l.match(/^#{lang}[. ]+[utf\-8 ]+/i)
96
+ @lang = $~[0] if l.match(/^#{lang}[. ]+utf\-8/i)
97
+ }
98
+ raise ArgumentError, "Lang #{lang} no found" if ! @utf8
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,82 @@
1
+ module Getch
2
+ module Gentoo
3
+ class Sources
4
+ def initialize
5
+ @lsmod = `lsmod`.chomp
6
+ @linux = '/usr/src/linux'
7
+ end
8
+
9
+ def build_others
10
+ install_wifi if ismatch?('iwlwifi')
11
+ install_zfs if ismatch?('zfs')
12
+ virtualbox_guest
13
+ qemu_guest
14
+ end
15
+
16
+ def build_kspp
17
+ puts "Adding KSPP to the kernel source"
18
+ exec("./kernel.sh -b -a systemd -k #{@linux}")
19
+ end
20
+
21
+ def make
22
+ puts "Compiling kernel sources"
23
+ cmd = "cd #{@linux} && make -j$(nproc) && make modules_install && make install"
24
+ exec_chroot(cmd)
25
+ is_kernel = Dir.glob("#{MOUNTPOINT}/boot/vmlinuz-*")
26
+ raise "No kernel installed, compiling source fail..." if is_kernel == []
27
+ end
28
+
29
+ def only_make
30
+ exec_chroot("cd #{@linux} && make -j$(nproc)")
31
+ end
32
+
33
+ def init_config
34
+ exec_chroot("env-update && cd #{@linux} && make localyesconfig")
35
+ end
36
+
37
+ private
38
+
39
+ def virtualbox_guest
40
+ exec("./kernel.sh -b -a virtualbox-guest -k #{@linux}") if ismatch?('vmwgfx')
41
+ Helpers::emerge("app-emulation/virtualbox-guest-additions", MOUNTPOINT)
42
+ end
43
+
44
+ def qemu_guest
45
+ exec("./kernel.sh -a qemu-guest -k #{@linux}") if ismatch?('virtio')
46
+ exec("./kernel.sh -a kvm -k #{@linux}") if ismatch?('kvm')
47
+ end
48
+
49
+ def ismatch?(arg)
50
+ @lsmod.match?(/#{arg}/)
51
+ end
52
+
53
+ def exec(cmd)
54
+ script = "chroot #{MOUNTPOINT} /bin/bash -c \"
55
+ source /etc/profile
56
+ cd /root/garden-master
57
+ #{cmd}
58
+ \""
59
+ Getch::Command.new(script).run!
60
+ end
61
+
62
+ def exec_chroot(cmd)
63
+ script = "chroot #{MOUNTPOINT} /bin/bash -c \"
64
+ source /etc/profile
65
+ #{cmd}
66
+ \""
67
+ Getch::Command.new(script).run!
68
+ end
69
+
70
+ def install_wifi
71
+ exec("./kernel.sh -b -a wifi -k #{@linux}")
72
+ Helpers::emerge("net-wireless/iw wpa_supplicant", MOUNTPOINT)
73
+ end
74
+
75
+ def install_zfs
76
+ exec("./kernel.sh -b -a zfs -k #{@linux}")
77
+ only_make # a first 'make' is necessary before emerge zfs
78
+ Helpers::emerge("zfs", MOUNTPOINT)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,74 @@
1
+ require 'open-uri'
2
+ require 'open3'
3
+
4
+ module Getch
5
+ module Gentoo
6
+ class Stage
7
+
8
+ def initialize
9
+ @mirror = "https://mirrors.soeasyto.com/distfiles.gentoo.org"
10
+ @release = release
11
+ @stage_file="stage3-amd64-systemd-#{@release}.tar.xz"
12
+ end
13
+
14
+ def stage3
15
+ @mirror + '/releases/amd64/autobuilds/latest-stage3-amd64-systemd.txt'
16
+ end
17
+
18
+ def release
19
+ URI.open(stage3) do |file|
20
+ file.read.match(/^[[:alnum:]]+/)
21
+ end
22
+ end
23
+
24
+ def file
25
+ "#{@release}/#{@stage_file}"
26
+ end
27
+
28
+ def get_stage3
29
+ Dir.chdir(MOUNTPOINT)
30
+ return if File.exist?(@stage_file)
31
+ puts "Download the last #{@stage_file}, please wait..."
32
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file, @stage_file)
33
+ end
34
+
35
+ def control_files
36
+ puts "Download the DIGESTS"
37
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file + ".DIGESTS", "#{@stage_file}.DIGESTS")
38
+ puts "Download the DIGESTS.asc"
39
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file + ".DIGESTS.asc", "#{@stage_file}.DIGESTS.asc")
40
+ puts "Download the CONTENTS.gz"
41
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file + ".CONTENTS.gz", "#{@stage_file}.CONTENTS.gz")
42
+ end
43
+
44
+ def checksum
45
+ puts 'Check the SHA512 checksum.'
46
+ command = "awk '/SHA512 HASH/{getline;print}' #{@stage_file}.DIGESTS.asc | sha512sum --check"
47
+ _, stderr, status = Open3.capture3(command)
48
+ if status.success? then
49
+ puts "Checksum is ok"
50
+ decompress
51
+ cleaning
52
+ else
53
+ cleaning
54
+ raise "Problem with the checksum, stderr\n#{stderr}"
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ # https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Stage
61
+ def decompress
62
+ puts "Decompressing archive #{@stage_file}..."
63
+ cmd = "tar xpvf #{@stage_file} --xattrs-include='*.*' --numeric-owner"
64
+ Getch::Command.new(cmd).run!
65
+ end
66
+
67
+ def cleaning
68
+ Dir.glob("stage3-amd64-systemd*").each do |f|
69
+ File.delete(f)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,49 @@
1
+ require 'open-uri'
2
+ require 'open3'
3
+ require 'fileutils'
4
+ require_relative 'command'
5
+
6
+ module Helpers
7
+ def self.efi?
8
+ Dir.exist? '/sys/firmware/efi/efivars'
9
+ end
10
+
11
+ def self.get_file_online(url, dest)
12
+ URI.open(url) do |l|
13
+ File.open(dest, "wb") do |f|
14
+ f.write(l.read)
15
+ end
16
+ end
17
+ end
18
+
19
+ def self.exec_or_die(cmd)
20
+ _, stderr, status = Open3.capture3(cmd)
21
+ unless status.success?
22
+ raise "Problem running #{cmd}, stderr was:\n#{stderr}"
23
+ end
24
+ end
25
+
26
+ def self.create_dir(path, perm = 0755)
27
+ FileUtils.mkdir_p path, mode: perm if ! Dir.exist?(path)
28
+ end
29
+
30
+ def self.add_file(path, content = '')
31
+ File.write path, content if ! File.exist? path
32
+ end
33
+
34
+ def self.emerge(pkgs, path)
35
+ cmd = "chroot #{path} /bin/bash -c \"
36
+ source /etc/profile
37
+ emerge --changed-use #{pkgs}
38
+ \""
39
+ Getch::Command.new(cmd).run!
40
+ end
41
+
42
+ def self.run_chroot(cmd, path)
43
+ script = "chroot #{path} /bin/bash -c \"
44
+ source /etc/profile
45
+ #{cmd}
46
+ \""
47
+ Getch::Command.new(script).run!
48
+ end
49
+ end