KVM host image creator.
Revisão | 4376dd8af2371da3ba4dfc3acfffbd1beaea947f (tree) |
---|---|
Hora | 2020-12-28 23:10:56 |
Autor | Tatsuki SUGIURA <sugi@osdn...> |
Commiter | Tatsuki SUGIURA |
Add preparement for AWS env.
@@ -113,14 +113,107 @@ class ImageCreator | ||
113 | 113 | end |
114 | 114 | end |
115 | 115 | |
116 | + def prepare_awsenv | |
117 | + puts "Prepareing AWS cloud boot environment..." | |
118 | + with_rootfs do |dir, devices| | |
119 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, *%w(sudo dpkg --remove td-agent smartmontools snmpd lm-sensors arrayprobe megacli megacli-check-change megaclisas megaclisas-status fancontrol)) | |
120 | + | |
121 | + pkgs = %w(apt-transport-https locales-all ntp rsync python-apt git subversion less lv cloud-init tmux screen) | |
122 | + if File.read("#{dir}/etc/debian_version").to_f >= 9.0 | |
123 | + pkgs << 'cloud-guest-utils' | |
124 | + end | |
125 | + | |
126 | + system("chroot", dir, "apt-get", "-qy", "update") | |
127 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt-get", "-y", "--allow-unauthenticated", "install", *pkgs) | |
128 | + | |
129 | + if File.exists? "#{dir}//var/spool/nullmailer/queue" | |
130 | + puts "Remove nullmailer queue" | |
131 | + system("chroot", dir, "find", "/var/spool/nullmailer/queue", "-type", "f", "-name", '[0-9]*.[0-9]*', "-delete") | |
132 | + end | |
133 | + | |
134 | + unless system("chroot", dir, *%w(grep -rq cdn-aws.deb.debian.org /etc/apt/sources.list /etc/apt/sources.list.d)) | |
135 | + puts "Rewrite apt sources list for AWS CDN" | |
136 | + rel = `chroot #{dir} lsb_release -c -s`.chomp | |
137 | + s = [ | |
138 | + "deb http://cdn-aws.deb.debian.org/debian #{rel} main main contrib non-free", | |
139 | + "deb http://cdn-aws.deb.debian.org/debian #{rel}-updates main main contrib non-free", | |
140 | + ] | |
141 | + if File.exists? "#{dir}/etc/apt/sources.list" | |
142 | + s << File.read("#{dir}/etc/apt/sources.list") | |
143 | + end | |
144 | + File.write "#{dir}/etc/apt/sources.list", s.join("\n") | |
145 | + end | |
146 | + | |
147 | + unless File.read("#{dir}/etc/ntp.conf").include? "169.254.169.123" | |
148 | + puts "Rewrite ntp.conf" | |
149 | + c = File.read("#{dir}/etc/ntp.conf") | |
150 | + c.gsub!(/^(pool|server) /) { "##{$1}" } | |
151 | + c << "\nserver 169.254.169.123 prefer iburst\n" | |
152 | + File.write "#{dir}/etc/ntp.conf", c | |
153 | + end | |
154 | + end | |
155 | + end | |
156 | + | |
116 | 157 | def fix_boot |
117 | 158 | puts "Fixing boot environments..." |
159 | + with_rootfs do |dir, devices| | |
160 | + puts "Override grub with host version..." | |
161 | + root_dev = "/dev/#{devices.first[/loop\d+/]}" | |
162 | + rootfs_uuid = dirs.find { |d| d.path == '/'}.fs_uuid | |
163 | + puts "New rootfs UUID=#{rootfs_uuid}" | |
164 | + | |
165 | + system "rm", "-f", "#{dir}/etc/systemd/system/udev.service", "#{dir}/etc/systemd/system/systemd-udevd.service", "#{dir}/etc/udev/rules.d/70-persistent-net.rules" | |
166 | + | |
167 | + puts "Rewrite fstab..." | |
168 | + File.open "#{dir}/etc/fstab", "w" do |f| | |
169 | + dirs.each_with_index do |di, idx| | |
170 | + f << %W(UUID=#{di.fs_uuid} #{di.path} ext4 defaults,noatime 0 #{di.path == '/' ? 1 : 2}).join("\t") | |
171 | + f << "\n" | |
172 | + end | |
173 | + end | |
174 | + | |
175 | + system("chroot", dir, "apt-get", "-qy", "update") | |
176 | + if File.read("#{dir}/etc/debian_version").to_f >= 9.0 | |
177 | + # Note: 2019-10-08 時点で Debian9 のカーネルバージョンに対応していないので、エラー回避のために既存のカーネルを全て削除し、強制的に jessie のカーネルをイントールする | |
178 | + system("rm", "-f", "#{dir}/var/lib/dpkg/info/linux-image-#{`uname -r`.chomp}.prerm") | |
179 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "remove", "--purge", "-y", "linux-image-*") | |
180 | + system("wget", "-O", "#{dir}/tmp/linux.deb", "http://security-cdn.debian.org/debian-security/pool/updates/main/l/linux/linux-image-3.16.0-10-amd64_3.16.81-1_amd64.deb") or raise "Failed to get jessie kernel" | |
181 | + system("chroot", dir, "dpkg", "-i", "/tmp/linux.deb") | |
182 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "install", "-f", "-y") or raise "Failed to install jessie kernel" | |
183 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "-y", "install", "isc-dhcp-client") | |
184 | + else | |
185 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt-get", "-y", "--allow-unauthenticated", "install", "linux-image-amd64", "isc-dhcp-client") | |
186 | + end | |
187 | + | |
188 | + puts "Update grub..." | |
189 | + if File.exists? "#{dir}/var/lib/dpkg/info/grub-efi-amd64.list" | |
190 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "remove", "--purge", "-y", "grub-efi-amd64") or raise "Failed to purge grub-efi" | |
191 | + end | |
192 | + if !File.exists?("#{dir}/var/lib/dpkg/info/grub-pc.list") || !(File.exists?("#{dir}/usr/sbin/grub-bios-setup") || File.exists?("#{dir}/usr/sbin/grub-setup")) | |
193 | + system("chroot", dir, "apt-get", "-qy", "update") or raise "Failed to install grub-pc" | |
194 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt-get", "-y", "install", "grub-pc") | |
195 | + end | |
196 | + File.open "#{dir}/boot/grub/device.map", "w" do |f| | |
197 | + f.puts "(hd0)\t#{root_dev}" | |
198 | + end | |
199 | + system("chroot", dir, "grub-mkconfig", "-o", "/boot/grub/grub.cfg") or raise "grub-mkconfig fails." | |
200 | + system(*%W(grub-install --no-floppy --grub-mkdevicemap=#{dir}/boot/grub/device.map --root-directory=#{dir} #{root_dev})) or raise "grub-install failed." | |
201 | + | |
202 | + unless Array(run_cmds).empty? | |
203 | + Array(run_cmds).each do |cmd| | |
204 | + system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, *Array(cmd)) or raise "Failed to execute command (#{cmd}): #{$!}" | |
205 | + end | |
206 | + end | |
207 | + | |
208 | + cfg = File.read "#{dir}/boot/grub/grub.cfg" | |
209 | + cfg.gsub! %r{mapper/loop\d+p}, "sda" | |
210 | + File.write "#{dir}/boot/grub/grub.cfg", cfg | |
211 | + end | |
212 | + end | |
213 | + | |
214 | + def with_rootfs(&block) | |
118 | 215 | Dir.mktmpdir("ci-#{$$}-#{name}") do |dir| |
119 | 216 | with_loopdev do |devices| |
120 | - puts "Override grub with host version..." | |
121 | - root_dev = "/dev/#{devices.first[/loop\d+/]}" | |
122 | - rootfs_uuid = dirs.find { |d| d.path == '/'}.fs_uuid | |
123 | - puts "New rootfs UUID=#{rootfs_uuid}" | |
124 | 217 | begin |
125 | 218 | system("mount", devices.first, dir) or raise "Failed to mount #{devices.first} to #{dir}" |
126 | 219 | system("mount", "--bind", "/dev", "#{dir}/dev") or raise "Failed to mount /dev to #{dir}/dev" |
@@ -131,52 +224,8 @@ class ImageCreator | ||
131 | 224 | system("mount", di.device, "#{dir}#{di.path}") or raise "Failed to mount #{di.device} to #{dir}#{path}" |
132 | 225 | end |
133 | 226 | |
134 | - system "rm", "-f", "#{dir}/etc/systemd/system/udev.service", "#{dir}/etc/systemd/system/systemd-udevd.service", "#{dir}/etc/udev/rules.d/70-persistent-net.rules" | |
135 | - | |
136 | - puts "Rewrite fstab..." | |
137 | - File.open "#{dir}/etc/fstab", "w" do |f| | |
138 | - dirs.each_with_index do |di, idx| | |
139 | - f << %W(UUID=#{di.fs_uuid} #{di.path} ext4 defaults,noatime 0 #{di.path == '/' ? 1 : 2}).join("\t") | |
140 | - f << "\n" | |
141 | - end | |
142 | - end | |
143 | - | |
144 | - system("chroot", dir, "apt-get", "-qy", "update") | |
145 | - if File.read("#{dir}/etc/debian_version").to_f >= 9.0 | |
146 | - # Note: 2019-10-08 時点で Debian9 のカーネルバージョンに対応していないので、エラー回避のために既存のカーネルを全て削除し、強制的に jessie のカーネルをイントールする | |
147 | - system("rm", "-f", "#{dir}/var/lib/dpkg/info/linux-image-#{`uname -r`.chomp}.prerm") | |
148 | - system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "remove", "--purge", "-y", "linux-image-*") | |
149 | - system("wget", "-O", "#{dir}/tmp/linux.deb", "http://security-cdn.debian.org/debian-security/pool/updates/main/l/linux/linux-image-3.16.0-10-amd64_3.16.81-1_amd64.deb") or raise "Failed to get jessie kernel" | |
150 | - system("chroot", dir, "dpkg", "-i", "/tmp/linux.deb") | |
151 | - system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "install", "-f", "-y") or raise "Failed to install jessie kernel" | |
152 | - system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "-y", "install", "isc-dhcp-client") | |
153 | - else | |
154 | - system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt-get", "-y", "--allow-unauthenticated", "install", "linux-image-amd64", "isc-dhcp-client") | |
155 | - end | |
156 | - | |
157 | - puts "Update grub..." | |
158 | - if File.exists? "#{dir}/var/lib/dpkg/info/grub-efi-amd64.list" | |
159 | - system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "remove", "--purge", "-y", "grub-efi-amd64") or raise "Failed to purge grub-efi" | |
160 | - end | |
161 | - if !File.exists?("#{dir}/var/lib/dpkg/info/grub-pc.list") || !(File.exists?("#{dir}/usr/sbin/grub-bios-setup") || File.exists?("#{dir}/usr/sbin/grub-setup")) | |
162 | - system("chroot", dir, "apt-get", "-qy", "update") or raise "Failed to install grub-pc" | |
163 | - system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt-get", "-y", "install", "grub-pc") | |
164 | - end | |
165 | - File.open "#{dir}/boot/grub/device.map", "w" do |f| | |
166 | - f.puts "(hd0)\t#{root_dev}" | |
167 | - end | |
168 | - system("chroot", dir, "grub-mkconfig", "-o", "/boot/grub/grub.cfg") or raise "grub-mkconfig fails." | |
169 | - system(*%W(grub-install --no-floppy --grub-mkdevicemap=#{dir}/boot/grub/device.map --root-directory=#{dir} #{root_dev})) or raise "grub-install failed." | |
170 | - | |
171 | - unless Array(run_cmds).empty? | |
172 | - Array(run_cmds).each do |cmd| | |
173 | - system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, *Array(cmd)) or raise "Failed to execute command (#{cmd}): #{$!}" | |
174 | - end | |
175 | - end | |
227 | + yield(dir, devices) | |
176 | 228 | |
177 | - cfg = File.read "#{dir}/boot/grub/grub.cfg" | |
178 | - cfg.gsub! %r{mapper/loop\d+p}, "sda" | |
179 | - File.write "#{dir}/boot/grub/grub.cfg", cfg | |
180 | 229 | ensure |
181 | 230 | system("umount", "#{dir}/dev") |
182 | 231 | system("umount", "#{dir}/proc") |
@@ -208,6 +257,7 @@ class ImageCreator | ||
208 | 257 | create_disk |
209 | 258 | create_fs |
210 | 259 | sync_dirs |
260 | + prepare_awsenv | |
211 | 261 | fix_boot |
212 | 262 | write_json |
213 | 263 | puts "Image creation has been complated (#{name})" |