getch 0.0.8 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) 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 +44 -0
  5. data/README.md +57 -8
  6. data/bin/setup.sh +4 -2
  7. data/lib/getch.rb +43 -15
  8. data/lib/getch/command.rb +26 -5
  9. data/lib/getch/config.rb +58 -0
  10. data/lib/getch/filesystem.rb +6 -0
  11. data/lib/getch/filesystem/clean.rb +51 -0
  12. data/lib/getch/filesystem/device.rb +61 -0
  13. data/lib/getch/filesystem/ext4.rb +1 -0
  14. data/lib/getch/filesystem/ext4/config.rb +8 -9
  15. data/lib/getch/filesystem/ext4/device.rb +2 -7
  16. data/lib/getch/filesystem/ext4/encrypt/config.rb +69 -47
  17. data/lib/getch/filesystem/ext4/encrypt/deps.rb +21 -15
  18. data/lib/getch/filesystem/ext4/encrypt/device.rb +5 -9
  19. data/lib/getch/filesystem/ext4/encrypt/format.rb +10 -6
  20. data/lib/getch/filesystem/ext4/encrypt/mount.rb +6 -43
  21. data/lib/getch/filesystem/ext4/encrypt/partition.rb +57 -55
  22. data/lib/getch/filesystem/ext4/format.rb +3 -5
  23. data/lib/getch/filesystem/ext4/mount.rb +7 -46
  24. data/lib/getch/filesystem/ext4/partition.rb +16 -39
  25. data/lib/getch/filesystem/lvm.rb +1 -0
  26. data/lib/getch/filesystem/lvm/config.rb +12 -15
  27. data/lib/getch/filesystem/lvm/deps.rb +5 -20
  28. data/lib/getch/filesystem/lvm/device.rb +33 -9
  29. data/lib/getch/filesystem/lvm/encrypt.rb +15 -0
  30. data/lib/getch/filesystem/lvm/encrypt/config.rb +71 -0
  31. data/lib/getch/filesystem/lvm/encrypt/deps.rb +46 -0
  32. data/lib/getch/filesystem/lvm/encrypt/device.rb +46 -0
  33. data/lib/getch/filesystem/lvm/encrypt/format.rb +32 -0
  34. data/lib/getch/filesystem/lvm/encrypt/mount.rb +25 -0
  35. data/lib/getch/filesystem/lvm/encrypt/partition.rb +80 -0
  36. data/lib/getch/filesystem/lvm/format.rb +11 -7
  37. data/lib/getch/filesystem/lvm/mount.rb +7 -46
  38. data/lib/getch/filesystem/lvm/partition.rb +19 -31
  39. data/lib/getch/filesystem/mount.rb +56 -0
  40. data/lib/getch/filesystem/partition.rb +77 -0
  41. data/lib/getch/filesystem/zfs.rb +14 -0
  42. data/lib/getch/filesystem/zfs/config.rb +57 -0
  43. data/lib/getch/filesystem/zfs/deps.rb +95 -0
  44. data/lib/getch/filesystem/zfs/device.rb +58 -0
  45. data/lib/getch/filesystem/zfs/encrypt.rb +15 -0
  46. data/lib/getch/filesystem/zfs/encrypt/config.rb +67 -0
  47. data/lib/getch/filesystem/zfs/encrypt/deps.rb +97 -0
  48. data/lib/getch/filesystem/zfs/encrypt/device.rb +60 -0
  49. data/lib/getch/filesystem/zfs/encrypt/format.rb +105 -0
  50. data/lib/getch/filesystem/zfs/encrypt/mount.rb +51 -0
  51. data/lib/getch/filesystem/zfs/encrypt/partition.rb +65 -0
  52. data/lib/getch/filesystem/zfs/format.rb +114 -0
  53. data/lib/getch/filesystem/zfs/mount.rb +48 -0
  54. data/lib/getch/filesystem/zfs/partition.rb +64 -0
  55. data/lib/getch/gentoo.rb +8 -4
  56. data/lib/getch/gentoo/boot.rb +32 -17
  57. data/lib/getch/gentoo/chroot.rb +12 -26
  58. data/lib/getch/gentoo/config.rb +37 -12
  59. data/lib/getch/gentoo/sources.rb +26 -29
  60. data/lib/getch/gentoo/use.rb +43 -0
  61. data/lib/getch/gentoo/use_flag.rb +64 -0
  62. data/lib/getch/helpers.rb +35 -13
  63. data/lib/getch/options.rb +23 -8
  64. data/lib/getch/version.rb +1 -1
  65. metadata +46 -18
  66. metadata.gz.sig +0 -0
