Class BoxGrinder::ImageHelper
In: lib/boxgrinder-build/helpers/image-helper.rb
lib/boxgrinder-build/helpers/image-helper.rb
Parent: Object

Methods

Public Class methods

[Source]

    # File lib/boxgrinder-build/helpers/image-helper.rb, line 25
25:     def initialize(config, appliance_config, options = {})
26:       @config = config
27:       @appliance_config = appliance_config
28: 
29:       @log = options[:log] || LogHelper.new
30:       @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
31:     end

[Source]

    # File lib/boxgrinder-build/helpers/image-helper.rb, line 25
25:     def initialize(config, appliance_config, options = {})
26:       @config = config
27:       @appliance_config = appliance_config
28: 
29:       @log = options[:log] || LogHelper.new
30:       @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
31:     end

Public Instance methods

[Source]

    # File lib/boxgrinder-build/helpers/image-helper.rb, line 37
37:     def convert_disk(disk, format, destination)
38:       @log.debug "Conveting '#{disk}' disk to #{format} format and moving it to '#{destination}'..."
39: 
40:       unless File.exists?(destination)
41:         info = disk_info(disk)
42: 
43:         if info['file format'] == format.to_s
44:           @exec_helper.execute "cp '#{disk}' '#{destination}'"
45:         else
46: 
47:           format_with_options = format.to_s
48: 
49:           if format == :vmdk
50:             format_with_options += (`qemu-img --help | grep '\\-6'`.strip.chomp.empty? ? ' -o compat6' : ' -6')
51:           end
52: 
53:           @exec_helper.execute "qemu-img convert -f #{info['file format']} -O #{format_with_options} '#{disk}' '#{destination}'"
54:         end
55:       else
56:         @log.debug "Destination already exists, skipping disk conversion."
57:       end
58:     end

[Source]

    # File lib/boxgrinder-build/helpers/image-helper.rb, line 37
37:     def convert_disk(disk, format, destination)
38:       @log.debug "Conveting '#{disk}' disk to #{format} format and moving it to '#{destination}'..."
39: 
40:       unless File.exists?(destination)
41:         info = disk_info(disk)
42: 
43:         if info['file format'] == format.to_s
44:           @exec_helper.execute "cp '#{disk}' '#{destination}'"
45:         else
46: 
47:           format_with_options = format.to_s
48: 
49:           if format == :vmdk
50:             format_with_options += (`qemu-img --help | grep '\\-6'`.strip.chomp.empty? ? ' -o compat6' : ' -6')
51:           end
52: 
53:           @exec_helper.execute "qemu-img convert -f #{info['file format']} -O #{format_with_options} '#{disk}' '#{destination}'"
54:         end
55:       else
56:         @log.debug "Destination already exists, skipping disk conversion."
57:       end
58:     end

[Source]

     # File lib/boxgrinder-build/helpers/image-helper.rb, line 117
117:     def create_disk(disk, size)
118:       @log.trace "Preparing disk..."
119:       @exec_helper.execute "dd if=/dev/zero of='#{disk}' bs=1 count=0 seek=#{(size * 1024).to_i}M"
120:       @log.trace "Disk prepared"
121:     end

[Source]

     # File lib/boxgrinder-build/helpers/image-helper.rb, line 117
117:     def create_disk(disk, size)
118:       @log.trace "Preparing disk..."
119:       @exec_helper.execute "dd if=/dev/zero of='#{disk}' bs=1 count=0 seek=#{(size * 1024).to_i}M"
120:       @log.trace "Disk prepared"
121:     end

[Source]

     # File lib/boxgrinder-build/helpers/image-helper.rb, line 123
