3 # useradd(8) wrapper for vservers
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2004-2006 The Trustees of Princeton University
11 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
12 test -e "$UTIL_VSERVER_VARS" || {
13 echo "Can not find util-vserver installation; aborting..."
16 . "$UTIL_VSERVER_VARS"
26 pushd "$__DEFAULT_VSERVERDIR/.vref" >/dev/null
28 if [ -z "$TYPES" ] ; then
36 echo "Usage: vuseradd [OPTION]... [NAME]"
37 echo " -t Reference image type ($TYPES)"
42 while getopts "t:" opt ; do
52 shift $(($OPTIND - 1))
58 # Add slices group to /etc/group if not already present
59 groupadd slices 2>/dev/null || :
61 # Add slice name to /etc/passwd
62 useradd -g slices -s /bin/vsh $NAME -p '*'
66 GROUPNAME=`id -gn $NAME`
68 # Create /etc/vservers configuration files
69 if [ ! -d $__CONFDIR/$NAME ] ; then
70 # Move away the guest contents for now
71 if [ -d $__DEFAULT_VSERVERDIR/$NAME ] ; then
72 mkdir -p "$__DEFAULT_VSERVERDIR/.vtmp"
73 TMP=$(mktemp -d "$__DEFAULT_VSERVERDIR/.vtmp/$NAME.XXXXXX")
74 mv $__DEFAULT_VSERVERDIR/$NAME "$TMP"
80 $_VSERVER $NAME build -m skeleton --context $USERID \
81 --interface nodev:`hostname -i` \
82 --interface nodev:127.0.0.1 \
83 --flags persistent,~info_init
86 if [ $RETVAL -ne 0 ] ; then
87 echo "Error $RETVAL building $DIR"
88 rm -rf $DIR $__DEFAULT_VSERVERDIR/$NAME
90 mkdir -p $DIR/apps/init $DIR/rlimits $DIR/sched $DIR/dlimits/0 $DIR/sysctl/0
91 echo default > $DIR/apps/init/mark
92 echo 1000 > $DIR/rlimits/nproc.hard
94 # Set persistent for the network context
95 echo persistent,lback_allow > $DIR/nflags
97 # Set up the scheduler
98 echo 100 > $DIR/sched/interval
99 echo 1000 > $DIR/sched/interval2
100 echo 0 > $DIR/sched/fill-rate
101 echo 1 > $DIR/sched/fill-rate2
102 touch $DIR/sched/idle-time
103 echo 100 > $DIR/sched/tokens
104 echo 50 > $DIR/sched/tokens-min
105 echo 100 > $DIR/sched/tokens-max
107 # Set up disk limits (unlimited)
108 echo `$_READLINK $DIR/vdir` > $DIR/dlimits/0/directory
109 echo 2 > $DIR/dlimits/0/reserved
110 echo -1 > $DIR/dlimits/0/inodes_total
111 echo -1 > $DIR/dlimits/0/space_total
113 # Set up sysctl variables
114 echo net.ipv4.ip_forward > $DIR/sysctl/0/setting
115 echo 1 > $DIR/sysctl/0/value
117 # Disable mount namespaces
118 touch $DIR/nonamespace
120 # Add spaces directory
123 # Remove the basically empty guest directory
124 rm -rf $__DEFAULT_VSERVERDIR/$NAME
125 # Move the guest back
126 if [ "$HAS_VSERVERDIR" = 1 ] ; then
127 mv "$TMP/$NAME" $__DEFAULT_VSERVERDIR/$NAME
132 if [ ! -d "$__DEFAULT_VSERVERDIR/$NAME" ] ; then
134 if [ "$TYPE" = "default" ] ; then
135 for i in "$__DEFAULT_VSERVERDIR/.vcache/"* ; do
136 [ -d "$i" ] && mv "$i" "$__DEFAULT_VSERVERDIR/$NAME" && break
140 # Build slice from reference image
141 if [ ! -d "$__DEFAULT_VSERVERDIR/$NAME" ] ; then
142 REF="$__DEFAULT_VSERVERDIR/.vref/$TYPE"
144 # Build in temporary directory
145 mkdir -p "$__DEFAULT_VSERVERDIR/.vtmp"
146 TMP=$(mktemp -d "$__DEFAULT_VSERVERDIR/.vtmp/$NAME.XXXXXX")
147 "$_VCLONE" "$REF"/ "$TMP"/
150 # Move it to its permanent location when complete
151 if [ $RETVAL -eq 0 ] ; then
152 mv "$TMP" "$__DEFAULT_VSERVERDIR/$NAME"
154 echo "Error $RETVAL building $__DEFAULT_VSERVERDIR/$NAME"
155 rm -rf "$TMP" $__CONFDIR/$NAME $__PKGSTATEDIR/$NAME.ctx
162 if [ -d "$__DEFAULT_VSERVERDIR/$NAME" ] ; then
164 chmod 755 "$__DEFAULT_VSERVERDIR/$NAME"
166 # Add user in vserver
167 $_VSERVER $NAME start --rescue sh -c \
168 "groupadd -g $GROUPID $GROUPNAME ; useradd -m -u $USERID -g $GROUPID -p '' $NAME"
170 # Stop the guest (since it's persistent)
173 # Add an unrestricted entry to /etc/sudoers file
174 if [ -f "$__DEFAULT_VSERVERDIR/$NAME/etc/sudoers" ] && \
175 ! grep -q "^$NAME" "$__DEFAULT_VSERVERDIR/$NAME/etc/sudoers" ; then
176 echo "$NAME ALL=(ALL) ALL" >> "$__DEFAULT_VSERVERDIR/$NAME/etc/sudoers"
179 cp -a /dev/fuse $__DEFAULT_VSERVERDIR/$NAME/dev/