@@ -0,0 +1,46 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Lvm
4
+ module Encrypt
5
+ class Device < Getch::FileSystem::Device
6
+ def initialize
7
+ super
8
+ @vg = 'vg0'
9
+ @lv_root = "/dev/#{@vg}/root"
10
+ @lv_swap = "/dev/#{@vg}/swap"
11
+ @lv_home = @home_disk ? "/dev/#{@vg}/home" : nil
12
+ @luks_root = "/dev/mapper/cryptroot"
13
+ @luks_home = @home_disk ? "/dev/mapper/crypthome" : nil
14
+ end
15
+
16
+ private
17
+
18
+ def search_boot
19
+ if @efi
20
+ if @boot_disk
21
+ @dev_esp = "/dev/#{@boot_disk}1"
22
+ else
23
+ @dev_esp = "/dev/#{@disk}1"
24
+ @root_part += 1
25
+ end
26
+ else
27
+ if @boot_disk
28
+ @dev_gpt = "/dev/#{@boot_disk}1"
29
+ @dev_boot = "/dev/#{@boot_disk}2"
30
+ @dev_grub = "/dev/#{@boot_disk}"
31
+ else
32
+ @dev_gpt = "/dev/#{@disk}1"
33
+ @dev_boot = "/dev/#{@disk}2"
34
+ @dev_grub = "/dev/#{@disk}"
35
+ @root_part += 2
36
+ end
37
+ end
38
+ end
39
+
40
+ def search_swap
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ 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}"
16
+ exec("mkfs.fat -F32 #{@dev_esp}") if @dev_esp
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,25 @@
1
+ module Getch
2
+ module FileSystem
3
+ module Lvm
4
+ module Encrypt
5
+ class Mount < Getch::FileSystem::Lvm::Encrypt::Device
6
+ def initialize
7
+ super
8
+ @mount = Getch::FileSystem::Mount.new
9
+ @state = Getch::States.new
10
+ end
11
+
12
+ def run
13
+ return if STATES[:mount]
14
+ @mount.swap(@lv_swap)
15
+ @mount.root(@lv_root)
16
+ @mount.boot(@dev_boot)
17
+ @mount.esp(@dev_esp)
18
+ @mount.home(@lv_home)
19
+ @state.mount
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,80 @@
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
+ @clean = Getch::FileSystem::Clean
10
+ @partition = Getch::FileSystem::Partition.new
11
+ @log = Log.new
12
+ run_partition
13
+ end
14
+
15
+ def run_partition
16
+ return if STATES[:partition ]
17
+ @clean.old_vg(@dev_root, @vg)
18
+ @clean.struct(@disk, @cache_disk, @home_disk)
19
+ @clean.hdd(@disk, @cache_disk, @home_disk)
20
+ partition
21
+ encrypt
22
+ lvm
23
+ @state.partition
24
+ end
25
+
26
+ private
27
+
28
+ def partition
29
+ if Helpers::efi?
30
+ @partition.efi(@dev_esp)
31
+ @partition.root(@dev_root, "8e00")
32
+ else
33
+ @partition.gpt(@dev_gpt)
34
+ @partition.boot(@dev_boot)
35
+ @partition.root(@dev_root, "8e00")
36
+ end
37
+ end
38
+
39
+ def encrypt
40
+ @log.info("Format root")
41
+ Helpers::sys("cryptsetup luksFormat #{@dev_root}")
42
+ @log.debug("Opening root")
43
+ Helpers::sys("cryptsetup open --type luks #{@dev_root} cryptroot")
44
+ end
45
+
46
+ def lvm
47
+ mem=`awk '/MemTotal/ {print $2}' /proc/meminfo`.chomp + 'K'
48
+ exec("pvcreate -f #{@luks_root}")
49
+ exec("vgcreate -f #{@vg} #{@luks_root}")
50
+ # Wipe old signature: https://github.com/chef-cookbooks/lvm/issues/45
51
+ exec("lvcreate -y -Wy -Zy -L #{mem} -n swap #{@vg}")
52
+
53
+ if @user
54
+ exec("lvcreate -y -Wy -Zy -L 18G -n root #{@vg}")
55
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n home #{@vg}")
56
+ else
57
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n root #{@vg}")
58
+ end
59
+
60
+ exec("vgchange --available y")
61
+ end
62
+
63
+ # Follow https://wiki.archlinux.org/index.php/Partitioning
64
+ # Partition_efi
65
+ # /efi - EFI system partition - 260MB
66
+ # / - Root
67
+
68
+ # Partition_bios
69
+ # None - Bios Boot Partition - 1MiB
70
+ # /boot - Boot - 8300
71
+ # / - Root
72
+
73
+ def exec(cmd)
74
+ Getch::Command.new(cmd).run!
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -4,21 +4,25 @@ module Getch
4
4
  class Format < Getch::FileSystem::Lvm::Device
