getch 0.1.9 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
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
data/lib/mkfs/zfs.rb ADDED
@@ -0,0 +1,167 @@
1
+ require 'getch/log'
2
+
3
+ module Mkfs
4
+ class Zfs < Root
5
+ def initialize(devs, options)
6
+ @mountpoint = options[:mountpoint]
7
+ @zfs = options[:zfs_name] ||= 'pool'
8
+ @os = options[:os]
9
+ @encrypt = options[:encrypt]
10
+ @zlog = devs[:zlog] ||= nil
11
+ @zcache = devs[:zcache] ||= nil
12
+ @rpool = "r#{@zfs}"
13
+ @bpool = "b#{@zfs}"
14
+ @hpool = "h#{@zfs}"
15
+ @log = Getch::Log.new
16
+ super
17
+ end
18
+
19
+ # reorder process, root should be formatted first
20
+ def x
21
+ format_efi
22
+ format_root
23
+ format_boot
24
+ format_swap
25
+ format_home
26
+ add_dataset
27
+ end
28
+
29
+ # https://openzfs.github.io/openzfs-docs/Getting%20Started/Ubuntu/Ubuntu%2020.04%20Root%20on%20ZFS.html#id13
30
+ def format_boot
31
+ @boot || return
32
+
33
+ id = Getch::Helpers.get_id(@boot)
34
+ ashift = get_ashift @boot
35
+ args = "-f -o ashift=#{ashift} -o autotrim=on"
36
+ args << ' -o feature@async_destroy=enabled'
37
+ args << ' -o feature@bookmarks=enabled'
38
+ args << ' -o feature@embedded_data=enabled'
39
+ args << ' -o feature@empty_bpobj=enabled'
40
+ args << ' -o feature@enabled_txg=enabled'
41
+ args << ' -o feature@extensible_dataset=enabled'
42
+ args << ' -o feature@filesystem_limits=enabled'
43
+ args << ' -o feature@hole_birth=enabled'
44
+ args << ' -o feature@large_blocks=enabled'
45
+ args << ' -o feature@lz4_compress=enabled'
46
+ args << ' -o feature@spacemap_histogram=enabled'
47
+ args << ' -O acltype=posixacl -O canmount=off -O compression=lz4'
48
+ args << ' -O devices=off -O normalization=formD -O atime=off -O xattr=sa'
49
+ args << ' -O mountpoint=/boot'
50
+ args << " -R #{@mountpoint} #{@bpool} #{id}"
51
+ sh 'zpool', 'create', args
52
+ end
53
+
54
+ def format_swap
55
+ mk_swap "/dev/#{@swap}"
56
+ add_zlog
57
+ add_zcache
58
+ end
59
+
60
+ def add_zlog
61
+ @zlog || return
62
+
63
+ id = Getch::Helpers.get_id(@zlog)
64
+ sh 'zpool', 'add', @rpool, 'log', id
65
+ end
66
+
67
+ def add_zcache
68
+ @zcache || return
69
+
70
+ id = Getch::Helpers.get_id(@zcache)
71
+ sh 'zpool', 'add', @rpool, 'cache', id
72
+ end
73
+
74
+ def format_root
75
+ id = Getch::Helpers.get_id(@root)
76
+ ashift = get_ashift @root
77
+ args = "-f -o ashift=#{ashift} -o autotrim=on"
78
+ @encrypt && args << ' -O encryption=aes-256-gcm'
79
+ @encrypt && args << ' -O keylocation=prompt -O keyformat=passphrase'
80
+ args << ' -O acltype=posixacl -O canmount=off -O compression=lz4'
81
+ args << ' -O xattr=sa -O mountpoint=/'
82
+ args << " -R #{@mountpoint} #{@rpool} #{id}"
83
+ sh 'zpool', 'create', args
84
+ end
85
+
86
+ def format_home
87
+ @home || return
88
+
89
+ id = Getch::Helpers.get_id(@home)
90
+ ashift = get_ashift @home
91
+ args = "-f -o ashift=#{ashift} -o autotrim=on"
92
+ @encrypt && args << ' -O encryption=aes-256-gcm'
93
+ @encrypt && args << ' -O keylocation=prompt -O keyformat=passphrase'
94
+ args << ' -O acltype=posixacl -O canmount=off -O compression=lz4'
95
+ args << ' -O xattr=sa -O mountpoint=/home'
96
+ args << " -R #{@mountpoint} #{@hpool} #{id}"
97
+ sh 'zpool', 'create', args
98
+ end
99
+
100
+ def add_dataset
101
+ zfs_create "-o canmount=off -o mountpoint=none #{@rpool}/ROOT"
102
+ zfs_create "-o canmount=noauto -o mountpoint=/ #{@rpool}/ROOT/#{@os}"
103
+ Getch::Command.new("zfs mount #{@rpool}/ROOT/#{@os}")
104
+
105
+ zfs_create "-o canmount=off #{@rpool}/ROOT/#{@os}/usr"
106
+ zfs_create "#{@rpool}/ROOT/#{@os}/usr/src"
107
+
108
+ zfs_create "-o canmount=off #{@rpool}/ROOT/#{@os}/var"
109
+ zfs_create "#{@rpool}/ROOT/#{@os}/var/log"
110
+ zfs_create "#{@rpool}/ROOT/#{@os}/var/db"
111
+ zfs_create "#{@rpool}/ROOT/#{@os}/var/tmp"
112
+ zfs_create "#{@rpool}/ROOT/#{@os}/var/lib"
113
+ zfs_create "#{@rpool}/ROOT/#{@os}/var/lib/docker"
114
+
115
+ boot_dataset
116
+ user_dataset
117
+ end
118
+
119
+ def boot_dataset
120
+ @boot || return
121
+
122
+ zfs_create "-o canmount=off -o mountpoint=none #{@bpool}/BOOT"
123
+ zfs_create "-o canmount=noauto -o mountpoint=/boot #{@bpool}/BOOT/#{@os}"
124
+ end
125
+
126
+ def user_dataset
127
+ if @home
128
+ zfs_create "-o canmount=off -o mountpoint=/ #{@hpool}/USERDATA"
129
+ zfs_create "-o canmount=on -o mountpoint=/root #{@hpool}/USERDATA/root"
130
+ zfs_create "-o canmount=on -o mountpoint=/home #{@hpool}/USERDATA/home"
131
+ else
132
+ zfs_create "-o canmount=off -o mountpoint=/ #{@rpool}/USERDATA"
133
+ zfs_create "-o canmount=on -o mountpoint=/root #{@rpool}/USERDATA/root"
134
+ zfs_create "-o canmount=on -o mountpoint=/home #{@rpool}/USERDATA/home"
135
+ end
136
+ end
137
+
138
+ private
139
+
140
+ def get_ashift(dev)
141
+ bs = Getch::Helpers.get_bs("/dev/#{dev}")
142
+ case bs
143
+ when /8096/ then 13
144
+ when /4096/ then 12
145
+ else 9
146
+ end
147
+ end
148
+
149
+ def zfs_create(*args)
150
+ Getch::Command.new('zfs', 'create', args)
151
+ end
152
+
153
+ def sh(*args)
154
+ @encrypt ?
155
+ cmd_crypt(args) :
156
+ Getch::Command.new(args)
157
+ end
158
+
159
+ def cmd_crypt(*args)
160
+ system args.join(' ')
161
+ return if $?.exitstatus == 0
162
+
163
+ @log.dbg $?
164
+ @log.fatal 'die'
165
+ end
166
+ end
167
+ end
data/lib/mkfs.rb ADDED
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'getch/command'
4
+ require 'getch/helpers'
5
+
6
+ module Mkfs
7
+ class Root
8
+ def initialize(devs, options)
9
+ @efi = devs[:efi] ||= nil
10
+ @boot = devs[:boot] ||= nil
11
+ @swap = devs[:swap] ||= nil
12
+ @root = devs[:root] ||= nil
13
+ @home = devs[:home] ||= nil
14
+ @fs = options[:fs]
15
+ x
16
+ end
17
+
18
+ protected
19
+
20
+ def x
21
+ format_efi
22
+ format_boot
23
+ format_swap
24
+ format_root
25
+ format_home
26
+ end
27
+
28
+ def format_efi
29
+ @efi || return
30
+
31
+ mkfs_vfat "/dev/#{@efi}"
32
+ end
33
+
34
+ def format_boot
35
+ @boot || return
36
+
37
+ mkfs "/dev/#{@boot}"
38
+ end
39
+
40
+ def format_swap
41
+ @swap || return
42
+
43
+ mk_swap "/dev/#{@swap}"
44
+ end
45
+
46
+ def format_root
47
+ @root || return
48
+
49
+ mkfs "/dev/#{@root}"
50
+ end
51
+
52
+ def format_home
53
+ @home || return
54
+
55
+ mkfs "/dev/#{@home}"
56
+ end
57
+
58
+ private
59
+
60
+ def mkfs(path)
61
+ case @fs
62
+ when 'ext4' then mkfs_ext4 path
63
+ when 'xfs' then mkfs_xfs path
64
+ end
65
+ end
66
+
67
+ def mkfs_vfat(path)
68
+ Getch::Command.new('mkfs.fat', '-F32', path)
69
+ end
70
+
71
+ def mk_swap(path)
72
+ Getch::Command.new('mkswap', '-f', path)
73
+ end
74
+
75
+ def mkfs_ext4(path)
76
+ bs = Getch::Helpers.get_bs(path)
77
+ Getch::Command.new('mkfs.ext4', '-F', '-b', bs, path)
78
+ end
79
+
80
+ def mkfs_xfs(path)
81
+ bs = Getch::Helpers.get_bs(path)
82
+ Getch::Command.new('mkfs.xfs', '-f', '-s', "size=#{bs}", path)
83
+ end
84
+ end
85
+
86
+ class Lvm < Root
87
+ def initialize(devs, options)
88
+ @vg = options[:vg_name]
89
+ super
90
+ end
91
+
92
+ def format_swap
93
+ mk_swap "/dev/#{@vg}/swap"
94
+ end
95
+
96
+ def format_root
97
+ mkfs "/dev/#{@vg}/root"
98
+ end
99
+
100
+ def format_home
101
+ mkfs "/dev/#{@vg}/home"
102
+ end
103
+ end
104
+
105
+ class Encrypt < Root
106
+ def initialize(devs, options)
107
+ @luks = options[:luks_name]
108
+ super
109
+ end
110
+
111
+ # Boot is alrealy formatted
112
+ def format_boot
113
+ end
114
+
115
+ # Swap will be encrypted after the reboot
116
+ def format_swap
117
+ end
118
+
119
+ def format_root
120
+ File.exist? "/dev/mapper/root-#{@luks}" || abort("No root-#{@luks} found")
121
+
122
+ mkfs "/dev/mapper/root-#{@luks}"
123
+ end
124
+
125
+ def format_home
126
+ @home || return
127
+
128
+ mkfs "/dev/mapper/home-#{@luks}"
129
+ end
130
+ end
131
+
132
+ class Hybrid < Lvm
133
+
134
+ # Boot is alrealy formatted
135
+ def format_boot
136
+ end
137
+ end
138
+ end
139
+
140
+ require_relative 'mkfs/zfs'
data/lib/mountfs.rb ADDED
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nito'
4
+
5
+ module MountFs
6
+ class Minimal
7
+ include NiTo
8
+
9
+ def initialize(devs, options)
10
+ @efi = devs[:efi] ||= nil
11
+ @boot = devs[:boot] ||= nil
12
+ @swap = devs[:swap] ||= nil
13
+ @root = devs[:root] ||= nil
14
+ @home = devs[:home] ||= nil
15
+ @mountpoint = options[:mountpoint]
16
+ x
17
+ end
18
+
19
+ protected
20
+
21
+ def x
22
+ mount_root
23
+ mount_efi
24
+ mount_boot
25
+ mount_swap
26
+ mount_home
27
+ end
28
+
29
+ def mount_root
30
+ mount "/dev/#{@root}", @mountpoint
31
+ end
32
+
33
+ def mount_efi
34
+ @efi || return
35
+
36
+ mount "/dev/#{@efi}", "#{@mountpoint}/efi"
37
+ end
38
+
39
+ def mount_boot
40
+ @boot || return
41
+
42
+ mount "/dev/#{@root}", "#{@mountpoint}/boot"
43
+ end
44
+
45
+ def mount_swap
46
+ @swap || return
47
+
48
+ grep?('/proc/swaps', "/dev/#{@swap}") && return
49
+
50
+ swapon "/dev/#{@swap}"
51
+ end
52
+
53
+ def mount_home
54
+ @home || return
55
+
56
+ mount "/dev/#{@home}", "#{@mountpoint}/home"
57
+ end
58
+
59
+ private
60
+
61
+ def swapon(path)
62
+ Getch::Command.new('swapon', path)
63
+ end
64
+ end
65
+
66
+ class Lvm < Minimal
67
+ def initialize(devs, options)
68
+ @vg = options[:vg_name]
69
+ super
70
+ end
71
+
72
+ def mount_root
73
+ mount "/dev/#{@vg}/root", @mountpoint
74
+ end
75
+
76
+ def mount_swap
77
+ dm = Getch::Helpers.get_dm "#{@vg0}-swap"
78
+
79
+ grep?('/proc/swaps', "/dev/#{dm}") && return
80
+
81
+ swapon "/dev/#{@vg}/swap"
82
+ end
83
+
84
+ def mount_home
85
+ mount "/dev/#{@vg}/home", "#{@mountpoint}/home"
86
+ end
87
+ end
88
+
89
+ class Encrypt < Minimal
90
+ def initialize(devs, options)
91
+ @luks = options[:luks_name]
92
+ super
93
+ end
94
+
95
+ def mount_root
96
+ umount "/dev/mapper/boot-#{@luks}"
97
+ mount "/dev/mapper/root-#{@luks}", @mountpoint
98
+ end
99
+
100
+ def mount_boot
101
+ mount "/dev/mapper/boot-#{@luks}", "#{@mountpoint}/boot"
102
+ end
103
+
104
+ def mount_swap
105
+ end
106
+
107
+ def mount_home
108
+ @home || return
109
+
110
+ mount "/dev/mapper/home-#{@luks}", "#{@mountpoint}/home"
111
+ end
112
+ end
113
+
114
+ class Hybrid < Encrypt
115
+ def initialize(devs, options)
116
+ @vg = options[:vg_name]
117
+ super
118
+ end
119
+
120
+ def mount_root
121
+ umount "/dev/mapper/boot-#{@luks}"
122
+ mount "/dev/#{@vg}/root", @mountpoint
123
+ end
124
+
125
+ def mount_boot
126
+ mount "/dev/mapper/boot-#{@luks}", "#{@mountpoint}/boot"
127
+ end
128
+
129
+ def mount_home
130
+ mount "/dev/#{@vg}/home", "#{@mountpoint}/home"
131
+ end
132
+ end
133
+
134
+ class Zfs < Minimal
135
+ def initialize(devs, options)
136
+ @zfs = options[:zfs_name]
137
+ @os = options[:os]
138
+ super
139
+ end
140
+
141
+ # Root should be alrealy mounted
142
+ def mount_root
143
+ end
144
+
145
+ def mount_boot
146
+ @boot || return
147
+
148
+ Getch::Command.new("zfs mount b#{@zfs}/BOOT/#{@os}")
149
+ end
150
+
151
+ def mount_home
152
+ end
153
+ end
154
+ end
data/lib/nito.rb ADDED
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'open3'
5
+ require 'tempfile'
6
+ require_relative 'getch/log'
7
+ require_relative 'getch/command'
8
+
9
+ # uNix Tools like mkdir, mount in Ruby code
10
+ module NiTo
11
+ module_function
12
+
13
+ def mkdir(path, perm = 0755)
14
+ return if Dir.exist? path
15
+
16
+ FileUtils.mkdir_p path, mode: perm
17
+ end
18
+
19
+ def grep?(file, search)
20
+ is_found = false
21
+ return is_found unless File.exist? file
22
+
23
+ File.open(file).each do |l|
24
+ is_found = true if l =~ /#{search}/
25
+ end
26
+ is_found
27
+ end
28
+
29
+ def rm(file)
30
+ File.exist?(file) && File.delete(file)
31
+ end
32
+
33
+ def umount(dir)
34
+ return unless mount? dir
35
+
36
+ Getch::Command.new('umount', dir)
37
+ end
38
+
39
+ # Mount, accept *args, the last argument should be the destination
40
+ # e.g: mount '--types proc', '/proc', '/mnt/getch/proc'
41
+ def mount(*args)
42
+ return if mount? args.last
43
+
44
+ mkdir args.last
45
+ Getch::Command.new('mount', args.join(' '))
46
+ end
47
+
48
+ def mount?(dir)
49
+ res = false
50
+ File.open('/proc/mounts').each do |l|
51
+ res = true if l =~ /#{dir}/
52
+ end
53
+ res
54
+ end
55
+
56
+ # Like echo 'content' > to_file
57
+ def echo(file, content)
58
+ File.write file, "#{content}\n", mode: 'w'
59
+ end
60
+
61
+ # Like echo 'content' >> to_file
62
+ def echo_a(file, content)
63
+ File.write file, "#{content}\n", mode: 'a' unless grep? file, content
64
+ end
65
+
66
+ def cp(src, dest)
67
+ FileUtils.cp src, dest
68
+ end
69
+
70
+ # create a void file
71
+ def touch(file)
72
+ File.write file, '' unless File.exist? file
73
+ end
74
+
75
+ # Like sed -i /old:new/ file
76
+ def sed(file, regex, change)
77
+ tmp_file = Tempfile.new
78
+ File.open(file).each do |l|
79
+ if l.match(regex)
80
+ echo_a tmp_file, change
81
+ else
82
+ File.write tmp_file, l, mode: 'a'
83
+ end
84
+ end
85
+ cp tmp_file, file
86
+ end
87
+
88
+ def search_proc_swaps(path)
89
+ found = nil
90
+ File.open('/proc/swaps').each do |l|
91
+ if l =~ /#{path}/
92
+ found = l.split(' ')
93
+ end
94
+ end
95
+ found
96
+ end
97
+
98
+ def swapoff(dev)
99
+ return unless grep? '/proc/swaps', dev
100
+
101
+ found = search_proc_swaps(dev)
102
+ found ?
103
+ Getch::Command.new('swapoff', found[0]) :
104
+ return
105
+ end
106
+
107
+ def swapoff_dm(name)
108
+ dm = Getch::Helpers.get_dm name
109
+ dm || return
110
+
111
+ found = search_proc_swaps(dm)
112
+ found ?
113
+ Getch::Command.new('swapoff', found[0]) :
114
+ return
115
+ end
116
+
117
+ def sh(*args)
118
+ log = Log.new
119
+ Open3.popen3 args.join(' ') do |_, stdout, stderr, wait_thr|
120
+ if wait_thr.value.success?
121
+ log.info_res 'Ok'
122
+ return stdout.read.chomp
123
+ end
124
+ puts
125
+ log.dbg args.join(' ') + "\nEXIT:#{wait_thr.value}"
126
+ log.dbg "STDERR:#{stderr.read}"
127
+ log.dbg "STDOUT:#{stdout.read}"
128
+ log.fatal 'Die'
129
+ end
130
+ end
131
+ end