1 # Example showing how to create a disk image.
10 # Create a raw-format sparse disk image, 512 MB in size.
11 f = open (output, "w")
12 f.truncate (512 * 1024 * 1024)
15 # Set the trace flag so that we can see each libguestfs call.
18 # Set the autosync flag so that the disk will be synchronized
19 # automatically when the libguestfs handle is closed.
22 # Attach the disk image to libguestfs.
23 g.add_drive_opts (output, format = "raw", readonly = 0)
25 # Run the libguestfs back-end.
28 # Get the list of devices. Because we only added one drive
29 # above, we expect that this list should contain a single
31 devices = g.list_devices ()
32 assert (len (devices) == 1)
34 # Partition the disk as one single MBR partition.
35 g.part_disk (devices[0], "mbr")
37 # Get the list of partitions. We expect a single element, which
38 # is the partition we have just created.
39 partitions = g.list_partitions ()
40 assert (len (partitions) == 1)
42 # Create a filesystem on the partition.
43 g.mkfs ("ext4", partitions[0])
45 # Now mount the filesystem so that we can add files.
46 g.mount_options ("", partitions[0], "/")
48 # Create some files and directories.
50 message = "Hello, world\n"
51 g.write ("/hello", message)
54 # This one uploads the local file /etc/resolv.conf into
56 g.upload ("/etc/resolv.conf", "/foo/resolv.conf")
58 # Because 'autosync' was set (above) we can just close the handle
59 # and the disk contents will be synchronized. You can also do
60 # this manually by calling g.umount_all and g.sync.