Class: SystemBuilder::DiskSquashfsImage

Inherits:
Object
  • Object
show all
Defined in:
lib/system_builder/disk_squashfs_image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ DiskSquashfsImage

Returns a new instance of DiskSquashfsImage.



8
9
10
11
# File 'lib/system_builder/disk_squashfs_image.rb', line 8

def initialize(file)
  @file = file
  @size = 512.megabytes
end

Instance Attribute Details

#bootObject

Returns the value of attribute boot.



5
6
7
# File 'lib/system_builder/disk_squashfs_image.rb', line 5

def boot
  @boot
end

#build_dirObject

Returns the value of attribute build_dir.



66
67
68
# File 'lib/system_builder/disk_squashfs_image.rb', line 66

def build_dir
  @build_dir
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/system_builder/disk_squashfs_image.rb', line 6

def file
  @file
end

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/system_builder/disk_squashfs_image.rb', line 5

def size
  @size
end

Instance Method Details

#boot_fs_block_sizeObject



145
146
147
148
# File 'lib/system_builder/disk_squashfs_image.rb', line 145

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_offsetObject



141
142
143
# File 'lib/system_builder/disk_squashfs_image.rb', line 141

def boot_fs_offset
  free_sectors * 512
end

#compress_root_fsObject



76
77
78
79
80
81
82
83
84
# File 'lib/system_builder/disk_squashfs_image.rb', line 76

def compress_root_fs
  FileUtils::sudo "mksquashfs #{boot.root}/ #{squashfs_file} -noappend -e #{boot.root}/boot"
  FileUtils::sudo "chown #{ENV['USER']} #{squashfs_file} && chmod +r #{squashfs_file}"
  
  mount_boot_fs do |mount_dir|
    FileUtils::sudo "cp #{squashfs_file} #{mount_dir}/filesystem.squashfs"
  end
  FileUtils.touch file
end

#compute_checksumsObject



121
122
123
124
125
126
127
# File 'lib/system_builder/disk_squashfs_image.rb', line 121

def compute_checksums
  mount_boot_fs do |mount_dir|
    FileUtils::cd("#{mount_dir}") do
      FileUtils::sh"md5sum * |sudo tee MD5SUM"
    end 
  end
end

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/system_builder/disk_squashfs_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

  compress_root_fs
  install_extlinux
  compute_checksums

  self
end

#create_fileObject



35
36
37
38
# File 'lib/system_builder/disk_squashfs_image.rb', line 35

def create_file
  FileUtils::mkdir_p File.dirname(file)
  FileUtils::sh "dd if=/dev/zero of=#{file} count=#{size.in_megabytes.to_i} bs=1M"
end

#create_partition_tableObject



40
41
42
43
# File 'lib/system_builder/disk_squashfs_image.rb', line 40

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_fsObject



45
46
47
48
49
50
51
52
53
# File 'lib/system_builder/disk_squashfs_image.rb', line 45

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_sectorsObject



137
138
139
# File 'lib/system_builder/disk_squashfs_image.rb', line 137

def free_sectors
  64
end

#fs_labelObject



129
130
131
# File 'lib/system_builder/disk_squashfs_image.rb', line 129

def fs_label
  "boot"
end

#install_extlinuxObject



112
113
114
115
116
117
118
119
# File 'lib/system_builder/disk_squashfs_image.rb', line 112

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/system_builder/disk_squashfs_image.rb', line 95

def install_extlinux_files(options = {})
  root = (options[:root] or "LABEL=#{fs_label}")
  version = (options[:version] or Time.now.strftime("%Y%m%d%H%M"))

  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=local root=/boot/filesystem.squashfs rootflags=loop rootfstype=squashfs rootdelay=6"
      end
    end
  end
end

#mount_boot_fs(mount_dir = "/tmp/mount_boot_fs", &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/system_builder/disk_squashfs_image.rb', line 55

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


133
134
135
# File 'lib/system_builder/disk_squashfs_image.rb', line 133

def readlink_boot_file(boot_file)
  File.basename(%x{readlink #{boot.root}/#{boot_file}}.strip)
end

#squashfs_fileObject



72
73
74
# File 'lib/system_builder/disk_squashfs_image.rb', line 72

def squashfs_file
  "#{build_dir}/filesystem.squashfs"
end

#sync_boot_fsObject



86
87
88
89
90
91
92
93
# File 'lib/system_builder/disk_squashfs_image.rb', line 86

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"
  end
  FileUtils.touch file
end