| 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 113
113: def calculate_disk_offsets(disk)
114: @log.debug "Calculating offsets for '#{File.basename(disk)}' disk..."
115: loop_device = get_loop_device
116:
117: @exec_helper.execute("losetup #{loop_device} '#{disk}'")
118: offsets = @exec_helper.execute("parted #{loop_device} 'unit B print' | grep -e '^ [0-9]' | awk '{ print $2 }'").scan(/\d+/)
119: # wait one secont before freeing loop device
120: sleep 1
121: @exec_helper.execute("losetup -d #{loop_device}")
122:
123: @log.trace "Offsets:\n#{offsets}"
124:
125: offsets
126: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 113
113: def calculate_disk_offsets(disk)
114: @log.debug "Calculating offsets for '#{File.basename(disk)}' disk..."
115: loop_device = get_loop_device
116:
117: @exec_helper.execute("losetup #{loop_device} '#{disk}'")
118: offsets = @exec_helper.execute("parted #{loop_device} 'unit B print' | grep -e '^ [0-9]' | awk '{ print $2 }'").scan(/\d+/)
119: # wait one secont before freeing loop device
120: sleep 1
121: @exec_helper.execute("losetup -d #{loop_device}")
122:
123: @log.trace "Offsets:\n#{offsets}"
124:
125: offsets
126: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 80
80: def convert_disk(disk, format, destination)
81: @log.debug "Conveting '#{disk}' disk to #{format} format and moving it to '#{destination}'..."
82:
83: unless File.exists?(destination)
84: info = disk_info(disk)
85:
86: if info['file format'] == format.to_s
87: @exec_helper.execute "cp '#{disk}' '#{destination}'"
88: else
89:
90: format_with_options = format.to_s
91:
92: if format == :vmdk
93: format_with_options += (`qemu-img --help | grep '\\-6'`.strip.chomp.empty? ? ' -o compat6' : ' -6')
94: end
95:
96: @exec_helper.execute "qemu-img convert -f #{info['file format']} -O #{format_with_options} '#{disk}' '#{destination}'"
97: end
98: else
99: @log.debug "Destination already exists, skipping disk conversion."
100: end
101: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 80
80: def convert_disk(disk, format, destination)
81: @log.debug "Conveting '#{disk}' disk to #{format} format and moving it to '#{destination}'..."
82:
83: unless File.exists?(destination)
84: info = disk_info(disk)
85:
86: if info['file format'] == format.to_s
87: @exec_helper.execute "cp '#{disk}' '#{destination}'"
88: else
89:
90: format_with_options = format.to_s
91:
92: if format == :vmdk
93: format_with_options += (`qemu-img --help | grep '\\-6'`.strip.chomp.empty? ? ' -o compat6' : ' -6')
94: end
95:
96: @exec_helper.execute "qemu-img convert -f #{info['file format']} -O #{format_with_options} '#{disk}' '#{destination}'"
97: end
98: else
99: @log.debug "Destination already exists, skipping disk conversion."
100: end
101: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 128
128: def create_disk(disk, size)
129: @log.trace "Preparing disk..."
130: @exec_helper.execute "dd if=/dev/zero of='#{disk}' bs=1 count=0 seek=#{size * 1024}M"
131: @log.trace "Disk prepared"
132: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 128
128: def create_disk(disk, size)
129: @log.trace "Preparing disk..."
130: @exec_helper.execute "dd if=/dev/zero of='#{disk}' bs=1 count=0 seek=#{size * 1024}M"
131: @log.trace "Disk prepared"
132: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 134
134: def create_filesystem(loop_device, options = {})
135: options = {
136: :type => @appliance_config.hardware.partitions['/']['type'],
137: :label => '/'
138: }.merge(options)
139:
140: @log.trace "Creating filesystem..."
141:
142: case options[:type]
143: when 'ext3', 'ext4'
144: @exec_helper.execute "mke2fs -T #{options[:type]} -L '#{options[:label]}' -F #{loop_device}"
145: else
146: raise "Unsupported filesystem specified: #{options[:type]}"
147: end
148:
149: @log.trace "Filesystem created"
150: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 134
134: def create_filesystem(loop_device, options = {})
135: options = {
136: :type => @appliance_config.hardware.partitions['/']['type'],
137: :label => '/'
138: }.merge(options)
139:
140: @log.trace "Creating filesystem..."
141:
142: case options[:type]
143: when 'ext3', 'ext4'
144: @exec_helper.execute "mke2fs -T #{options[:type]} -L '#{options[:label]}' -F #{loop_device}"
145: else
146: raise "Unsupported filesystem specified: #{options[:type]}"
147: end
148:
149: @log.trace "Filesystem created"
150: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 158
158: def customize(disk_path)
159: GuestFSHelper.new(disk_path, :log => @log).customize(:ide_disk => ((@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5') ? true : false) do |guestfs, guestfs_helper|
160: yield guestfs, guestfs_helper
161: end
162: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 158
158: def customize(disk_path)
159: GuestFSHelper.new(disk_path, :log => @log).customize(:ide_disk => ((@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5') ? true : false) do |guestfs, guestfs_helper|
160: yield guestfs, guestfs_helper
161: end
162: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 76
76: def disk_info(disk)
77: YAML.load(@exec_helper.execute("qemu-img info '#{disk}'"))
78: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 76
76: def disk_info(disk)
77: YAML.load(@exec_helper.execute("qemu-img info '#{disk}'"))
78: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 103
103: def get_loop_device
104: begin
105: loop_device = @exec_helper.execute("losetup -f 2>&1").strip
106: rescue
107: raise "No free loop devices available, please free at least one. See 'losetup -d' command."
108: end
109:
110: loop_device
111: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 103
103: def get_loop_device
104: begin
105: loop_device = @exec_helper.execute("losetup -f 2>&1").strip
106: rescue
107: raise "No free loop devices available, please free at least one. See 'losetup -d' command."
108: end
109:
110: loop_device
111: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 33
33: def mount_image(disk, mount_dir)
34: offsets = calculate_disk_offsets(disk)
35:
36: @log.debug "Mounting image #{File.basename(disk)} in #{mount_dir}..."
37: FileUtils.mkdir_p(mount_dir)
38:
39: mounts = {}
40:
41: offsets.each do |offset|
42: loop_device = get_loop_device
43: @exec_helper.execute("losetup -o #{offset.to_s} #{loop_device} '#{disk}'")
44: label = @exec_helper.execute("e2label #{loop_device}").strip.chomp.gsub('_', '')
45: label = '/' if label == ''
46: mounts[label] = loop_device
47: end
48:
49: @exec_helper.execute("mount #{mounts['/']} '#{mount_dir}'")
50:
51: mounts.reject { |key, value| key == '/' }.each do |mount_point, loop_device|
52: @exec_helper.execute("mount #{loop_device} '#{mount_dir}#{mount_point}'")
53: end
54:
55: # Give some time to mount the images
56: sleep 2
57:
58: @log.trace "Mounts:\n#{mounts}"
59:
60: mounts
61: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 33
33: def mount_image(disk, mount_dir)
34: offsets = calculate_disk_offsets(disk)
35:
36: @log.debug "Mounting image #{File.basename(disk)} in #{mount_dir}..."
37: FileUtils.mkdir_p(mount_dir)
38:
39: mounts = {}
40:
41: offsets.each do |offset|
42: loop_device = get_loop_device
43: @exec_helper.execute("losetup -o #{offset.to_s} #{loop_device} '#{disk}'")
44: label = @exec_helper.execute("e2label #{loop_device}").strip.chomp.gsub('_', '')
45: label = '/' if label == ''
46: mounts[label] = loop_device
47: end
48:
49: @exec_helper.execute("mount #{mounts['/']} '#{mount_dir}'")
50:
51: mounts.reject { |key, value| key == '/' }.each do |mount_point, loop_device|
52: @exec_helper.execute("mount #{loop_device} '#{mount_dir}#{mount_point}'")
53: end
54:
55: # Give some time to mount the images
56: sleep 2
57:
58: @log.trace "Mounts:\n#{mounts}"
59:
60: mounts
61: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 152
152: def sync_files(from_dir, to_dir)
153: @log.debug "Syncing files between #{from_dir} and #{to_dir}..."
154: @exec_helper.execute "rsync -Xura #{from_dir.gsub(' ', '\ ')}/* '#{to_dir}'"
155: @log.debug "Sync finished."
156: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 152
152: def sync_files(from_dir, to_dir)
153: @log.debug "Syncing files between #{from_dir} and #{to_dir}..."
154: @exec_helper.execute "rsync -Xura #{from_dir.gsub(' ', '\ ')}/* '#{to_dir}'"
155: @log.debug "Sync finished."
156: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 63
63: def umount_image(disk, mount_dir, mounts)
64: @log.debug "Unmounting image '#{File.basename(disk)}'..."
65:
66: mounts.each { |mount_point, loop_device| @exec_helper.execute("umount -d #{loop_device}") unless mount_point == '/' }
67:
68: @exec_helper.execute("umount -d #{mounts['/']}")
69:
70: # Give some time to umount the image
71: sleep 2
72:
73: FileUtils.rm_rf(mount_dir)
74: end
# File lib/boxgrinder-build/helpers/image-helper.rb, line 63
63: def umount_image(disk, mount_dir, mounts)
64: @log.debug "Unmounting image '#{File.basename(disk)}'..."
65:
66: mounts.each { |mount_point, loop_device| @exec_helper.execute("umount -d #{loop_device}") unless mount_point == '/' }
67:
68: @exec_helper.execute("umount -d #{mounts['/']}")
69:
70: # Give some time to umount the image
71: sleep 2
72:
73: FileUtils.rm_rf(mount_dir)
74: end