1 /* Example showing how to create a disk image. */
11 main (int argc, char *argv[])
16 g = guestfs_create ();
18 perror ("failed to create libguestfs handle");
22 /* Create a raw-format sparse disk image, 512 MB in size. */
23 int fd = open ("disk.img", O_CREAT|O_WRONLY|O_TRUNC|O_NOCTTY, 0666);
28 if (ftruncate (fd, 512 * 1024 * 1024) == -1) {
29 perror ("disk.img: truncate");
32 if (close (fd) == -1) {
33 perror ("disk.img: close");
37 /* Set the trace flag so that we can see each libguestfs call. */
38 guestfs_set_trace (g, 1);
40 /* Set the autosync flag so that the disk will be synchronized
41 * automatically when the libguestfs handle is closed.
43 guestfs_set_autosync (g, 1);
45 /* Add the disk image to libguestfs. */
46 if (guestfs_add_drive_opts (g, "disk.img",
47 GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", /* raw format */
48 GUESTFS_ADD_DRIVE_OPTS_READONLY, 0, /* for write */
49 -1) /* this marks end of optional arguments */
53 /* Run the libguestfs back-end. */
54 if (guestfs_launch (g) == -1)
57 /* Get the list of devices. Because we only added one drive
58 * above, we expect that this list should contain a single
61 char **devices = guestfs_list_devices (g);
64 if (devices[0] == NULL || devices[1] != NULL) {
65 fprintf (stderr, "error: expected a single device from list-devices\n");
69 /* Partition the disk as one single MBR partition. */
70 if (guestfs_part_disk (g, devices[0], "mbr") == -1)
73 /* Get the list of partitions. We expect a single element, which
74 * is the partition we have just created.
76 char **partitions = guestfs_list_partitions (g);
77 if (partitions == NULL)
79 if (partitions[0] == NULL || partitions[1] != NULL) {
80 fprintf (stderr, "error: expected a single partition from list-partitions\n");
84 /* Create a filesystem on the partition. */
85 if (guestfs_mkfs (g, "ext4", partitions[0]) == -1)
88 /* Now mount the filesystem so that we can add files. */
89 if (guestfs_mount_options (g, "", partitions[0], "/") == -1)
92 /* Create some files and directories. */
93 if (guestfs_touch (g, "/empty") == -1)
95 const char *message = "Hello, world\n";
96 if (guestfs_write (g, "/hello", message, strlen (message)) == -1)
98 if (guestfs_mkdir (g, "/foo") == -1)
101 /* This one uploads the local file /etc/resolv.conf into
104 if (guestfs_upload (g, "/etc/resolv.conf", "/foo/resolv.conf") == -1)
107 /* Because 'autosync' was set (above) we can just close the handle
108 * and the disk contents will be synchronized. You can also do
109 * this manually by calling guestfs_umount_all and guestfs_sync.
113 /* Free up the lists. */
114 for (i = 0; devices[i] != NULL; ++i)
117 for (i = 0; partitions[i] != NULL; ++i)
118 free (partitions[i]);