KVM host image creator.Jessica Lockwood
リビジョン | dc529c373ce1b032966b80bdc8daf38b5891c4e0 (tree) |
---|---|
日時 | 2019-09-11 10:00:05 |
作者 | Tatsuki Sugiura <sugi@nemu...> |
コミッター | Tatsuki Sugiura |
Support for lxc host without kernel & grub.
@@ -96,7 +96,7 @@ class ImageCreator | ||
96 | 96 | begin |
97 | 97 | system("mount", dev, mount_point) or raise "Failed to mount file system #{dev} on #{mount_point}" |
98 | 98 | puts "Copying #{src_host}:#{di.srcpath} to #{dev}..." |
99 | - system("rsync", "-azHSAX", "--numeric-ids", "--info=progress2", "#{src_host}:#{di.srcpath}/", "#{mount_point}/", *((["--exclude"] * di.exclude.size).zip(di.exclude).flatten)) | |
99 | + system("rsync", "-azHSAX", "--numeric-ids", "--info=progress2", "#{src_host}:#{di.srcpath}/", "#{mount_point}/", *((["--exclude"] * di.exclude.size).zip(di.exclude).flatten)) or raise "rsync fails" | |
100 | 100 | ensure |
101 | 101 | system("umount", mount_point) |
102 | 102 | File.directory?(mount_point) and |
@@ -113,23 +113,45 @@ class ImageCreator | ||
113 | 113 | coreimg = "#{dir}/core.img" |
114 | 114 | system("grub-mkimage", "-o", coreimg, "-O", "i386-pc", "-p", "(hd0,gpt2)/boot/grub", "biosdisk", "part_gpt", "ext2", "gzio", "xzio", "lzopio") or raise "Failed to create grub core image." |
115 | 115 | with_loopdev do |devices, root| |
116 | - puts "Override grub..." | |
116 | + puts "Override grub with host version..." | |
117 | 117 | system("grub-bios-setup", "-d", dir, root) or raise "Failed to run grub-bios-setup" |
118 | 118 | rootfs_uuid=`blkid -o value -s UUID #{devices[1]}`.chomp("\n") |
119 | 119 | puts "New rootfs UUID=#{rootfs_uuid}" |
120 | 120 | begin |
121 | 121 | system("mount", devices[1], dir) |
122 | - system("cp -a /usr/lib/grub/i386-pc/*.mod #{dir}/boot/grub/i386-pc/") or raise "Failed to copy grub modules" | |
123 | - grubconf = File.read "#{dir}/boot/grub/grub.cfg" | |
124 | - if old_uuid = grubconf[/root=UUID=(\S+)/, 1] | |
125 | - File.write "#{dir}/boot/grub/grub.cfg", grubconf.gsub(/#{old_uuid}/, rootfs_uuid) | |
126 | - end | |
127 | 122 | |
128 | 123 | puts "Rewrite fstab..." |
129 | 124 | fstab = File.read "#{dir}/etc/fstab" |
130 | 125 | fstab.gsub!(%r{^(UUID=|/)\S+(\s+/\s+)}, "UUID=#{rootfs_uuid}\\2") |
131 | 126 | fstab.gsub!(%r{^(\S+\s+\S+\s+\S+\s+sw(?=\b))}, '#\1') |
132 | 127 | File.write "#{dir}/etc/fstab", fstab |
128 | + | |
129 | + unless File.exists? "#{dir}/vmlinuz" | |
130 | + system("chroot", dir, "apt-get", "-qy", "update") | |
131 | + system("chroot", dir, "apt-get", "-y", "install", "linux-image-amd64") | |
132 | + end | |
133 | + | |
134 | + puts "Update grub.cfg..." | |
135 | + system("mkdir", "-p", "#{dir}/boot/grub/i386-pc") or raise "Failed to create grub dir" | |
136 | + system("cp -a /usr/lib/grub/i386-pc/*.mod #{dir}/boot/grub/i386-pc/") or raise "Failed to copy grub modules" | |
137 | + if File.exists? "#{dir}/boot/grub/grub.cfg" | |
138 | + grubconf = File.read "#{dir}/boot/grub/grub.cfg" | |
139 | + if old_uuid = grubconf[/root=UUID=(\S+)/, 1] | |
140 | + File.write "#{dir}/boot/grub/grub.cfg", grubconf.gsub(/#{old_uuid}/, rootfs_uuid) | |
141 | + end | |
142 | + else | |
143 | + File.write "#{dir}/boot/grub/grub.cfg", <<-EOC | |
144 | + set timeout=5 | |
145 | + insmod part_gpt | |
146 | + insmod ext2 | |
147 | + insmod linux | |
148 | + search --no-floppy --fs-uuid --set=root #{rootfs_uuid} | |
149 | + menuentry 'Linux' { | |
150 | + linux /vmlinuz root=UUID=#{rootfs_uuid} ro | |
151 | + initrd /initrd.img | |
152 | + } | |
153 | + EOC | |
154 | + end | |
133 | 155 | ensure |
134 | 156 | system("umount", dir) |
135 | 157 | end |