getch 0.0.9 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +45 -0
  5. data/README.md +57 -11
  6. data/bin/setup.sh +4 -2
  7. data/lib/getch.rb +27 -10
  8. data/lib/getch/command.rb +25 -4
  9. data/lib/getch/config.rb +58 -0
  10. data/lib/getch/filesystem.rb +6 -0
  11. data/lib/getch/filesystem/clean.rb +58 -0
  12. data/lib/getch/filesystem/device.rb +61 -0
  13. data/lib/getch/filesystem/ext4/config.rb +8 -9
  14. data/lib/getch/filesystem/ext4/device.rb +2 -7
  15. data/lib/getch/filesystem/ext4/encrypt/config.rb +8 -9
  16. data/lib/getch/filesystem/ext4/encrypt/deps.rb +3 -19
  17. data/lib/getch/filesystem/ext4/encrypt/device.rb +3 -8
  18. data/lib/getch/filesystem/ext4/encrypt/format.rb +3 -5
  19. data/lib/getch/filesystem/ext4/encrypt/mount.rb +6 -43
  20. data/lib/getch/filesystem/ext4/encrypt/partition.rb +19 -38
  21. data/lib/getch/filesystem/ext4/format.rb +3 -5
  22. data/lib/getch/filesystem/ext4/mount.rb +7 -46
  23. data/lib/getch/filesystem/ext4/partition.rb +16 -39
  24. data/lib/getch/filesystem/lvm/config.rb +11 -15
  25. data/lib/getch/filesystem/lvm/deps.rb +5 -20
  26. data/lib/getch/filesystem/lvm/device.rb +33 -9
  27. data/lib/getch/filesystem/lvm/encrypt/config.rb +9 -12
  28. data/lib/getch/filesystem/lvm/encrypt/deps.rb +5 -22
  29. data/lib/getch/filesystem/lvm/encrypt/device.rb +33 -9
  30. data/lib/getch/filesystem/lvm/encrypt/format.rb +3 -3
  31. data/lib/getch/filesystem/lvm/encrypt/mount.rb +7 -46
  32. data/lib/getch/filesystem/lvm/encrypt/partition.rb +20 -31
  33. data/lib/getch/filesystem/lvm/format.rb +11 -7
  34. data/lib/getch/filesystem/lvm/mount.rb +7 -46
  35. data/lib/getch/filesystem/lvm/partition.rb +19 -31
  36. data/lib/getch/filesystem/mount.rb +56 -0
  37. data/lib/getch/filesystem/partition.rb +77 -0
  38. data/lib/getch/filesystem/zfs.rb +14 -0
  39. data/lib/getch/filesystem/zfs/config.rb +57 -0
  40. data/lib/getch/filesystem/zfs/deps.rb +95 -0
  41. data/lib/getch/filesystem/zfs/device.rb +58 -0
  42. data/lib/getch/filesystem/zfs/encrypt.rb +15 -0
  43. data/lib/getch/filesystem/zfs/encrypt/config.rb +67 -0
  44. data/lib/getch/filesystem/zfs/encrypt/deps.rb +97 -0
  45. data/lib/getch/filesystem/zfs/encrypt/device.rb +60 -0
  46. data/lib/getch/filesystem/zfs/encrypt/format.rb +104 -0
  47. data/lib/getch/filesystem/zfs/encrypt/mount.rb +51 -0
  48. data/lib/getch/filesystem/zfs/encrypt/partition.rb +66 -0
  49. data/lib/getch/filesystem/zfs/format.rb +113 -0
  50. data/lib/getch/filesystem/zfs/mount.rb +48 -0
  51. data/lib/getch/filesystem/zfs/partition.rb +65 -0
  52. data/lib/getch/gentoo.rb +11 -4
  53. data/lib/getch/gentoo/boot.rb +34 -16
  54. data/lib/getch/gentoo/chroot.rb +16 -25
  55. data/lib/getch/gentoo/config.rb +59 -7
  56. data/lib/getch/gentoo/sources.rb +54 -26
  57. data/lib/getch/gentoo/use.rb +43 -0
  58. data/lib/getch/gentoo/use_flag.rb +64 -0
  59. data/lib/getch/guard.rb +62 -0
  60. data/lib/getch/helpers.rb +31 -13
  61. data/lib/getch/options.rb +24 -9
  62. data/lib/getch/version.rb +1 -1
  63. metadata +40 -18
  64. metadata.gz.sig +0 -0
