merge with 0.30.213
[util-vserver.git] / scripts / vserver-setup.functions
index 127f42f..31aab65 100644 (file)
@@ -1,6 +1,6 @@
-# $Id: vserver-setup.functions,v 1.18 2005/02/25 23:32:02 ensc Exp $   --*- sh -*--
+# $Id: vserver-setup.functions 2517 2007-03-18 22:02:22Z dhozac $      --*- sh -*--
 
-# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+# Copyright (C) 2003,2004,2005,2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 #  
 # 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
@@ -24,14 +24,18 @@ SETUP_LOCKFILE=
 SETUP_CONFDIR=
 SETUP_CONTEXT=
 SETUP_INITSTYLE=
+SETUP_CPUSET=
+SETUP_CPUSETCPUS=
+SETUP_CPUSETMEMS=
+SETUP_CPUSETVIRT=
 
 declare -a SETUP_INTERFACES=()
 declare -a SETUP_FLAGS=()
 
-declare -r SETUP_OPTIONS="confdir:,lockfile:,hostname:,netdev:,netmask:,netprefix:,netbcast:,interface:,flags:,context:,initstyle:"
+declare -r SETUP_OPTIONS="confdir:,lockfile:,hostname:,netdev:,netmask:,netprefix:,netbcast:,interface:,flags:,context:,initstyle:,cpuset:,cpusetcpus:,cpusetmems:,cpusetvirt"
 declare -r SETUP_HELPMSG=$"
-    --context   ...  the static context of the vserver [default: none; a dynamic
-                     context will be assumed]
+    --context   ...  the static context of the vserver [default: none; one will
+                     be generated for you]
     --confdir   ...  [default: $__CONFDIR/<name>]
     --lockfile <filename>
                ...  [default: $__RUNDIR/<name>]
@@ -55,6 +59,19 @@ declare -r SETUP_HELPMSG=$"
                            this becomes a per vserver limit)
                     private: No other process can join this security context.
                            Even root
+    --cpuset <name>
+                ...  declares the CPUSET this vserver will run in [default: none]
+    --cpusetcpus <number[-number][:<exclusive>]>
+                ...  sets which cpus belong to the CPUSET,
+                     exclusive is a flag (0|1) prohibiting any other cpuset from
+                     using those cpus
+    --cpusetmems <number[-number][:<exclusive>]>
+                ...  sets which memory pools belong to the CPUSET,
+                     exclusive is a flag (0|1) prohibiting any other cpuset from
+                     using those memory pools
+    --cpusetvirt
+                ...  virtualize cpuset (guest will see only CPUs defined in cpuset)
+                     Requires kernel patch from http://www.bullopensource.org/cpuset/
     --initstyle <style>
                 ...  configures the initstyle (e.g. minit,sysv,plain)
 "
@@ -71,7 +88,23 @@ function setup_setOption2
        (--netprefix)   SETUP_NETPREFIX=$2;;
        (--netbcast)    SETUP_NETBCAST=$2;;
        (--interface)   SETUP_INTERFACES=( "${SETUP_INTERFACES[@]}" "$2" );;
-       (--initstyle)   SETUP_INITSTYLE=$2;;
+       (--initstyle)   SETUP_INITSTYLE=$2;;
+       (--cpuset)      SETUP_CPUSET=$2;;
+       (--cpusetcpus)  old_IFS=$IFS
+                       IFS=:
+                       set -- $2
+                       SETUP_CPUSETCPUS=$1
+                       SETUP_CPUSETCPUSEXCL=$2
+                       IFS=$old_IFS
+                       ;;
+       (--cpusetmems)  old_IFS=$IFS
+                       IFS=:
+                       set -- $2
+                       SETUP_CPUSETMEMS=$1
+                       SETUP_CPUSETMEMSEXCL=$2
+                       IFS=$old_IFS
+                       ;;
+       (--cpusetvirt)  SETUP_CPUSETVIRT=1;;
        (--flags)       old_IFS=$IFS
                        IFS=,
                        set -- $2
