KVM host image creator.
Revision | f3d3a6ad0cc570d2603a32854b6348fc2fce44a2 (tree) |
---|---|
Time | 2020-06-28 17:30:44 |
Author | Tatsuki SUGIURA <sugi@osdn...> |
Commiter | Tatsuki SUGIURA |
Add commannd runner option.
@@ -20,14 +20,15 @@ class SyncDirDef | ||
20 | 20 | end |
21 | 21 | |
22 | 22 | class ImageCreator |
23 | - attr_accessor :name, :dirs, :src_host, :img_path_base | |
23 | + attr_accessor :name, :dirs, :src_host, :img_path_base, :run_cmds | |
24 | 24 | MiB = 1024 ** 2 |
25 | 25 | GiB = 1024 ** 3 |
26 | 26 | |
27 | - def initialize(name, dirs, src_host: nil) | |
27 | + def initialize(name, dirs, src_host: nil, run_cmds: nil) | |
28 | 28 | @name = name |
29 | 29 | @dirs = dirs |
30 | 30 | @src_host = src_host || name |
31 | + @run_cmds = run_cmds | |
31 | 32 | @img_path_base = "#{name}_#{Time.now.strftime '%FT%T%z'}" |
32 | 33 | end |
33 | 34 |
@@ -168,6 +169,12 @@ class ImageCreator | ||
168 | 169 | cfg = File.read "#{dir}/boot/grub/grub.cfg" |
169 | 170 | cfg.gsub! %r{mapper/loop0p}, "sda" |
170 | 171 | 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 | |
171 | 178 | ensure |
172 | 179 | system("umount", "#{dir}/dev") |
173 | 180 | system("umount", "#{dir}/proc") |
@@ -195,7 +202,6 @@ class ImageCreator | ||
195 | 202 | File.write "#{img_path_base}.json", JSON.pretty_generate(jdef) |
196 | 203 | end |
197 | 204 | |
198 | - | |
199 | 205 | def run |
200 | 206 | create_disk |
201 | 207 | create_fs |
@@ -218,12 +224,17 @@ if $0 == __FILE__ | ||
218 | 224 | list.each do |imgdef| |
219 | 225 | name = nil |
220 | 226 | dirs = [] |
227 | + opts = {} | |
221 | 228 | if imgdef.kind_of?(Hash) |
222 | 229 | name = imgdef['name'] |
223 | 230 | (imgdef['dirs'] || {}).each do |path, opts| |
224 | 231 | opts.kind_of?(Hash) or opts = {size: opts} |
225 | 232 | dirs << SyncDirDef.new({path: path}.merge(opts.keys.map(&:to_sym).zip(opts.values).to_h)) |
226 | 233 | end |
234 | + imgdef.keys.each do |k| | |
235 | + next if %w[dirs name].member?(k) | |
236 | + opts[k.to_sym] = imgdef[k] | |
237 | + end | |
227 | 238 | else |
228 | 239 | name = imgdef |
229 | 240 | end |
@@ -231,6 +242,6 @@ if $0 == __FILE__ | ||
231 | 242 | limit_pat.match?(name) or next |
232 | 243 | end |
233 | 244 | dirs.empty? and dirs << SyncDirDef.new |
234 | - ImageCreator.new(name, dirs).run | |
245 | + ImageCreator.new(name, dirs, **opts).run | |
235 | 246 | end |
236 | 247 | end |