class SyncDirDef
DEFAULT_EXCLUDE = %w[/proc/* /sys/* /dev/mqueue /dev/hugepages /run/* /var/lib/os-prober/mount /swap /dev/shm/* /var/lib/lxcfs/*]
- attr_accessor :path, :size, :exclude, :size, :srcpath, :fs_features, :device
+ attr_accessor :path, :size, :exclude, :size, :srcpath, :fs_features, :device, :fs_uuid
def initialize(path: '/', size: 8, exclude: DEFAULT_EXCLUDE, srcpath: nil, fs_features: nil)
@path = path
@size = size.to_f
@srcpath = srcpath || path
@fs_features = fs_features
@device = nil
+ @fs_uuid = nil
end
end
end
system("parted", "-s", path, "mklabel", use_gpt ? 'gpt' : 'msdos') or raise "Failed to create partition label"
system("parted", "-s", path, "mkpart", "primary", "1MiB", "#{size_gb * 1024 - 1}MiB") or raise "Failed to create partition"
- if use_gpt
- system("parted", "-s", path, "name", "1", di.path) or raise "Failed to set part label"
- else
+ if !use_gpt
system("parted", "-s", path, "set", "1", "boot", "on") or raise "Failed to set bios boot partition"
end
puts "Image partition created."
def create_fs
with_loopdev do |devices|
- devices.each_with_index do |dev, index|
+ dirs.each_with_index do |di, index|
+ dev = di.device
puts "Creating filesystem on #{dev}..."
cmd = %w(mkfs.ext4 -q)
di = dirs[index]
end
cmd << dev
system(*cmd) or raise "Failed to create file system on #{dev}"
+ system "e2label", dev, di.path == '/' ? 'ROOT' : di.path[1..-1].tr('/', '-')
+ di.fs_uuid = `blkid -o value -s UUID #{di.device}`.chomp("\n")
end
end
end
coreimg = "#{dir}/core.img"
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."
with_loopdev do |devices|
- root_dev = "/dev/#{devices.first[/loop\d+/]}"
puts "Override grub with host version..."
+ root_dev = "/dev/#{devices.first[/loop\d+/]}"
system("grub-bios-setup", "-d", dir, root_dev) or raise "Failed to run grub-bios-setup"
- fs_uuids = devices.map { |d| `blkid -o value -s UUID #{d}`.chomp("\n") }
- rootfs_uuid = fs_uuids.first
+ rootfs_uuid = dirs.find { |d| d.path == '/'}.fs_uuid
puts "New rootfs UUID=#{rootfs_uuid}"
begin
system("mount", devices.first, dir) or raise "Failed to mount #{devices.first} to #{dir}"
puts "Rewrite fstab..."
File.open "#{dir}/etc/fstab", "w" do |f|
- devices.map.with_index { |d, idx|
- f << %W(UUID=#{fs_uuids[idx]} #{dirs[idx].path} ext4 defaults,noatime 0 #{dirs[idx].path == '/' ? 1 : 2}).join("\t")
+ dirs.each_with_index do |di, idx|
+ f << %W(UUID=#{di.fs_uuid} #{di.path} ext4 defaults,noatime 0 #{di.path == '/' ? 1 : 2}).join("\t")
f << "\n"
- }
+ end
end
unless File.exists? "#{dir}/vmlinuz"