getch 0.0.4 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) 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 +40 -5
  5. data/README.md +31 -2
  6. data/bin/setup.sh +25 -11
  7. data/lib/getch.rb +41 -30
  8. data/lib/getch/command.rb +156 -0
  9. data/lib/getch/filesystem.rb +7 -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/gentoo.rb +4 -2
  39. data/lib/getch/gentoo/boot.rb +46 -11
  40. data/lib/getch/gentoo/chroot.rb +18 -14
  41. data/lib/getch/gentoo/config.rb +24 -9
  42. data/lib/getch/gentoo/sources.rb +54 -29
  43. data/lib/getch/gentoo/stage.rb +2 -2
  44. data/lib/getch/helpers.rb +28 -1
  45. data/lib/getch/log.rb +54 -0
  46. data/lib/getch/options.rb +16 -7
  47. data/lib/getch/version.rb +1 -1
  48. metadata +34 -5
  49. metadata.gz.sig +3 -4
  50. data/lib/getch/disk.rb +0 -77
  51. data/lib/getch/mount.rb +0 -73
@@ -0,0 +1,7 @@
1
+ module Getch
2
+ module FileSystem
3
+ end
4
+ end
5
+
6
+ require_relative 'filesystem/ext4'
7
+ require_relative 'filesystem/lvm'
@@ -0,0 +1,14 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ end
5
+ end
6
+ end
7
+
8
+ require_relative 'ext4/device'
9
+ require_relative 'ext4/partition'
10
+ require_relative 'ext4/format'
11
+ require_relative 'ext4/mount'
12
+ require_relative 'ext4/config'
13
+ require_relative 'ext4/deps'
14
+ require_relative 'ext4/encrypt'
@@ -0,0 +1,59 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ class Config < Getch::FileSystem::Ext4::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
+ "options root=PARTUUID=#{@partuuid_root} init=#{@init} rw"
26
+ ]
27
+ File.write("#{dir}/gentoo.conf", datas_gentoo.join("\n"))
28
+ end
29
+
30
+ def grub
31
+ return if Helpers::efi?
32
+ file = "#{@root_dir}/etc/default/grub"
33
+ cmdline = "GRUB_CMDLINE_LINUX=\"resume=#{@dev_swap} root=#{@dev_root} init=#{@init} rw slub_debug=P page_poison=1 slab_nomerge pti=on vsyscall=none spectre_v2=on spec_store_bypass_disable=seccomp iommu=force\"\n"
34
+ File.write(file, cmdline, mode: 'a')
35
+ end
36
+
37
+ private
38
+
39
+ def gen_uuid
40
+ @partuuid_root = `lsblk -o "PARTUUID" #{@dev_root} | tail -1`.chomp() if @dev_root
41
+ @uuid_swap = `lsblk -o "UUID" #{@dev_swap} | tail -1`.chomp() if @dev_swap
42
+ @uuid_root = `lsblk -o "UUID" #{@dev_root} | tail -1`.chomp() if @dev_root
43
+ @uuid_boot = `lsblk -o "UUID" #{@dev_boot} | tail -1`.chomp() if @dev_boot
44
+ @uuid_boot_efi = `lsblk -o "UUID" #{@dev_boot_efi} | tail -1`.chomp() if @dev_boot_efi
45
+ @uuid_home = `lsblk -o "UUID" #{@dev_home} | tail -1`.chomp() if @dev_home
46
+ end
47
+
48
+ def data_fstab
49
+ boot_efi = @dev_boot_efi ? "UUID=#{@uuid_boot_efi} /boot/efi vfat noauto,noatime 1 2" : ''
50
+ swap = @dev_swap ? "UUID=#{@uuid_swap} none swap discard 0 0" : ''
51
+ root = @dev_root ? "UUID=#{@uuid_root} / ext4 defaults 0 1" : ''
52
+ home = @dev_home ? "UUID=#{@uuid_home} /home/#{@user} ext4 defaults 0 2" : ''
53
+
54
+ [ boot_efi, swap, root, home ]
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ class Deps
5
+ def initialize
6
+ if Helpers::efi?
7
+ install_efi
8
+ else
9
+ install_bios
10
+ end
11
+ end
12
+
13
+ private
14
+ def install_efi
15
+ end
16
+
17
+ def install_bios
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ class Device
5
+ def initialize
6
+ @disk = DEFAULT_OPTIONS[:disk]
7
+ @user = DEFAULT_OPTIONS[:username]
8
+ @dev_boot_efi = Helpers::efi? ? "/dev/#{@disk}1" : nil
9
+ @dev_root = "/dev/#{@disk}2"
10
+ @dev_swap = "/dev/#{@disk}3"
11
+ @dev_home = @user ? "/dev/#{@disk}4" : nil
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ module Encrypt
5
+ end
6
+ end
7
+ end
8
+ end
9
+
10
+ require_relative 'encrypt/device'
11
+ require_relative 'encrypt/partition'
12
+ require_relative 'encrypt/format'
13
+ require_relative 'encrypt/mount'
14
+ require_relative 'encrypt/config'
15
+ require_relative 'encrypt/deps'
@@ -0,0 +1,85 @@
1
+ require 'fileutils'
2
+
3
+ module Getch
4
+ module FileSystem
5
+ module Ext4
6
+ module Encrypt
7
+ class Config < Getch::FileSystem::Ext4::Encrypt::Device
8
+ def initialize
9
+ super
10
+ gen_uuid
11
+ @root_dir = MOUNTPOINT
12
+ @init = '/usr/lib/systemd/systemd'
13
+ move_secret_keys
14
+ crypttab
15
+ end
16
+
17
+ def fstab
18
+ file = "#{@root_dir}/etc/fstab"
19
+ datas = data_fstab
20
+ File.write(file, datas.join("\n"))
21
+ end
22
+
23
+ def systemd_boot
24
+ return if ! Helpers::efi?
25
+ esp = '/boot/efi'
26
+ dir = "#{@root_dir}/#{esp}/loader/entries/"
27
+ datas_gentoo = [
28
+ 'title Gentoo Linux',
29
+ 'linux /vmlinuz',
30
+ 'initrd /initramfs',
31
+ "options crypt_root=UUID=#{@uuid_root} root=/dev/mapper/root init=#{@init} keymap=#{DEFAULT_OPTIONS[:keymap]} rw"
32
+ ]
33
+ File.write("#{dir}/gentoo.conf", datas_gentoo.join("\n"))
34
+ end
35
+
36
+ def crypttab
37
+ home = @dev_home ? "crypthome UUID=#{@uuid_home} /root/secretkeys/crypto_keyfile.bin luks" : ''
38
+ datas = [
39
+ "cryptswap UUID=#{@uuid_swap} /dev/urandom swap,cipher=aes-xts-plain64:sha256,size=256",
40
+ home
41
+ ]
42
+ File.write("#{@root_dir}/etc/crypttab", datas.join("\n"))
43
+ end
44
+
45
+ def grub
46
+ return if Helpers::efi?
47
+ file = "#{@root_dir}/etc/default/grub"
48
+ cmdline = [
49
+ "GRUB_CMDLINE_LINUX=\"crypt_root=UUID=#{@uuid_dev_root} init=#{@init} rw slub_debug=P page_poison=1 slab_nomerge pti=on vsyscall=none spectre_v2=on spec_store_bypass_disable=seccomp iommu=force keymap=#{DEFAULT_OPTIONS[:keymap]}\"",
50
+ "GRUB_ENABLE_CRYPTODISK=y"
51
+ ]
52
+ File.write(file, cmdline.join("\n"), mode: 'a')
53
+ end
54
+
55
+ private
56
+
57
+ def gen_uuid
58
+ @partuuid_root = `lsblk -o "PARTUUID" #{@dev_root} | tail -1`.chomp() if @dev_root
59
+ @uuid_swap = `lsblk -o "UUID" #{@dev_swap} | tail -1`.chomp() if @dev_swap
60
+ @uuid_dev_root = `lsblk -d -o "UUID" #{@dev_root} | tail -1`.chomp() if @dev_root
61
+ @uuid_boot_efi = `lsblk -o "UUID" #{@dev_boot_efi} | tail -1`.chomp() if @dev_boot_efi
62
+ @uuid_root = `lsblk -d -o "UUID" #{@luks_root} | tail -1`.chomp() if @dev_root
63
+ @uuid_home = `lsblk -d -o "UUID" #{@dev_home} | tail -1`.chomp() if @luks_home
64
+ end
65
+
66
+ def data_fstab
67
+ boot_efi = @dev_boot_efi ? "UUID=#{@uuid_boot_efi} /boot/efi vfat noauto,noatime 1 2" : ''
68
+ swap = @dev_swap ? "#{@luks_swap} none swap discard 0 0 " : ''
69
+ root = @dev_root ? "UUID=#{@uuid_root} / ext4 defaults 0 1" : ''
70
+ home = @dev_home ? "#{@luks_home} /home/#{@user} ext4 defaults 0 2" : ''
71
+
72
+ [ boot_efi, swap, root, home ]
73
+ end
74
+
75
+ def move_secret_keys
76
+ return if ! @luks_home
77
+ puts "Moving secret keys"
78
+ keys_path = "#{@root_dir}/root/secretkeys"
79
+ FileUtils.mv("/root/secretkeys", keys_path) if ! Dir.exist?(keys_path)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,59 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ module Encrypt
5
+ class Deps
6
+ def initialize
7
+ if Helpers::efi?
8
+ install_efi
9
+ else
10
+ install_bios
11
+ end
12
+ install_deps
13
+ end
14
+
15
+ def make
16
+ genkernel
17
+ Getch::Make.new("genkernel --kernel-config=/usr/src/linux/.config all").run!
18
+ end
19
+
20
+ private
21
+ def install_efi
22
+ end
23
+
24
+ def genkernel
25
+ grub = Helpers::efi? ? 'BOOTLOADER="no"' : 'BOOTLOADER="grub2"'
26
+ datas = [
27
+ '',
28
+ grub,
29
+ 'INSTALL="yes"',
30
+ 'MENUCONFIG="no"',
31
+ 'CLEAN="yes"',
32
+ 'KEYMAP="yes"',
33
+ 'SAVE_CONFIG="yes"',
34
+ 'MOUNTBOOT="yes"',
35
+ 'MRPROPER="no"',
36
+ 'LUKS="yes"',
37
+ ]
38
+ file = "#{MOUNTPOINT}/etc/genkernel.conf"
39
+ File.write(file, datas.join("\n"), mode: 'a')
40
+ end
41
+
42
+ def install_bios
43
+ exec("euse -p sys-boot/grub -E device-mapper")
44
+ exec("euse -p sys-fs/cryptsetup -E luks1_default")
45
+ end
46
+
47
+ def install_deps
48
+ exec("euse -E cryptsetup") if ! Helpers::grep?("#{MOUNTPOINT}/etc/portage/make.conf", /cryptsetup/)
49
+ Getch::Emerge.new('genkernel sys-apps/systemd sys-fs/cryptsetup').pkg!
50
+ end
51
+
52
+ def exec(cmd)
53
+ Helpers::run_chroot(cmd, MOUNTPOINT)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,21 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
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_root = "/dev/#{@disk}2"
11
+ @dev_swap = "/dev/#{@disk}3"
12
+ @dev_home = @user ? "/dev/#{@disk}4" : nil
13
+ @luks_root = "/dev/mapper/cryptroot"
14
+ @luks_home = @user ? "/dev/mapper/crypthome" : nil
15
+ @luks_swap = "/dev/mapper/cryptswap"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ module Encrypt
5
+ class Format < Getch::FileSystem::Ext4::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 Helpers::efi?
17
+ exec("mkfs.#{@fs} -F #{@luks_root}")
18
+ exec("mkswap -f #{@dev_swap}")
19
+ exec("mkfs.#{@fs} -F #{@luks_home}") if @dev_home
20
+ @state.format
21
+ end
22
+
23
+ private
24
+
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 Ext4
6
+ module Encrypt
7
+ class Mount < Getch::FileSystem::Ext4::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 ! @dev_swap
31
+ system("swapon #{@dev_swap}")
32
+ end
33
+
34
+ def mount_root
35
+ return if ! @dev_root
36
+ Dir.mkdir(@root_dir, 0700) if ! Dir.exist?(@root_dir)
37
+ system("mount #{@luks_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 ! @dev_home
54
+ if @user != nil then
55
+ FileUtils.mkdir_p @home_dir, mode: 0700 if ! Dir.exist?(@home_dir)
56
+ system("mount #{@luks_home} #{@home_dir}")
57
+ end
58
+ @state.mount
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,116 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Ext4
4
+ module Encrypt
5
+ class Partition < Getch::FileSystem::Ext4::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
+ if Helpers::efi?
18
+ partition_efi
19
+ encrypt_efi
20
+ else
21
+ partition_bios
22
+ encrypt_bios
23
+ end
24
+ @state.partition
25
+ end
26
+
27
+ private
28
+
29
+ def clear_struct
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
+ # Follow https://wiki.archlinux.org/index.php/Partitioning
47
+ def partition_efi
48
+ # /boot/efi - EFI system partition - 260MB
49
+ # / - Root
50
+ # swap - Linux Swap - size of the ram
51
+ # /home - Home
52
+ mem=`awk '/MemTotal/ {print $2}' /proc/meminfo`.chomp + 'K'
53
+
54
+ exec("sgdisk -n1:1M:+260M -t1:EF00 /dev/#{@disk}")
55
+ exec("sgdisk -n2:0:+15G -t2:8309 /dev/#{@disk}")
56
+ exec("sgdisk -n3:0:+#{mem} -t3:8200 /dev/#{@disk}")
57
+ exec("sgdisk -n4:0:0 -t4:8309 /dev/#{@disk}") if @dev_home
58
+ end
59
+
60
+ def encrypt_efi
61
+ @log.info("Format root")
62
+ Helpers::sys("cryptsetup luksFormat #{@dev_root}")
63
+ @log.debug("Opening root")
64
+ Helpers::sys("cryptsetup open --type luks #{@dev_root} cryptroot")
65
+ encrypt_home
66
+ end
67
+
68
+ def encrypt_bios
69
+ @log.info("Format root for bios")
70
+ Helpers::sys("cryptsetup luksFormat --type luks1 #{@dev_root}")
71
+ @log.debug("Opening root")
72
+ Helpers::sys("cryptsetup open --type luks1 #{@dev_root} cryptroot")
73
+ encrypt_home
74
+ end
75
+
76
+ def encrypt_home
77
+ if @dev_home then
78
+ create_secret_keys
79
+ @log.info("Format home with #{@key_path}")
80
+ Helpers::sys("cryptsetup luksFormat #{@dev_home} #{@key_path}")
81
+ @log.debug("Open home with key #{@key_path}")
82
+ exec("cryptsetup open --type luks -d #{@key_path} #{@dev_home} crypthome")
83
+ end
84
+ end
85
+
86
+ def create_secret_keys
87
+ return if ! @dev_home
88
+ @log.info("Creating secret keys")
89
+ keys_dir = "/root/secretkeys"
90
+ key_name = "crypto_keyfile.bin"
91
+ @key_path = "#{keys_dir}/#{key_name}"
92
+ FileUtils.mkdir keys_dir, mode: 0700 if ! Dir.exist?(keys_dir)
93
+ Getch::Command.new("dd bs=512 count=4 if=/dev/urandom of=#{@key_path}").run!
94
+ end
95
+
96
+ def partition_bios
97
+ # None - Bios Boot Partition - 1MiB
98
+ # / - Root
99
+ # swap - Linux Swap - size of the ram
100
+ # /home - Home
101
+ mem=`awk '/MemTotal/ {print $2}' /proc/meminfo`.chomp + 'K'
102
+
103
+ exec("sgdisk -n1:1MiB:+1MiB -t1:EF02 /dev/#{@disk}")
104
+ exec("sgdisk -n2:0:+15G -t2:8309 /dev/#{@disk}")
105
+ exec("sgdisk -n3:0:+#{mem} -t3:8200 /dev/#{@disk}")
106
+ exec("sgdisk -n4:0:0 -t4:8309 /dev/#{@disk}") if @dev_home
107
+ end
108
+
109
+ def exec(cmd)
110
+ Getch::Command.new(cmd).run!
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end