getch 0.1.9 → 0.3.0

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 (161) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +35 -29
  4. data/bin/getch +8 -6
  5. data/lib/clean.rb +149 -0
  6. data/lib/cryptsetup.rb +132 -0
  7. data/lib/devs.rb +199 -0
  8. data/lib/dracut/encrypt.rb +36 -0
  9. data/lib/dracut/hybrid.rb +15 -0
  10. data/lib/dracut/lvm.rb +14 -0
  11. data/lib/dracut/minimal.rb +11 -0
  12. data/lib/dracut/root.rb +45 -0
  13. data/lib/dracut/zfs.rb +35 -0
  14. data/lib/dracut.rb +11 -0
  15. data/lib/fstab/encrypt.rb +44 -0
  16. data/lib/fstab/hybrid.rb +34 -0
  17. data/lib/fstab/lvm.rb +25 -0
  18. data/lib/fstab/minimal.rb +6 -0
  19. data/lib/fstab/root.rb +93 -0
  20. data/lib/fstab/zfs.rb +23 -0
  21. data/lib/fstab.rb +11 -0
  22. data/lib/getch/assembly.rb +150 -0
  23. data/lib/getch/command.rb +88 -128
  24. data/lib/getch/config/account.rb +39 -0
  25. data/lib/getch/config/dhcp.rb +104 -0
  26. data/lib/getch/config/grub.rb +42 -0
  27. data/lib/getch/config/iwd.rb +60 -0
  28. data/lib/getch/config/keymap.rb +78 -0
  29. data/lib/getch/config/locale.rb +96 -0
  30. data/lib/getch/config/portage.rb +90 -0
  31. data/lib/getch/config/pre_network.rb +37 -0
  32. data/lib/getch/config/timezone.rb +52 -0
  33. data/lib/getch/config/void.rb +0 -36
  34. data/lib/getch/config.rb +16 -37
  35. data/lib/getch/device.rb +67 -0
  36. data/lib/getch/filesystem/ext4/encrypt/config.rb +9 -71
  37. data/lib/getch/filesystem/ext4/encrypt/deps.rb +15 -25
  38. data/lib/getch/filesystem/ext4/encrypt/device.rb +11 -5
  39. data/lib/getch/filesystem/ext4/encrypt/format.rb +6 -15
  40. data/lib/getch/filesystem/ext4/encrypt/mount.rb +7 -11
  41. data/lib/getch/filesystem/ext4/encrypt/partition.rb +10 -76
  42. data/lib/getch/filesystem/ext4/encrypt/void.rb +0 -38
  43. data/lib/getch/filesystem/ext4/encrypt.rb +2 -1
  44. data/lib/getch/filesystem/ext4/hybrid/config.rb +27 -0
  45. data/lib/getch/filesystem/ext4/hybrid/deps.rb +55 -0
  46. data/lib/getch/filesystem/ext4/hybrid/device.rb +24 -0
  47. data/lib/getch/filesystem/ext4/hybrid/format.rb +23 -0
  48. data/lib/getch/filesystem/ext4/hybrid/mount.rb +21 -0
  49. data/lib/getch/filesystem/ext4/hybrid/partition.rb +27 -0
  50. data/lib/getch/filesystem/{lvm/encrypt → ext4/hybrid}/void.rb +0 -39
  51. data/lib/getch/filesystem/ext4/hybrid.rb +19 -0
  52. data/lib/getch/filesystem/ext4/lvm/config.rb +25 -0
  53. data/lib/getch/filesystem/ext4/lvm/deps.rb +56 -0
  54. data/lib/getch/filesystem/ext4/lvm/device.rb +28 -0
  55. data/lib/getch/filesystem/ext4/lvm/format.rb +21 -0
  56. data/lib/getch/filesystem/ext4/lvm/mount.rb +21 -0
  57. data/lib/getch/filesystem/ext4/lvm/partition.rb +28 -0
  58. data/lib/getch/filesystem/{lvm.rb → ext4/lvm.rb} +6 -4
  59. data/lib/getch/filesystem/ext4/minimal/config.rb +25 -0
  60. data/lib/getch/filesystem/ext4/{device.rb → minimal/deps.rb} +3 -1
  61. data/lib/getch/filesystem/ext4/minimal/device.rb +22 -0
  62. data/lib/getch/filesystem/ext4/minimal/format.rb +23 -0
  63. data/lib/getch/filesystem/ext4/minimal/mount.rb +21 -0
  64. data/lib/getch/filesystem/ext4/minimal/partition.rb +28 -0
  65. data/lib/getch/filesystem/ext4/minimal.rb +19 -0
  66. data/lib/getch/filesystem/ext4.rb +7 -7
  67. data/lib/getch/filesystem/zfs/encrypt/config.rb +10 -39
  68. data/lib/getch/filesystem/zfs/encrypt/deps.rb +4 -55
  69. data/lib/getch/filesystem/zfs/encrypt/device.rb +7 -51
  70. data/lib/getch/filesystem/zfs/encrypt/format.rb +6 -90
  71. data/lib/getch/filesystem/zfs/encrypt/mount.rb +16 -35
  72. data/lib/getch/filesystem/zfs/encrypt/partition.rb +6 -54
  73. data/lib/getch/filesystem/zfs/encrypt.rb +2 -1
  74. data/lib/getch/filesystem/zfs/minimal/config.rb +37 -0
  75. data/lib/getch/filesystem/zfs/minimal/deps.rb +126 -0
  76. data/lib/getch/filesystem/zfs/minimal/device.rb +24 -0
  77. data/lib/getch/filesystem/zfs/minimal/format.rb +23 -0
  78. data/lib/getch/filesystem/zfs/minimal/mount.rb +23 -0
  79. data/lib/getch/filesystem/zfs/minimal/partition.rb +23 -0
  80. data/lib/getch/filesystem/zfs/minimal.rb +19 -0
  81. data/lib/getch/filesystem/zfs.rb +1 -7
  82. data/lib/getch/filesystem.rb +0 -6
  83. data/lib/getch/gentoo/bootloader.rb +23 -44
  84. data/lib/getch/gentoo/finalize.rb +25 -0
  85. data/lib/getch/gentoo/post_config.rb +75 -0
  86. data/lib/getch/gentoo/pre_config.rb +37 -0
  87. data/lib/getch/gentoo/services.rb +18 -0
  88. data/lib/getch/gentoo/sources.rb +39 -33
  89. data/lib/getch/gentoo/tarball.rb +91 -0
  90. data/lib/getch/gentoo/terraform.rb +34 -0
  91. data/lib/getch/gentoo/update.rb +54 -0
  92. data/lib/getch/gentoo/use.rb +9 -6
  93. data/lib/getch/gentoo/use_flag.rb +17 -20
  94. data/lib/getch/gentoo.rb +9 -75
  95. data/lib/getch/guard.rb +2 -2
  96. data/lib/getch/helpers.rb +63 -115
  97. data/lib/getch/log.rb +87 -25
  98. data/lib/getch/options.rb +41 -11
  99. data/lib/getch/states.rb +28 -8
  100. data/lib/getch/tree.rb +56 -0
  101. data/lib/getch/version.rb +1 -1
  102. data/lib/getch/void/bootloader.rb +18 -0
  103. data/lib/getch/void/finalize.rb +31 -0
  104. data/lib/getch/void/post_config.rb +19 -0
  105. data/lib/getch/void/pre_config.rb +18 -0
  106. data/lib/getch/void/services.rb +18 -0
  107. data/lib/getch/void/{stage.rb → tarball.rb} +34 -14
  108. data/lib/getch/void/terraform.rb +28 -0
  109. data/lib/getch/void/update.rb +33 -0
  110. data/lib/getch/void.rb +9 -59
  111. data/lib/getch.rb +58 -90
  112. data/lib/luks.rb +239 -0
  113. data/lib/lvm2.rb +112 -0
  114. data/lib/mkfs/zfs.rb +167 -0
  115. data/lib/mkfs.rb +140 -0
  116. data/lib/mountfs.rb +154 -0
  117. data/lib/nito.rb +131 -0
  118. data/lib/sgdisk.rb +160 -0
  119. data.tar.gz.sig +0 -0
  120. metadata +83 -47
  121. metadata.gz.sig +0 -0
  122. data/lib/getch/config/gentoo.rb +0 -58
  123. data/lib/getch/filesystem/clean.rb +0 -58
  124. data/lib/getch/filesystem/device.rb +0 -63
  125. data/lib/getch/filesystem/ext4/config.rb +0 -62
  126. data/lib/getch/filesystem/ext4/deps.rb +0 -24
  127. data/lib/getch/filesystem/ext4/format.rb +0 -31
  128. data/lib/getch/filesystem/ext4/mount.rb +0 -26
  129. data/lib/getch/filesystem/ext4/partition.rb +0 -55
  130. data/lib/getch/filesystem/ext4/void.rb +0 -44
  131. data/lib/getch/filesystem/lvm/config.rb +0 -44
  132. data/lib/getch/filesystem/lvm/deps.rb +0 -44
  133. data/lib/getch/filesystem/lvm/device.rb +0 -45
  134. data/lib/getch/filesystem/lvm/encrypt/config.rb +0 -74
  135. data/lib/getch/filesystem/lvm/encrypt/deps.rb +0 -49
  136. data/lib/getch/filesystem/lvm/encrypt/device.rb +0 -48
  137. data/lib/getch/filesystem/lvm/encrypt/format.rb +0 -35
  138. data/lib/getch/filesystem/lvm/encrypt/mount.rb +0 -27
  139. data/lib/getch/filesystem/lvm/encrypt/partition.rb +0 -84
  140. data/lib/getch/filesystem/lvm/encrypt.rb +0 -18
  141. data/lib/getch/filesystem/lvm/format.rb +0 -32
  142. data/lib/getch/filesystem/lvm/mount.rb +0 -26
  143. data/lib/getch/filesystem/lvm/partition.rb +0 -72
  144. data/lib/getch/filesystem/lvm/void.rb +0 -46
  145. data/lib/getch/filesystem/mount.rb +0 -63
  146. data/lib/getch/filesystem/partition.rb +0 -85
  147. data/lib/getch/filesystem/zfs/config.rb +0 -43
  148. data/lib/getch/filesystem/zfs/deps.rb +0 -67
  149. data/lib/getch/filesystem/zfs/device.rb +0 -66
  150. data/lib/getch/filesystem/zfs/encrypt/void.rb +0 -97
  151. data/lib/getch/filesystem/zfs/format.rb +0 -117
  152. data/lib/getch/filesystem/zfs/mount.rb +0 -47
  153. data/lib/getch/filesystem/zfs/partition.rb +0 -69
  154. data/lib/getch/filesystem/zfs/void.rb +0 -81
  155. data/lib/getch/gentoo/boot.rb +0 -64
  156. data/lib/getch/gentoo/chroot.rb +0 -75
  157. data/lib/getch/gentoo/config.rb +0 -167
  158. data/lib/getch/gentoo/stage.rb +0 -73
  159. data/lib/getch/void/boot.rb +0 -84
  160. data/lib/getch/void/chroot.rb +0 -56
  161. data/lib/getch/void/config.rb +0 -90
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+ require 'open3'
5
+
6
+ module Getch
7
+ module Gentoo
8
+ class Tarball
9
+ def initialize
10
+ @log = Log.new
11
+ @mirror = 'https://mirror.rackspace.com/gentoo'
12
+ @release = release
13
+ @stage_file = OPTIONS[:musl] ?
14
+ "stage3-amd64-musl-#{@release}.tar.xz" :
15
+ "stage3-amd64-systemd-#{@release}.tar.xz"
16
+ end
17
+
18
+ def x
19
+ get_stage3
20
+ control_files
21
+ checksum
22
+ install
23
+ end
24
+
25
+ protected
26
+
27
+ def stage3
28
+ OPTIONS[:musl] ?
29
+ @mirror + '/releases/amd64/autobuilds/latest-stage3-amd64-musl.txt' :
30
+ @mirror + '/releases/amd64/autobuilds/latest-stage3-amd64-systemd.txt'
31
+ end
32
+
33
+ def release
34
+ URI.open(stage3) do |file|
35
+ file.read.match(/^[[:alnum:]]+/)
36
+ end
37
+ rescue Net::OpenTimeout => e
38
+ @log.fatal "Problem with DNS? #{e}"
39
+ end
40
+
41
+ def file
42
+ "#{@release}/#{@stage_file}"
43
+ end
44
+
45
+ def get_stage3
46
+ Dir.chdir OPTIONS[:mountpoint]
47
+ return if File.exist? @stage_file
48
+
49
+ @log.info "wget #{@stage_file}, please wait...\n"
50
+ Helpers.get_file_online(@mirror + '/releases/amd64/autobuilds/' + file, @stage_file)
51
+ end
52
+
53
+ def control_files
54
+ @log.info "Download other files..."
55
+ ['DIGESTS', 'asc', 'CONTENTS.gz'].each do |f|
56
+ Helpers.get_file_online("#{@mirror}/releases/amd64/autobuilds/#{file}.#{f}", "#{@stage_file}.#{f}")
57
+ end
58
+ @log.result_ok
59
+ end
60
+
61
+ def checksum
62
+ @log.info 'Checking SHA512 checksum...'
63
+ command = "awk '/SHA512 HASH/{getline;print}' #{@stage_file}.DIGESTS | sha512sum --check"
64
+ _, stderr, status = Open3.capture3(command)
65
+ if status.success? then
66
+ @log.result_ok
67
+ else
68
+ cleaning
69
+ @log.fatal "Problem with the checksum, stderr\n#{stderr}"
70
+ end
71
+ end
72
+
73
+ def install
74
+ decompress
75
+ cleaning
76
+ end
77
+
78
+ private
79
+
80
+ # https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Stage
81
+ def decompress
82
+ cmd = "tar xpf #{@stage_file} --xattrs-include=\'*.*\' --numeric-owner"
83
+ Getch::Command.new(cmd)
84
+ end
85
+
86
+ def cleaning
87
+ Dir.glob('stage3-amd64-*').each { |f| File.delete(f) }
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,34 @@
1
+ module Getch
2
+ module Gentoo
3
+ class Terraform
4
+ def initialize
5
+ x
6
+ end
7
+
8
+ protected
9
+
10
+ def x
11
+ Gentoo::Sources.new
12
+ install_pkgs
13
+ emerge_deep
14
+ end
15
+
16
+ def install_pkgs
17
+ @pkgs = 'app-portage/gentoolkit'
18
+ @pkgs << ' app-admin/sudo'
19
+ @pkgs << ' app-editors/vim'
20
+ @pkgs << ' net-firewall/iptables'
21
+ @pkgs << ' net-wireless/iwd'
22
+ @pkgs << ' net-misc/dhcpcd' unless Helpers.systemd?
23
+ @pkgs << ' sys-kernel/linux-firmware'
24
+ @pkgs << ' sys-firmware/intel-microcode'
25
+ @pkgs << ' sys-fs/dosfstools' if Helpers.efi?
26
+ Install.new(@pkgs)
27
+ end
28
+
29
+ def emerge_deep
30
+ ChrootOutput.new('emerge --deep --newuse @world')
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nito'
4
+
5
+ module Getch
6
+ module Gentoo
7
+ class Update
8
+ include NiTo
9
+
10
+ def initialize
11
+ @log = Log.new
12
+ x
13
+ end
14
+
15
+ protected
16
+
17
+ def x
18
+ sync
19
+ add_musl_repo if OPTIONS[:musl]
20
+ update
21
+ end
22
+
23
+ private
24
+
25
+ def sync
26
+ gentoo_conf = "#{OPTIONS[:mountpoint]}/etc/portage/repos.conf/gentoo.conf"
27
+ @log.info "Synchronize index, please waiting...\n"
28
+ ChrootOutput.new('emaint sync --auto')
29
+ sed gentoo_conf, /^sync-type/, 'sync-type = rsync'
30
+ end
31
+
32
+ def add_musl_repo
33
+ Install.new('dev-vcs/git')
34
+
35
+ file = "#{OPTIONS[:mountpoint]}/etc/portage/repos.conf/musl.conf"
36
+ content = <<~CONF
37
+ [musl]
38
+ location = /var/db/repos/musl
39
+ sync-type = git
40
+ sync-uri = https://github.com/gentoo/musl.git
41
+ auto-sync = Yes
42
+ CONF
43
+ File.write file, "#{content}\n"
44
+
45
+ ChrootOutput.new('emaint sync -r musl')
46
+ end
47
+
48
+ def update
49
+ cmd = 'emerge --update --deep --newuse @world'
50
+ ChrootOutput.new(cmd)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,13 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'nito'
4
+
3
5
  module Getch
