getch 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) 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 +30 -0
  5. data/README.md +31 -6
  6. data/bin/setup.sh +29 -13
  7. data/lib/getch.rb +43 -30
  8. data/lib/getch/command.rb +163 -0
  9. data/lib/getch/filesystem.rb +8 -0
  10. data/lib/getch/filesystem/ext4.rb +14 -0
  11. data/lib/getch/filesystem/ext4/config.rb +59 -0
  12. data/lib/getch/filesystem/ext4/deps.rb +22 -0
  13. data/lib/getch/filesystem/ext4/device.rb +16 -0
  14. data/lib/getch/filesystem/ext4/encrypt.rb +15 -0
  15. data/lib/getch/filesystem/ext4/encrypt/config.rb +85 -0
  16. data/lib/getch/filesystem/ext4/encrypt/deps.rb +59 -0
  17. data/lib/getch/filesystem/ext4/encrypt/device.rb +21 -0
  18. data/lib/getch/filesystem/ext4/encrypt/format.rb +32 -0
  19. data/lib/getch/filesystem/ext4/encrypt/mount.rb +64 -0
  20. data/lib/getch/filesystem/ext4/encrypt/partition.rb +116 -0
  21. data/lib/getch/filesystem/ext4/format.rb +30 -0
  22. data/lib/getch/filesystem/ext4/mount.rb +62 -0
  23. data/lib/getch/filesystem/ext4/partition.rb +75 -0
  24. data/lib/getch/filesystem/lvm.rb +14 -0
  25. data/lib/getch/filesystem/lvm/config.rb +63 -0
  26. data/lib/getch/filesystem/lvm/deps.rb +57 -0
  27. data/lib/getch/filesystem/lvm/device.rb +19 -0
  28. data/lib/getch/filesystem/lvm/encrypt.rb +15 -0
  29. data/lib/getch/filesystem/lvm/encrypt/config.rb +74 -0
  30. data/lib/getch/filesystem/lvm/encrypt/deps.rb +63 -0
  31. data/lib/getch/filesystem/lvm/encrypt/device.rb +22 -0
  32. data/lib/getch/filesystem/lvm/encrypt/format.rb +32 -0
  33. data/lib/getch/filesystem/lvm/encrypt/mount.rb +64 -0
  34. data/lib/getch/filesystem/lvm/encrypt/partition.rb +92 -0
  35. data/lib/getch/filesystem/lvm/format.rb +25 -0
  36. data/lib/getch/filesystem/lvm/mount.rb +62 -0
  37. data/lib/getch/filesystem/lvm/partition.rb +81 -0
  38. data/lib/getch/filesystem/zfs.rb +14 -0
  39. data/lib/getch/filesystem/zfs/config.rb +58 -0
  40. data/lib/getch/filesystem/zfs/deps.rb +90 -0
  41. data/lib/getch/filesystem/zfs/device.rb +19 -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 +94 -0
  45. data/lib/getch/filesystem/zfs/encrypt/device.rb +21 -0
  46. data/lib/getch/filesystem/zfs/encrypt/format.rb +22 -0
  47. data/lib/getch/filesystem/zfs/encrypt/mount.rb +67 -0
  48. data/lib/getch/filesystem/zfs/encrypt/partition.rb +151 -0
  49. data/lib/getch/filesystem/zfs/format.rb +20 -0
  50. data/lib/getch/filesystem/zfs/mount.rb +67 -0
  51. data/lib/getch/filesystem/zfs/partition.rb +147 -0
  52. data/lib/getch/gentoo.rb +3 -2
  53. data/lib/getch/gentoo/boot.rb +29 -25
  54. data/lib/getch/gentoo/chroot.rb +18 -14
  55. data/lib/getch/gentoo/config.rb +18 -9
  56. data/lib/getch/gentoo/sources.rb +45 -31
  57. data/lib/getch/gentoo/stage.rb +2 -2
  58. data/lib/getch/helpers.rb +24 -6
  59. data/lib/getch/log.rb +54 -0
  60. data/lib/getch/options.rb +16 -7
  61. data/lib/getch/version.rb +1 -1
  62. metadata +48 -5
  63. metadata.gz.sig +0 -0
  64. data/lib/getch/disk.rb +0 -77
  65. data/lib/getch/mount.rb +0 -73
