util-vserver 0.30.215.
[util-vserver.git] / src / vdevmap.c
index 5b70b51..a56c00e 100644 (file)
@@ -1,4 +1,4 @@
-// $Id: vdevmap.c 2490 2007-02-05 20:45:25Z dhozac $    --*- c -*--
+// $Id: vdevmap.c 2703 2008-03-15 16:05:49Z dhozac $    --*- c -*--
 
 // Copyright (C) 2006 Daniel Hokka Zakrisson
 //  
@@ -43,6 +43,8 @@ CMDLINE_OPTIONS[] = {
   { "help",       no_argument,       0, CMD_HELP },
   { "version",    no_argument,       0, CMD_VERSION },
   { "xid",        required_argument, 0, 'x' },
+  { "set",        no_argument,       0, 's' },
+  { "unset",      no_argument,       0, 'u' },
   { "open",       no_argument,       0, 'o' },
   { "create",     no_argument,       0, 'c' },
   { "remap",      no_argument,       0, 'r' },
@@ -58,7 +60,24 @@ showHelp(int fd, char const *cmd)
   WRITE_MSG(fd, "Usage: ");
   WRITE_STR(fd, cmd);
   WRITE_MSG(fd,
-           " --xid <xid> [--flags <flags>] [--open] [--create] [--remap] [--device <dev>] [--target <dev>]\n"
+           " --xid <xid> {--set|--unset} [--flags <flags>] [--open] [--create]\n"
+           "        [--device <dev>] [--remap --target <dev>]\n"
+           "\n"
+           "    --flags <flags>     Set the specified flags\n"
+           "    --open              Allow opening of the device\n"
+           "    --create            If CAP_MKNOD is given, allow mknod(2)\n"
+           "    --device <dev>      Device to apply the command to\n"
+           "    --remap             Remap the device to the target\n"
+           "    --target <dev>      Target for --remap\n"
+           "\n"
+           "EXAMPLES\n"
+           "  Remap /dev/hda1 to /dev/vroot1 for xid 42\n"
+           "    vdevmap --xid 42 --set --open --device /dev/hda1 --target /dev/vroot1 --remap\n"
+           "  Let xid 42 create all device nodes\n"
+           "    vdevmap --xid 42 --set --open --create --target /dev/null\n"
+           "    vdevmap --xid 42 --set --open --create --target /dev/root\n"
+           "  Let xid 43 create just /dev/null\n"
+           "    vdevmap --xid 43 --set --open --create --device /dev/null\n"
            "\n"
            "Please report bugs to " PACKAGE_BUGREPORT "\n");
 
@@ -85,10 +104,11 @@ int main(int argc, char *argv[])
   uint32_t     flags           = 0;
   char         *device         = NULL;
   char         *target         = NULL;
+  bool         set             = true;
   unsigned long        tmp             = 0;
   
   while (1) {
-    int                c = getopt_long(argc, argv, "+x:ocrf:d:t:", CMDLINE_OPTIONS, 0);
+    int                c = getopt_long(argc, argv, "+x:suocrf:d:t:", CMDLINE_OPTIONS, 0);
     if (c==-1) break;
 
     switch (c) {
@@ -100,6 +120,8 @@ int main(int argc, char *argv[])
       case 'r'         :  do_remap = true;                     break;
       case 'd'         :  device = optarg;                     break;
       case 't'         :  target = optarg;                     break;
+      case 's'         :  set = 1;                             break;
+      case 'u'         :  set = 0;                             break;
       case 'f'         :
        if (!isNumberUnsigned(optarg, &tmp, false)) {
          WRITE_MSG(2, "Invalid flags argument: '");
@@ -123,12 +145,18 @@ int main(int argc, char *argv[])
   if (allow_create)    flags |= VC_DATTR_CREATE;
   if (do_remap)                flags |= VC_DATTR_REMAP;
 
-  if (xid==VC_NOCTX)
+  if (!target && do_remap)
+    WRITE_MSG(2, "Remapping specified without a target; try '--help' for more information\n");
+  else if (xid==VC_NOCTX)
     WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
   else if (optind!=argc)
     WRITE_MSG(2, "Unused argument(s); try '--help' for more information\n");
-  else if (vc_set_mapping(xid, device, target, flags)==-1)
+  else if (!device && !target)
+    WRITE_MSG(2, "Device and target are missing; try '--help' for more information\n");
+  else if (set && vc_set_mapping(xid, device, target, flags)==-1)
       perror("vc_set_mapping()");
+  else if (!set && vc_unset_mapping(xid, device, target, flags)==-1)
+      perror("vc_unset_mapping()");
   else
     return EXIT_SUCCESS;