Class: SystemBuilder::DiskNfsRootImage
- Defined in:
- lib/system_builder/disk_nfsroot_image.rb
Instance Attribute Summary collapse
-
#boot ⇒ Object
Returns the value of attribute boot.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #boot_fs_block_size ⇒ Object
- #boot_fs_offset ⇒ Object
- #create ⇒ Object
- #create_file ⇒ Object
- #create_partition_table ⇒ Object
- #format_boot_fs ⇒ Object
- #free_sectors ⇒ Object
- #fs_label ⇒ Object
-
#initialize(file) ⇒ DiskNfsRootImage
constructor
A new instance of DiskNfsRootImage.
- #install_extlinux ⇒ Object
- #install_extlinux_files(options = {}) ⇒ Object
- #mount_boot_fs(mount_dir = "/tmp/mount_boot_fs", &block) ⇒ Object
- #readlink_boot_file(boot_file) ⇒ Object
- #sync_boot_fs ⇒ Object
Constructor Details
#initialize(file) ⇒ DiskNfsRootImage
Returns a new instance of DiskNfsRootImage.
8 9 10 11 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 8 def initialize(file) @file = file @size = 40.megabytes end |
Instance Attribute Details
#boot ⇒ Object
Returns the value of attribute boot.
5 6 7 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 5 def boot @boot end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
6 7 8 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 6 def file @file end |
#size ⇒ Object
Returns the value of attribute size.
5 6 7 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 5 def size @size end |
Instance Method Details
#boot_fs_block_size ⇒ Object
116 117 118 119 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 116 def boot_fs_block_size linux_partition_info = `/sbin/sfdisk -l #{file}`.scan(%r{#{file}.*Linux}).first linux_partition_info.split[5].to_i end |
#boot_fs_offset ⇒ Object
112 113 114 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 112 def boot_fs_offset free_sectors * 512 end |
#create ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 13 def create boot.configurators << SystemBuilder::InitRamFsConfigurator.new boot.create file_creation = (not File.exists?(file)) if file_creation create_file create_partition_table format_boot_fs end sync_boot_fs install_extlinux_files install_extlinux self end |
#create_file ⇒ Object
32 33 34 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 32 def create_file FileUtils::sh "dd if=/dev/zero of=#{file} count=#{size.in_megabytes.to_i} bs=1M" end |
#create_partition_table ⇒ Object
36 37 38 39 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 36 def create_partition_table # Partition must be bootable for syslinux FileUtils::sh "echo '#{free_sectors},,L,*' | /sbin/sfdisk --force --no-reread -uS -H16 -S63 #{file}" end |
#format_boot_fs ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 41 def format_boot_fs loop_device = "/dev/loop0" begin FileUtils::sudo "losetup -o #{boot_fs_offset} #{loop_device} #{file}" FileUtils::sudo "mke2fs -L #{fs_label} -jqF #{loop_device} #{boot_fs_block_size}" ensure FileUtils::sudo "losetup -d #{loop_device}" end end |
#free_sectors ⇒ Object
108 109 110 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 108 def free_sectors 64 end |
#fs_label ⇒ Object
100 101 102 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 100 def fs_label "boot" end |
#install_extlinux ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 91 def install_extlinux mount_boot_fs("#{boot.root}/boot") do boot.chroot do |chroot| chroot.sudo "extlinux --install -H16 -S63 /boot" end end FileUtils::sh "dd if=#{boot.root}/usr/lib/syslinux/mbr.bin of=#{file} conv=notrunc" end |
#install_extlinux_files(options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 71 def install_extlinux_files( = {}) root = ([:root] or "LABEL=#{fs_label}") version = ([:version] or Time.now.strftime("%Y%m%d%H%M")) nfssvr = %x{/sbin/ip link show|grep -e "eth.:"|cut -d' ' -f 2|cut -d':' -f 1|xargs ip addr show|grep "inet "|cut -d' ' -f 6|cut -d'/' -f 1}.strip nfsroot = %x{readlink -f #{boot.root}}.strip nfsopt = ([:nfsroot] or "#{nfssvr}:#{nfsroot}") mount_boot_fs do |mount_dir| SystemBuilder::DebianBoot::Image.new(mount_dir).tap do |image| image.open("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 initrd=/initrd.img boot=nfs nfsroot=#{nfsopt}" end end end end |
#mount_boot_fs(mount_dir = "/tmp/mount_boot_fs", &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 51 def mount_boot_fs(mount_dir = "/tmp/mount_boot_fs", &block) FileUtils::mkdir_p mount_dir begin FileUtils::sudo "mount -o loop,offset=#{boot_fs_offset} #{file} #{mount_dir}" yield mount_dir ensure FileUtils::sudo "umount #{mount_dir}" end end |
#readlink_boot_file(boot_file) ⇒ Object
104 105 106 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 104 def readlink_boot_file(boot_file) File.basename(%x{readlink #{boot.root}/#{boot_file}}.strip) end |
#sync_boot_fs ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/system_builder/disk_nfsroot_image.rb', line 62 def sync_boot_fs mount_boot_fs do |mount_dir| FileUtils::sudo "rsync -a --delete #{boot.root}/boot/ #{mount_dir}/" FileUtils::sudo "ln -s #{readlink_boot_file('initrd.img')} #{mount_dir}/initrd.img" FileUtils::sudo "ln -s #{readlink_boot_file('vmlinuz')} #{mount_dir}/vmlinuz.img" end FileUtils.touch file end |