KVM host image creator.Jessica Lockwood
Revision | 9b4cf3e12f8781ed0f00c94fa734e9f8b7bdc1e6 (tree) |
---|---|
Time | 2019-09-24 20:30:05 |
Author | Tatsuki Sugiura <sugi@nemu...> |
Commiter | Tatsuki Sugiura |
Update.
@@ -6,7 +6,7 @@ require 'yaml' | ||
6 | 6 | |
7 | 7 | class SyncDirDef |
8 | 8 | DEFAULT_EXCLUDE = %w[/proc/* /sys/* /dev/mqueue /dev/hugepages /run/* /var/lib/os-prober/mount /swap /dev/shm/* /var/lib/lxcfs/*] |
9 | - attr_accessor :path, :size, :exclude, :size, :srcpath, :fs_features, :device | |
9 | + attr_accessor :path, :size, :exclude, :size, :srcpath, :fs_features, :device, :fs_uuid | |
10 | 10 | def initialize(path: '/', size: 8, exclude: DEFAULT_EXCLUDE, srcpath: nil, fs_features: nil) |
11 | 11 | @path = path |
12 | 12 | @size = size.to_f |
@@ -14,6 +14,7 @@ class SyncDirDef | ||
14 | 14 | @srcpath = srcpath || path |
15 | 15 | @fs_features = fs_features |
16 | 16 | @device = nil |
17 | + @fs_uuid = nil | |
17 | 18 | end |
18 | 19 | end |
19 | 20 |
@@ -48,9 +49,7 @@ class ImageCreator | ||
48 | 49 | end |
49 | 50 | system("parted", "-s", path, "mklabel", use_gpt ? 'gpt' : 'msdos') or raise "Failed to create partition label" |
50 | 51 | system("parted", "-s", path, "mkpart", "primary", "1MiB", "#{size_gb * 1024 - 1}MiB") or raise "Failed to create partition" |
51 | - if use_gpt | |
52 | - system("parted", "-s", path, "name", "1", di.path) or raise "Failed to set part label" | |
53 | - else | |
52 | + if !use_gpt | |
54 | 53 | system("parted", "-s", path, "set", "1", "boot", "on") or raise "Failed to set bios boot partition" |
55 | 54 | end |
56 | 55 | puts "Image partition created." |
@@ -76,7 +75,8 @@ class ImageCreator | ||
76 | 75 | |
77 | 76 | def create_fs |
78 | 77 | with_loopdev do |devices| |
79 | - devices.each_with_index do |dev, index| | |
78 | + dirs.each_with_index do |di, index| | |
79 | + dev = di.device | |
80 | 80 | puts "Creating filesystem on #{dev}..." |
81 | 81 | cmd = %w(mkfs.ext4 -q) |
82 | 82 | di = dirs[index] |
@@ -85,6 +85,8 @@ class ImageCreator | ||
85 | 85 | end |
86 | 86 | cmd << dev |
87 | 87 | system(*cmd) or raise "Failed to create file system on #{dev}" |
88 | + system "e2label", dev, di.path == '/' ? 'ROOT' : di.path[1..-1].tr('/', '-') | |
89 | + di.fs_uuid = `blkid -o value -s UUID #{di.device}`.chomp("\n") | |
88 | 90 | end |
89 | 91 | end |
90 | 92 | end |
@@ -115,11 +117,10 @@ class ImageCreator | ||
115 | 117 | coreimg = "#{dir}/core.img" |
116 | 118 | system("grub-mkimage", "-o", coreimg, "-O", "i386-pc", "-p", "(hd0,msdos1)/boot/grub", "biosdisk", "part_msdos", "ext2", "gzio", "xzio", "lzopio") or raise "Failed to create grub core image." |
117 | 119 | with_loopdev do |devices| |
118 | - root_dev = "/dev/#{devices.first[/loop\d+/]}" | |
119 | 120 | puts "Override grub with host version..." |
121 | + root_dev = "/dev/#{devices.first[/loop\d+/]}" | |
120 | 122 | system("grub-bios-setup", "-d", dir, root_dev) or raise "Failed to run grub-bios-setup" |
121 | - fs_uuids = devices.map { |d| `blkid -o value -s UUID #{d}`.chomp("\n") } | |
122 | - rootfs_uuid = fs_uuids.first | |
123 | + rootfs_uuid = dirs.find { |d| d.path == '/'}.fs_uuid | |
123 | 124 | puts "New rootfs UUID=#{rootfs_uuid}" |
124 | 125 | begin |
125 | 126 | system("mount", devices.first, dir) or raise "Failed to mount #{devices.first} to #{dir}" |
@@ -132,10 +133,10 @@ class ImageCreator | ||
132 | 133 | |
133 | 134 | puts "Rewrite fstab..." |
134 | 135 | File.open "#{dir}/etc/fstab", "w" do |f| |
135 | - devices.map.with_index { |d, idx| | |
136 | - f << %W(UUID=#{fs_uuids[idx]} #{dirs[idx].path} ext4 defaults,noatime 0 #{dirs[idx].path == '/' ? 1 : 2}).join("\t") | |
136 | + dirs.each_with_index do |di, idx| | |
137 | + f << %W(UUID=#{di.fs_uuid} #{di.path} ext4 defaults,noatime 0 #{di.path == '/' ? 1 : 2}).join("\t") | |
137 | 138 | f << "\n" |
138 | - } | |
139 | + end | |
139 | 140 | end |
140 | 141 | |
141 | 142 | unless File.exists? "#{dir}/vmlinuz" |