| Class | BoxGrinder::ImageHelper |
| In: |
lib/boxgrinder-build/helpers/image-helper.rb
lib/boxgrinder-build/helpers/image-helper.rb |
| Parent: | Object |
# 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
# 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
# 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
# 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
# File lib/boxgrinder-build/helpers/image-helper.rb, line 118
118: def create_disk(disk, size)
119: @log.trace "Preparing disk..."
120: @exec_helper.execute "dd if=/dev/zero of='#{disk}' bs=1 count=0 seek=#{(size * 1024).to_i}M"
121: @log.trace "Disk prepared"
122: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 118
118: def create_disk(disk, size)
119: @log.trace "Preparing disk..."
120: @exec_helper.execute "dd if=/dev/zero of='#{disk}' bs=1 count=0 seek=#{(size * 1024).to_i}M"
121: @log.trace "Disk prepared"
122: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 124
124: def customize(disks, options = {})
125: options = {
126: :ide_disk => ((@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5') ? true : false
127: }.merge(options)
128:
129: GuestFSHelper.new(disks, @appliance_config, @config, :log => @log).customize(options) do |guestfs, guestfs_helper|
130: yield guestfs, guestfs_helper
131: end
132: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 124
124: def customize(disks, options = {})
125: options = {
126: :ide_disk => ((@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5') ? true : false
127: }.merge(options)
128:
129: GuestFSHelper.new(disks, @appliance_config, @config, :log => @log).customize(options) do |guestfs, guestfs_helper|
130: yield guestfs, guestfs_helper
131: end
132: end
# 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
# 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
# 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 destination device
86: # https://issues.jboss.org/browse/BGBUILD-321
87: guestfs.mkfs(@appliance_config.hardware.partitions['/']['type'], out_device)
88: # Set root partition label
89: guestfs.set_e2label(out_device, '79d3d2d4') # This is a CRC32 from /
90:
91: # Mount empty EC2 disk to /out
92: guestfs_helper.mount_partition(out_device, '/out/in')
93: partitions.size > 0 ? guestfs_helper.mount_partitions(in_device, '/in') : guestfs_helper.mount_partition(in_device, '/in')
94:
95: @log.debug "Copying files..."
96:
97: # Copy the filesystem
98: guestfs.cp_a('/in/', '/out')
99:
100: @log.debug "Files copied."
101:
102: # Better make sure...
103: guestfs.sync
104:
105: guestfs_helper.umount_partition(out_device)
106: partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
107:
108: guestfs.rmmountpoint('/out/in')
109: guestfs.rmmountpoint('/out')
110: guestfs.rmmountpoint('/in')
111:
112: @log.info "Filesystems synchronized."
113:
114: # Remount the destination disk
115: guestfs_helper.mount_partition(out_device, '/')
116: 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
# 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 destination device
86: # https://issues.jboss.org/browse/BGBUILD-321
87: guestfs.mkfs(@appliance_config.hardware.partitions['/']['type'], out_device)
88: # Set root partition label
89: guestfs.set_e2label(out_device, '79d3d2d4') # This is a CRC32 from /
90:
91: # Mount empty EC2 disk to /out
92: guestfs_helper.mount_partition(out_device, '/out/in')
93: partitions.size > 0 ? guestfs_helper.mount_partitions(in_device, '/in') : guestfs_helper.mount_partition(in_device, '/in')
94:
95: @log.debug "Copying files..."
96:
97: # Copy the filesystem
98: guestfs.cp_a('/in/', '/out')
99:
100: @log.debug "Files copied."
101:
102: # Better make sure...
103: guestfs.sync
104:
105: guestfs_helper.umount_partition(out_device)
106: partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
107:
108: guestfs.rmmountpoint('/out/in')
109: guestfs.rmmountpoint('/out')
110: guestfs.rmmountpoint('/in')
111:
112: @log.info "Filesystems synchronized."
113:
114: # Remount the destination disk
115: guestfs_helper.mount_partition(out_device, '/')
116: end