@@ -7,6 +7,12 @@ module Getch
7
7
  mount
8
8
  end
9
9
 
10
+ def cpuflags
11
+ Getch::Emerge.new("app-portage/cpuid2cpuflags").pkg!
12
+ cpuflags = `chroot #{MOUNTPOINT} /bin/bash -c "source /etc/profile; cpuid2cpuflags"`.chomp
13
+ File.write("#{MOUNTPOINT}/etc/portage/package.use/00cpuflags", "*/* #{cpuflags}")
14
+ end
15
+
10
16
  def update
11
17
  return if STATES[:gentoo_update]
12
18
  puts "Downloading the last ebuilds for Gentoo..."
@@ -18,7 +24,7 @@ module Getch
18
24
  def world
19
25
  return if STATES[:gentoo_update]
20
26
  puts "Update Gentoo world"
21
- Getch::Emerge.new("emerge --update --deep --newuse @world").run!
27
+ Getch::Emerge.new("emerge --update --deep --changed-use --newuse @world").run!
22
28
  @state.update
23
29
  end
24
30
 
@@ -33,40 +39,29 @@ module Getch
33
39
  license = "#{MOUNTPOINT}/etc/portage/package.license"
34
40
  File.write(license, "sys-kernel/linux-firmware linux-fw-redistributable no-source-code\n")
35
41
  @pkgs << "sys-kernel/gentoo-sources"
36
- @pkgs << "dev-util/dwarves"
37
42
  end
38
43
 
39
44
  def kernel_deps
40
- puts "Installing Garden..."
41
- get_garden
42
- garden_dep
45
+ @pkgs << "sys-apps/kmod"
43
46
  end
44
47
 
45
48
  def install_pkgs
49
+ @pkgs << "app-portage/gentoolkit"
46
50
  @pkgs << "app-admin/sudo"
47
51
  @pkgs << "app-editors/vim"
52
+ @pkgs << "sys-kernel/linux-firmware"
48
53
  all_pkgs = @pkgs.join(" ")
49
54
  puts "Installing #{all_pkgs}..."
50
55
  Getch::Emerge.new(all_pkgs).pkg!
51
56
  end
52
57
 
53
- private
54
-
55
- def get_garden
56
- return if Dir.exist? "#{MOUNTPOINT}/root/garden-master"
57
- url = 'https://github.com/szorfein/garden/archive/master.tar.gz'
58
- file = 'garden-master.tar.gz'
59
-
60
- Dir.chdir("#{MOUNTPOINT}/root")
61
- Helpers::get_file_online(url, file)
62
- Getch::Command.new("tar xzf #{file}").run! if ! Dir.exist? 'garden-master'
58
+ # create a symbolic link for /usr/src/linux
59
+ def kernel_link
60
+ cmd = "eselect kernel set 1"
61
+ exec_chroot(cmd)
63
62
  end
64
63
 
65
- def garden_dep
66
- Getch::Emerge.new("gentoolkit").pkg!
67
- exec_chroot("euse -p sys-apps/kmod -E lzma")
68
- @pkgs << "sys-apps/kmod"
69
- end
64
+ private
70
65
 
71
66
  def mount
72
67
  puts "Populate /proc, /sys and /dev."
@@ -80,11 +75,7 @@ module Getch
80
75
  end
81
76
 
82
77
  def exec_chroot(cmd)
83
- script = "chroot #{MOUNTPOINT} /bin/bash -c \"
84
- source /etc/profile
85
- #{cmd}
86
- \""
87
- Getch::Command.new(script).run!
78
+ Getch::Chroot.new(cmd).run!
88
79
  end
89
80
  end
90
81
  end
@@ -7,17 +7,35 @@ module Getch
7
7
  class Config
8
8
  def initialize
9
9
  @make = "#{MOUNTPOINT}/etc/portage/make.conf"
10
+ @log = Getch::Log.new
10
11
  end
11
12
 
12
13
  def portage
