Class | BoxGrinder::GuestFSHelper |
In: |
lib/boxgrinder-build/helpers/guestfs-helper.rb
lib/boxgrinder-build/helpers/guestfs-helper.rb |
Parent: | Object |
guestfs | [R] | |
guestfs | [R] |
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 84 84: def initialize(raw_disk, options = {}) 85: @raw_disk = raw_disk 86: @log = options[:log] || LogHelper.new 87: 88: @partitions = {} 89: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 84 84: def initialize(raw_disk, options = {}) 85: @raw_disk = raw_disk 86: @log = options[:log] || LogHelper.new 87: 88: @partitions = {} 89: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 259 259: def augeas(&block) 260: AugeasHelper.new(@guestfs, self, :log => @log).edit(&block) 261: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 259 259: def augeas(&block) 260: AugeasHelper.new(@guestfs, self, :log => @log).edit(&block) 261: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 213 213: def clean_close 214: @log.trace "Closing guestfs..." 215: 216: @guestfs.sync 217: @guestfs.umount_all 218: @guestfs.close 219: 220: @log.trace "Guestfs closed." 221: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 213 213: def clean_close 214: @log.trace "Closing guestfs..." 215: 216: @guestfs.sync 217: @guestfs.umount_all 218: @guestfs.close 219: 220: @log.trace "Guestfs closed." 221: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 112 112: def customize(options = {}) 113: read_pipe, write_pipe = IO.pipe 114: 115: fork do 116: read_pipe.each do |o| 117: if o.chomp.strip.eql?("<EOF>") 118: exit 119: else 120: @log.trace "GFS: #{o.chomp.strip}" 121: end 122: end 123: end 124: 125: helper = execute(write_pipe, options) 126: 127: yield @guestfs, helper 128: 129: clean_close 130: 131: write_pipe.puts "<EOF>" 132: 133: Process.wait 134: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 112 112: def customize(options = {}) 113: read_pipe, write_pipe = IO.pipe 114: 115: fork do 116: read_pipe.each do |o| 117: if o.chomp.strip.eql?("<EOF>") 118: exit 119: else 120: @log.trace "GFS: #{o.chomp.strip}" 121: end 122: end 123: end 124: 125: helper = execute(write_pipe, options) 126: 127: yield @guestfs, helper 128: 129: clean_close 130: 131: write_pipe.puts "<EOF>" 132: 133: Process.wait 134: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 136 136: def execute(pipe = nil, options = {}) 137: options = {:ide_disk => false}.merge(options) 138: 139: @log.debug "Preparing guestfs..." 140: 141: @guestfs = pipe.nil? ? Guestfs::create : Guestfs::create.redirect(pipe) 142: 143: # https://bugzilla.redhat.com/show_bug.cgi?id=502058 144: @guestfs.set_append("noapic") 145: 146: @log.trace "Setting debug + trace..." 147: @guestfs.set_verbose(1) 148: @guestfs.set_trace(1) 149: 150: @log.trace "Enabling SElinux support in guestfs..." 151: @guestfs.set_selinux(1) 152: 153: unless hw_virtualization_available? 154: # This wrapper is required especially for EC2 where running qemu-kvm crashes libguestfs 155: qemu_wrapper = (RbConfig::CONFIG['host_cpu'].eql?('x86_64') ? "/usr/bin/qemu-system-x86_64" : "/usr/bin/qemu") 156: 157: if File.exists?(qemu_wrapper) 158: @log.trace "Setting QEMU wrapper to #{qemu_wrapper}..." 159: @guestfs.set_qemu(qemu_wrapper) 160: @log.trace "QEMU wrapper set." 161: end 162: end 163: 164: @log.trace "Adding drive '#{@raw_disk}'..." 165: if options[:ide_disk] 166: @guestfs.add_drive_with_if(@raw_disk, 'ide') 167: else 168: @guestfs.add_drive(@raw_disk) 169: end 170: @log.trace "Drive added." 171: 172: if @guestfs.respond_to?('set_network') 173: @log.debug "Enabling networking for GuestFS..." 174: @guestfs.set_network(1) 175: end 176: 177: @log.debug "Launching guestfs..." 178: @guestfs.launch 179: 180: case @guestfs.list_partitions.size 181: when 0 182: mount_partition(@guestfs.list_devices.first, '/') 183: when 1 184: mount_partition(@guestfs.list_partitions.first, '/') 185: else 186: mount_partitions 187: end 188: 189: load_selinux_policy 190: 191: @log.trace "Guestfs launched." 192: 193: self 194: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 136 136: def execute(pipe = nil, options = {}) 137: options = {:ide_disk => false}.merge(options) 138: 139: @log.debug "Preparing guestfs..." 140: 141: @guestfs = pipe.nil? ? Guestfs::create : Guestfs::create.redirect(pipe) 142: 143: # https://bugzilla.redhat.com/show_bug.cgi?id=502058 144: @guestfs.set_append("noapic") 145: 146: @log.trace "Setting debug + trace..." 147: @guestfs.set_verbose(1) 148: @guestfs.set_trace(1) 149: 150: @log.trace "Enabling SElinux support in guestfs..." 151: @guestfs.set_selinux(1) 152: 153: unless hw_virtualization_available? 154: # This wrapper is required especially for EC2 where running qemu-kvm crashes libguestfs 155: qemu_wrapper = (RbConfig::CONFIG['host_cpu'].eql?('x86_64') ? "/usr/bin/qemu-system-x86_64" : "/usr/bin/qemu") 156: 157: if File.exists?(qemu_wrapper) 158: @log.trace "Setting QEMU wrapper to #{qemu_wrapper}..." 159: @guestfs.set_qemu(qemu_wrapper) 160: @log.trace "QEMU wrapper set." 161: end 162: end 163: 164: @log.trace "Adding drive '#{@raw_disk}'..." 165: if options[:ide_disk] 166: @guestfs.add_drive_with_if(@raw_disk, 'ide') 167: else 168: @guestfs.add_drive(@raw_disk) 169: end 170: @log.trace "Drive added." 171: 172: if @guestfs.respond_to?('set_network') 173: @log.debug "Enabling networking for GuestFS..." 174: @guestfs.set_network(1) 175: end 176: 177: @log.debug "Launching guestfs..." 178: @guestfs.launch 179: 180: case @guestfs.list_partitions.size 181: when 0 182: mount_partition(@guestfs.list_devices.first, '/') 183: when 1 184: mount_partition(@guestfs.list_partitions.first, '/') 185: else 186: mount_partitions 187: end 188: 189: load_selinux_policy 190: 191: @log.trace "Guestfs launched." 192: 193: self 194: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 93 93: def hw_virtualization_available? 94: @log.trace "Checking if HW virtualization is available..." 95: 96: begin 97: ec2 = Resolv.getname("169.254.169.254").include?(".ec2.internal") 98: rescue Resolv::ResolvError 99: ec2 = false 100: end 101: 102: if `cat /proc/cpuinfo | grep flags | grep vmx | wc -l`.chomp.strip.to_i > 0 and !ec2 103: @log.trace "HW acceleration available." 104: return true 105: end 106: 107: @log.trace "HW acceleration not available." 108: 109: false 110: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 93 93: def hw_virtualization_available? 94: @log.trace "Checking if HW virtualization is available..." 95: 96: begin 97: ec2 = Resolv.getname("169.254.169.254").include?(".ec2.internal") 98: rescue Resolv::ResolvError 99: ec2 = false 100: end 101: 102: if `cat /proc/cpuinfo | grep flags | grep vmx | wc -l`.chomp.strip.to_i > 0 and !ec2 103: @log.trace "HW acceleration available." 104: return true 105: end 106: 107: @log.trace "HW acceleration not available." 108: 109: false 110: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 196 196: def load_selinux_policy 197: return unless @guestfs.exists('/etc/sysconfig/selinux') != 0 198: 199: @log.trace "Loading SElinux policy..." 200: 201: @guestfs.aug_init("/", 32) 202: @guestfs.aug_rm("/augeas/load//incl[. != '/etc/sysconfig/selinux']") 203: @guestfs.aug_load 204: 205: selinux = @guestfs.aug_get("/files/etc/sysconfig/selinux/SELINUX") 206: 207: @guestfs.sh("/usr/sbin/load_policy") if !selinux.nil? and !selinux.eql?('disabled') 208: @guestfs.aug_close 209: 210: @log.trace "SElinux policy loaded." 211: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 196 196: def load_selinux_policy 197: return unless @guestfs.exists('/etc/sysconfig/selinux') != 0 198: 199: @log.trace "Loading SElinux policy..." 200: 201: @guestfs.aug_init("/", 32) 202: @guestfs.aug_rm("/augeas/load//incl[. != '/etc/sysconfig/selinux']") 203: @guestfs.aug_load 204: 205: selinux = @guestfs.aug_get("/files/etc/sysconfig/selinux/SELINUX") 206: 207: @guestfs.sh("/usr/sbin/load_policy") if !selinux.nil? and !selinux.eql?('disabled') 208: @guestfs.aug_close 209: 210: @log.trace "SElinux policy loaded." 211: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 223 223: def mount_partition(part, mount_point) 224: @log.trace "Mounting #{part} partition to #{mount_point}..." 225: @guestfs.mount_options("", part, mount_point) 226: @log.trace "Partition mounted." 227: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 223 223: def mount_partition(part, mount_point) 224: @log.trace "Mounting #{part} partition to #{mount_point}..." 225: @guestfs.mount_options("", part, mount_point) 226: @log.trace "Partition mounted." 227: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 229 229: def mount_partitions 230: root_partition = nil 231: 232: @guestfs.list_partitions.each do |partition| 233: mount_partition(partition, '/') 234: 235: # TODO: use this http://libguestfs.org/guestfs.3.html#guestfs_vfs_label 236: if @guestfs.exists('/sbin/e2label') != 0 237: root_partition = partition 238: break 239: end 240: @guestfs.umount(partition) 241: end 242: 243: raise "No root partition found for '#{File.basename(@raw_disk)}' disk!" if root_partition.nil? 244: 245: @guestfs.list_partitions.each do |partition| 246: next if partition == root_partition 247: mount_partition(partition, @guestfs.sh("/sbin/e2label #{partition}").strip.chomp.gsub('_', '')) 248: end 249: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 229 229: def mount_partitions 230: root_partition = nil 231: 232: @guestfs.list_partitions.each do |partition| 233: mount_partition(partition, '/') 234: 235: # TODO: use this http://libguestfs.org/guestfs.3.html#guestfs_vfs_label 236: if @guestfs.exists('/sbin/e2label') != 0 237: root_partition = partition 238: break 239: end 240: @guestfs.umount(partition) 241: end 242: 243: raise "No root partition found for '#{File.basename(@raw_disk)}' disk!" if root_partition.nil? 244: 245: @guestfs.list_partitions.each do |partition| 246: next if partition == root_partition 247: mount_partition(partition, @guestfs.sh("/sbin/e2label #{partition}").strip.chomp.gsub('_', '')) 248: end 249: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 251 251: def sh(cmd, options = {}) 252: arch = options[:arch] || `uname -m`.chomp.strip 253: 254: @log.debug "Executing '#{cmd}' command..." 255: @guestfs.sh("setarch #{arch} << 'SETARCH_EOF'\n#{cmd}\nSETARCH_EOF") 256: @log.debug "Command '#{cmd}' executed." 257: end
# File lib/boxgrinder-build/helpers/guestfs-helper.rb, line 251 251: def sh(cmd, options = {}) 252: arch = options[:arch] || `uname -m`.chomp.strip 253: 254: @log.debug "Executing '#{cmd}' command..." 255: @guestfs.sh("setarch #{arch} << 'SETARCH_EOF'\n#{cmd}\nSETARCH_EOF") 256: @log.debug "Command '#{cmd}' executed." 257: end