lib/system_builder/image.rb in system-builder-0.0.3 vs lib/system_builder/image.rb in system-builder-0.0.4

- old
+ new

@@ -19,24 +19,25 @@ create_file create_partition_table format_root_fs end - install_grub_files :stage_files => %w{e2fs_stage1_5 stage?} + install_extlinux_files sync_root_fs - install_grub if file_creation + install_extlinux self end def create_file FileUtils::sh "dd if=/dev/zero of=#{file} count=#{size.in_megabytes.to_i} bs=1M" end def create_partition_table - FileUtils::sh "echo '63,' | /sbin/sfdisk --no-reread -uS -H16 -S63 #{file}" + # Partition must be bootable for syslinux + FileUtils::sh "echo '63,,L,*' | /sbin/sfdisk --no-reread -uS -H16 -S63 #{file}" end def format_root_fs loop_device = "/dev/loop0" begin @@ -45,35 +46,68 @@ ensure FileUtils::sudo "losetup -d #{loop_device}" end end - def sync_root_fs + def mount_root_fs(&block) + # TODO use a smarter mount_dir mount_dir = "/tmp/mount_root_fs" FileUtils::mkdir_p mount_dir - + begin FileUtils::sudo "mount -o loop,offset=#{fs_offset} #{file} #{mount_dir}" - FileUtils::sudo "rsync -av --delete #{boot.root}/ #{mount_dir}" + yield mount_dir ensure FileUtils::sudo "umount #{mount_dir}" end + end + def sync_root_fs + mount_root_fs do |mount_dir| + FileUtils::sudo "rsync -a --delete #{boot.root}/ #{mount_dir}" + end FileUtils.touch file end + def install_extlinux_files(options = {}) + root = (options[:root] or "LABEL=#{fs_label}") + version = (options[:version] or Time.now.strftime("%Y%m%d%H%M")) + + boot.image do |image| + image.mkdir "/boot/extlinux" + + boot.image.open("/boot/extlinux/extlinux.conf") do |f| + f.puts "DEFAULT linux" + f.puts "LABEL linux" + f.puts "SAY Now booting #{version} from syslinux ..." + f.puts "KERNEL /vmlinuz" + f.puts "APPEND ro root=#{root} initrd=/initrd.img" + end + end + end + def install_grub_files(options = {}) stage_files = Array(options[:stage_files]).flatten boot.image do |image| image.mkdir "/boot/grub" - install_grub_menu options + install_grub_menu options image.install "boot/grub", stage_files.collect { |f| '/usr/lib/grub/**/' + f } end end + def install_extlinux + # TODO install extlinux.sys only when needed + mount_root_fs do |mount_dir| + FileUtils::sudo "extlinux --install -H16 -S63 #{mount_dir}/boot/extlinux" + end + # TODO install mbr only when needed + # install MBR + FileUtils::sh "dd if=/usr/lib/syslinux/mbr.bin of=#{file} conv=notrunc" + end + def install_grub IO.popen("sudo grub --device-map=/dev/null","w") { |grub| grub.puts "device (hd0) #{file}" grub.puts "root (hd0,0)" grub.puts "setup (hd0)" @@ -102,10 +136,10 @@ end end def fs_block_size linux_partition_info = `/sbin/sfdisk -l #{file}`.scan(%r{#{file}.*Linux}).first - linux_partition_info.split[4].to_i + linux_partition_info.split[5].to_i end def fs_offset 32256 end