Module: Loopless

Defined in:
lib/loopless.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.binary_pathObject



21
22
23
# File 'lib/loopless.rb', line 21

def self.binary_path
  File.expand_path("../../#{platform}/loopless",__FILE__)
end

.create(file, size) ⇒ Object



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

def self.create(file,size)
  run file, 'create', size.to_s
  nil
end

.create_gpt(file) ⇒ Object



39
40
41
42
# File 'lib/loopless.rb', line 39

def self.create_gpt(file)
  run file, 'create-gpt'
  nil
end

.create_part(file, part_num: nil, start: nil, size: nil, label: nil) ⇒ Object



44
45
46
47
# File 'lib/loopless.rb', line 44

def self.create_part(file, part_num: nil, start: nil, size: nil, label: nil)
  run file, 'create-part', *[part_num||0,start||0,size||0,label].compact.map(&:to_s)
  nil
end

.create_vmdk(file, vmdk_path) ⇒ Object



34
35
36
37
# File 'lib/loopless.rb', line 34

def self.create_vmdk(file,vmdk_path)
  run file, 'create-vmdk', vmdk_path
  nil
end

.fix_path(path) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/loopless.rb', line 129

def self.fix_path(path)
  if path[0] == '/'
    path
  else
    '/' + path
  end
end

.fmt_part(file, part) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/loopless.rb', line 137

def self.fmt_part(file,part)
  if part == nil
    file
  elsif part.respond_to? :to_i
    "#{file}:#{part.to_i}"
  elsif part.respond_to?(:to_a) && (part=part.to_a) && part.size == 2 && part.all?{|e| e.is_a? Integer}
    "#{file}:#{part[0]},#{part[1]}"
  else
    raise ArgumentError.new "Invalid loopless part: #{part.inspect}"
  end
end

.install_mbr(file) ⇒ Object



62
63
64
65
# File 'lib/loopless.rb', line 62

def self.install_mbr(file)
  run file, 'install-mbr'
  nil
end

.ls(file, part = nil) ⇒ Object



94
95
96
# File 'lib/loopless.rb', line 94

def self.ls(file,part=nil)
  run_part(file,part,'ls').split("\n")
end

.mkfs(file, *args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/loopless.rb', line 77

def self.mkfs(file,*args)
  part = nil
  label = []
  if args.size == 1
    if args[0].is_a?(Array) || args[0].is_a?(Integer)
      part = args[0]
    else
      label = args
    end
  elsif args.size == 2
    part,label[0] = *args
  end

  run_part file,part,'mkfs', *label
  nil
end

.part_info(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/loopless.rb', line 49

def self.part_info(file)
  run(file, 'part-info').split("\n").map do |l|
    fields = l.split("\t")
    {
      number: fields[0].to_i,
      start: fields[1].to_i,
      size: fields[2].to_i,
      bootable: fields[3] == 'true',
      label: fields[4] != '' ? fields[4] : nil
    }
  end
end

.platformObject



11
12
13
14
15
16
17
18
19
# File 'lib/loopless.rb', line 11

def self.platform
  if RUBY_PLATFORM =~ /darwin|mac os x/
    :darwin
  elsif RUBY_PLATFORM =~ /linux/
    :linux
  else
    raise RuntimeError.new "Invalid loopless platform. Only Mac/Linux supported."
  end
end

.read(file, part = nil, path) ⇒ Object



103
104
105
# File 'lib/loopless.rb', line 103

def self.read(file,part=nil,path)
  run_part file,part,'read',fix_path(path)
end

.rm(file, part = nil, path) ⇒ Object



98
99
100
101
# File 'lib/loopless.rb', line 98

def self.rm(file,part=nil,path)
  run_part file,part,'rm',fix_path(path)
  nil
end

.run(*command, **opts) ⇒ Object

Raises:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/loopless.rb', line 153

def self.run(*command, **opts)
  out_read, out_write = IO.pipe
  input = if opts[:in].respond_to? :fileno
            opts[:in]
          elsif opts[:in]
            in_read,in_write = IO.pipe
            in_read
          else
            '/dev/null'
          end
  pid = spawn(binary_path,*command,in: input, out: out_write,err: '/dev/null')
  out_write.close
  if in_write
    in_read.close
    in_write.write opts[:in]
    in_write.close
  end
  output = out_read.read.strip
  out_read.close
  Process.wait pid
  raise Loopless::Error.new($?.exitstatus) unless $?.success?
  output
end

.run_part(file, part, *command, **opts) ⇒ Object



149
150
151
# File 'lib/loopless.rb', line 149

def self.run_part(file,part,*command,**opts)
  run(fmt_part(file,part),*command,**opts)
end

.set_bootable(file, part_num, value = true) ⇒ Object



67
68
69
70
# File 'lib/loopless.rb', line 67

def self.set_bootable(file,part_num,value = true)
  run "#{file}:#{part_num}", "set-bootable", (value ? 'true' : 'false')
  nil
end

.syslinux(file, part = nil) ⇒ Object



72
73
74
75
# File 'lib/loopless.rb', line 72

def self.syslinux(file,part=nil)
  run_part file,part,'syslinux'
  nil
end

.versionObject



25
26
27
# File 'lib/loopless.rb', line 25

def self.version
  @version ||= run('version')
end

.write(file, *args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/loopless.rb', line 107

def self.write(file,*args)
  part = nil
  perm = '644'
  case args.size
  when 2 then path,input = args
  when 3
    has_perm = args[1].to_s =~ /^[0-8]{3}$/
    has_part = args[0].is_a?(Integer) || args[0].is_a?(Array)
    if has_perm && !has_part
      path,perm,input=args
    elsif !has_perm && has_part
      part,path,input=args
    else
      raise ArgumentError.new "Ambigious arguments"
    end
  when 4
    part,path,perm,input = args
  end
  run_part file,part,'write',fix_path(path),perm.to_s, in: input
  nil
end