getch 0.0.9 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) 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 +45 -0
  5. data/README.md +57 -11
  6. data/bin/setup.sh +4 -2
  7. data/lib/getch.rb +27 -10
  8. data/lib/getch/command.rb +25 -4
  9. data/lib/getch/config.rb +58 -0
  10. data/lib/getch/filesystem.rb +6 -0
  11. data/lib/getch/filesystem/clean.rb +58 -0
  12. data/lib/getch/filesystem/device.rb +61 -0
  13. data/lib/getch/filesystem/ext4/config.rb +8 -9
  14. data/lib/getch/filesystem/ext4/device.rb +2 -7
  15. data/lib/getch/filesystem/ext4/encrypt/config.rb +8 -9
  16. data/lib/getch/filesystem/ext4/encrypt/deps.rb +3 -19
  17. data/lib/getch/filesystem/ext4/encrypt/device.rb +3 -8
  18. data/lib/getch/filesystem/ext4/encrypt/format.rb +3 -5
  19. data/lib/getch/filesystem/ext4/encrypt/mount.rb +6 -43
  20. data/lib/getch/filesystem/ext4/encrypt/partition.rb +19 -38
  21. data/lib/getch/filesystem/ext4/format.rb +3 -5
  22. data/lib/getch/filesystem/ext4/mount.rb +7 -46
  23. data/lib/getch/filesystem/ext4/partition.rb +16 -39
  24. data/lib/getch/filesystem/lvm/config.rb +11 -15
  25. data/lib/getch/filesystem/lvm/deps.rb +5 -20
  26. data/lib/getch/filesystem/lvm/device.rb +33 -9
  27. data/lib/getch/filesystem/lvm/encrypt/config.rb +9 -12
  28. data/lib/getch/filesystem/lvm/encrypt/deps.rb +5 -22
  29. data/lib/getch/filesystem/lvm/encrypt/device.rb +33 -9
  30. data/lib/getch/filesystem/lvm/encrypt/format.rb +3 -3
  31. data/lib/getch/filesystem/lvm/encrypt/mount.rb +7 -46
  32. data/lib/getch/filesystem/lvm/encrypt/partition.rb +20 -31
  33. data/lib/getch/filesystem/lvm/format.rb +11 -7
  34. data/lib/getch/filesystem/lvm/mount.rb +7 -46
  35. data/lib/getch/filesystem/lvm/partition.rb +19 -31
  36. data/lib/getch/filesystem/mount.rb +56 -0
  37. data/lib/getch/filesystem/partition.rb +77 -0
  38. data/lib/getch/filesystem/zfs.rb +14 -0
  39. data/lib/getch/filesystem/zfs/config.rb +57 -0
  40. data/lib/getch/filesystem/zfs/deps.rb +95 -0
  41. data/lib/getch/filesystem/zfs/device.rb +58 -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 +97 -0
  45. data/lib/getch/filesystem/zfs/encrypt/device.rb +60 -0
  46. data/lib/getch/filesystem/zfs/encrypt/format.rb +104 -0
  47. data/lib/getch/filesystem/zfs/encrypt/mount.rb +51 -0
  48. data/lib/getch/filesystem/zfs/encrypt/partition.rb +66 -0
  49. data/lib/getch/filesystem/zfs/format.rb +113 -0
  50. data/lib/getch/filesystem/zfs/mount.rb +48 -0
  51. data/lib/getch/filesystem/zfs/partition.rb +65 -0
  52. data/lib/getch/gentoo.rb +11 -4
  53. data/lib/getch/gentoo/boot.rb +34 -16
  54. data/lib/getch/gentoo/chroot.rb +16 -25
  55. data/lib/getch/gentoo/config.rb +59 -7
  56. data/lib/getch/gentoo/sources.rb +54 -26
  57. data/lib/getch/gentoo/use.rb +43 -0
  58. data/lib/getch/gentoo/use_flag.rb +64 -0
  59. data/lib/getch/guard.rb +62 -0
  60. data/lib/getch/helpers.rb +31 -13
  61. data/lib/getch/options.rb +24 -9
  62. data/lib/getch/version.rb +1 -1
  63. metadata +40 -18
  64. metadata.gz.sig +0 -0
@@ -6,14 +6,18 @@ module Getch
6
6
  def initialize
7
7
  super
8
8
  @state = Getch::States.new()
9
+ @clean = Getch::FileSystem::Clean
10
+ @partition = Getch::FileSystem::Partition.new
9
11
  @log = Log.new
10
12
  run_partition
11
13
  end
12
14
 
13
15
  def run_partition
14
16
  return if STATES[:partition ]
15
- clear_struct
16
- cleaning
17
+ @clean.old_vg(@dev_root, @vg)
18
+ @clean.hdd(@disk)
19
+ @clean.external_disk(@disk, @boot_disk, @cache_disk, @home_disk)
20
+
17
21
  partition
18
22
  encrypt
19
23
  lvm
@@ -22,35 +26,14 @@ module Getch
22
26
 
23
27
  private
24
28
 
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
29
  def partition
47
30
  if Helpers::efi?
48
- exec("sgdisk -n1:1M:+260M -t1:EF00 /dev/#{@disk}")
49
- exec("sgdisk -n2:0:+0 -t2:8e00 /dev/#{@disk}")
31
+ @partition.efi(@dev_esp)
32
+ @partition.root(@dev_root, "8e00")
50
33
  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}")
34
+ @partition.gpt(@dev_gpt)
35
+ @partition.boot(@dev_boot)
36
+ @partition.root(@dev_root, "8e00")
54
37
  end
55
38
  end
56
39
 
@@ -66,15 +49,21 @@ module Getch
66
49
  exec("pvcreate -f #{@luks_root}")
67
50
  exec("vgcreate -f #{@vg} #{@luks_root}")
68
51
  # Wipe old signature: https://github.com/chef-cookbooks/lvm/issues/45
69
- exec("lvcreate -y -Wy -Zy -L 15G -n root #{@vg}")
70
52
  exec("lvcreate -y -Wy -Zy -L #{mem} -n swap #{@vg}")
71
- exec("lvcreate -y -Wy -Zy -l 100%FREE -n home #{@vg}") if @user
53
+
54
+ if @user
55
+ exec("lvcreate -y -Wy -Zy -L 18G -n root #{@vg}")
56
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n home #{@vg}")
57
+ else
58
+ exec("lvcreate -y -Wy -Zy -l 100%FREE -n root #{@vg}")
59
+ end
60
+
72
61
  exec("vgchange --available y")
73
62
  end
74
63
 
75
64
  # Follow https://wiki.archlinux.org/index.php/Partitioning
76
65
  # Partition_efi
77
- # /boot/efi - EFI system partition - 260MB
66
+ # /efi - EFI system partition - 260MB
78
67
  # / - Root
79
68
 
80
69
  # Partition_bios
@@ -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.hdd(@disk)
17
+ @clean.external_disk(@disk, @boot_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
@@ -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,57 @@
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 = '/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_esp = `lsblk -o "UUID" #{@dev_esp} | tail -1`.chomp() if @dev_esp
46
+ end
47
+
48
+ def data_fstab
49
+ efi = @dev_esp ? "UUID=#{@uuid_esp} /efi vfat noauto,noatime 1 2" : ''
50
+ swap = @dev_swap ? "UUID=#{@uuid_swap} none swap discard 0 0" : ''
51
+
52
+ [ efi, swap ]
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end