hammer2 - Make snapshot directive more convenient
authorMatthew Dillon <dillon@apollo.backplane.com>
Fri, 1 Aug 2014 06:01:16 +0000 (23:01 -0700)
committerMatthew Dillon <dillon@apollo.backplane.com>
Fri, 1 Aug 2014 06:01:16 +0000 (23:01 -0700)
* Make the snapshot directive more intuitive.  The optional arguments
  are now (1) the <path> 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 <path> still works, it just isn't as intuitive.

sbin/hammer2/cmd_snapshot.c
sbin/hammer2/hammer2.h
sbin/hammer2/main.c
sbin/newfs_hammer2/newfs_hammer2.c

index 2025646..37fdc50 100644 (file)
  */
 
 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;
 }
index 524438b..144c692 100644 (file)
@@ -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);
index b09ffff..96c0cb1 100644 (file)
@@ -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 [<path>]            "
                        "List PFSs\n"
                "    pfs-clid <label>             "
                        "Print cluster id for specific PFS\n"
@@ -411,8 +422,8 @@ usage(int code)
                        "Create a PFS\n"
                "    pfs-delete <label>           "
                        "Destroy a PFS\n"
-               "    snapshot [<label>]           "
-                       "Snapshot a PFS\n"
+               "    snapshot <path> [<label>]           "
+                       "Snapshot a PFS or directory\n"
                "    service                      "
                        "Start service daemon\n"
                "    stat [<path>]                "
index 11cb4b6..9203cf8 100644 (file)
@@ -567,7 +567,7 @@ format_hammer2(int fd, hammer2_off_t total_space, hammer2_off_t free_space)
                /*
                 * Compression mode and supported copyids.
                 *
-                * Do not allow compression when creating a "BOOT" label
+                * Do not allow compression when creating any "BOOT" label
                 * (pfs-create also does the same if the pfs is named "BOOT")
                 */
                if (strcasecmp(Label[i], "BOOT") == 0) {