4
6
  module Gentoo
5
7
  class Use
8
+ include NiTo
9
+
6
10
  def initialize(pkg = nil)
7
- @use_dir = "#{MOUNTPOINT}/etc/portage/package.use"
11
+ @use_dir = "#{OPTIONS[:mountpoint]}/etc/portage/package.use"
8
12
  @pkg = pkg
9
13
  @file = @pkg ? @pkg.match(/[\w]+$/) : nil
10
- @make = "#{MOUNTPOINT}/etc/portage/make.conf"
14
+ @make = "#{OPTIONS[:mountpoint]}/etc/portage/make.conf"
11
15
  end
12
16
 
13
17
  def add(*flags)
@@ -24,15 +28,14 @@ module Getch
24
28
 
25
29
  def write
26
30
  content = "#{@pkg} #{@flags}\n"
27
- File.write("#{@use_dir}/#{@file}", content, mode: 'w')
31
+ echo "#{@use_dir}/#{@file}", content
28
32
  end
29
33
 
30
34
  def write_global
31
35
  list = []
32
- @flags.each { |f| list << f unless Helpers.grep?(@make, /#{f}/) }
36
+ @flags.each { |f| list << f unless grep?(@make, f) }
33
37
  use = list.join(' ')
34
- line = "USE=\"${USE} #{use}\"\n"
35
- File.write(@make, line, mode: 'a')
38
+ echo_a @make, "USE=\"${USE} #{use}\""
36
39
  end
37
40
  end
38
41
  end
@@ -4,13 +4,15 @@ module Getch
4
4
  module Gentoo
5
5
  class UseFlag
6
6
  def initialize
7
- @efi = Helpers.efi?
7
+ x
8
8
  end
9
9
 
10
- def apply
10
+ protected
11
+
12
+ def x
13
+ dist_kernel
11
14
  systemd
12
15
  pam
13
- kernel
14
16
  kmod
15
17
  grub
16
18
  zfs
@@ -20,41 +22,40 @@ module Getch
20
22
 
21
23
  private
22
24
 
25
+ # https://wiki.gentoo.org/wiki/Project:Distribution_Kernel#Trying_it_out
26
+ def dist_kernel
27
+ use = Getch::Gentoo::Use.new
28
+ use.add_global('dist-kernel')
29
+ end
30
+
23
31
  def systemd
32
+ return unless Helpers.systemd?
33
+
24
34
  flags = []
25
35
  use = Getch::Gentoo::Use.new('sys-apps/systemd')
26
36
  flags << 'dns-over-tls'
27
- flags << 'gnuefi' if @efi
37
+ flags << 'gnuefi' if Helpers.efi?
28
38
  use.add(flags)
29
39
  end
30
40
 
31
41
  def pam
32
42
  flags = []
33
43
  use = Getch::Gentoo::Use.new('sys-auth/pambase')
34
- flags << '-passwdqc'
35
- flags << 'pwquality'
36
44
  flags << 'sha512'
37
45
  use.add(flags)
38
46
  end
39
47
 
40
- def kernel
41
- use = Getch::Gentoo::Use.new('sys-kernel/gentoo-kernel')
42
- use.add('hardened')
43
- end
44
-
45
48
  def kmod
46
49
  use = Getch::Gentoo::Use.new('sys-apps/kmod')
47
50
  use.add('zstd', 'lzma')
48
51
  end
49
52
 
50
53
  def grub
51
- return if @efi
52
-
53
54
  flags = []
54
55
  use = Getch::Gentoo::Use.new('sys-boot/grub')
55
- flags << '-grub_platforms_efi-64'
56
- flags << 'libzfs' if Getch::OPTIONS[:fs] == 'zfs'
57
- flags << 'device-mapper' if Getch::OPTIONS[:fs] == 'lvm'
56
+ flags << '-grub_platforms_efi-64' unless Helpers.efi?
57
+ flags << 'libzfs' if OPTIONS[:fs] == 'zfs'
58
+ flags << 'device-mapper' if OPTIONS[:fs] == 'lvm' or OPTIONS[:encrypt]
58
59
  use.add(flags)
59
60
  end
60
61
 
@@ -65,10 +66,6 @@ module Getch
65
66
  use.add('rootfs')
66
67
  use = Getch::Gentoo::Use.new('sys-fs/zfs')
67
68
  use.add('rootfs')
68
-
69
- # https://wiki.gentoo.org/wiki/Project:Distribution_Kernel
70
- use = Getch::Gentoo::Use.new
71
- use.add_global('dist-kernel')
72
69
  end
73
70
 
74
71
  def lvm
data/lib/getch/gentoo.rb CHANGED
@@ -1,85 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'gentoo/stage'
4
- require_relative 'gentoo/config'
5
- require_relative 'gentoo/chroot'
6
- require_relative 'gentoo/bootloader'
7
3
  require_relative 'gentoo/sources'
8
- require_relative 'gentoo/boot'
9
4
  require_relative 'gentoo/use'
10
5
  require_relative 'gentoo/use_flag'
11
6
 
12
7
  module Getch
13
8
  module Gentoo
14
- class Main
15
- def initialize
16
- @state = Getch::States.new
17
- end
18
-
19
- def stage3
20
- return if STATES[:gentoo_base]
21
-
22
- stage = Getch::Gentoo::Stage.new
23
- stage.get_stage3
24
- stage.control_files
25
- stage.checksum
26
- @state.stage3
27
- end
28
-
29
- def config
30
- return if STATES[:gentoo_config]
31
-
32
- config = Getch::Gentoo::Config.new
33
- config.portage
34
- config.portage_fs
35
- config.portage_bashrc
36
- config.repo
37
- config.network
38
- config.systemd
39
- config.hostname
40
- @state.config
41
- end
42
-
43
- def chroot
44
- return if STATES[:gentoo_update]
45
-
46
- chroot = Getch::Gentoo::Chroot.new
47
- chroot.update
48
- chroot.cpuflags
49
- chroot.systemd
50
-
51
- flags = Getch::Gentoo::UseFlag.new
52
- flags.apply
53
-
54
- chroot.world
55
- chroot.kernel_license
56
- chroot.install_pkgs
57
- @state.update
58
- end
59
-
60
- def bootloader
61
- return if STATES[:gentoo_bootloader]
62
-
63
- bootloader = Getch::Gentoo::Bootloader.new
64
- bootloader.start
65
- @state.bootloader
66
- end
67
-
68
- def kernel
69
- return if STATES[:gentoo_kernel]
70
-
71
- source = Getch::Gentoo::Sources.new
72
- source.bask
73
- source.configs
74
- source.make
75
- source.load_modules
76
- @state.kernel
77
- end
78
-
79
- def boot
80
- boot = Getch::Gentoo::Boot.new
81
- boot.start
82
- end
83
- end
84
9
  end
85
10
  end
11
+
12
+ require_relative 'gentoo/tarball'
13
+ require_relative 'gentoo/pre_config'
14
+ require_relative 'gentoo/update'
15
+ require_relative 'gentoo/post_config'
16
+ require_relative 'gentoo/terraform'
17
+ require_relative 'gentoo/services'
18
+ require_relative 'gentoo/bootloader'
19
+ require_relative 'gentoo/finalize'
data/lib/getch/guard.rb CHANGED
@@ -27,8 +27,8 @@ module Getch
27
27
 
28
28
  def self.format(name)
29
29
  raise InvalidFormat, 'No format specified.' unless name
30
- raise InvalidFormat, "Format #{name} not yet available." if name.match(/btrfs/)
31
- raise InvalidFormat, "Format #{name} not supported." unless name.match(/zfs|lvm|ext4/)
30
+ raise InvalidFormat, "Format #{name} not yet available." if name.match(/btrfs|xfs/)
31
+ raise InvalidFormat, "Format #{name} not supported." unless name.match(/zfs|ext4/)
32
32
 
33
33
  name
34
34
  rescue InvalidFormat => e
data/lib/getch/helpers.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'open-uri'
4
4
  require 'open3'
5
5
  require 'fileutils'
6
+ require 'nito'
6
7
 
7
8
  module Getch
8
9
  module Helpers
@@ -10,63 +11,40 @@ module Getch
10
11
  Dir.exist? '/sys/firmware/efi/efivars'
11
12
  end
12
13
 
13
- def self.get_file_online(url, dest)
14
- URI.open(url) do |l|
15
- File.open(dest, "wb") { |f| f.write(l.read) }
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 unless Dir.exist? path
28
- end
29
-
30
- def self.add_file(path, content = '')
31
- File.write path, content unless File.exist? path
32
- end
33
-
34
- def self.mkdir(dir)
35
- FileUtils.mkdir_p dir unless Dir.exist? dir
14
+ def self.systemd?
15
+ Dir.exist? "#{OPTIONS[:mountpoint]}/etc/systemd"
36
16
  end
37
17
 
38
- def self.touch(file)
39
- File.write file, '' unless File.exist? file
18
+ def self.openrc?
19
+ File.exist? "#{OPTIONS[:mountpoint]}/etc/conf.d/keymaps"
40
20
  end
41
21
 
42
- def self.echo(src, content = '')
43
- File.write(src, "#{content}\n", mode: 'w')
22
+ def self.runit?
23
+ Dir.exist? "#{OPTIONS[:mountpoint]}/etc/runit"
44
24
  end
45
25
 
46
- def self.echo_a(src, content = '')
47
- raise "No file #{src} found !" unless File.exist? src
48
-
49
- File.write(src, "#{content}\n", mode: 'a') unless self.grep?(src, content)
26
+ def self.grub?
27
+ File.exist? "#{OPTIONS[:mountpoint]}/etc/default/grub"
50
28
  end
51
29
 
52
- def self.cp(src, dest)
53
- raise "Src file #{src} no found" unless File.exist? src
54
-
55
- FileUtils.cp(src, dest)
30
+ def self.get_file_online(url, dest)
31
+ URI.open(url) do |l|
32
+ File.open(dest, "wb") { |f| f.write(l.read) }
33
+ end
34
+ rescue Net::OpenTimeout => e
35
+ abort "DNS error #{e}"
56
36
  end
57
37
 
58
- def self.grep?(file, regex)
59
- is_found = false
60
- return is_found unless File.exist? file
61
- File.open(file) do |f|
62
- f.each { |l| is_found = true if l.match(regex) }
38
+ def self.exec_or_die(cmd)
39
+ _, stderr, status = Open3.capture3(cmd)
40
+ unless status.success?
41
+ abort "Problem running #{cmd}, stderr was:\n#{stderr}"
63
42
  end
64
- is_found
65
43
  end
66
44
 
67
45
  def self.sys(cmd)
68
46
  system(cmd)
69
- raise "Error with #{cmd}" unless $?.success?
47
+ $?.success? || abort("Error with #{cmd}")
70
48
  end
71
49
 
72
50
  def self.partuuid(dev)
@@ -79,39 +57,58 @@ module Getch
79
57
  return f.delete_prefix('/dev/disk/by-uuid/')
80
58
  end
81
59
  end
60
+ Log.new.fatal("UUID on #{dev} is no found")
82
61
  end
83
62
 
84
- # Used with ZFS for the pool name
85
- def self.pool_id(dev)
86
- if dev.match(/[0-9]/)
87
- sleep 1
88
- `lsblk -o PARTUUID #{dev}`.delete("\n").delete('PARTUUID').match(/\w{5}/)
89
- else
90
- puts 'Please, enter a pool name'
91
- while true
92
- print "\n> "
93
- value = gets
94
- if value.match(/[a-z]{4,20}/)
95
- return value
96
- end
97
- puts "Bad name, you enter: #{value}"
98
- puts 'Valid pool name use character only, between 4-20.'
63
+ def self.get_dm(name)
64
+ Dir.glob('/dev/mapper/*').each do |f|
65
+ if f =~ /#{name}/ && f != '/dev/mapper/control'
66
+ return File.readlink(f).tr('../', '')
99
67
  end
100
68
  end
69
+ Log.new.fatal("Dm for #{name} is no found")
101
70
  end
102
71
 
103
- module Void
104
- def command(args)
105
- print " => Exec: #{args}..."
106
- cmd = "chroot #{Getch::MOUNTPOINT} /bin/bash -c \"#{args}\""
107
- _, stderr, status = Open3.capture3(cmd)
108
- if status.success?
109
- puts "\s[OK]"
110
- return
72
+ # Used by ZFS for the pool creation
73
+ # sleep is necessary here at least the first time
74
+ def self.get_id(dev)
75
+ sleep 3
76
+ Dir.glob('/dev/disk/by-id/*').each do |f|
77
+ if File.readlink(f).match(/#{dev}/)
78
+ return f.delete_prefix('/dev/disk/by-id/')
111
79
  end
112
- raise "\n[-] Fail cmd #{args} - #{stderr}."
113
80
  end
81
+ Log.new.fatal("ID on #{dev} is no found")
82
+ end
83
+
84
+ # https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Base
85
+ def self.mount_all
86
+ dest = OPTIONS[:mountpoint]
87
+ NiTo.mount '--types proc /proc', "#{dest}/proc"
88
+ ['dev', 'sys', 'run'].each do |d|
89
+ NiTo.mount '--rbind', "/#{d}", "#{dest}/#{d}"
90
+ NiTo.mount '--make-rslave', "#{dest}/#{d}"
91
+ end
92
+ end
93
+
94
+ def self.get_memory
95
+ mem = nil
96
+ File.open('/proc/meminfo').each do |l|
97
+ t = l.split(' ') if l =~ /memtotal/i
98
+ t && mem = t[1]
99
+ end
100
+ mem || Log.new.fatal('get_memory - failed to get memory')
101
+
102
+ mem += 'K'
103
+ end
114
104
 
105
+ # get the sector size of a disk
106
+ def self.get_bs(path)
107
+ cmd = Getch::Command.new('blockdev', '--getpbsz', path)
108
+ cmd.res
109
+ end
110
+
111
+ module Void
115
112
  def command_output(args)
116
113
  print " => Exec: #{args}..."
117
114
  cmd = "chroot #{Getch::MOUNTPOINT} /bin/bash -c \"#{args}\""
@@ -126,19 +123,6 @@ module Getch
126
123
  end
127
124
  end
128
125
 
129
- def add_line(file, line)
130
- raise "No file #{file} found !" unless File.exist? file
131
-
132
- File.write(file, "#{line}\n", mode: 'a')
133
- end
134
-
135
- def search(file, text)
136
- File.open(file).each do |line|
137
- return true if line.match(/#{text}/)
138
- end
139
- false
140
- end
141
-
142
126
  # Used only when need password
143
127
  def chroot(cmd)
144
128
  unless system('chroot', Getch::MOUNTPOINT, '/bin/bash', '-c', cmd)
@@ -162,42 +146,6 @@ module Getch
162
146
 
163
147
  add_line(conf, "PARTUUID=#{device} #{rest}")
164
148
  end
165
-
166
- def grub_cmdline(*args)
167
- conf = "#{Getch::MOUNTPOINT}/etc/default/grub"
168
- list = args.join(' ')
169
- secs = "GRUB_CMDLINE_LINUX=\"#{list} init_on_alloc=1 init_on_free=1"
170
- secs += ' slab_nomerge pti=on slub_debug=ZF vsyscall=none"'
171
- raise 'No default/grub found' unless File.exist? conf
172
-
173
- unless search(conf, 'GRUB_CMDLINE_LINUX=')
174
- File.write(conf, "#{secs}\n", mode: 'a')
175
- end
176
- end
177
- end
178
-
179
- module Cryptsetup
180
- def encrypt(dev)
181
- raise "No device #{dev}" unless File.exist? dev
182
-
183
- puts " => Encrypting device #{dev}..."
184
- if Helpers.efi? && Getch::OPTIONS[:os] == 'gentoo'
185
- Helpers.sys("cryptsetup luksFormat --type luks #{dev}")
186
- else
187
- Helpers.sys("cryptsetup luksFormat --type luks1 #{dev}")
188
- end
189
- end
190
-
191
- def open_crypt(dev, map_name)
192
- raise "No device #{dev}" unless File.exist? dev
193
-
194
- puts " => Opening encrypted device #{dev}..."
195
- if Helpers.efi? && Getch::OPTIONS[:os] == 'gentoo'
196
- Helpers.sys("cryptsetup open --type luks #{dev} #{map_name}")
197
- else
198
- Helpers.sys("cryptsetup open --type luks1 #{dev} #{map_name}")
199
- end
200
- end
201
149
  end
202
150
  end
203
151
  end