5
5
  def initialize
6
6
  super
7
- @fs = 'ext4'
8
7
  @state = Getch::States.new()
9
8
  format
10
9
  end
11
10
 
12
11
  def format
13
12
  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
13
+ exec("mkfs.fat -F32 #{@dev_esp}") if @dev_esp
14
+ exec("mkfs.ext4 -F #{@dev_boot}") if @dev_boot
15
+ exec("mkswap -f #{@lv_swap}")
16
+ exec("mkfs.ext4 -F #{@lv_root}")
17
+ exec("mkfs.ext4 -F #{@lv_home}") if @lv_home
20
18
  @state.format
21
19
  end
20
+
21
+ private
22
+
23
+ def exec(cmd)
24
+ Getch::Command.new(cmd).run!
25
+ end
22
26
  end
23
27
  end
24
28
  end
@@ -1,59 +1,20 @@
1
- require 'fileutils'
2
-
3
1
  module Getch
4
2
  module FileSystem
5
3
  module Lvm
6
4
  class Mount < Getch::FileSystem::Lvm::Device
7
5
  def initialize
8
6
  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()
7
+ @mount = Getch::FileSystem::Mount.new
8
+ @state = Getch::States.new
14
9
  end
15
10
 
16
11
  def run
17
12
  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
13
+ @mount.swap(@lv_swap)
14
+ @mount.root(@lv_root)
15
+ @mount.boot(@dev_boot)
16
+ @mount.esp(@dev_esp)
17
+ @mount.home(@lv_home)
57
18
  @state.mount
58
19
  end
59
20
  end
@@ -5,13 +5,16 @@ module Getch
5
5
  def initialize
6
6
  super
7
7
  @state = Getch::States.new()
8
+ @partition = Getch::FileSystem::Partition.new
9
+ @clean = Getch::FileSystem::Clean
8
10
  run_partition
9
11
  end
10
12
 
11
13
  def run_partition
12
14
  return if STATES[:partition ]
13
- clear_struct
14
- cleaning
15
+ @clean.old_vg(@dev_root, @vg)
16
+ @clean.struct(@disk, @cache_disk, @home_disk)
17
+ @clean.hdd(@disk, @cache_disk, @home_disk)
15
18
  partition
16
19
  lvm
17
20
  @state.partition
@@ -19,35 +22,14 @@ module Getch
19
22
 
20
23
  private
21
24
 
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
25
  def partition
44
26
  if Helpers::efi?
45
- exec("sgdisk -n1:1M:+260M -t1:EF00 /dev/#{@disk}")
46
- exec("sgdisk -n2:0:+0 -t2:8e00 /dev/#{@disk}")
27
+ @partition.efi(@dev_esp)
28
+ @partition.root(@dev_root, "8e00")
47
29
  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}")
30
+ @partition.gpt(@dev_gpt)
31
+ @partition.boot(@dev_boot)
32
+ @partition.root(@dev_root, "8e00")
51
33
  end
52
34
  end
53
35
 
@@ -56,15 +38,21 @@ module Getch
56
38
  exec("pvcreate -f #{@dev_root}")
57
39
  exec("vgcreate -f #{@vg} #{@dev_root}")
58
40
  # Wipe old signature: https://github.com/chef-cookbooks/lvm/issues/45
59
- exec("lvcreate -y -Wy -Zy -L 15G -n root #{@vg}")
60
41
  exec("lvcreate -y -Wy -Zy -L #{mem} -n swap #{@vg}")
61
- exec("lvcreate -y -Wy -Zy -l 100%FREE -n home #{@vg}") if @user
42
+
43
+ if @user
44
+ exec("lvcreate -y -Wy -Zy -L 18G -n root #{@vg}")
45
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n home #{@vg}")
46
+ else
47
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n root #{@vg}")
48
+ end
49
+
62
50
  exec("vgchange --available y")
63
51
  end
64
52
 
65
53
  # Follow https://wiki.archlinux.org/index.php/Partitioning
66
54
  # Partition_efi
67
- # /boot/efi - EFI system partition - 260MB
55
+ # /efi - EFI system partition - 260MB
68
56
  # / - Root
69
57
 
70
58
  # Partition_bios
