9 DEFAULT_EXCLUDE = %w[/proc/* /sys/* /dev/mqueue /dev/hugepages /run/* /var/lib/os-prober/mount /swap /dev/shm/* /var/lib/lxcfs/*]
10 attr_accessor :path, :size, :exclude, :size, :srcpath, :fs_features, :device, :fs_uuid
11 def initialize(path: '/', size: 8, exclude: DEFAULT_EXCLUDE, srcpath: nil, fs_features: nil)
14 @exclude = (DEFAULT_EXCLUDE + [*exclude]).uniq
15 @srcpath = srcpath || path
16 @fs_features = fs_features
23 attr_accessor :name, :dirs, :src_host, :img_path_base
27 def initialize(name, dirs, src_host: nil)
30 @src_host = src_host || name
31 @img_path_base = "#{name}_#{Time.now.strftime '%FT%T%z'}"
35 "#{img_path_base}_#{idx}.img"
39 dirs.each_with_index do |di, idx|
40 _create_disk imgpath(idx), di, idx != 0
44 def _create_disk path, di, use_gpt = false
46 raise "Disk image #{path} is already exists!" if File.exists? path
47 puts "Creating disk image #{path} (#{'%5.2f' % size_gb.to_f} GiB)..."
48 File.open(path, "w") do |f|
49 f.truncate(size_gb * GiB)
51 system("parted", "-s", path, "mklabel", use_gpt ? 'gpt' : 'msdos') or raise "Failed to create partition label"
52 system("parted", "-s", path, "mkpart", "primary", "1MiB", "#{size_gb * 1024 - 1}MiB") or raise "Failed to create partition"
54 system("parted", "-s", path, "set", "1", "boot", "on") or raise "Failed to set bios boot partition"
56 puts "Image partition has been created."
59 def with_loopdev &block
62 dirs.each_with_index do |di, idx|
63 system("kpartx", "-as", imgpath(idx)) or raise "Failed to map loop device"
64 di.device = "/dev/mapper/" + `kpartx -l #{Shellwords.escape imgpath(idx)}`.split("\n").first[/loop\d+p\d+/]
69 dirs.each_with_index do |di, idx|
70 system "kpartx", "-d", imgpath(idx), err: "/dev/null"
77 with_loopdev do |devices|
78 dirs.each_with_index do |di, index|
80 puts "Creating filesystem on #{dev}..."
81 cmd = %w(mkfs.ext4 -q)
84 cmd << '-O' << di.fs_features
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")
95 with_loopdev do |devices|
96 devices.each_with_index do |dev, idx|
98 mount_point = "/mnt/ci-#{$$}-#{name}-#{idx}"
99 system("mkdir", "-p", mount_point)
101 system("mount", dev, mount_point) or raise "Failed to mount file system #{dev} on #{mount_point}"
102 puts "Copying #{src_host}:#{di.srcpath} to #{dev}..."
103 unless system("rsync", "-azHSAX", "--numeric-ids", "--info=progress2", "#{src_host}:#{di.srcpath}/", "#{mount_point}/", *((["--exclude"] * di.exclude.size).zip(di.exclude).flatten))
104 warn "rsync exit with error, file transfer may not be completed."
107 system("umount", mount_point)
108 File.directory?(mount_point) and
109 Dir.rmdir mount_point
116 puts "Fixing boot environments..."
117 Dir.mktmpdir("ci-#{$$}-#{name}") do |dir|
118 with_loopdev do |devices|
119 puts "Override grub with host version..."
120 root_dev = "/dev/#{devices.first[/loop\d+/]}"
121 rootfs_uuid = dirs.find { |d| d.path == '/'}.fs_uuid
122 puts "New rootfs UUID=#{rootfs_uuid}"
124 system("mount", devices.first, dir) or raise "Failed to mount #{devices.first} to #{dir}"
125 system("mount", "--bind", "/dev", "#{dir}/dev") or raise "Failed to mount /dev to #{dir}/dev"
126 system("mount", "--bind", "/proc", "#{dir}/proc") or raise "Failed to mount /proc to #{dir}/proc"
128 dirs[1..-1].each_with_index do |di, idx|
129 system "mkdir", "-p", "#{dir}#{di.path}"
130 system("mount", di.device, "#{dir}#{di.path}") or raise "Failed to mount #{di.device} to #{dir}#{path}"
133 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 puts "Rewrite fstab..."
136 File.open "#{dir}/etc/fstab", "w" do |f|
137 dirs.each_with_index do |di, idx|
138 f << %W(UUID=#{di.fs_uuid} #{di.path} ext4 defaults,noatime 0 #{di.path == '/' ? 1 : 2}).join("\t")
143 system("chroot", dir, "apt-get", "-qy", "update")
144 if File.read("#{dir}/etc/debian_version").to_f >= 9.0
145 # Note: 2019-10-08 時点で Debian9 のカーネルバージョンに対応していないので、エラー回避のために既存のカーネルを全て削除し、強制的に jessie のカーネルをイントールする
146 system("rm", "-f", "#{dir}/var/lib/dpkg/info/linux-image-#{`uname -r`.chomp}.prerm")
147 system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "remove", "--purge", "-y", "linux-image-*")
148 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"
149 system("chroot", dir, "dpkg", "-i", "/tmp/linux.deb")
150 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-get", "-y", "--allow-unauthenticated", "install", "linux-image-amd64")
155 puts "Update grub..."
156 if File.exists? "#{dir}/var/lib/dpkg/info/grub-efi-amd64.list"
157 system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt", "remove", "--purge", "-y", "grub-efi-amd64") or raise "Failed to purge grub-efi"
159 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"))
160 system("chroot", dir, "apt-get", "-qy", "update") or raise "Failed to install grub-pc"
161 system({'DEBIAN_FRONTEND' => 'noninteractive'}, "chroot", dir, "apt-get", "-y", "install", "grub-pc")
163 File.open "#{dir}/boot/grub/device.map", "w" do |f|
164 f.puts "(hd0)\t#{root_dev}"
166 system("chroot", dir, "grub-mkconfig", "-o", "/boot/grub/grub.cfg") or raise "grub-mkconfig fails."
167 system(*%W(grub-install --no-floppy --grub-mkdevicemap=#{dir}/boot/grub/device.map --root-directory=#{dir} #{root_dev})) or raise "grub-install failed."
168 cfg = File.read "#{dir}/boot/grub/grub.cfg"
169 cfg.gsub! %r{mapper/loop0p}, "sda"
170 File.write "#{dir}/boot/grub/grub.cfg", cfg
172 system("umount", "#{dir}/dev")
173 system("umount", "#{dir}/proc")
174 dirs.reverse[0..-2].each do |di, idx|
175 system("umount", "#{dir}#{di.path}")
177 system("umount", dir)
185 dirs.each_with_index do |dir, idx|
187 "Description" => dir.path == '/' ? 'root' : dir.path[1..-1].tr('/', '-'),
190 "S3Bucket" => ENV.fetch("S3_BUCKET", "osdn-base-images"),
191 "S3Key" => "#{ENV.fetch("S3_KEY_PREFIX", "src-disks/")}#{img_path_base}_#{idx}.img"
195 File.write "#{img_path_base}.json", JSON.pretty_generate(jdef)
205 puts "Image creation has been complated (#{name})"
212 opts = ARGV.getopts('l:', 'limit:', 'skip:')
213 limit_pat = opts['limit'] || opts['l']
215 limit_pat = Regexp.new(limit_pat)
217 list = YAML.load_file(ARGV[0] || 'image-list.yml')
218 list.each do |imgdef|
221 if imgdef.kind_of?(Hash)
222 name = imgdef['name']
223 (imgdef['dirs'] || {}).each do |path, opts|
224 opts.kind_of?(Hash) or opts = {size: opts}
225 dirs << SyncDirDef.new({path: path}.merge(opts.keys.map(&:to_sym).zip(opts.values).to_h))
231 limit_pat.match?(name) or next
233 dirs.empty? and dirs << SyncDirDef.new
234 ImageCreator.new(name, dirs).run