5408112b14a07e332515f5206ee9ac39f65b4222
[util-vserver.git] / src / vrsetup.c
1 // $Id: vrsetup.c,v 1.4 2004/02/27 04:42:10 ensc Exp $    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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
25 #include <getopt.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29
30 #define ENSC_WRAPPERS_FCNTL     1
31 #define ENSC_WRAPPERS_IOCTL     1
32 #define ENSC_WRAPPERS_UNISTD    1
33 #include <wrappers.h>
34
35 int wrapper_exit_code = 1;
36
37
38 #define VROOT_SET_DEV           0x5600
39 #define VROOT_CLR_DEV           0x5601
40 #define VROOT_INC_USE           0x56FE
41 #define VROOT_DEC_USE           0x56FF
42
43 struct option const
44 CMDLINE_OPTIONS[] = {
45   { "help",     no_argument,  0, 'h' },
46   { "version",  no_argument,  0, 'v' },
47   { 0,0,0,0 }
48 };
49
50 static void
51 showHelp(int fd, char const *cmd, int res)
52 {
53   WRITE_MSG(fd, "Usage:  ");
54   WRITE_STR(fd, cmd);
55   WRITE_MSG(fd,
56             " [-dID] <rootdev>\n"
57             "    or  ");
58   WRITE_STR(fd, cmd);
59   WRITE_MSG(fd,
60             " <rootdev> <real-rootdev>\n\n"
61             "Please report bugs to " PACKAGE_BUGREPORT "\n");
62   exit(res);
63 }
64
65 static void
66 showVersion()
67 {
68   WRITE_MSG(1,
69             "vrsetup " VERSION " -- set up and control vroot devices\n"
70             "This program is part of " PACKAGE_STRING "\n\n"
71             "Copyright (C) 2004 Enrico Scholz\n"
72             VERSION_COPYRIGHT_DISCLAIMER);
73   exit(0);
74 }
75
76
77 int main(int argc, char *argv[])
78 {
79   bool          do_delete    = false;
80   bool          do_decrement = false;
81   bool          do_increment = false;
82   bool          do_setup     = false;
83   char const *  root_device      = 0;
84   char const *  real_root_device = 0;
85   int           fd;
86   
87   while (1) {
88     int         c = getopt_long(argc, argv, "dID", CMDLINE_OPTIONS, 0);
89     if (c==-1) break;
90
91     switch (c) {
92       case 'h'          :  showHelp(1, argv[0], 0);
93       case 'v'          :  showVersion();
94       case 'd'          :  do_delete    = true; break;
95       case 'D'          :  do_decrement = true; break;
96       case 'I'          :  do_increment = true; break;
97       default           :
98         WRITE_MSG(2, "Try '");
99         WRITE_STR(2, argv[0]);
100         WRITE_MSG(2, " --help\" for more information.\n");
101         return EXIT_FAILURE;
102         break;
103     }
104   }
105
106   do_setup = !(do_delete || do_decrement || do_increment);
107
108   if (optind+1>argc) {
109     WRITE_MSG(2, "No vroot-device given\n");
110     return EXIT_FAILURE;
111   }
112   
113   if (do_setup && optind+2>argc) {
114     WRITE_MSG(2, "No real root-device given\n");
115     return EXIT_FAILURE;
116   }
117         
118   root_device = argv[optind];
119   if (do_setup) real_root_device = argv[optind+1];
120
121   fd = EopenD(root_device, O_RDONLY, 0);
122   if      (do_increment) Eioctl(fd, VROOT_INC_USE, 0);
123   else if (do_decrement) Eioctl(fd, VROOT_DEC_USE, 0);
124   else if (do_delete)    Eioctl(fd, VROOT_CLR_DEV, 0);
125   else {
126     int         dfd = EopenD(real_root_device, O_RDONLY, 0);
127     Eioctl(fd, VROOT_SET_DEV, (void*)dfd);
128     Eclose(dfd);
129   }
130
131   Eclose(fd);
132   return EXIT_SUCCESS;
133 }