Vserver tools

From YobiWiki
Jump to navigation Jump to search

De-unifying tool

From within a vserver, unified files cannot be directly modified.
You could face such problem when upgrading with apt-get when apt tries to make .dpkg-tmp copies for some critical(?) files
The user can of course make a copy, delete the file and re-create it but this is inconvenient therefore this tool :-)

#!/bin/bash

# Copyright Philippe Teuwen <phil_at_teuwen.org>
# License: GPLv3 or later

if [ "$1" = "-v" ]; then
    DEBUG=true
    shift
else
    DEBUG=false
fi

files="$*"
if [ "$files" = "" ]; then
    echo "Usage: $0 [-v] <file(s) to deunify>"
    echo "       (-v for verbose mode)"
    exit
fi
for f in $files; do
    if [ -f "$f" ]&&[ ! -L "$f" ]; then
	if lsattr "$f"|cut -f1 -d " "|grep -q "....i.......E...."; then
	    $DEBUG && echo "Deunifying file $f..."
	    $DEBUG && echo -n "inode : " && ls -i "$f"|cut -f1 -d " "
	    $DEBUG && echo -n "attr  : " && lsattr "$f"|cut -f1 -d " "
    	    tmpfile="$(mktemp /tmp/deunify.XXXXXX)"
	    cp -a "$f" "$tmpfile"
	    mv -f "$tmpfile" "$f"
	    echo "File $f deunified!"
	    $DEBUG && echo -n "inode : " && ls -i "$f"|cut -f1 -d " "
	    $DEBUG && echo -n "attr  : " && lsattr "$f"|cut -f1 -d " "
	else
	    $DEBUG && echo "Skipping file $f, already deunified..."
	    $DEBUG && echo -n "inode : " && ls -i "$f"|cut -f1 -d " "
	    $DEBUG && echo -n "attr  : " && lsattr "$f"|cut -f1 -d " "
	fi
    else
	$DEBUG && echo "$f does not appear to be a regular file, skipping..."
    fi
done

Deleting tool

Be careful with this one, of course

#!/bin/bash

# Copyright Philippe Teuwen <phil_at_teuwen.org>
# Lincense: GPLv3 or later

VSERVER=$1
vserver $VSERVER status
ret=$?
if [ "$ret" = 5 ]; then
    echo "Please specify an existing vserver!"
    exit 1
fi
if [ "$ret" != 3 ]; then
    vserver $VSERVER stop
fi
echo Deleting /etc/vservers/$VSERVER ...
rm -rf /etc/vservers/$VSERVER
echo Deleting /var/run/vservers/$VSERVER ...
rm -rf /var/run/vservers/$VSERVER
echo Deleting symlinks in /var/run/vservers.rev/ ...
ls -l /var/run/vservers.rev/|\
    grep -o "[0-9]\+ -> /etc/vservers/$VSERVER"|\
    cut -d ' ' -f 1|\
    xargs rm -f
echo -n Deleting /etc/vservers/.defaults/vdirbase/$VSERVER ...
lsof|grep $VSERVER
i=0
# We need to insist a bit on this one...
while [[ "$i" -lt 10 ]] && ! rm -rf /etc/vservers/.defaults/vdirbase/$VSERVER >& /dev/null ; do
    echo -n .
    i=$(($i+1))
    sleep 1
done
echo
echo Done.

Dupvserver patch

Make dupvserver usable with the new vserver config method and handle static contextes

--- dupvserver	2006-03-29 20:04:43.000000000 +0200
+++ dupvserver.new	2006-03-29 20:04:15.000000000 +0200
@@ -17,11 +17,11 @@
 # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 # 02111-1307, USA.

-VSERVERS_ROOT=/vservers
+VSERVERS_ROOT=/etc/vservers/.defaults/vdirbase

-if [ -r /etc/vservers.conf ] ; then
-    . /etc/vservers.conf
-fi
+#if [ -r /etc/vservers.conf ] ; then
+#    . /etc/vservers.conf
+#fi

 usage ()
 {
@@ -95,14 +95,14 @@
 		    echo "ERROR: Vserver $FROM do not exist."
 		    exit 1
 		fi
-		if [ ! -r "/etc/vservers/$FROM.conf" ] ; then
+		if [ ! -d "/etc/vservers/$FROM" ] ; then
 		    echo "ERROR: Vserver config for $FROM do not exist."
 		    exit 1
 		fi
-		. /etc/vservers/$FROM.conf
-		FROMNAME=$S_HOSTNAME
-		FROMIP=$IPROOT
-		FROMDEV=$IPROOTDEV
+#		. /etc/vservers/$FROM.conf
+		FROMNAME="$(cat /etc/vservers/$FROM/name)"
+		FROMIP="$(cat /etc/vservers/$FROM/interfaces/0/ip)"
+		FROMDEV="$(cat /etc/vservers/$FROM/interfaces/0/dev)"
 		;;
 	    --to)
 		TO="$2"
