Add vdevmap and required functionality.
authorDaniel Hokka Zakrisson <daniel@hozac.com>
Tue, 12 Dec 2006 01:39:54 +0000 (01:39 +0000)
committerDaniel Hokka Zakrisson <daniel@hozac.com>
Tue, 12 Dec 2006 01:39:54 +0000 (01:39 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2427 94cd875c-1c1d-0410-91d2-eb244daf1a30

kernel/device_cmd.h [new file with mode: 0644]
kernel/switch.h
lib/Makefile-files
lib/syscall_setmapping-v21.hc [new file with mode: 0644]
lib/syscall_setmapping.c [new file with mode: 0644]
lib/virtual.h
lib/vserver.h
src/Makefile-files
src/vdevmap.c [new file with mode: 0644]

diff --git a/kernel/device_cmd.h b/kernel/device_cmd.h
new file mode 100644 (file)
index 0000000..5ebf825
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef _VX_DEVICE_CMD_H
+#define _VX_DEVICE_CMD_H
+
+
+/*  device vserver commands */
+
+#define VCMD_set_mapping       VC_CMD(DEVICE, 1, 0)
+
+struct vcmd_set_mapping_v0 {
+       const char *device;
+       const char *target;
+       uint32_t flags;
+};
+
+
+#endif /* _VX_DEVICE_CMD_H */
index 672ed15..95eb6b4 100644 (file)
@@ -19,7 +19,7 @@
         |STATS  |DESTROY|ALTER  |CHANGE |LIMIT  |TEST   | |       |       |
         |INFO   |SETUP  |       |MOVE   |       |       | |       |       |
   -------+-------+-------+-------+-------+-------+-------+ +-------+-------+
-  SYSTEM |VERSION|VSETUP |VHOST  |       |       |       | |DEVICES|       |
+  SYSTEM |VERSION|VSETUP |VHOST  |       |       |       | |DEVICE |       |
   HOST   |     00|     01|     02|     03|     04|     05| |     06|     07|
   -------+-------+-------+-------+-------+-------+-------+ +-------+-------+
   CPU    |       |VPROC  |PROCALT|PROCMIG|PROCTRL|       | |SCHED. |       |
@@ -51,6 +51,8 @@
 #define VC_CAT_VSETUP          1
 #define VC_CAT_VHOST           2
 
+#define VC_CAT_DEVICE          6
+
 #define VC_CAT_VPROC           9
 #define VC_CAT_PROCALT         10
 #define VC_CAT_PROCMIG         11
index 1ea2848..fab3875 100644 (file)
@@ -126,7 +126,9 @@ lib_v21_SRCS =                      lib/syscall_setccaps-v21.hc \
                                lib/syscall_getspacemask-v21.hc \
                                lib/syscall_enternamespace-v21.hc \
                                lib/syscall_setnamespace-v21.hc \
-                               lib/syscall_ctxmigrate-v21.hc
+                               lib/syscall_ctxmigrate-v21.hc \
+                               lib/syscall_setmapping.c \
+                               lib/syscall_setmapping-v21.hc
 
 if ENSC_HAVE_C99_COMPILER
 lib_v13_SRCS +=                        lib/syscall_adddlimit-v13.hc \
diff --git a/lib/syscall_setmapping-v21.hc b/lib/syscall_setmapping-v21.hc
new file mode 100644 (file)
index 0000000..6df80ba
--- /dev/null
@@ -0,0 +1,29 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+// Copyright (C) 2006 Daniel Hokka Zakrisson
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+static inline ALWAYSINLINE int
+vc_set_mapping_v21(xid_t xid, const char *device, const char *target, uint32_t flags)
+{
+  struct vcmd_set_mapping_v0 data = { .device = device, .target = target, .flags = flags };
+  return vserver(VCMD_set_mapping, xid, &data);
+}
diff --git a/lib/syscall_setmapping.c b/lib/syscall_setmapping.c
new file mode 100644 (file)
index 0000000..8f635c2
--- /dev/null
@@ -0,0 +1,38 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+// Copyright (C) 2006 Daniel Hokka Zakrisson
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "vserver.h"
+#include "vserver-internal.h"
+#include "virtual.h"
+
+#if defined(VC_ENABLE_API_V21)
+#  include "syscall_setmapping-v21.hc"
+#endif
+
+#if defined(VC_ENABLE_API_V21)
+int
+vc_set_mapping(xid_t xid, const char *device, const char *target, uint32_t flags)
+{
+  CALL_VC(CALL_VC_V21(vc_set_mapping, xid, device, target, flags));
+}
+#endif
index 828183a..aadf2fa 100644 (file)
@@ -30,6 +30,7 @@
 #include <kernel/network_cmd.h>
 #include <kernel/sched_cmd.h>
 #include <kernel/signal_cmd.h>
+#include <kernel/device_cmd.h>
 
 #include <kernel/legacy.h>
 
index bb0b93a..5ab9bc4 100644 (file)
 #define VC_VCI_SPACES                  (1 << 10)
 
 
+// the device mapping flags
+#define VC_DATTR_CREATE                        0x00000001
+#define VC_DATTR_OPEN                  0x00000002
+
+#define VC_DATTR_REMAP                 0x00000010
+
+
 #ifndef CLONE_NEWNS
 #  define CLONE_NEWNS                  0x00020000
 #endif
@@ -849,6 +856,8 @@ extern "C" {
    *  \ingroup syscalls
    */
   int          vc_wait_exit(xid_t xid);
+
+  int          vc_set_mapping(xid_t xid, const char *device, const char *target, uint32_t flags);
     
   typedef enum { vcFEATURE_VKILL,  vcFEATURE_IATTR,   vcFEATURE_RLIMIT,
                 vcFEATURE_COMPAT, vcFEATURE_MIGRATE, vcFEATURE_NAMESPACE,
index 4cd4f59..4e101c7 100644 (file)
@@ -77,7 +77,8 @@ DIETPROGS +=          src/chcontext-compat \
                        src/vwait \
                        src/ncontext \
                        src/nattribute \
-                       src/naddress
+                       src/naddress \
+                       src/vdevmap
 
 if ENSC_CAN_BEECRYPT_WITH_DIETLIBC
 DIETPROGS +=           src/vhashify
@@ -154,6 +155,7 @@ sbin_PROGRAMS +=    src/exec-cd \
                        src/ncontext \
                        src/nattribute \
                        src/naddress \
+                       src/vdevmap \
                        $(src_sbin_CXX_X_PROGS)
 
 if ENSC_HAVE_C99_COMPILER
@@ -319,6 +321,10 @@ src_naddress_SOURCES               = src/naddress.c
 src_naddress_LDADD             = $(VSERVER_LDADDS) $(LIBINTERNAL)
 src_naddress_LDFLAGS           = $(VSERVER_LDFLGS)
 
+src_vdevmap_SOURCES            = src/vdevmap.c
+src_vdevmap_LDADD              = $(VSERVER_LDADDS) $(LIBINTERNAL)
+src_vdevmap_LDFLAGS            = $(VSERVER_LDFLGS)
+
 EXTRA_PROGRAMS +=              $(src_sbin_CXX_PROGS) $(src_pkglib_CXX_PROGS)
 
 TESTS_ENVIRONMENT +=           srctestsuitedir=$(top_builddir)/src/testsuite
diff --git a/src/vdevmap.c b/src/vdevmap.c
new file mode 100644 (file)
index 0000000..4019c19
--- /dev/null
@@ -0,0 +1,132 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2006 Daniel Hokka Zakrisson
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "util.h"
+#include <lib/internal.h>
+
+#include <vserver.h>
+
+#include <getopt.h>
+#include <errno.h>
+
+#define ENSC_WRAPPERS_PREFIX   "vdevmap: "
+#define ENSC_WRAPPERS_UNISTD   1
+#define ENSC_WRAPPERS_VSERVER  1
+#include <wrappers.h>
+
+#define CMD_HELP               0x1000
+#define CMD_VERSION            0x1001
+
+int            wrapper_exit_code  =  1;
+
+struct option const
+CMDLINE_OPTIONS[] = {
+  { "help",       no_argument,       0, CMD_HELP },
+  { "version",    no_argument,       0, CMD_VERSION },
+  { "xid",        required_argument, 0, 'x' },
+  { "open",       no_argument,       0, 'o' },
+  { "create",     no_argument,       0, 'c' },
+  { "remap",      no_argument,       0, 'r' },
+  { "flags",      required_argument, 0, 'f' },
+  { "device",     required_argument, 0, 'd' },
+  { "target",     required_argument, 0, 't' },
+  {0,0,0,0}
+};
+
+static void
+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"
+           "\n"
+           "Please report bugs to " PACKAGE_BUGREPORT "\n");
+
+  exit(0);
+}
+
+static void
+showVersion()
+{
+  WRITE_MSG(1,
+           "vdevmap " VERSION " -- manages device mappings\n"
+           "This program is part of " PACKAGE_STRING "\n\n"
+           "Copyright (C) 2006 Daniel Hokka Zakrisson\n"
+           VERSION_COPYRIGHT_DISCLAIMER);
+  exit(0);
+}
+
+int main(int argc, char *argv[])
+{
+  xid_t                xid             = VC_NOCTX;
+  bool         allow_open      = false;
+  bool         allow_create    = false;
+  bool         do_remap        = false;
+  uint32_t     flags           = 0;
+  char         *device         = NULL;
+  char         *target         = NULL;
+  
+  while (1) {
+    int                c = getopt_long(argc, argv, "+x:ocrf:d:t:", CMDLINE_OPTIONS, 0);
+    if (c==-1) break;
+
+    switch (c) {
+      case CMD_HELP    :  showHelp(1, argv[0]);
+      case CMD_VERSION :  showVersion();
+      case 'x'         :  xid = Evc_xidopt2xid(optarg, true);  break;
+      case 'o'         :  allow_open = true;                   break;
+      case 'c'         :  allow_create = true;                 break;
+      case 'r'         :  do_remap = true;                     break;
+      case 'd'         :  device = optarg;                     break;
+      case 't'         :  target = optarg;                     break;
+      case 'f'         :
+       if (!isNumberUnsigned(optarg, &flags, false)) {
+         WRITE_MSG(2, "Invalid flags argument: '");
+         WRITE_STR(2, optarg);
+         WRITE_MSG(2, "'; try '--help' for more information\n");
+         return EXIT_FAILURE;
+       }
+       break;
+
+      default          :
+       WRITE_MSG(2, "Try '");
+       WRITE_STR(2, argv[0]);
+       WRITE_MSG(2, " --help' for more information.\n");
+       return EXIT_FAILURE;
+       break;
+    }
+  }
+
+  if (allow_open)      flags |= VC_DATTR_OPEN;
+  if (allow_create)    flags |= VC_DATTR_CREATE;
+  if (do_remap)                flags |= VC_DATTR_REMAP;
+
+  if (xid==VC_NOCTX)
+    WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
+  else if (vc_set_mapping(xid, device, target, flags)==-1)
+      perror("vc_set_mapping()");
+  else
+    return EXIT_SUCCESS;
+
+  return EXIT_FAILURE;
+}