13
- nproc = `nproc`.chomp()
14
14
  grub_pc = Helpers::efi? ? '' : 'GRUB_PLATFORMS="pc"'
15
- quiet = DEFAULT_OPTIONS[:verbose] ? '' : "EMERGE_DEFAULT_OPTS=\"--jobs=#{nproc} --load-average=#{nproc}\""
15
+ nproc = `nproc`.chomp()
16
+
17
+ # Add cpu name
18
+ cpu=`chroot #{MOUNTPOINT} /bin/bash -c \"source /etc/profile ; gcc -c -Q -march=native --help=target | grep march\" | awk '{print $2}' | head -1`.chomp
19
+ raise "Error, no cpu found" if ! cpu or cpu == ""
20
+ @log.debug "CPU found ==> #{cpu}"
21
+
22
+ tmp = Tempfile.new('make.conf')
23
+
24
+ File.open(@make).each { |l|
25
+ if l.match(/^COMMON_FLAGS/)
26
+ File.write(tmp, "COMMON_FLAGS=\"-march=#{cpu} -O2 -pipe -fomit-frame-pointer\"\n", mode: 'a')
27
+ else
28
+ File.write(tmp, l, mode: 'a')
29
+ end
30
+ }
31
+
32
+ FileUtils.copy_file(tmp, @make, preserve = true)
33
+
34
+ # Add the rest
16
35
  data = [
17
36
  '',
37
+ "MAKEOPTS=\"-j#{nproc}\"",
18
38
  'ACCEPT_KEYWORDS="amd64"',
19
- "MAKEOPTS=\"-j#{nproc} -l#{nproc}\"",
20
- quiet,
21
39
  'INPUT_DEVICES="libinput"',
22
40
  grub_pc
23
41
  ]
@@ -42,13 +60,13 @@ module Getch
42
60
  line_count += 1
43
61
  }
44
62
 
45
- FileUtils.copy_file(tmp, "#{dest}/gentoo.conf", preserve = false)
63
+ FileUtils.copy_file(tmp, "#{dest}/gentoo.conf", preserve = true)
46
64
  end
47
65
 
48
66
  def network
49
67
  src = '/etc/resolv.conf'
50
68
  dest = "#{MOUNTPOINT}/etc/resolv.conf"
51
- FileUtils.copy_file(src, dest, preserve = false)
69
+ FileUtils.copy_file(src, dest, preserve = true)
52
70
  end
53
71
 
54
72
  def systemd(options)
@@ -76,6 +94,40 @@ module Getch
76
94
  Helpers::add_file("#{portage}/package.unmask/zzz_via_autounmask")
77
95
  end
78
96
 
97
+ def portage_bashrc
98
+ conf = "#{MOUNTPOINT}/etc/portage/bashrc"
99
+ content = %q{
100
+ # https://wiki.gentoo.org/wiki/Signed_kernel_module_support
101
+ function pre_pkg_preinst() {
102
+ # This hook signs any out-of-tree kernel modules.
103
+ if [[ "$(type -t linux-mod_pkg_preinst)" != "function" ]]; then
104
+ # The package does not seem to install any kernel modules.
105
+ return
106
+ fi
107
+ # Get the signature algorithm used by the kernel.
108
+ local module_sig_hash="$(grep -Po '(?<=CONFIG_MODULE_SIG_HASH=").*(?=")' "${KERNEL_DIR}/.config")"
109
+ # Get the key file used by the kernel.
110
+ local module_sig_key="$(grep -Po '(?<=CONFIG_MODULE_SIG_KEY=").*(?=")' "${KERNEL_DIR}/.config")"
111
+ module_sig_key="${module_sig_key:-certs/signing_key.pem}"
112
+ # Path to the key file or PKCS11 URI
113
+ if [[ "${module_sig_key#pkcs11:}" == "${module_sig_key}" && "${module_sig_key#/}" == "${module_sig_key}" ]]; then
114
+ local key_path="${KERNEL_DIR}/${module_sig_key}"
115
+ else
116
+ local key_path="${module_sig_key}"
117
+ fi
118
+ # Certificate path
119
+ local cert_path="${KERNEL_DIR}/certs/signing_key.x509"
120
+ # Sign all installed modules before merging.
121
+ find "${D%/}/${INSDESTTREE#/}/" -name "*.ko" -exec "${KERNEL_DIR}/scripts/sign-file" "${module_sig_hash}" "${key_path}" "${cert_path}" '{}' \;
122
+ }
123
+ }
124
+
125
+ f = File.new(conf, "w")
126
+ f.write("#{content}\n")
127
+ f.chmod(0644)
128
+ f.close
129
+ end
130
+
79
131
  private
