KVM host image creator.Jessica Lockwood
修訂 | 0b71cee5ebe70b855ad785897d1526f5f0ef1923 (tree) |
---|---|
時間 | 2019-09-30 12:58:55 |
作者 | Tatsuki Sugiura <sugi@nemu...> |
Commiter | Tatsuki Sugiura |
Change grub install method, run gru-install.
@@ -52,8 +52,7 @@ class ImageCreator | ||
52 | 52 | if !use_gpt |
53 | 53 | system("parted", "-s", path, "set", "1", "boot", "on") or raise "Failed to set bios boot partition" |
54 | 54 | end |
55 | - puts "Image partition created." | |
56 | - #system "sfdisk", "-l", path | |
55 | + puts "Image partition has been created." | |
57 | 56 | end |
58 | 57 | |
59 | 58 | def with_loopdev &block |
@@ -113,17 +112,16 @@ class ImageCreator | ||
113 | 112 | def fix_boot |
114 | 113 | puts "Fixing boot environments..." |
115 | 114 | Dir.mktmpdir("ci-#{$$}-#{name}") do |dir| |
116 | - system("cp", "-a", "/usr/lib/grub/i386-pc/boot.img", dir) or raise "Failed to copy boot.img" | |
117 | - coreimg = "#{dir}/core.img" | |
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." | |
119 | 115 | with_loopdev do |devices| |
120 | 116 | puts "Override grub with host version..." |
121 | 117 | root_dev = "/dev/#{devices.first[/loop\d+/]}" |
122 | - system("grub-bios-setup", "-d", dir, root_dev) or raise "Failed to run grub-bios-setup" | |
123 | 118 | rootfs_uuid = dirs.find { |d| d.path == '/'}.fs_uuid |
124 | 119 | puts "New rootfs UUID=#{rootfs_uuid}" |
125 | 120 | begin |
126 | 121 | system("mount", devices.first, dir) or raise "Failed to mount #{devices.first} to #{dir}" |
122 | + system("mount", "--bind", "/dev", "#{dir}/dev") or raise "Failed to mount /dev to #{dir}/dev" | |
123 | + system("mount", "--bind", "/proc", "#{dir}/proc") or raise "Failed to mount /proc to #{dir}/proc" | |
124 | + | |
127 | 125 | dirs[1..-1].each_with_index do |di, idx| |
128 | 126 | system "mkdir", "-p", "#{dir}#{di.path}" |
129 | 127 | system("mount", di.device, "#{dir}#{di.path}") or raise "Failed to mount #{di.device} to #{dir}#{path}" |
@@ -144,30 +142,19 @@ class ImageCreator | ||
144 | 142 | system("chroot", dir, "apt-get", "-y", "install", "linux-image-amd64") |
145 | 143 | end |
146 | 144 | |
147 | - puts "Update grub.cfg..." | |
148 | - system("mkdir", "-p", "#{dir}/boot/grub/i386-pc") or raise "Failed to create grub dir" | |
149 | - system("cp -a /usr/lib/grub/i386-pc/*.mod #{dir}/boot/grub/i386-pc/") or raise "Failed to copy grub modules" | |
150 | - if File.exists? "#{dir}/boot/grub/grub.cfg" | |
151 | - grubconf = File.read "#{dir}/boot/grub/grub.cfg" | |
152 | - if old_uuid = grubconf[/root=UUID=(\S+)/, 1] | |
153 | - File.write "#{dir}/boot/grub/grub.cfg", grubconf.gsub(/#{old_uuid}/, rootfs_uuid) | |
154 | - end | |
155 | - else | |
156 | - system("chroot", dir, "apt-get", "-qy", "update") | |
145 | + puts "Update grub..." | |
146 | + unless File.exists? "#{dir}/boot/grub/grub.cfg" | |
147 | + system("chroot", dir, "apt-get", "-qy", "update") or raise "Failed to install grub-pc" | |
157 | 148 | system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt-get", "-y", "install", "grub-pc") |
158 | - File.write "#{dir}/boot/grub/grub.cfg", <<-EOC | |
159 | - set timeout=5 | |
160 | - insmod part_msdos | |
161 | - insmod ext2 | |
162 | - insmod linux | |
163 | - search --no-floppy --fs-uuid --set=root #{rootfs_uuid} | |
164 | - menuentry 'Linux' { | |
165 | - linux /vmlinuz root=UUID=#{rootfs_uuid} ro | |
166 | - initrd /initrd.img | |
167 | - } | |
168 | - EOC | |
169 | 149 | end |
150 | + File.open "#{dir}/boot/grub/device.map", "w" do |f| | |
151 | + f.puts "(hd0)\t#{root_dev}" | |
152 | + end | |
153 | + system("chroot", dir, "grub-mkconfig", "-o", "/boot/grub/grub.cfg") or raise "grub-mkconfig fails." | |
154 | + system(*%W(grub-install --no-floppy --grub-mkdevicemap=#{dir}/boot/grub/device.map --root-directory=#{dir} #{root_dev})) or raise "grub-install failed." | |
170 | 155 | ensure |
156 | + system("umount", "#{dir}/dev") | |
157 | + system("umount", "#{dir}/proc") | |
171 | 158 | dirs.reverse[0..-2].each do |di, idx| |
172 | 159 | system("umount", "#{dir}#{di.path}") |
173 | 160 | end |