Module: Getch::Helpers
- Defined in:
- lib/getch/helpers.rb
Overview
Various helpers function defined here
Defined Under Namespace
Modules: Void
Class Method Summary collapse
- .efi? ⇒ Boolean
- .exec_or_die(cmd) ⇒ Object
-
.get_bs(path) ⇒ Object
get the sector size of a disk.
- .get_dm(name) ⇒ Object
- .get_file_online(url, dest) ⇒ Object
-
.get_id(dev) ⇒ Object
Used by ZFS for the pool creation sleep is necessary here at least the first time.
- .get_memory ⇒ Object
- .grub? ⇒ Boolean
- .id(dev) ⇒ Object
- .mount_all ⇒ Object
- .openrc? ⇒ Boolean
- .partuuid(dev) ⇒ Object
- .runit? ⇒ Boolean
- .sys(cmd) ⇒ Object
- .systemd? ⇒ Boolean
-
.systemd_minimal? ⇒ Boolean
if systemd without encryption.
- .uuid(dev) ⇒ Object
Class Method Details
.efi? ⇒ Boolean
11 12 13 |
# File 'lib/getch/helpers.rb', line 11 def self.efi? Dir.exist? '/sys/firmware/efi/efivars' end |
.exec_or_die(cmd) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/getch/helpers.rb', line 44 def self.exec_or_die(cmd) _, stderr, status = Open3.capture3(cmd) return if status.success? abort "Problem running #{cmd}, stderr was:\n#{stderr}" end |
.get_bs(path) ⇒ Object
get the sector size of a disk
118 119 120 121 |
# File 'lib/getch/helpers.rb', line 118 def self.get_bs(path) cmd = Getch::Command.new('blockdev', '--getpbsz', path) cmd.res end |
.get_dm(name) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/getch/helpers.rb', line 76 def self.get_dm(name) Dir.glob('/dev/mapper/*').each do |f| if f =~ /#{name}/ && f != '/dev/mapper/control' return File.readlink(f).tr('../', '') end end Log.new.fatal("Dm for #{name} is no found") end |
.get_file_online(url, dest) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/getch/helpers.rb', line 36 def self.get_file_online(url, dest) URI.open(url) do |l| File.open(dest, 'wb') { |f| f.write(l.read) } end rescue Net::OpenTimeout => e abort "DNS error #{e}" end |
.get_id(dev) ⇒ Object
Used by ZFS for the pool creation sleep is necessary here at least the first time
87 88 89 90 91 92 93 94 |
# File 'lib/getch/helpers.rb', line 87 def self.get_id(dev) sleep 3 Dir.glob('/dev/disk/by-id/*').each do |f| p = File.readlink(f) return f.delete_prefix('/dev/disk/by-id/') if p.match?(/#{dev}/) end Log.new.fatal("ID on #{dev} is no found") end |
.get_memory ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/getch/helpers.rb', line 106 def self.get_memory mem = nil File.open('/proc/meminfo').each do |l| t = l.split(' ') if l =~ /memtotal/i t && mem = t[1] end mem_to_mb = mem.to_i / 1024 || Log.new.fatal('get_memory - failed to get memory') mem_to_mb.to_s end |
.grub? ⇒ Boolean
27 28 29 |
# File 'lib/getch/helpers.rb', line 27 def self.grub? File.exist? "#{OPTIONS[:mountpoint]}/etc/default/grub" end |
.id(dev) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/getch/helpers.rb', line 68 def self.id(dev) Dir.glob('/dev/disk/by-id/*').each do |f| p = File.readlink(f) return f.delete_prefix('/dev/disk/by-id/') if p.match?(/#{dev}/) end Log.new.fatal("ID on #{dev} is no found") end |
.mount_all ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/getch/helpers.rb', line 97 def self.mount_all dest = OPTIONS[:mountpoint] NiTo.mount '--types proc /proc', "#{dest}/proc" %w[dev sys run].each do |d| NiTo.mount '--rbind', "/#{d}", "#{dest}/#{d}" NiTo.mount '--make-rslave', "#{dest}/#{d}" end end |
.openrc? ⇒ Boolean
19 20 21 |
# File 'lib/getch/helpers.rb', line 19 def self.openrc? File.exist? "#{OPTIONS[:mountpoint]}/etc/conf.d/keymaps" end |
.partuuid(dev) ⇒ Object
56 57 58 |
# File 'lib/getch/helpers.rb', line 56 def self.partuuid(dev) `lsblk -o PARTUUID #{dev}`.match(/\w+-\w+-\w+-\w+-\w+/) end |
.runit? ⇒ Boolean
23 24 25 |
# File 'lib/getch/helpers.rb', line 23 def self.runit? Dir.exist? "#{OPTIONS[:mountpoint]}/etc/runit" end |
.sys(cmd) ⇒ Object
51 52 53 54 |
# File 'lib/getch/helpers.rb', line 51 def self.sys(cmd) system(cmd) $?.success? || abort("Error with #{cmd}") end |
.systemd? ⇒ Boolean
15 16 17 |
# File 'lib/getch/helpers.rb', line 15 def self.systemd? Dir.exist? "#{OPTIONS[:mountpoint]}/etc/systemd" end |
.systemd_minimal? ⇒ Boolean
if systemd without encryption
32 33 34 |
# File 'lib/getch/helpers.rb', line 32 def self.systemd_minimal? systemd? && efi? && !OPTIONS[:encrypt] end |