@@ -100,9 +133,14 @@ function _setup_writeInterface
 
     tmp=${tmp##${name}=}
     local dev=${tmp%%:*}
-    test "$dev" != "$tmp" || dev=
+    local nodev=
+    test "$dev" != "$tmp"  || dev=
 
     tmp=${tmp##${dev}:}
+    test "$dev" != "nodev" || {
+       dev=
+       nodev=1
+    }
     local mask=${tmp##*/}
     test "$mask" != "$tmp"  || mask=
 
@@ -123,8 +161,11 @@ function _setup_writeInterface
     _setup_writeSingleOption "$mask"   $d/mask
     _setup_writeSingleOption "$prefix" $d/prefix
 
-    test -n "$dev" -o -n "$SETUP_NETDEV" || \
-       echo $"No device specified for interface '$idx'; do not forget to set the 'nodev' option" >&2
+    test -n "$dev" -o -n "$SETUP_NETDEV" || {
+       test -n "$nodev" || \
+           echo $"No device specified for interface '$idx'; setting 'nodev'" >&2
+       $_TOUCH $d/nodev
+    }
 }
 
 function setup_setDefaults
@@ -134,13 +175,27 @@ function setup_setDefaults
     findFile SETUP_FSTAB "$__CONFDIR"/.defaults/fstab "$__PKGLIBDEFAULTDIR"/fstab
 }
 
+function _setup_generateContext
+{
+    if test -z "$SETUP_CONTEXT" && test ! -e "$__CONFDIR/.defaults/context.dynamic"; then
+       if test -e "$__CONFDIR/.defaults/context.next"; then
+           SETUP_CONTEXT=`$_CAT "$__CONFDIR/.defaults/context.next"`
+       else
+           SETUP_CONTEXT=`$_CAT "$__PKGLIBDEFAULTDIR/context.start"`
+       fi
+       expr "$SETUP_CONTEXT" + 1 > "$__CONFDIR/.defaults/context.next"
+    fi
+}
+
 function setup_writeOption
 {
     local name=$1
     local cfgdir=${SETUP_CONFDIR:?}
     local i
 
-    mkdir -p "$cfgdir"/interfaces "$cfgdir"/apps/init "$cfgdir"/uts
+    mkdir -p "$cfgdir"/interfaces "$cfgdir"/apps/init "$cfgdir"/uts "$cfgdir"/cpuset
+
+    _setup_generateContext
 
     _setup_writeSingleOption "$name"            "$cfgdir"/name
     _setup_writeSingleOption "$SETUP_CONTEXT"   "$cfgdir"/context
@@ -150,6 +205,12 @@ function setup_writeOption
     _setup_writeSingleOption "$SETUP_NETPREFIX" "$cfgdir"/interfaces/prefix
     _setup_writeSingleOption "$SETUP_NETBCAST"  "$cfgdir"/interfaces/bcast
     _setup_writeSingleOption "$SETUP_INITSTYLE" "$cfgdir"/apps/init/style
+    _setup_writeSingleOption "$SETUP_CPUSET"    "$cfgdir"/cpuset/name
+    _setup_writeSingleOption "$SETUP_CPUSETCPUS"     "$cfgdir"/cpuset/cpus
+    _setup_writeSingleOption "$SETUP_CPUSETCPUSEXCL" "$cfgdir"/cpuset/cpus_exclusive
+    _setup_writeSingleOption "$SETUP_CPUSETMEMS"     "$cfgdir"/cpuset/mems
+    _setup_writeSingleOption "$SETUP_CPUSETMEMSEXCL" "$cfgdir"/cpuset/mem_exclusive
+    _setup_writeSingleOption "$SETUP_CPUSETVIRT"     "$cfgdir"/cpuset/virtualized
 
     local idx=0
     for i in "${SETUP_INTERFACES[@]}"; do