Class: Luks::Main

Inherits:
Object
  • Object
show all
Includes:
Luks, NiTo
Defined in:
lib/luks.rb

Overview

define luks name, path, etc…

Direct Known Subclasses

Boot, Home, Root

Constant Summary collapse

Permission =
Class.new(StandardError)

Instance Method Summary collapse

Methods included from NiTo

cp, echo, echo_a, grep?, mkdir, mount, mount?, mv, rm, search_proc_swaps, sed, sh, swapoff, swapoff_dm, touch, umount

Constructor Details

#initialize(disk, options) ⇒ Main

Returns a new instance of Main.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/luks.rb', line 16

def initialize(disk, options)
  @disk = disk
  @format = options[:fs]
  @mountpoint = options[:mountpoint]
  @luks_type = nil
  @key_dir = nil
  @key_name = nil
  @mount = nil
  @bootloader = false
  @log = Getch::Log.new
  @bs = sector_size
end

Instance Method Details

#closeObject



93
94
95
96
97
98
# File 'lib/luks.rb', line 93

def close
  return unless File.exist? "/dev/mapper/#{@luks_name}"

  @log.info "Closing #{@luks_name}...\n"
  cmd_crypt 'cryptsetup', 'close', @luks_name
end

#encryptObject



29
30
31
32
33
# File 'lib/luks.rb', line 29

def encrypt
  args = @luks_type == 'luks2' ? "#{@command_args} --sector-size #{@bs}" : @command_args
  @log.info "Encrypting #{@luks_name} > #{@disk}...\n"
  cmd_crypt 'cryptsetup', 'luksFormat', args, "/dev/#{@disk}"
end

#encrypt_with_keyObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/luks.rb', line 35

def encrypt_with_key
  make_key
  args = if @luks_type == 'luks2'
           "#{@command_args} -q --sector-size #{@bs} -d #{@full_key_path}"
         else
           "#{@command_args} -q -d #{@full_key_path}"
         end
  @log.info "Encrypting #{@luks_name} with #{@full_key_path}...\n"
  cmd_crypt 'cryptsetup', 'luksFormat', args, "/dev/#{@disk}"
end

#external_keyObject



77
78
79
80
81
# File 'lib/luks.rb', line 77

def external_key
  make_key
  @log.info "Adding key for #{@luks_name}...\n"
  cmd_crypt 'cryptsetup', 'luksAddKey', "/dev/#{@disk}", @full_key_path
end

#formatObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/luks.rb', line 64

def format
  case @format
  when 'ext4'
    format_ext4
  when 'xfs'
    format_xfs
  when 'fat'
    format_fat
  else
    @log.fatal "#{@format} not yet supported."
  end
end

#gen_datasObject



100
# File 'lib/luks.rb', line 100

def gen_datas; end

#mountObject



88
89
90
91
# File 'lib/luks.rb', line 88

def mount
  mountpoint = @luks_name =~ /^root/ ? @mountpoint : "#{@mountpoint}#{@mount}"
  NiTo.mount "/dev/mapper/#{@luks_name}", mountpoint
end

#openObject



46
47
48
49
50
51
52
53
# File 'lib/luks.rb', line 46

def open
  return if File.exist? "/dev/mapper/#{@luks_name}"

  @log.info "Opening #{@luks_name} > #{@disk}...\n"
  cmd_crypt 'cryptsetup', 'open', @command_args, "/dev/#{@disk}", @luks_name

  raise "No dev /dev/mapper/#{@luks_name}, open it first..." unless File.exist? "/dev/mapper/#{@luks_name}"
end

#open_with_key(file = nil) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/luks.rb', line 55

def open_with_key(file = nil)
  return if File.exist? "/dev/mapper/#{@luks_name}"

  @full_key_path = "#{@mountpoint}#{@key_path}"
  key = file ? file : @full_key_path
  @log.info "Opening #{@luks_name} disk #{@disk} with #{key}...\n"
  cmd_crypt 'cryptsetup', 'open', @command_args, '-d', key, "/dev/#{@disk}", @luks_name
end

#write_configObject



83
84
85
86
# File 'lib/luks.rb', line 83

def write_config
  config
  perm
end