From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L9had-0002RP-C0 for qemu-devel@nongnu.org; Mon, 08 Dec 2008 10:00:03 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L9hab-0002R8-RL for qemu-devel@nongnu.org; Mon, 08 Dec 2008 10:00:03 -0500 Received: from [199.232.76.173] (port=57567 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L9hab-0002R5-Ol for qemu-devel@nongnu.org; Mon, 08 Dec 2008 10:00:01 -0500 Received: from mail.suse.de ([195.135.220.2]:37877 helo=mx1.suse.de) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1L9haa-0005tc-Sq for qemu-devel@nongnu.org; Mon, 08 Dec 2008 10:00:01 -0500 Received: from Relay2.suse.de (mail2.suse.de [195.135.221.8]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.suse.de (Postfix) with ESMTP id 665CC45847 for ; Mon, 8 Dec 2008 15:59:59 +0100 (CET) Message-ID: <493D3774.5020508@suse.de> Date: Mon, 08 Dec 2008 16:04:20 +0100 From: Kevin Wolf MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------010901000204000806010509" Subject: [Qemu-devel] [PATCH] snapshot subcommand for qemu-img Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This is a multi-part message in MIME format. --------------010901000204000806010509 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit Add snapshot subcommand to qemu-img which allows to list, create, apply and delete snapshots on qcow2 images. Signed-off-by: Kevin Wolf --------------010901000204000806010509 Content-Type: text/x-patch; name="qemu-img-snapshots.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="qemu-img-snapshots.patch" Index: qemu-svn/qemu-img.c =================================================================== --- qemu-svn.orig/qemu-img.c +++ qemu-svn/qemu-img.c @@ -60,6 +60,7 @@ static void help(void) " commit [-f fmt] filename\n" " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n" " info [-f fmt] filename\n" + " snapshot [-l|-a snapshot|-c snapshot|-d snapshot] filename\n" "\n" "Command parameters:\n" " 'filename' is a disk image filename\n" @@ -77,6 +78,13 @@ static void help(void) " '-c' indicates that target image must be compressed (qcow format only)\n" " '-e' indicates that the target image must be encrypted (qcow format only)\n" " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n" + "\n" + " Parameters to snapshot subcommand:\n" + " 'snapshot' is the name of the snapshot to create, apply or delete\n" + " '-a' applies a snapshot (revert disk to saved state)\n" + " '-c' creates a snapshot\n" + " '-d' deletes a snapshot\n" + " '-l' lists all snapshots in the given image\n" ); printf("\nSupported format:"); bdrv_iterate_format(format_print, NULL); @@ -732,6 +740,124 @@ static int img_info(int argc, char **arg return 0; } +#define SNAPSHOT_LIST 1 +#define SNAPSHOT_CREATE 2 +#define SNAPSHOT_APPLY 3 +#define SNAPSHOT_DELETE 4 + +static void img_snapshot(int argc, char **argv) +{ + BlockDriverState *bs; + QEMUSnapshotInfo sn; + char *filename, *snapshot_name = NULL; + char c; + int ret; + int action = 0; +#ifdef _WIN32 + struct _timeb tb; +#else + struct timeval tv; +#endif + + /* Parse commandline parameters */ + for(;;) { + c = getopt(argc, argv, "la:c:d:h"); + if (c == -1) + break; + switch(c) { + case 'h': + help(); + return; + case 'l': + if (action) { + help(); + return; + } + action = SNAPSHOT_LIST; + break; + case 'a': + if (action) { + help(); + return; + } + action = SNAPSHOT_APPLY; + snapshot_name = optarg; + break; + case 'c': + if (action) { + help(); + return; + } + action = SNAPSHOT_CREATE; + snapshot_name = optarg; + break; + case 'd': + if (action) { + help(); + return; + } + action = SNAPSHOT_DELETE; + snapshot_name = optarg; + break; + } + } + + if (optind >= argc) + help(); + filename = argv[optind++]; + + /* Open the image */ + bs = bdrv_new(""); + if (!bs) + error("Not enough memory"); + + if (bdrv_open2(bs, filename, 0, NULL) < 0) { + error("Could not open '%s'", filename); + } + + /* Perform the requested action */ + switch(action) { + case SNAPSHOT_LIST: + dump_snapshots(bs); + break; + + case SNAPSHOT_CREATE: + memset(&sn, 0, sizeof(sn)); + pstrcpy(sn.name, sizeof(sn.name), snapshot_name); +#ifdef _WIN32 + _ftime(&tb); + sn.date_sec = tb.time; + sn.date_nsec = tb.millitm * 1000000; +#else + gettimeofday(&tv, NULL); + sn.date_sec = tv.tv_sec; + sn.date_nsec = tv.tv_usec * 1000; +#endif + ret = bdrv_snapshot_create(bs, &sn); + if (ret) + error("Could not create snapshot '%s': %d (%s)", + snapshot_name, ret, strerror(-ret)); + break; + + case SNAPSHOT_APPLY: + ret = bdrv_snapshot_goto(bs, snapshot_name); + if (ret) + error("Could not apply snapshot '%s': %d (%s)", + snapshot_name, ret, strerror(-ret)); + break; + + case SNAPSHOT_DELETE: + ret = bdrv_snapshot_delete(bs, snapshot_name); + if (ret) + error("Could not delete snapshot '%s': %d (%s)", + snapshot_name, ret, strerror(-ret)); + break; + } + + /* Cleanup */ + bdrv_delete(bs); +} + int main(int argc, char **argv) { const char *cmd; @@ -749,6 +875,8 @@ int main(int argc, char **argv) img_convert(argc, argv); } else if (!strcmp(cmd, "info")) { img_info(argc, argv); + } else if (!strcmp(cmd, "snapshot")) { + img_snapshot(argc, argv); } else { help(); } --------------010901000204000806010509--