@@ -0,0 +1,22 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Lvm
4
+ module Encrypt
5
+ class Device
6
+ def initialize
7
+ @disk = DEFAULT_OPTIONS[:disk]
8
+ @user = DEFAULT_OPTIONS[:username]
9
+ @dev_boot_efi = Helpers::efi? ? "/dev/#{@disk}1" : nil
10
+ @dev_boot = Helpers::efi? ? nil : "/dev/#{@disk}2"
11
+ @dev_root = Helpers::efi? ? "/dev/#{@disk}2" : "/dev/#{@disk}3"
12
+ @vg = 'vg0'
13
+ @lv_root = "/dev/mapper/#{@vg}-root"
14
+ @lv_swap = "/dev/mapper/#{@vg}-swap"
15
+ @lv_home = @user ? "/dev/mapper/#{@vg}-home" : nil
16
+ @luks_root = "/dev/mapper/cryptroot"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Lvm
4
+ module Encrypt
5
+ class Format < Getch::FileSystem::Lvm::Encrypt::Device
6
+ def initialize
7
+ super
8
+ @fs = 'ext4'
9
+ @state = Getch::States.new()
10
+ format
11
+ end
12
+
13
+ def format
14
+ return if STATES[:format]
15
+ puts "Format #{@disk} with #{@fs}"
16
+ exec("mkfs.fat -F32 #{@dev_boot_efi}") if @dev_boot_efi
17
+ exec("mkfs.#{@fs} -F #{@dev_boot}") if @dev_boot
18
+ #exec("mkswap -f #{@lv_swap}")
19
+ exec("mkfs.#{@fs} -F #{@lv_root}")
20
+ exec("mkfs.#{@fs} -F #{@lv_home}") if @lv_home
21
+ @state.format
22
+ end
23
+
24
+ private
25
+ def exec(cmd)
26
+ Getch::Command.new(cmd).run!
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,64 @@
1
+ require 'fileutils'
2
+
3
+ module Getch
4
+ module FileSystem
5
+ module Lvm
6
+ module Encrypt
7
+ class Mount < Getch::FileSystem::Lvm::Encrypt::Device
8
+ def initialize
9
+ super
10
+ @root_dir = MOUNTPOINT
11
+ @boot_dir = "#{@root_dir}/boot"
12
+ @boot_efi_dir = "#{@root_dir}/boot/efi"
13
+ @home_dir = @user ? "#{@root_dir}/home/#{@user}" : nil
14
+ @state = Getch::States.new()
15
+ end
16
+
17
+ def run
18
+ return if STATES[:mount]
19
+ mount_swap
20
+ mount_root
21
+ mount_boot
22
+ mount_home
23
+ mount_boot_efi
24
+ @state.mount
25
+ end
26
+
27
+ private
28
+
29
+ def mount_swap
30
+ return if ! @lv_swap
31
+ system("swapon #{@lv_swap}")
32
+ end
33
+
34
+ def mount_root
35
+ return if ! @lv_root
36
+ Dir.mkdir(@root_dir, 0700) if ! Dir.exist?(@root_dir)
37
+ system("mount #{@lv_root} #{@root_dir}")
38
+ end
39
+
40
+ def mount_boot_efi
41
+ return if ! @dev_boot_efi
42
+ FileUtils.mkdir_p @boot_efi_dir, mode: 0700 if ! Dir.exist?(@boot_efi_dir)
43
+ system("mount #{@dev_boot_efi} #{@boot_efi_dir}")
44
+ end
45
+
46
+ def mount_boot
47
+ return if ! @dev_boot
48
+ FileUtils.mkdir_p @boot_dir, mode: 0700 if ! Dir.exist?(@boot_dir)
49
+ system("mount #{@dev_boot} #{@boot_dir}")
50
+ end
51
+
52
+ def mount_home
53
+ return if ! @lv_home
54
+ if @user != nil then
55
+ FileUtils.mkdir_p @home_dir, mode: 0700 if ! Dir.exist?(@home_dir)
56
+ system("mount #{@lv_home} #{@home_dir}")
57
+ end
58
+ @state.mount
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,92 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Lvm
4
+ module Encrypt
5
+ class Partition < Getch::FileSystem::Lvm::Encrypt::Device
6
+ def initialize
7
+ super
8
+ @state = Getch::States.new()
9
+ @log = Log.new
10
+ run_partition
11
+ end
12
+
13
+ def run_partition
14
+ return if STATES[:partition ]
15
+ clear_struct
16
+ cleaning
17
+ partition
18
+ encrypt
19
+ lvm
20
+ @state.partition
21
+ end
22
+
23
+ private
24
+
25
+ def clear_struct
26
+ oldvg = `vgdisplay | grep #{@vg}`.chomp
27
+ exec("vgremove -f #{@vg}") if oldvg != '' # remove older volume group
28
+ exec("pvremove -f #{@dev_root}") if oldvg != '' and File.exist? @dev_root # remove older volume group
29
+
30
+ exec("sgdisk -Z /dev/#{@disk}")
31
+ exec("wipefs -a /dev/#{@disk}")
32
+ end
33
+
34
+ def cleaning
35
+ puts
36
+ print "Cleaning data on #{@disk}, can be long, avoid this on Flash Memory (SSD,USB,...) ? (n,y) "
37
+ case gets.chomp
38
+ when /^y|^Y/
39
+ bloc=`blockdev --getbsz /dev/#{@disk}`.chomp
40
+ exec("dd if=/dev/urandom of=/dev/#{@disk} bs=#{bloc} status=progress")
41
+ else
42
+ return
43
+ end
44
+ end
45
+
46
+ def partition
47
+ if Helpers::efi?
48
+ exec("sgdisk -n1:1M:+260M -t1:EF00 /dev/#{@disk}")
49
+ exec("sgdisk -n2:0:+0 -t2:8e00 /dev/#{@disk}")
50
+ else
51
+ exec("sgdisk -n1:1MiB:+1MiB -t1:EF02 /dev/#{@disk}")
52
+ exec("sgdisk -n2:0:+128MiB -t2:8300 /dev/#{@disk}")
53
+ exec("sgdisk -n3:0:+0 -t3:8e00 /dev/#{@disk}")
54
+ end
55
+ end
56
+
57
+ def encrypt
58
+ @log.info("Format root")
59
+ Helpers::sys("cryptsetup luksFormat #{@dev_root}")
60
+ @log.debug("Opening root")
61
+ Helpers::sys("cryptsetup open --type luks #{@dev_root} cryptroot")
62
+ end
63
+
64
+ def lvm
65
+ mem=`awk '/MemTotal/ {print $2}' /proc/meminfo`.chomp + 'K'
66
+ exec("pvcreate -f #{@luks_root}")
67
+ exec("vgcreate -f #{@vg} #{@luks_root}")
68
+ # Wipe old signature: https://github.com/chef-cookbooks/lvm/issues/45
69
+ exec("lvcreate -y -Wy -Zy -L 15G -n root #{@vg}")
70
+ exec("lvcreate -y -Wy -Zy -L #{mem} -n swap #{@vg}")
71
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n home #{@vg}") if @user
72
+ exec("vgchange --available y")
73
+ end
74
+
75
+ # Follow https://wiki.archlinux.org/index.php/Partitioning
76
+ # Partition_efi
77
+ # /boot/efi - EFI system partition - 260MB
78
+ # / - Root
79
+
80
+ # Partition_bios
81
+ # None - Bios Boot Partition - 1MiB
82
+ # /boot - Boot - 8300
83
+ # / - Root
84
+
85
+ def exec(cmd)
86
+ Getch::Command.new(cmd).run!
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,25 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Lvm
4
+ class Format < Getch::FileSystem::Lvm::Device
5
+ def initialize
6
+ super
7
+ @fs = 'ext4'
8
+ @state = Getch::States.new()
9
+ format
10
+ end
11
+
12
+ def format
13
+ return if STATES[:format]
14
+ puts "Format #{@disk} with #{@fs}"
15
+ system("mkfs.fat -F32 #{@dev_boot_efi}") if @dev_boot_efi
16
+ system("mkfs.#{@fs} -F #{@dev_boot}") if @dev_boot
17
+ system("mkswap -f #{@lv_swap}")
18
+ system("mkfs.#{@fs} -F #{@lv_root}")
19
+ system("mkfs.#{@fs} -F #{@lv_home}") if @lv_home
20
+ @state.format
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,62 @@
1
+ require 'fileutils'
2
+
3
+ module Getch
4
+ module FileSystem
5
+ module Lvm
6
+ class Mount < Getch::FileSystem::Lvm::Device
7
+ def initialize
8
+ super
9
+ @root_dir = MOUNTPOINT
10
+ @boot_dir = "#{@root_dir}/boot"
11
+ @boot_efi_dir = "#{@root_dir}/boot/efi"
12
+ @home_dir = @user ? "#{@root_dir}/home/#{@user}" : nil
13
+ @state = Getch::States.new()
14
+ end
15
+
16
+ def run
17
+ return if STATES[:mount]
18
+ mount_swap
19
+ mount_root
20
+ mount_boot
21
+ mount_home
22
+ mount_boot_efi
23
+ @state.mount
24
+ end
25
+
26
+ private
27
+
28
+ def mount_swap
29
+ return if ! @lv_swap
30
+ system("swapon #{@lv_swap}")
31
+ end
32
+
33
+ def mount_root
34
+ return if ! @lv_root
35
+ Dir.mkdir(@root_dir, 0700) if ! Dir.exist?(@root_dir)
36
+ system("mount #{@lv_root} #{@root_dir}")
37
+ end
38
+
39
+ def mount_boot_efi
40
+ return if ! @dev_boot_efi
41
+ FileUtils.mkdir_p @boot_efi_dir, mode: 0700 if ! Dir.exist?(@boot_efi_dir)
42
+ system("mount #{@dev_boot_efi} #{@boot_efi_dir}")
43
+ end
44
+
45
+ def mount_boot
46
+ return if ! @dev_boot
47
+ FileUtils.mkdir_p @boot_dir, mode: 0700 if ! Dir.exist?(@boot_dir)
48
+ system("mount #{@dev_boot} #{@boot_dir}")
49
+ end
50
+
51
+ def mount_home
52
+ return if ! @lv_home
53
+ if @user != nil then
54
+ FileUtils.mkdir_p @home_dir, mode: 0700 if ! Dir.exist?(@home_dir)
55
+ system("mount #{@lv_home} #{@home_dir}")
56
+ end
57
+ @state.mount
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,81 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Lvm
4
+ class Partition < Getch::FileSystem::Lvm::Device
5
+ def initialize
6
+ super
7
+ @state = Getch::States.new()
8
+ run_partition
9
+ end
10
+
11
+ def run_partition
12
+ return if STATES[:partition ]
13
+ clear_struct
14
+ cleaning
15
+ partition
16
+ lvm
17
+ @state.partition
18
+ end
19
+
20
+ private
21
+
22
+ def clear_struct
23
+ oldvg = `vgdisplay | grep #{@vg}`.chomp
24
+ exec("vgremove -f #{@vg}") if oldvg != '' # remove older volume group
25
+ exec("pvremove -f #{@dev_root}") if oldvg != '' and File.exist? @dev_root # remove older volume group
26
+
27
+ exec("sgdisk -Z /dev/#{@disk}")
28
+ exec("wipefs -a /dev/#{@disk}")
29
+ end
30
+
31
+ def cleaning
32
+ puts
33
+ print "Cleaning data on #{@disk}, can be long, avoid this on Flash Memory (SSD,USB,...) ? (n,y) "
34
+ case gets.chomp
35
+ when /^y|^Y/
36
+ bloc=`blockdev --getbsz /dev/#{@disk}`.chomp
37
+ exec("dd if=/dev/urandom of=/dev/#{@disk} bs=#{bloc} status=progress")
38
+ else
39
+ return
40
+ end
41
+ end
42
+
43
+ def partition
44
+ if Helpers::efi?
45
+ exec("sgdisk -n1:1M:+260M -t1:EF00 /dev/#{@disk}")
46
+ exec("sgdisk -n2:0:+0 -t2:8e00 /dev/#{@disk}")
47
+ else
48
+ exec("sgdisk -n1:1MiB:+1MiB -t1:EF02 /dev/#{@disk}")
49
+ exec("sgdisk -n2:0:+128MiB -t2:8300 /dev/#{@disk}")
50
+ exec("sgdisk -n3:0:+0 -t3:8e00 /dev/#{@disk}")
51
+ end
52
+ end
53
+
54
+ def lvm
55
+ mem=`awk '/MemTotal/ {print $2}' /proc/meminfo`.chomp + 'K'
56
+ exec("pvcreate -f #{@dev_root}")
57
+ exec("vgcreate -f #{@vg} #{@dev_root}")
58
+ # Wipe old signature: https://github.com/chef-cookbooks/lvm/issues/45
59
+ exec("lvcreate -y -Wy -Zy -L 15G -n root #{@vg}")
60
+ exec("lvcreate -y -Wy -Zy -L #{mem} -n swap #{@vg}")
61
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n home #{@vg}") if @user
62
+ exec("vgchange --available y")
63
+ end
64
+
65
+ # Follow https://wiki.archlinux.org/index.php/Partitioning
66
+ # Partition_efi
67
+ # /boot/efi - EFI system partition - 260MB
68
+ # / - Root
69
+
70
+ # Partition_bios
71
+ # None - Bios Boot Partition - 1MiB
72
+ # /boot - Boot - 8300
73
+ # / - Root
74
+
75
+ def exec(cmd)
76
+ Getch::Command.new(cmd).run!
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,14 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Zfs
4
+ end
5
+ end
6
+ end
7
+
8
+ require_relative 'zfs/device'
9
+ require_relative 'zfs/partition'
10
+ require_relative 'zfs/format'
11
+ require_relative 'zfs/mount'
12
+ require_relative 'zfs/config'
13
+ require_relative 'zfs/deps'
14
+ require_relative 'zfs/encrypt'
@@ -0,0 +1,58 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Zfs
4
+ class Config < Getch::FileSystem::Zfs::Device
5
+ def initialize
6
+ super
7
+ gen_uuid
8
+ @root_dir = MOUNTPOINT
9
+ @init = '/usr/lib/systemd/systemd'
10
+ end
11
+
12
+ def fstab
13
+ file = "#{@root_dir}/etc/fstab"
14
+ datas = data_fstab
15
+ File.write(file, datas.join("\n"))
16
+ end
17
+
18
+ def systemd_boot
19
+ return if ! Helpers::efi?
20
+ esp = '/boot/efi'
21
+ dir = "#{@root_dir}/#{esp}/loader/entries/"
22
+ datas_gentoo = [
23
+ 'title Gentoo Linux',
24
+ 'linux /vmlinuz',
25
+ 'initrd /initramfs',
26
+ "options resume=UUID=#{@uuid_swap} root=ZFS=#{@pool_name}/ROOT/gentoo init=#{@init} dozfs"
27
+ ]
28
+ File.write("#{dir}/gentoo.conf", datas_gentoo.join("\n"))
29
+ end
30
+
31
+ # See https://wiki.gentoo.org/wiki/ZFS#ZFS_root
32
+ def grub
33
+ return if Helpers::efi?
34
+ file = "#{@root_dir}/etc/default/grub"
35
+ cmdline = [
36
+ "GRUB_CMDLINE_LINUX=\"resume=UUID=#{@uuid_swap} root=ZFS=#{@pool_name}/ROOT/gentoo init=#{@init} dozfs\""
37
+ ]
38
+ File.write("#{file}", cmdline.join("\n"), mode: 'a')
39
+ end
40
+
41
+ private
42
+
43
+ def gen_uuid
44
+ @uuid_swap = `lsblk -o "UUID" #{@dev_swap} | tail -1`.chomp()
45
+ @uuid_boot = `lsblk -o "UUID" #{@dev_boot} | tail -1`.chomp() if @dev_boot
46
+ @uuid_boot_efi = `lsblk -o "UUID" #{@dev_boot_efi} | tail -1`.chomp() if @dev_boot_efi
47
+ end
48
+
49
+ def data_fstab
50
+ boot_efi = @dev_boot_efi ? "UUID=#{@uuid_boot_efi} /boot/efi vfat noauto,noatime 1 2" : ''
51
+ swap = @dev_swap ? "UUID=#{@uuid_swap} none swap discard 0 0" : ''
52
+
53
+ [ boot_efi, swap ]
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end