[sheepdog] [PATCH 1/2] collie: confirm when rolling back to the snapshot

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Mon Apr 8 18:46:01 CEST 2013


'collie vdi rollback' discards any changes made since the previous
snapshot was taken, so it is safe to confirm it to users before doing
the operation.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/cluster.c | 16 ++--------------
 collie/collie.h  |  1 +
 collie/common.c  | 10 ++++++++++
 collie/vdi.c     |  3 +++
 tests/039        | 14 +++++++-------
 tests/039.out    | 15 ++++++++++-----
 6 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index d7de019..083801c 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -109,20 +109,8 @@ static int cluster_format(int argc, char **argv)
 		if (test_bit(nr, vdi_inuse))
 			break;
 
-	if (nr != SD_NR_VDIS) {
-		int i, l;
-		char str[123] = {'\0'};
-
-		printf(FORMAT_PRINT);
-		ret = scanf("%s", str);
-		if (ret < 0)
-			return EXIT_SYSFAIL;
-		l = strlen(str);
-		for (i = 0; i < l; i++)
-			str[i] = tolower(str[i]);
-		if (strncmp(str, "yes", 3) != 0)
-			return EXIT_SUCCESS;
-	}
+	if (nr != SD_NR_VDIS)
+		confirm(FORMAT_PRINT);
 
 	gettimeofday(&tv, NULL);
 
diff --git a/collie/collie.h b/collie/collie.h
index 3546290..311ad97 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -73,6 +73,7 @@ int send_light_req_get_response(struct sd_req *hdr, const char *host, int port);
 int collie_exec_req(int sockfd, struct sd_req *hdr, void *data);
 int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
 int update_node_list(int max_nodes, uint32_t epoch);
+void confirm(const char *message);
 
 extern struct command vdi_command;
 extern struct command node_command;
diff --git a/collie/common.c b/collie/common.c
index 34ce624..0c2dc0c 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -274,3 +274,13 @@ int do_generic_subcommand(struct subcommand *sub, int argc, char **argv)
 	subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
 	return EXIT_FAILURE;
 }
+
+void confirm(const char *message)
+{
+	char input[8] = "";
+
+	printf("%s", message);
+	fgets(input, sizeof(input), stdin);
+	if (strncasecmp(input, "yes", 3) != 0)
+		exit(EXIT_SUCCESS);
+}
diff --git a/collie/vdi.c b/collie/vdi.c
index 67c96d1..0a0814f 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -801,6 +801,9 @@ static int vdi_rollback(int argc, char **argv)
 	if (ret != EXIT_SUCCESS)
 		return ret;
 