@@ -0,0 +1,56 @@
1
+ require 'fileutils'
2
+
3
+ module Getch
4
+ module FileSystem
5
+ class Mount
6
+ def initialize
7
+ @root_dir = MOUNTPOINT
8
+ @boot_dir = "#{@root_dir}/boot"
9
+ @boot_efi_dir = "#{@root_dir}/efi"
10
+ @home_dir = "#{@root_dir}/home"
11
+ @state = Getch::States.new()
12
+ @log = Getch::Log.new
13
+ end
14
+
15
+ def swap(dev)
16
+ return if ! dev
17
+ if Helpers::grep?('/proc/swaps', /^\/dev/)
18
+ exec("swapoff #{dev}")
19
+ end
20
+
21
+ exec("swapon #{dev}")
22
+ end
23
+
24
+ def root(dev)
25
+ return if ! dev
26
+ Helpers::mkdir(@root_dir)
27
+ exec("mount #{dev} #{@root_dir}")
28
+ end
29
+
30
+ def esp(dev)
31
+ return if ! dev
32
+ Helpers::mkdir(@boot_efi_dir)
33
+ exec("mount #{dev} #{@boot_efi_dir}")
34
+ end
35
+
36
+ def boot(dev)
37
+ return if ! dev
38
+ Helpers::mkdir(@boot_dir)
39
+ exec("mount #{dev} #{@boot_dir}")
40
+ end
41
+
42
+ def home(dev)
43
+ return if ! dev
44
+ Helpers::mkdir(@home_dir)
45
+ exec("mount #{dev} #{@home_dir}")
46
+ end
47
+
48
+ private
49
+
50
+ def exec(cmd)
51
+ @log.info("==> #{cmd}")
52
+ Helpers::sys(cmd)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,77 @@
1
+ module Getch
2
+ module FileSystem
3
+ class Partition
4
+ def initialize
5
+ @log = Getch::Log.new
6
+ end
7
+
8
+ def gpt(dev)
9
+ return if ! dev
10
+ disk = disk_name(dev)
11
+ part = dev.match(/[0-9]/)
12
+ exec("sgdisk -n#{part}:1MiB:+1MiB -t#{part}:EF02 #{disk}")
13
+ end
14
+
15
+ def boot(dev)
16
+ return if ! dev
17
+ disk = disk_name(dev)
18
+ part = dev.match(/[0-9]/)
19
+ if DEFAULT_OPTIONS[:fs] == "zfs"
20
+ exec("sgdisk -n#{part}:0:+2G -t#{part}:BE00 #{disk}")
21
+ else
22
+ exec("sgdisk -n#{part}:0:+128MiB -t#{part}:8300 #{disk}")
23
+ end
24
+ end
25
+
26
+ def efi(dev)
27
+ return if ! dev
28
+ disk = disk_name(dev)
29
+ part = dev.match(/[0-9]/)
30
+ exec("sgdisk -n#{part}:1M:+260M -t#{part}:EF00 #{disk}")
31
+ end
32
+
33
+ def swap(dev)
34
+ return if ! dev
35
+ disk = disk_name(dev)
36
+ part = dev.match(/[0-9]/)
37
+ if DEFAULT_OPTIONS[:cache_disk]
38
+ exec("sgdisk -n#{part}:0:0 -t#{part}:8200 #{disk}")
39
+ else
40
+ mem=`awk '/MemTotal/ {print $2}' /proc/meminfo`.chomp + 'K'
41
+ exec("sgdisk -n#{part}:0:+#{mem} -t#{part}:8200 #{disk}")
42
+ end
43
+ end
44
+
45
+ def root(dev, code)
46
+ return if ! dev
47
+ disk = disk_name(dev)
48
+ part = dev.match(/[0-9]/)
49
+ exec("sgdisk -n#{part}:0:0 -t#{part}:#{code} #{disk}")
50
+ end
51
+
52
+ def home(dev, code)
53
+ return if ! dev
54
+ disk = disk_name(dev)
55
+ part = dev.match(/[0-9]/)
56
+ if DEFAULT_OPTIONS[:home_disk]
57
+ exec("sgdisk -n#{part}:0:0 -t#{part}:#{code} #{disk}")
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def disk_name(dev)
64
+ dev.match(/[^0-9]+/)
65
+ end
66
+
67
+ def exec(cmd)
68
+ @log.debug "Partition disk with #{cmd}"
69
+ if DEFAULT_OPTIONS[:encrypt]
70
+ Helpers::sys(cmd)
71
+ else
72
+ Getch::Command.new(cmd).run!
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end