1 # Example showing how to create a disk image.
7 g = Guestfs::Guestfs.new()
9 # Create a raw-format sparse disk image, 512 MB in size.
10 File.open(output, "w") {
11 |f| f.truncate(512 * 1024 * 1024)
14 # Set the trace flag so that we can see each libguestfs call.
17 # Set the autosync flag so that the disk will be synchronized
18 # automatically when the libguestfs handle is closed.
21 # Attach the disk image to libguestfs.
22 g.add_drive_opts(output, :format => "raw")
24 # Run the libguestfs back-end.
27 # Get the list of devices. Because we only added one drive
28 # above, we expect that this list should contain a single
30 devices = g.list_devices()
31 if devices.length != 1 then
32 raise "error: expected a single device from list-devices"
35 # Partition the disk as one single MBR partition.
36 g.part_disk(devices[0], "mbr")
38 # Get the list of partitions. We expect a single element, which
39 # is the partition we have just created.
40 partitions = g.list_partitions()
41 if partitions.length != 1 then
42 raise "error: expected a single partition from list-partitions"
45 # Create a filesystem on the partition.
46 g.mkfs("ext4", partitions[0])
48 # Now mount the filesystem so that we can add files.
49 g.mount_options("", partitions[0], "/")
51 # Create some files and directories.
53 message = "Hello, world\n"
54 g.write("/hello", message)
57 # This one uploads the local file /etc/resolv.conf into
59 g.upload("/etc/resolv.conf", "/foo/resolv.conf")
61 # Because 'autosync' was set (above) we can just close the handle
62 # and the disk contents will be synchronized. You can also do
63 # this manually by calling g#umount_all and g#sync.