+	confirm("This operation dicards any changes made since the previous\n"
+		"snapshot was taken.  Continue? [yes/no]: ");
+
 	ret = do_vdi_delete(vdiname, 0, NULL);
 	if (ret != SD_RES_SUCCESS) {
 		fprintf(stderr, "Failed to delete the current state\n");
diff --git a/tests/039 b/tests/039
index 2d737f6..55bd65d 100755
--- a/tests/039
+++ b/tests/039
@@ -28,7 +28,7 @@ qemu-io -c "write 0 512 -P 1" sheepdog:test | _filter_qemu_io
 $COLLIE vdi snapshot test -s snap1
 qemu-io -c "write 0 512 -P 2" sheepdog:test | _filter_qemu_io
 
-$COLLIE vdi rollback test -s snap1
+echo yes | $COLLIE vdi rollback test -s snap1
 qemu-io -c "read 0 512 -P 1" sheepdog:test | _filter_qemu_io
 $COLLIE vdi tree | _filter_short_date
 $COLLIE vdi list | _filter_short_date
@@ -37,17 +37,17 @@ qemu-io -c "write 0 512 -P 2" sheepdog:test | _filter_qemu_io
 $COLLIE vdi snapshot test -s snap2
 qemu-io -c "write 0 512 -P 3" sheepdog:test | _filter_qemu_io
 
-$COLLIE vdi rollback test -s snap1
+echo yes | $COLLIE vdi rollback test -s snap1
 qemu-io -c "read 0 512 -P 1" sheepdog:test | _filter_qemu_io
 $COLLIE vdi tree | _filter_short_date
 $COLLIE vdi list | _filter_short_date
 
-$COLLIE vdi rollback test -s snap2
+echo yes | $COLLIE vdi rollback test -s snap2
 qemu-io -c "read 0 512 -P 2" sheepdog:test | _filter_qemu_io
 $COLLIE vdi tree | _filter_short_date
 $COLLIE vdi list | _filter_short_date
 
-$COLLIE vdi rollback test -s snap1
+echo yes | $COLLIE vdi rollback test -s snap1
 qemu-io -c "read 0 512 -P 1" sheepdog:test | _filter_qemu_io
 $COLLIE vdi tree | _filter_short_date
 $COLLIE vdi list | _filter_short_date
@@ -58,10 +58,10 @@ qemu-io -c "write 0 512 -P 4" sheepdog:test | _filter_qemu_io
 $COLLIE vdi snapshot test -s snap4
 
 # these fail since the snap ids don't belong to snapshots
-$COLLIE vdi rollback test -s 0
-$COLLIE vdi rollback test -s 5
+echo yes | $COLLIE vdi rollback test -s 0
+echo yes | $COLLIE vdi rollback test -s 5
 
-$COLLIE vdi rollback test -s snap3
+echo yes | $COLLIE vdi rollback test -s snap3
 qemu-io -c "read 0 512 -P 3" sheepdog:test | _filter_qemu_io
 $COLLIE vdi tree | _filter_short_date
 $COLLIE vdi list | _filter_short_date
diff --git a/tests/039.out b/tests/039.out
index e35881a..5207bb2 100644
--- a/tests/039.out
+++ b/tests/039.out
@@ -4,7 +4,8 @@ wrote 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 wrote 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-read 512/512 bytes at offset 0
+This operation dicards any changes made since the previous
+snapshot was taken.  Continue? [yes/no]: read 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 test---[DATE]---(you are here)
   Name        Id    Size    Used  Shared    Creation time   VDI id  Copies  Tag
@@ -14,7 +15,8 @@ wrote 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 wrote 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-read 512/512 bytes at offset 0
+This operation dicards any changes made since the previous
+snapshot was taken.  Continue? [yes/no]: read 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 test---[DATE]-+-[DATE]
                           `-(you are here)
@@ -22,14 +24,16 @@ test---[DATE]-+-[DATE]
 s test         1  4.0 GB  4.0 MB  0.0 MB DATE   7c2b25     3         snap1
 s test         2  4.0 GB  4.0 MB  0.0 MB DATE   7c2b26     3         snap2
   test         0  4.0 GB  0.0 MB  4.0 MB DATE   7c2b27     3              
-read 512/512 bytes at offset 0
+This operation dicards any changes made since the previous
+snapshot was taken.  Continue? [yes/no]: read 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 test---[DATE]---[DATE]---(you are here)
   Name        Id    Size    Used  Shared    Creation time   VDI id  Copies  Tag
 s test         1  4.0 GB  4.0 MB  0.0 MB DATE   7c2b25     3         snap1
 s test         2  4.0 GB  4.0 MB  0.0 MB DATE   7c2b26     3         snap2
   test         0  4.0 GB  0.0 MB  4.0 MB DATE   7c2b27     3              
-read 512/512 bytes at offset 0
+This operation dicards any changes made since the previous
+snapshot was taken.  Continue? [yes/no]: read 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 test---[DATE]-+-[DATE]
                           `-(you are here)
@@ -44,7 +48,8 @@ wrote 512/512 bytes at offset 0
 The snapshot id must be larger than zero
 Cannot get VDI info for test 5 : Invalid parameters
 Failed to open VDI test
-read 512/512 bytes at offset 0
+This operation dicards any changes made since the previous
+snapshot was taken.  Continue? [yes/no]: read 512/512 bytes at offset 0
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 test---[DATE]-+-[DATE]
                           `-[DATE]-+-[DATE]
-- 
1.8.1.3.566.gaa39828




More information about the sheepdog mailing list