From: Matthew Dillon Date: Fri, 1 Aug 2014 06:01:16 +0000 (-0700) Subject: hammer2 - Make snapshot directive more convenient X-Git-Tag: v4.1.0~348 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/d0ceb67117bb129afc8c0d2884d298b24a243c20 hammer2 - Make snapshot directive more convenient * Make the snapshot directive more intuitive. The optional arguments are now (1) the to snapshot and (2) the PFS label to use. If not specified, the PFS label is named after the PFS the snapshot is taken from, the last component of the path being snapshotted, and the date and time. * pfs-list now takes an optional argument pointing at a mounted hammer2 filesystem. -s still works, it just isn't as intuitive. --- diff --git a/sbin/hammer2/cmd_snapshot.c b/sbin/hammer2/cmd_snapshot.c index 2025646520..37fdc50f94 100644 --- a/sbin/hammer2/cmd_snapshot.c +++ b/sbin/hammer2/cmd_snapshot.c @@ -52,18 +52,34 @@ */ int -cmd_pfs_snapshot(const char *sel_path, const char *name) +cmd_pfs_snapshot(const char *sel_path, const char *path, const char *label) { hammer2_ioc_pfs_t pfs; int ecode = 0; int fd; char filename[HAMMER2_INODE_MAXNAME]; + char *xname; time_t t; struct tm *tp; - if ((fd = hammer2_ioctl_handle(sel_path)) < 0) - return(1); - if (name == NULL) { + if (path == NULL) { + fd = hammer2_ioctl_handle(sel_path); + xname = strdup(""); + } else { + fd = open(path, O_RDONLY); + if (fd < 0) + fprintf(stderr, "Unable to open %s\n", path); + if (strrchr(path, '/')) + asprintf(&xname, ".%s", strrchr(path, '/') + 1); + else if (*path) + asprintf(&xname, ".%s", path); + else + xname = strdup(""); + } + if (fd < 0) + return 1; + + if (label == NULL) { time(&t); tp = localtime(&t); bzero(&pfs, sizeof(pfs)); @@ -72,25 +88,26 @@ cmd_pfs_snapshot(const char *sel_path, const char *name) perror("ioctl"); } snprintf(filename, sizeof(filename), - "%s.%04d%02d%02d.%02d%02d%02d", + "%s%s.%04d%02d%02d.%02d%02d%02d", pfs.name, + xname, tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec); - name = filename; + label = filename; } bzero(&pfs, sizeof(pfs)); - snprintf(pfs.name, sizeof(pfs.name), "%s", name); + snprintf(pfs.name, sizeof(pfs.name), "%s", label); if (ioctl(fd, HAMMER2IOC_PFS_SNAPSHOT, &pfs) < 0) { perror("ioctl"); ecode = 1; } else { - printf("created snapshot %s\n", name); + printf("created snapshot %s\n", label); } return ecode; } diff --git a/sbin/hammer2/hammer2.h b/sbin/hammer2/hammer2.h index 524438bc44..144c69217c 100644 --- a/sbin/hammer2/hammer2.h +++ b/sbin/hammer2/hammer2.h @@ -127,7 +127,7 @@ int cmd_pfs_list(const char *sel_path); int cmd_pfs_create(const char *sel_path, const char *name, uint8_t pfs_type, const char *uuid_str); int cmd_pfs_delete(const char *sel_path, const char *name); -int cmd_pfs_snapshot(const char *sel_path, const char *name); +int cmd_pfs_snapshot(const char *sel_path, const char *name, const char *label); int cmd_service(void); int cmd_hash(int ac, const char **av); diff --git a/sbin/hammer2/main.c b/sbin/hammer2/main.c index b09ffff85b..96c0cb185c 100644 --- a/sbin/hammer2/main.c +++ b/sbin/hammer2/main.c @@ -200,7 +200,11 @@ main(int ac, char **av) /* * List all PFSs */ - ecode = cmd_pfs_list(sel_path); + if (ac > 2) { + fprintf(stderr, "pfs-list: too many arguments\n"); + usage(1); + } + ecode = cmd_pfs_list((ac == 2) ? av[1] : sel_path); } else if (strcmp(av[0], "pfs-create") == 0) { /* * Create new PFS using pfs_type @@ -224,14 +228,21 @@ main(int ac, char **av) * Create snapshot with optional pfs-type and optional * label override. */ - if (ac > 2) { + if (ac > 3) { fprintf(stderr, "pfs-snapshot: too many arguments\n"); usage(1); } - if (ac != 2) - ecode = cmd_pfs_snapshot(sel_path, NULL); - else - ecode = cmd_pfs_snapshot(sel_path, av[1]); + switch(ac) { + case 1: + ecode = cmd_pfs_snapshot(sel_path, NULL, NULL); + break; + case 2: + ecode = cmd_pfs_snapshot(sel_path, av[1], NULL); + break; + case 3: + ecode = cmd_pfs_snapshot(sel_path, av[1], av[2]); + break; + } } else if (strcmp(av[0], "service") == 0) { /* * Start the service daemon. This daemon accepts @@ -388,7 +399,7 @@ void usage(int code) { fprintf(stderr, - "hammer2 [-s path] command...\n" + "hammer2 [options] command...\n" " -s path Select filesystem\n" " -t type PFS type for pfs-create\n" " -u uuid uuid for pfs-create\n" @@ -401,7 +412,7 @@ usage(int code) "Print directory hash\n" " status " "Report cluster status\n" - " pfs-list " + " pfs-list [] " "List PFSs\n" " pfs-clid