• R/O
  • HTTP
  • SSH
  • HTTPS

image-creator: Commit

KVM host image creator.


Commit MetaInfo

Revisãof3d3a6ad0cc570d2603a32854b6348fc2fce44a2 (tree)
Hora2020-06-28 17:30:44
AutorTatsuki SUGIURA <sugi@osdn...>
CommiterTatsuki SUGIURA

Mensagem de Log

Add commannd runner option.

Mudança Sumário

Diff

--- a/create-image
+++ b/create-image
@@ -20,14 +20,15 @@ class SyncDirDef
2020 end
2121
2222 class ImageCreator
23- attr_accessor :name, :dirs, :src_host, :img_path_base
23+ attr_accessor :name, :dirs, :src_host, :img_path_base, :run_cmds
2424 MiB = 1024 ** 2
2525 GiB = 1024 ** 3
2626
27- def initialize(name, dirs, src_host: nil)
27+ def initialize(name, dirs, src_host: nil, run_cmds: nil)
2828 @name = name
2929 @dirs = dirs
3030 @src_host = src_host || name
31+ @run_cmds = run_cmds
3132 @img_path_base = "#{name}_#{Time.now.strftime '%FT%T%z'}"
3233 end
3334
@@ -168,6 +169,12 @@ class ImageCreator
168169 cfg = File.read "#{dir}/boot/grub/grub.cfg"
169170 cfg.gsub! %r{mapper/loop0p}, "sda"
170171 File.write "#{dir}/boot/grub/grub.cfg", cfg
172+
173+ unless Array(run_cmds).empty?
174+ Array(run_cmds).each do |cmd|
175+ system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, *Array(cmd)) or raise "Failed to execute command (#{cmd}): #{$!}"
176+ end
177+ end
171178 ensure
172179 system("umount", "#{dir}/dev")
173180 system("umount", "#{dir}/proc")
@@ -195,7 +202,6 @@ class ImageCreator
195202 File.write "#{img_path_base}.json", JSON.pretty_generate(jdef)
196203 end
197204
198-
199205 def run
200206 create_disk
201207 create_fs
@@ -218,12 +224,17 @@ if $0 == __FILE__
218224 list.each do |imgdef|
219225 name = nil
220226 dirs = []
227+ opts = {}
221228 if imgdef.kind_of?(Hash)
222229 name = imgdef['name']
223230 (imgdef['dirs'] || {}).each do |path, opts|
224231 opts.kind_of?(Hash) or opts = {size: opts}
225232 dirs << SyncDirDef.new({path: path}.merge(opts.keys.map(&:to_sym).zip(opts.values).to_h))
226233 end
234+ imgdef.keys.each do |k|
235+ next if %w[dirs name].member?(k)
236+ opts[k.to_sym] = imgdef[k]
237+ end
227238 else
228239 name = imgdef
229240 end
@@ -231,6 +242,6 @@ if $0 == __FILE__
231242 limit_pat.match?(name) or next
232243 end
233244 dirs.empty? and dirs << SyncDirDef.new
234- ImageCreator.new(name, dirs).run
245+ ImageCreator.new(name, dirs, **opts).run
235246 end
236247 end
Show on old repository browser