80
132
 
81
133
  def control_options(options)
@@ -93,7 +145,7 @@ module Getch
93
145
  end
94
146
 
95
147
  def search_zone(zone)
96
- if ! File.exist?("#{MOUNTPOINT}/usr/share/zoneinfo/#{zone}")
148
+ if !File.exist?("#{MOUNTPOINT}/usr/share/zoneinfo/#{zone}")
97
149
  raise ArgumentError, "Zoneinfo #{zone} doesn\'t exist."
98
150
  end
99
151
  end
@@ -7,37 +7,35 @@ module Getch
7
7
  end
8
8
 
9
9
  def build_others
10
- install_zfs if ismatch?('zfs')
10
+ cryptsetup
11
11
  virtualbox_guest
12
12
  qemu_guest
13
13
  install_wifi
14
14
  install_audio
15
+ flash_mod
15
16
  end
16
17
 
17
18
  def build_kspp
18
19
  puts "Adding KSPP to the kernel source"
19
- garden("-b -a systemd")
20
+ bask("-b -a systemd")
20
21
  end
21
22
 
22
23
  def make
23
- if DEFAULT_OPTIONS[:fs] == 'lvm' or DEFAULT_OPTIONS[:encrypt]
24
+ if DEFAULT_OPTIONS[:fs] == 'lvm' || DEFAULT_OPTIONS[:fs] == 'zfs' || DEFAULT_OPTIONS[:encrypt]
24
25
  @filesystem.make
25
26
  else
26
- just_make
27
+ make_kernel
27
28
  end
28
29
  end
29
30
 
30
- def init_config
31
- Getch::Make.new("make localyesconfig").run!
31
+ def firewall
32
+ bask("-a iptables")
33
+ Getch::Emerge.new("net-firewall/iptables").pkg!
32
34
  end
33
35
 
34
36
  private
35
37
 
36
- def only_make
37
- Getch::Make.new("make -j$(nproc)").run!
38
- end
39
-
40
- def just_make
38
+ def make_kernel
41
39
  puts "Compiling kernel sources"
42
40
  cmd = "make -j$(nproc) && make modules_install && make install"
43
41
  Getch::Make.new(cmd).run!
@@ -45,46 +43,76 @@ module Getch
45
43
  raise "No kernel installed, compiling source fail..." if is_kernel == []
46
44
  end
47
45
 
46
+ def cryptsetup
47
+ return unless DEFAULT_OPTIONS[:encrypt]
48
+ make_conf = "#{MOUNTPOINT}/etc/portage/make.conf"
49
+
50
+ puts "Adding support for cryptsetup."
51
+ bask("-a cryptsetup")
52
+ Getch::Chroot.new("euse -E cryptsetup").run! unless Helpers::grep?(make_conf, /cryptsetup/)
53
+ Getch::Emerge.new("sys-fs/cryptsetup").pkg!
54
+ end
55
+
48
56
  def virtualbox_guest
49
- return if ! ismatch?('vmwgfx')
50
- garden("-a virtualbox-guest")
57
+ systemd=`systemd-detect-virt`.chomp
58
+ return if ! ismatch?('vmwgfx') || systemd.match(/none/)
59
+ bask("-a virtualbox-guest")
51
60
  Getch::Emerge.new("app-emulation/virtualbox-guest-additions").pkg!
52
61
  end
53
62
 
54
63
  def qemu_guest
55
- garden("-a kvm-guest") if ismatch?('virtio')
56
- garden("-a kvm") if ismatch?('kvm')
64
+ bask("-a kvm-host") if ismatch?('kvm')
65
+ bask("-a kvm-guest") if ismatch?('virtio')
57
66
  end
58
67
 