123:     def customize(disks, options = {})
124:       options = {
125:           :ide_disk => ((@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5') ? true : false
126:       }.merge(options)
127: 
128:       GuestFSHelper.new(disks, @appliance_config, @config, :log => @log).customize(options) do |guestfs, guestfs_helper|
129:         yield guestfs, guestfs_helper
130:       end
131:     end

[Source]

     # File lib/boxgrinder-build/helpers/image-helper.rb, line 123
123:     def customize(disks, options = {})
124:       options = {
125:           :ide_disk => ((@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5') ? true : false
126:       }.merge(options)
127: 
128:       GuestFSHelper.new(disks, @appliance_config, @config, :log => @log).customize(options) do |guestfs, guestfs_helper|
129:         yield guestfs, guestfs_helper
130:       end
131:     end

[Source]

    # File lib/boxgrinder-build/helpers/image-helper.rb, line 33
33:     def disk_info(disk)
34:       YAML.load(@exec_helper.execute("qemu-img info '#{disk}'"))
35:     end

[Source]

    # File lib/boxgrinder-build/helpers/image-helper.rb, line 33
33:     def disk_info(disk)
34:       YAML.load(@exec_helper.execute("qemu-img info '#{disk}'"))
35:     end

Synchronizes filesystem from one image with an empty disk image. Input image can be a partioned image or a partition image itself. Output disk is a partition image.

See also bugzilla.redhat.com/show_bug.cgi?id=690819

[Source]

     # File lib/boxgrinder-build/helpers/image-helper.rb, line 66
 66:     def sync_filesystem(guestfs, guestfs_helper)
 67:       devices = guestfs.list_devices
 68:       in_device = devices.first
 69:       out_device = devices.last
 70:       partitions = guestfs.list_partitions.reject { |i| !(i =~ /^#{in_device}/) }
 71: 
 72:       @log.debug "Loading SELinux policy to sync filesystem..."
 73:       partitions.size > 0 ? guestfs_helper.mount_partitions(in_device) : guestfs_helper.mount_partition(in_device, '/')
 74:       guestfs_helper.load_selinux_policy
 75:       partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
 76:       @log.debug "SELinux policy was loaded, we're ready to sync filesystem."
 77: 
 78:       @log.info "Synchronizing filesystems..."
 79: 
 80:       # Create mount points in libguestfs
 81:       guestfs.mkmountpoint('/in')
 82:       guestfs.mkmountpoint('/out')
 83:       guestfs.mkmountpoint('/out/in')
 84: 
 85:       # Create filesystem on EC2 disk
 86:       guestfs.mkfs(@appliance_config.default_filesystem_type, out_device)
 87:       # Set root partition label
 88:       guestfs.set_e2label(out_device, '79d3d2d4') # This is a CRC32 from /
 89: 
 90:       # Mount empty EC2 disk to /out
 91:       guestfs_helper.mount_partition(out_device, '/out/in')
 92:       partitions.size > 0 ? guestfs_helper.mount_partitions(in_device, '/in') : guestfs_helper.mount_partition(in_device, '/in')
 93: 
 94:       @log.debug "Copying files..."
 95: 
 96:       # Copy the filesystem
 97:       guestfs.cp_a('/in/', '/out')
 98: 
 99:       @log.debug "Files copied."
100: 
101:       # Better make sure...
102:       guestfs.sync
103: 
104:       guestfs_helper.umount_partition(out_device)
105:       partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
106: 
107:       guestfs.rmmountpoint('/out/in')
108:       guestfs.rmmountpoint('/out')
109:       guestfs.rmmountpoint('/in')
110: 
111:       @log.info "Filesystems synchronized."
112: 
113:       # Remount the destination disk
114:       guestfs_helper.mount_partition(out_device, '/')
115:     end

Synchronizes filesystem from one image with an empty disk image. Input image can be a partioned image or a partition image itself. Output disk is a partition image.

See also bugzilla.redhat.com/show_bug.cgi?id=690819

[Source]

     # File lib/boxgrinder-build/helpers/image-helper.rb, line 66
 66:     def sync_filesystem(guestfs, guestfs_helper)
 67:       devices = guestfs.list_devices
 68:       in_device = devices.first
 69:       out_device = devices.last
 70:       partitions = guestfs.list_partitions.reject { |i| !(i =~ /^#{in_device}/) }
 71: 
 72:       @log.debug "Loading SELinux policy to sync filesystem..."
 73:       partitions.size > 0 ? guestfs_helper.mount_partitions(in_device) : guestfs_helper.mount_partition(in_device, '/')
 74:       guestfs_helper.load_selinux_policy
 75:       partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
 76:       @log.debug "SELinux policy was loaded, we're ready to sync filesystem."
 77: 
 78:       @log.info "Synchronizing filesystems..."
 79: 
 80:       # Create mount points in libguestfs
 81:       guestfs.mkmountpoint('/in')
 82:       guestfs.mkmountpoint('/out')
 83:       guestfs.mkmountpoint('/out/in')
 84: 
 85:       # Create filesystem on EC2 disk
 86:       guestfs.mkfs(@appliance_config.default_filesystem_type, out_device)
 87:       # Set root partition label
 88:       guestfs.set_e2label(out_device, '79d3d2d4') # This is a CRC32 from /
 89: 
 90:       # Mount empty EC2 disk to /out
 91:       guestfs_helper.mount_partition(out_device, '/out/in')
 92:       partitions.size > 0 ? guestfs_helper.mount_partitions(in_device, '/in') : guestfs_helper.mount_partition(in_device, '/in')
 93: 
 94:       @log.debug "Copying files..."
 95: 
 96:       # Copy the filesystem
 97:       guestfs.cp_a('/in/', '/out')
 98: 
 99:       @log.debug "Files copied."
100: 
101:       # Better make sure...
102:       guestfs.sync
103: 
104:       guestfs_helper.umount_partition(out_device)
105:       partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
106: 
107:       guestfs.rmmountpoint('/out/in')
108:       guestfs.rmmountpoint('/out')
109:       guestfs.rmmountpoint('/in')
110: 
111:       @log.info "Filesystems synchronized."
112: 
113:       # Remount the destination disk
114:       guestfs_helper.mount_partition(out_device, '/')
115:     end

[Validate]