#!/usr/bin/ruby # Assumes you have run ./bootstrap to create the /tmp/kernel, # /tmp/initrd and appliance disk (/tmp/root) require 'libvirt' require 'guestfs' # Create a dummy disk for libguestfs to edit. File.open("/tmp/disk.img", "w") { |f| f.seek(500*1024*1024); f.write("\0") } xml = < guestfs_test0 500000 500000 1 hvm /tmp/kernel /tmp/initrd panic=1 console=ttyS0 udevtimeout=300 no_timer_check acpi=off printk.time=1 cgroup_disable=memory selinux=0 guestfs_verbose=1 TERM=xterm destroy destroy destroy EOF begin File.delete("/tmp/socket") rescue Exception end # Run the guest, locally for now. conn = Libvirt::open('qemu:///session') # 2 == autodestroy, so the domain is killed when conn is closed dom = conn.create_domain_xml(xml, 2) # Run libguestfs and try to connect to the socket. g = Guestfs::Guestfs.new() g.set_attach_method("unix:/tmp/socket") g.set_trace(1) g.launch() # Send a message to the daemon to check we're talking to it OK. r = g.echo_daemon(["one", "two", "three"]) if r != "one two three" raise "bad response from daemon" end # Do some disk operations on /tmp/disk.img. g.part_disk("/dev/sda", "mbr") g.mkfs("ext3", "/dev/sda1") g.mount_options("", "/dev/sda1", "/") g.write("/hello_world.txt", "hello from libvirt") # Close everything. Transient guest should go away. g.close() conn.close() puts "create test run OK" puts "/tmp/disk.img should be a complete disk image"