59
68
  def ismatch?(arg)
60
69
  @lsmod.match?(/#{arg}/)
61
70
  end
62
71
 
63
- def garden(cmd)
64
- Getch::Garden.new(cmd).run!
72
+ def bask(cmd)
73
+ Getch::Bask.new(cmd).run!
65
74
  end
66
75
 
67
76
  def install_wifi
68
77
  return if ! ismatch?('cfg80211')
69
- garden("-a wifi")
78
+ bask("-a wifi")
70
79
  wifi_drivers
71
80
  Getch::Emerge.new("net-wireless/iw wpa_supplicant net-wireless/iwd").pkg!
72
81
  end
73
82
 
74
83
  def install_audio
75
84
  return if ! ismatch?('snd_pcm')
76
- garden("-a sound")
85
+ bask("-a sound")
77
86
  end
78
87
 
79
- def install_zfs
80
- return if ! DEFAULT_OPTIONS[:fs] == 'zfs'
81
- garden("-a zfs")
82
- only_make # a first 'make' is necessary before emerge zfs
83
- Getch::Emerge.new("sys-fs/zfs").pkg!
88
+ def wifi_drivers
89
+ conf = "#{MOUNTPOINT}/etc/modules-load.d/wifi.conf"
90
+ File.delete(conf) if File.exists? conf
91
+
92
+ if ismatch?('ath9k')
93
+ bask("-a ath9k-driver")
94
+ end
95
+
96
+ module_load("iwlmvm", conf)
97
+ module_load("ath9k", conf)
84
98
  end
85
99
 
86
- def wifi_drivers
87
- garden("-a ath9k-driver") if ismatch?('ath9k')
100
+ def flash_mod
101
+ conf = "#{MOUNTPOINT}/etc/modules-load.d/usb.conf"
102
+ File.delete(conf) if File.exists? conf
103
+
104
+ module_load("ehci_pci", conf)
105
+ module_load("rtsx_pci_sdmmc", conf)
106
+ module_load("sdhci_pci", conf)
107
+ module_load("uas", conf)
108
+ module_load("uhci_hcd", conf)
109
+ module_load("xhci_pci", conf)
110
+ end
111
+
112
+ def module_load(name, file)
113
+ return unless name
114
+ return unless ismatch?(name)
115
+ File.write(file, "#{name}\n", mode: 'a')
88
116
  end
89
117
  end
90
118
  end
@@ -0,0 +1,43 @@
1
+ module Getch
2
+ module Gentoo
3
+ class Use
4
+ def initialize(pkg = nil)
5
+ @use_dir = "#{MOUNTPOINT}/etc/portage/package.use"
6
+ @pkg = pkg
7
+ @file = @pkg ? @pkg.match(/[\w]+$/) : nil
8
+ @make = "#{MOUNTPOINT}/etc/portage/make.conf"
9
+ end
10
+
11
+ def add(*flags)
12
+ @flags = flags.join(' ')
13
+ write
14
+ end
15
+
16
+ def add_global(*flags)
17
+ @flags = flags
18
+ write_global
19
+ end
20
+
21
+ private
22
+
23
+ def write
24
+ content = "#{@pkg} #{@flags}\n"
25
+ File.write("#{@use_dir}/#{@file}", content, mode: 'w')
26
+ end
27
+
28
+ def write_global
29
+ list = []
30
+
31
+ @flags.each { |f|
32
+ unless Helpers::grep?(@make, /#{f}/)
33
+ list << f
34
+ end
35
+ }
36
+
37
+ use = list.join(' ')
38
+ line = "USE=\"${USE} #{use}\"\n"
39
+ File.write(@make, line, mode: 'a')
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,64 @@
1
+ # lib/use_flag.rb
2
+
3
+ module Getch::Gentoo
4
+ class UseFlag
5
+ def initialize(options)
6
+ @efi = Helpers::efi?
7
+ @o = options
8
+ end
9
+
10
+ def apply
11
+ systemd
12
+ kmod
13
+ grub
14
+ zfs
15
+ lvm
16
+ cryptsetup
17
+ end
18
+
19
+ private
20
+
21
+ def systemd
22
+ flags = []
23
+ use = Getch::Gentoo::Use.new('sys-apps/systemd')
24
+ flags << 'dns-over-tls'
25
+ flags << 'gnuefi' if @efi
26
+ use.add(flags)
27
+ end
28
+
29
+ def kmod
30
+ use = Getch::Gentoo::Use.new('sys-apps/kmod')
31
+ use.add('zstd', 'lzma')
32
+ end
33
+
34
+ def grub
35
+ return if @efi
36
+ flags = []
37
+ use = Getch::Gentoo::Use.new('sys-boot/grub')
38
+ flags << '-grub_platforms_efi-64'
39
+ flags << 'libzfs' if @o.fs == 'zfs'
40
+ flags << 'device-mapper' if @o.fs == 'lvm'
41
+ use.add(flags)
42
+ end
43
+
44
+ def zfs
45
+ return unless @o.fs == 'zfs'
46
+ use = Getch::Gentoo::Use.new('sys-fs/zfs-kmod')
47
+ use.add('rootfs')
48
+ use = Getch::Gentoo::Use.new('sys-fs/zfs')
49
+ use.add('rootfs')
50
+ end
51
+
52
+ def lvm
53
+ return unless @o.fs == 'lvm'
54
+ use = Getch::Gentoo::Use.new
55
+ use.add_global('lvm', 'device-mapper')
56
+ end
57
+
58
+ def cryptsetup
59
+ return unless @o.encrypt
60
+ use = Getch::Gentoo::Use.new
61
+ use.add_global('cryptsetup')
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,62 @@
1
+ class InvalidDisk < StandardError
2
+ end
3
+
4
+ class InvalidFormat < StandardError
5
+ end
6
+
7
+ class InvalidZone < StandardError
8
+ end
9
+
10
+ class InvalidKeymap < StandardError
11
+ end
12
+
13
+ module Getch::Guard
14
+ def self.disk(name)
15
+ raise InvalidDisk, "No disk." unless name
16
+ raise InvalidDisk, "Bad device name #{name}." unless name.match(/^sd[a-z]{1}$/)
17
+ raise InvalidDisk, "Disk /dev/#{name} no found." unless File.exist? "/dev/#{name}"
18
+ name
19
+ rescue InvalidDisk => e
20
+ puts "#{e.class} => #{e}"
21
+ exit 1
22
+ end
23
+
24
+ def self.format(name)
25
+ raise InvalidFormat, "No format specified." unless name
26
+ raise InvalidFormat, "Format #{name} not yet available." if name.match(/btrfs/)
27
+ raise InvalidFormat, "Format #{name} not supported." unless name.match(/zfs|lvm|zfs/)
28
+ rescue InvalidFormat => e
29
+ puts "#{e.class} => #{e}"
30
+ exit 1
31
+ end
32
+
33
+ def self.zone(name)
34
+ raise InvalidZone, "No zoneinfo specified." unless name
35
+ raise InvalidZone, "Directory /usr/share/zoneinfo/ no found on this system..." unless Dir.exist? "/usr/share/zoneinfo/"
36
+ raise InvalidZone, "Zoneinfo #{name} is no found in /usr/share/zoneinfo/." unless File.exist? "/usr/share/zoneinfo/#{name}"
37
+ rescue InvalidZone => e
38
+ puts "#{e.class} => #{e}"
39
+ exit 1
40
+ end
41
+
42
+ def self.keymap(name)
43
+ raise InvalidKeymap, "No keymap specified." unless name
44
+ key = []
45
+
46
+ if Dir.exist? '/usr/share/keymaps'
47
+ key = Dir.glob("/usr/share/keymaps/**/#{name}.map.gz")
48
+ elsif Dir.exist? '/usr/share/kbd/keymaps'
49
+ key = Dir.glob("/usr/share/kbd/keymaps/**/#{name}.map.gz")
50
+ else
51
+ raise InvalidKeymap, "No directory found for keymap."
52
+ end
53
+
54
+ if key == []
55
+ raise InvalidKeymap, "Keymap #{name} no found."
56
+ end
57
+ name
58
+ rescue InvalidKeymap => e
59
+ puts "#{e.class} => #{e}"
60
+ exit 1
61
+ end
62
+ end