Add vdevmap and required functionality.
[util-vserver.git] / src / vdevmap.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2006 Daniel Hokka Zakrisson
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24 #include <lib/internal.h>
25
26 #include <vserver.h>
27
28 #include <getopt.h>
29 #include <errno.h>
30
31 #define ENSC_WRAPPERS_PREFIX    "vdevmap: "
32 #define ENSC_WRAPPERS_UNISTD    1
33 #define ENSC_WRAPPERS_VSERVER   1
34 #include <wrappers.h>
35
36 #define CMD_HELP                0x1000
37 #define CMD_VERSION             0x1001
38
39 int             wrapper_exit_code  =  1;
40
41 struct option const
42 CMDLINE_OPTIONS[] = {
43   { "help",       no_argument,       0, CMD_HELP },
44   { "version",    no_argument,       0, CMD_VERSION },
45   { "xid",        required_argument, 0, 'x' },
46   { "open",       no_argument,       0, 'o' },
47   { "create",     no_argument,       0, 'c' },
48   { "remap",      no_argument,       0, 'r' },
49   { "flags",      required_argument, 0, 'f' },
50   { "device",     required_argument, 0, 'd' },
51   { "target",     required_argument, 0, 't' },
52   {0,0,0,0}
53 };
54
55 static void
56 showHelp(int fd, char const *cmd)
57 {
58   WRITE_MSG(fd, "Usage: ");
59   WRITE_STR(fd, cmd);
60   WRITE_MSG(fd,
61             " --xid <xid> [--flags <flags>] [--open] [--create] [--remap] [--device <dev>] [--target <dev>]\n"
62             "\n"
63             "Please report bugs to " PACKAGE_BUGREPORT "\n");
64
65   exit(0);
66 }
67
68 static void
69 showVersion()
70 {
71   WRITE_MSG(1,
72             "vdevmap " VERSION " -- manages device mappings\n"
73             "This program is part of " PACKAGE_STRING "\n\n"
74             "Copyright (C) 2006 Daniel Hokka Zakrisson\n"
75             VERSION_COPYRIGHT_DISCLAIMER);
76   exit(0);
77 }
78
79 int main(int argc, char *argv[])
80 {
81   xid_t         xid             = VC_NOCTX;
82   bool          allow_open      = false;
83   bool          allow_create    = false;
84   bool          do_remap        = false;
85   uint32_t      flags           = 0;
86   char          *device         = NULL;
87   char          *target         = NULL;
88   
89   while (1) {
90     int         c = getopt_long(argc, argv, "+x:ocrf:d:t:", CMDLINE_OPTIONS, 0);
91     if (c==-1) break;
92
93     switch (c) {
94       case CMD_HELP     :  showHelp(1, argv[0]);
95       case CMD_VERSION  :  showVersion();
96       case 'x'          :  xid = Evc_xidopt2xid(optarg, true);  break;
97       case 'o'          :  allow_open = true;                   break;
98       case 'c'          :  allow_create = true;                 break;
99       case 'r'          :  do_remap = true;                     break;
100       case 'd'          :  device = optarg;                     break;
101       case 't'          :  target = optarg;                     break;
102       case 'f'          :
103         if (!isNumberUnsigned(optarg, &flags, false)) {
104           WRITE_MSG(2, "Invalid flags argument: '");
105           WRITE_STR(2, optarg);
106           WRITE_MSG(2, "'; try '--help' for more information\n");
107           return EXIT_FAILURE;
108         }
109         break;
110
111       default           :
112         WRITE_MSG(2, "Try '");
113         WRITE_STR(2, argv[0]);
114         WRITE_MSG(2, " --help' for more information.\n");
115         return EXIT_FAILURE;
116         break;
117     }
118   }
119
120   if (allow_open)       flags |= VC_DATTR_OPEN;
121   if (allow_create)     flags |= VC_DATTR_CREATE;
122   if (do_remap)         flags |= VC_DATTR_REMAP;
123
124   if (xid==VC_NOCTX)
125     WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
126   else if (vc_set_mapping(xid, device, target, flags)==-1)
127       perror("vc_set_mapping()");
128   else
129     return EXIT_SUCCESS;
130
131   return EXIT_FAILURE;
132 }