changes to be committed, although qemu can support this.
This is equivalent to the qemu parameter
-C<-drive file=filename,snapshot=on,if=...>.
+C<-drive file=filename,snapshot=on,readonly=on,if=...>.
C<if=...> is set at compile time by the configuration option
C<./configure --with-drive-if=...>. In the rare case where you
might need to change this at run time, use C<guestfs_add_drive_with_if>
or C<guestfs_add_drive_ro_with_if>.
+C<readonly=on> is only added where qemu supports this option.
+
Note that this call checks for the existence of C<filename>. This
stops you from specifying other types of drive which are supported
by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
guestfs__add_drive_ro_with_if (guestfs_h *g, const char *filename,
const char *drive_if)
{
- size_t len = strlen (filename) + 64;
- char buf[len];
-
if (strchr (filename, ',') != NULL) {
error (g, _("filename cannot contain ',' (comma) character"));
return -1;
return -1;
}
- snprintf (buf, len, "file=%s,snapshot=on,if=%s", filename, drive_if);
+ if (qemu_supports (g, NULL) == -1)
+ return -1;
+
+ /* Only SCSI and virtio drivers support readonly mode.
+ * This is only supported as a QEMU feature since 2010/01.
+ */
+ int supports_ro = 0;
+ if ((STREQ (drive_if, "scsi") || STREQ (drive_if, "virtio")) &&
+ qemu_supports (g, "readonly=on"))
+ supports_ro = 1;
+
+ size_t len = strlen (filename) + 100;
+ char buf[len];
+
+ snprintf (buf, len, "file=%s,snapshot=on,%sif=%s",
+ filename,
+ supports_ro ? "readonly=on," : "",
+ drive_if);
return guestfs__config (g, "-drive", buf);
}