@@ -184,16 +184,25 @@
 $FINDTOREPIP" | sort -u
 fi

-if [ ! -r /etc/vservers/$TO.conf -o "$FORCE" = "yes" ] ; then
-    cp /etc/vservers/$FROM.conf /etc/vservers/$TO.conf
-    perl -pi -e "s#$FROM#$TO#g;" \
-       /etc/vservers/$TO.conf
+if [ ! -d /etc/vservers/$TO -o "$FORCE" = "yes" ] ; then
+    mkdir /etc/vservers/$TO
+    cp -a /etc/vservers/$FROM/* /etc/vservers/$TO
+    rm /etc/vservers/$TO/run
+    ln -s /var/run/vservers/$TO /etc/vservers/$TO/run
+    rm /etc/vservers/$TO/vdir
+    ln -s /etc/vservers/.defaults/vdirbase/$TO /etc/vservers/$TO/vdir
+    find /etc/vservers/$TO -type f -exec perl -pi -e "s#$FROM#$TO#g;" {} \;
     if [ "$FROMIP" != "$TOIP" ] ; then
-       perl -pi -e "s#$FROMIP#$TOIP#g;" \
-           /etc/vservers/$TO.conf
+       find /etc/vservers/$TO -type f -exec perl -pi -e "s#$FROMIP#$TOIP#g;" {} \;
     fi
     if [ "$FROMDEV" != "$TODEV" -a -n "$TODEV" ] ; then
-       perl -pi -e "s#$FROMDEV#$TODEV#g;" \
-           /etc/vservers/$TO.conf
+       find /etc/vservers/$TO -type f -exec perl -pi -e "s#$FROMDEV#$TODEV#g;" {} \;
+    fi
+    if [ -e /etc/vservers/$TO/context ]; then
+       echo -n "Choose a new context: "
+       echo $RANDOM > /etc/vservers/$TO/context
+       cat /etc/vservers/$TO/context
+       echo "Apply new context to files..."
+       chxid -c $(cat /etc/vservers/$TO/context) -R $VSERVERS_ROOT/$TO
     fi
 fi

Newvserver patch

  • Add better support for etch
  • Prevent corruption of /etc/motd
--- newvserver	2006-03-29 20:04:28.000000000 +0200
+++ newvserver.new	2006-03-29 20:04:10.000000000 +0200
@@ -45,7 +45,7 @@
 REMOVE_PACKAGES="sparc-utils,dhcp-client,lilo,makedev,pcmcia-cs,ppp,pppconfig,pppoe,pppoeconf,setserial,syslinux,fdutils,libpcap0,iptables,pciutils"

 # sysvinit services relating to hardware access to remove
-REMOVE_LINKS="klogd hwclock.sh setserial urandom networking umountfs halt reboot mountvirtfs mountall.sh mountnfs.sh ifupdown"
+REMOVE_LINKS="klogd hwclock.sh setserial urandom networking umountfs umountroot halt reboot mountvirtfs mountall.sh mountnfs.sh ifupdown"

 # Post installation script
 POST_INSTALL_SCRIPT=""
@@ -384,7 +384,7 @@
 fi

 ## use "vserver ... build" to build the new vserver
-if ! /usr/sbin/vserver "$VHOST" build -m debootstrap \
+if ! vserver "$VHOST" build -m debootstrap \
        --rootdir "$VROOTDIR" --hostname "$VHOST" --interface "$INTERFACE:$IP" \
        -- -d "$DIST" -m "$MIRROR" \
        -- $ARCH_ARGUMENT \
@@ -398,12 +398,12 @@
 # Make it so that apt and friends work
 cat << EOF > "$VROOTDIR/$VHOST/etc/apt/sources.list"
 deb $MIRROR/ $DIST main non-free contrib
-deb-src $MIRROR/ $DIST main non-free contrib
+#deb-src $MIRROR/ $DIST main non-free contrib

-deb http://non-us.debian.org/debian-non-US $DIST/non-US main contrib non-free
-deb-src http://non-us.debian.org/debian-non-US $DIST/non-US main contrib non-free
+#deb http://non-us.debian.org/debian-non-US $DIST/non-US main contrib non-free
+#deb-src http://non-us.debian.org/debian-non-US $DIST/non-US main contrib non-free

-deb http://security.debian.org $DIST/updates main contrib non-free
+#deb http://security.debian.org $DIST/updates main contrib non-free

 EOF

@@ -455,8 +455,13 @@
 # uname -a > $VROOTDIR/$VHOST/etc/motd

 # Create a shorter motd (uname -a would give name of host-server)
-echo "Debian GNU/Linux ($DIST/$(uname -m)) $VHOST.$VDOMAIN" \
-    > "$VROOTDIR/$VHOST/etc/motd"
+if [ -L "$VROOTDIR/$VHOST/etc/motd" ]; then
+    echo "Debian GNU/Linux ($DIST/$(uname -m)) $VHOST.$VDOMAIN" \
+	> "$VROOTDIR/$VHOST/var/run/motd"
+else
+    echo "Debian GNU/Linux ($DIST/$(uname -m)) $VHOST.$VDOMAIN" \
+	> "$VROOTDIR/$VHOST/etc/motd"
+fi

 # Create a dummy fstab
 cat << EOF > "$VROOTDIR/$VHOST/etc/fstab"
@@ -526,19 +531,26 @@

 dselect update

-tzsetup -y
+if [ "$DIST" == "etch" ]||[ "$DIST" == "sid" ]; then
+    tzconfig
+else
+    tzsetup -y
+fi

 dpkg-reconfigure passwd

-tasksel
+if [ "$DIST" == "woody" ]||[ "$DIST" == "sarge" ]; then
+    tasksel
+fi

 if [ "$DIST" == "woody" ]; then
     rm -f /etc/exim/exim.conf
     eximconfig
 fi
-
-# because the --exclude flag doesn\'t seem to work on debootstrap
-dpkg -P `echo $REMOVE_PACKAGES | sed -e 's/,/ /g'`
+if [ "$DIST" == "woody" ]||[ "$DIST" == "sarge" ]; then
+    # because the --exclude flag doesn\'t seem to work on debootstrap
+    dpkg -P `echo $REMOVE_PACKAGES | sed -e 's/,/ /g'`
+fi

 for link in $REMOVE_LINKS
 do

Change also the /etc/hosts to assign localhost to the public ip

Imposing disk usage limits on vservers

This will also be used to monitor easily the disk usage of the vservers

#!/bin/bash

# Link this as /etc/vservers/<servername>/scripts/post-start.d script
# with the desired size limit in Mb.
# For example to set the limit at 10G:
# ln -s /usr/local/sbin/vdlimit_
#       /etc/vservers/<servername>/scripts/post-start.d/vdlimit_10240
# To change the limit on-the-fly simply rename the link and execute 
# ./vdlimit_<newsize> pre-stop <servername>;./vdlimit_<newsize> post-start <servername>;

# Copyright Philippe Teuwen <phil_at_teuwen.org>
# License: GPLv3 or later
# version 1.0

# The script will be called from within the vserver working dir
VSERVER=`pwd|sed 's/\/etc\/vservers\/\(.*\)\/vdir/\1/'`

# space in Mb
SPACE=$(basename $0 | sed 's/^vdlimit_//;')
# 10G if not specified
SPACE=${SPACE:-10240}

# space in kb
SPACE=$(($SPACE*1024))
INODES=$SPACE

vserver $VSERVER status &>/dev/null
ret=$?
if [ "$ret" = 5 ]; then
    echo "Please specify an existing vserver!"
    exit 1
fi
if [ "$ret" != 0 ]; then
    echo "Please specify a running vserver!"
    exit 1
fi

CTX=`cat /var/run/vservers/$VSERVER`
SPACE_USED=`du -sx /etc/vservers/.defaults/vdirbase/$VSERVER | awk '{print $1}'`
INODES_USED=`ls -1aRi /etc/vservers/.defaults/vdirbase/$VSERVER/ 2>/dev/null |\
 awk '/^[0-9]+ / { print $1 }' | sort -u | wc -l`
if [ $SPACE_USED -ge $SPACE ]||[ $INODES_USED -ge $INODES ]; then
    echo "Vserver $VSERVER is already taking more space/inodes than what you try to limit to!"
    exit 1
fi
/usr/sbin/vdlimit --xid $CTX \
                  --set space_total=$SPACE \
                  --set space_used=$SPACE_USED \
                  --set inodes_total=$INODES \
                  --set inodes_used=$INODES_USED \
                  --set reserved=5 /etc/vservers/.defaults/vdirbase/$VSERVER/