From 6e7d84cf0bdb34db2f7d9632011537b67a475f7e Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Mon, 20 Oct 2003 18:07:33 +0000 Subject: [PATCH] initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@235 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/doc/cfgdirdoc.dtd | 35 +++ util-vserver/doc/configuration.xml | 367 +++++++++++++++++++++++ util-vserver/scripts/vserver-build.apt-rpm | 53 ++++ util-vserver/scripts/vserver-build.functions | 202 +++++++++++++ util-vserver/scripts/vserver-build.functions.apt | 55 ++++ util-vserver/scripts/vserver-build.functions.rpm | 70 +++++ util-vserver/scripts/vserver-setup.functions | 180 +++++++++++ 7 files changed, 962 insertions(+) create mode 100644 util-vserver/doc/cfgdirdoc.dtd create mode 100644 util-vserver/doc/configuration.xml create mode 100644 util-vserver/scripts/vserver-build.apt-rpm create mode 100644 util-vserver/scripts/vserver-build.functions create mode 100644 util-vserver/scripts/vserver-build.functions.apt create mode 100644 util-vserver/scripts/vserver-build.functions.rpm create mode 100644 util-vserver/scripts/vserver-setup.functions diff --git a/util-vserver/doc/cfgdirdoc.dtd b/util-vserver/doc/cfgdirdoc.dtd new file mode 100644 index 0000000..795931b --- /dev/null +++ b/util-vserver/doc/cfgdirdoc.dtd @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/util-vserver/doc/configuration.xml b/util-vserver/doc/configuration.xml new file mode 100644 index 0000000..9453f3b --- /dev/null +++ b/util-vserver/doc/configuration.xml @@ -0,0 +1,367 @@ + + + + The configuration directory of the current vserver + + + The name of the current vserver + + + The fixed value of the current action (e.g. 'prepre-start', 'post-stop', ...). + + "> + + + + + + + + + + +]> + + + + + The base configuration directory. + + + + + Default mtab-file + + + + + + /vservers + + + + + + + /vservers/.pkg + + + + + + + + + + + + + + + + + Contains files with packages. + + File which contains the name of packages. On top of file the special + keywords '--reinstall' and '--can-fail' are possible. + + + + + Directory with GPG pubkeys which are used to sign the packages of this + distribution. + + + + Default content of the /etc/apt/ directory. + + + + Default content of the /etc/rpm directory. + + + + Directory which overrides /usr/lib/rpm. + + + + Directory with all executables and libraries which are required for this + distribution. + + + + + + The configuration directory for the vserver vserver-name. + + + Contains the hostname of the vserver. + + + + Contains the name of the vserver. When not given, the basename of the directory + will be assumed as this name. + + + + The domainname of the vserver. + + + + The nice-level on which the vserver will be started. + + + + Contains per line a capability. + + + + Contains the pathname of the shell which will be used by the "vserver ... enter" + command. + + + + Contains per line a flag. + + + + Contains the context which shall be used for the vserver. + + + + The fstab file for the vserver. + + + + + + The initial-mtab which will be used for the vserver. + + + + Contains the init-style; possible values are 'sysv', 'plain' and + 'minit'. + + + + The start runlevel. + + + + The start runlevel. + + + + The stop runlevel. + + + + Contains the 'signal [wait signal]*' sequence which is used to stop the + vserver. + + + + The command which is used to start the vserver. Each option must be on a + separate line. + + + + The command which is used to wait on the vserver after it has been + started. Each option must be on a separate line. This file will be ignored when the + 'sync' does not exist and the '--sync' option was not used. + + + + The command which is used to stop the vserver. Each option must be on a + separate line. + + + + The command which is used to wait on the vserver after it has been + stopped. Each option must be on a separate line. This file will be ignored when the + 'sync' does not exist and the '--sync' option was not used. + + + + The command which is used to setup the init-system (e.g. to set the runlevel + in the utmp-file). Each option must be on a separate line. + + + + Without this file, the 'cmd.*-sync' files will be ignored. + + + + + + + + &init-dir-text; &init-prepre-start; + + + + + + + + &init-dir-text; &init-pre-start; + + + + + + + + + &init-dir-text; &init-post-start; + + + + + + + + &init-dir-text; &init-pre-stop; + + + + + + + &init-dir-text; &init-post-stop; + + + + + + + &init-dir-text; &init-postpost-stop; + + + + + + + + + The deafult broadcast address. + + + The default network-device. + + + The default network-prefix. + + + The default network-mask. + + + The default scope of the network interfaces. + + + + + When this file exists, this interface will be ignored. + + + + The ip which will be assigned to this interface. + + + The broadcast address. + + + The network-device. + + + The network-prefix. + + + The network-mask. + + + The scope of the network interfaces. + + + When this file exists, the interface will be named. + + + + + + + + + diff --git a/util-vserver/scripts/vserver-build.apt-rpm b/util-vserver/scripts/vserver-build.apt-rpm new file mode 100644 index 0000000..772fec5 --- /dev/null +++ b/util-vserver/scripts/vserver-build.apt-rpm @@ -0,0 +1,53 @@ +# $Id$ --*- sh -*-- + +# Copyright (C) 2003 Enrico Scholz +# +# 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. + +tmp=$(getopt -o +d: -n "$0" -- "$@") || exit 1 +eval set -- "$tmp" + +. "$_LIB_VSERVER_BUILD_FUNCTIONS_RPM" +. "$_LIB_VSERVER_BUILD_FUNCTIONS_APT" + +while true; do + case "$1" in + -d) DISTRIBUTION=$2; shift;; + --) shift; break ;; + *) echo "vserver-build.apt-rpm: internal error: unrecognized option '$1'" + exit 1 + ;; + esac + shift +done + +getDistribution + +initVariables +initVariablesRPM +initVariablesApt + +initFilesystem "$OPTION_FORCE" +initFilesystemRPM +initFilesystemApt + +setup_writeOption "$VSERVER_NAME" +setup_writeInitialFstab + +test -z "$BUILD_INITPRE" || "$BUILD_INITPRE" "$SETUP_CONFDIR" "$_VSERVER" +importGPGPubKeys "$VSERVER_NAME" "$CONFDIR/.distributions/$DISTRIBUTION/pubkeys" "$DISTRIBDIR/$DISTRIBUTION/pubkeys" +"$_VAPT_GET" "$SETUP_CONFDIR" -- update +installBasePackages "$SETUP_CONFDIR" "$PKGDIR" +"$_VAPT_GET" "$SETUP_CONFDIR" -- dist-upgrade -y +test -z "$BUILD_INITPOST" || "$BUILD_INITPOST" "$SETUP_CONFDIR" "$_VSERVER" diff --git a/util-vserver/scripts/vserver-build.functions b/util-vserver/scripts/vserver-build.functions new file mode 100644 index 0000000..20829d2 --- /dev/null +++ b/util-vserver/scripts/vserver-build.functions @@ -0,0 +1,202 @@ +# $Id$ --*- sh -*-- + +# Copyright (C) 2003 Enrico Scholz +# +# 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. + +# Expected environment: +# $VSERVER_NAME ... name of vserver + +ROOTDIR= +ROOTDIR_REL= +PKGCFGBASE= +PKGCFGBASE_REL= +VSERVERDIRNAME= + +VDIR= + +_DEV_FILE= +_EXEC_DIR= + +BUILD_INITPRE= +BUILD_INITPOST= + +function makeDevEntry +{ + local dst=$1/$2 + case "$3" in + c|b) mknod -m$6 "$dst" $3 $4 $5;; + d) mkdir -p -m$4 "$dst";; + f) touch "$dst" + chmod $4 "$dst" + ;; + *) echo "Unknown dev-entry mode '$3'" >&2 + false + ;; + esac +} + +function populateDirectory +{ + local dst=$1 + local i + + shift + for i; do + local file= + + for file in "$i"/*; do + test -e "$file" || continue + case "$file" in + */CVS) test ! -d "$file" || continue;; + *.rpmsave|*.rpmnew|*.rpmorig) + continue;; + esac + + cp -a "$file" "$dst/" + done + done +} + +function _setRootDir +{ + test -z "$ROOTDIR" || return 0 + + for item in "\"$CONFDIR/.defaults/vdirbase\" 1" "/vservers"; do + eval set -- "$item" + ROOTDIR=$1 + ROOTDIR_REL=$2 + test ! -d "$ROOTDIR" || break + done + + test -d "$ROOTDIR" || { + echo "Root-directory '$ROOTDIR' does not exist or is invalid" + exit 1 + } +} + +function _setPkgCfgBase +{ + test -z "$PKGCFGBASE" || return 0 + + for item in "\"$CONFDIR/.defaults/apps/pkgmgmt/base\" 1" "$ROOTDIR/.pkg"; do + eval set -- "$item" + PKGCFGBASE=$1 + PKGCFGBASE_REL=$2 + test ! -d "$PKGCFGBASE" || break + done + + test -d "$PKGCFGBASE" || { + echo "pkgcfgbase-dir '$PKGCFGBASE' does not exists or is invalid" + exit 1 + } +} + +function _setPkgCfg +{ + _setPkgCfgBase + + test -z "$PKGCFGDIR" || return 0 + PKGCFGDIR="$PKGCFGBASE/$VSERVERDIRNAME" +} + +function _setVserverDir +{ + test -z "$VSERVERDIRNAME" || return 0 + VSERVERDIRNAME="$VSERVER_NAME" +} + +function _setVdir +{ + VDIR="$ROOTDIR/$VSERVERDIRNAME" +} + +function say +{ + test -z "$OPTION_SILENT" || return 0 + echo "$@" +} + +function _renameVserverCfg +{ + local suffix=.~$(date +'%s')~ + local i + + for i in "$VDIR" "$SETUP_CONFDIR"; do + test ! -e "$i" || { + mv "$i" "$i$suffix" + say "Renamed '$i' to '$i$suffix'" + } + done +} + +function getDistribution +{ + test -z "$DISTRIBUTION" || return 0 + + if test -e /etc/fedora-release; then + set -- $(cat /etc/fedora-release) + DISTRIBUTION=fdr$4 + elif test -e /etc/redhat-release; then + set -- $(cat /etc/redhat-release) + DISTRIBUTION=rh$5 + elif test -e /etc/debian_version; then + set -- $(cat /etc/debian_version) + DISTRIBUTION=deb$1 + else + echo "Can not determine distribution; please specify it manually" + echo "with the '-d' option" + exit 1 + fi >&2 +} + +## Usage: initFilesystem [force] +function initFilesystem +{ + test -z "$1" || _renameVserverCfg + test ! -d "$VDIR" -a ! -d "$SETUP_CONFDIR" || { + echo "vserver-topdirectory and/or configuration exist already; " + echo "please try to use '--force', or remove them manually" + exit 1 + } >&2 + + mkdir -p "$SETUP_CONFDIR"/apps/pkgmgmt "$VDIR"/{dev/pts,etc} "$SETUP_LOCKREVDIR" + + ln -s "$VDIR" "$SETUP_CONFDIR/vdir" + + test -z "$PKGCFGDIR" || { + ln -s "$PKGCFGDIR" "$SETUP_CONFDIR/apps/pkgmgmt/base" + + test "$_EXECDIR" = / || ln -s "$_EXECDIR" "$confdir/apps/pkgmgmt/execdir" + } + + local spec + while read spec; do + makeDevEntry "$VDIR"/dev $spec + done <$_DEV_FILE +} + +function initVariables +{ + _setRootDir + _setVserverDir + _setPkgCfg + _setVdir + + findFile _DEV_FILE "$CONFDIR/.distributions/$DISTRIBUTION/devs" "$DISTRIBDIR/$DISTRIBUTION/devs" "$DISTRIBDIR/defaults/devs" + findDir _EXECDIR "$CONFDIR/.distributions/$DISTRIBUTION/execdir" "$DISTRIBDIR/$DISTRIBUTION/execdir" / + findFile BUILD_INITPRE "$CONFDIR/.distributions/$DISTRIBUTION/initpre" "$DISTRIBDIR/$DISTRIBUTION/initpre" "" + findFile BUILD_INITPOST "$CONFDIR/.distributions/$DISTRIBUTION/initpost" "$DISTRIBDIR/$DISTRIBUTION/initpost" "" + findDir PKGDIR "$CONFDIR/.distributions/$DISTRIBUTION/pkgs" "$DISTRIBDIR/$DISTRIBUTION/pkgs" / +} diff --git a/util-vserver/scripts/vserver-build.functions.apt b/util-vserver/scripts/vserver-build.functions.apt new file mode 100644 index 0000000..ce86246 --- /dev/null +++ b/util-vserver/scripts/vserver-build.functions.apt @@ -0,0 +1,55 @@ +# $Id$ --*- sh -*-- + +# Copyright (C) 2003 Enrico Scholz +# +# 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. + +function installBasePackages +{ + local name="$1" + local dir="$2" + + test "$dir" != / || return 0 + for filelist in "$dir"/*; do + test -f "$filelist" || continue + local idx=0 + local can_fail=false + local flags= + + set -- $(<$filelist) + while test "$#" -gt 0; do + case "$1" in + --reinstall) flags='--reinstall';; + --can-fail) can_fail=true;; + *) break;; + esac + shift + done + "$_VAPT_GET" "$name" -- install -y $flags $* || $can_fail + done +} + +function initVariablesApt +{ + : +} + +function initFilesystemApt +{ + mkdir -p "$PKGCFGDIR"/apt/{etc,archives/partial,cache,state/lists/partial} + + populateDirectory "$PKGCFGDIR/apt/etc" \ + "$DISTRIBDIR/$DISTRIBUTION/apt" \ + "$CONFDIR/.distributions/$DISTRIBUTION/apt" +} diff --git a/util-vserver/scripts/vserver-build.functions.rpm b/util-vserver/scripts/vserver-build.functions.rpm new file mode 100644 index 0000000..ec6a081 --- /dev/null +++ b/util-vserver/scripts/vserver-build.functions.rpm @@ -0,0 +1,70 @@ +# $Id$ --*- sh -*-- + +# Copyright (C) 2003 Enrico Scholz +# +# 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. + +RPMDB_PATH= +RPMLIBDIR= + +function importGPGPubKeys +{ + local dst=$1 + local pkgs + declare -a pkgs + local i + + shift + for i; do + local file= + for file in "$i"/*; do + test -f "$file" || continue + + pkgs=( "${pkgs[@]}" "$file" ) + done + done + + test "$pkgs" || return 0 + "$_VRPM" "$dst" -- --import "${pkgs[@]}" +} + +function initVariablesRPM +{ + test "$WORKAROUND_106057" && \ + RPMDB_PATH=/.rpmdb || \ + RPMDB_PATH=/dev + + findDir RPMLIBDIR "$CONFDIR/.distributions/$DISTRIBUTION/rpmlib" "$DISTRIBDIR/$DISTRIBUTION/rpmlib" / +} + +function initFilesystemRPM +{ + test -z "$WORKAROUND_106057" || mkdir -p "$RPMDB_PATH" + mkdir -p "$VDIR$RPMDB_PATH" + + test -z "$WORKAROUND_APT_DBPATH" || { + mkdir -p "$VDIR/var/lib" + ln -s "$RPMDB_PATH" "$VDIR/var/lib/rpm" + } + + mkdir -p "$PKGCFGDIR"/rpm/{etc,state} + populateDirectory "$PKGCFGDIR/rpm/etc" \ + "$DISTRIBDIR/defaults/rpm" \ + "$DISTRIBDIR/$DISTRIBUTION/rpm" \ + "$CONFDIR/.distributions/$DISTRIBUTION/rpm" + + echo "%_dbpath $RPMDB_PATH" >>"$pkgcfg/etc/macros" + + test "$RPMLIBDIR" = / || ln -s "$RPMLIB" "$confdir/apps/pkgmgmt/rpmlib" +} diff --git a/util-vserver/scripts/vserver-setup.functions b/util-vserver/scripts/vserver-setup.functions new file mode 100644 index 0000000..ebab93c --- /dev/null +++ b/util-vserver/scripts/vserver-setup.functions @@ -0,0 +1,180 @@ +# $Id$ --*- sh -*-- + +# Copyright (C) 2003 Enrico Scholz +# +# 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. + +SETUP_HOSTNAME= +SETUP_NETDEV= +SETUP_NETMASK= +SETUP_NETPREFIX= +SETUP_BCAST= +SETUP_LOCKFILE= +SETUP_LOCKREVDIR= +SETUP_CONFDIR= + +declare -a SETUP_INTERFACES=() +declare -a SETUP_FLAGS=() + +declare -r SETUP_OPTIONS="confdir:lockfile:lockrevdir:hostname:,netdev:,netmask:,netprefix:,netbcast:,interface:,flags:" +declare -r SETUP_HELPMSG=$" + --confdir ... [default: $CONFDIR/] + --lockfile + ... [default: $RUNDIR/] + --lockrevdir + ... [default: $RUNDIR/rev/] + --hostname + --netdev + --netmask |--netprefix + ... sets the default netmask (a.b.c.d quadruple) or prefix + (length of the interface) + --interface [=][:][/] + ... declares an network-interface; this option can be specified + multiple times + --flags + + ... sets comma-separated list of flags; possible flags are + lock: Prevent the vserver from setting new security context + sched: Merge scheduler priority of all processes in the + vserver so that it acts a like a single one. + nproc: Limit the number of processes in the vserver + according to ulimit (instead of a per user limit, + this becomes a per vserver limit) + private: No other process can join this security context. + Even root +" + +function setup_setOption2 +{ + case "$1" in + --confdir) SETUP_CONFDIR=$2;; + --lockfile) SETUP_LOCKFILE=$2;; + --lockrevdir) SEUTP_LOCKREVDIR=$2;; + --hostname) SETUP_HOSTNAME=$2;; + --netdev) SETUP_NETDEV=$2;; + --netmask) SETUP_NETMASK=$2;; + --netprefix) SETUP_NETPREFIX=$2;; + --netbcast) SETUP_BCAST=$2;; + --interface) SETUP_INTERFACES=( "${SETUP_INTERFACES[@]}" "$2" );; + --flags) old_IFS=$IFS + IFS=, + set -- $2 + SETUP_FLAGS=( "${SETUP_FLAGS[@]}" "$@" ) + IFS=$old_IFS + ;; + *) return 1;; + esac + + return 0 +} + +function _setup_writeSingleOption +{ + test -z "$1" || echo "$1" >"$2" +} + +function _setup_writeInterface +{ + local vdir=$1 + local idx=$2 + local tmp=$3 + + local name=${tmp%%=*} + test "$name" != "$tmp" || name= + + tmp=${tmp##${name}=} + local dev=${tmp%%:*} + test "$dev" != "$tmp" || dev= + + tmp=${tmp##${dev}:} + local mask=${tmp##*/} + test "$mask" != "$tmp" || mask= + + local ip=${tmp%%/${mask}} + + local prefix= + test "${mask%%.*}" != "$mask" || { + prefix=$mask + mask= + } + + d=$vdir/interfaces/$idx + mkdir "$d" + + _setup_writeSingleOption "$name" $d/name + _setup_writeSingleOption "$dev" $d/dev + _setup_writeSingleOption "$ip" $d/ip + _setup_writeSingleOption "$mask" $d/mask + _setup_writeSingleOption "$prefix" $d/prefix +} + +function setup_setDefaults +{ + : ${SETUP_CONFDIR:=$CONFDIR/$1} + : ${SETUP_LOCKFILE:=$RUNDIR/$1} + : ${SETUP_LOCKREVDIR:=$RUNDIR/rev} +} + +function setup_writeOption +{ + local name=$1 + local cfgdir=$SETUP_CONFDIR + local i + + mkdir -p "$cfgdir/interfaces" + + _setup_writeSingleOption "$name" "$cfgdir"/name + _setup_writeSingleOption "$SETUP_HOSTNAME" "$cfgdir"/hostname + _setup_writeSingleOption "$SETUP_NETDEV" "$cfgdir"/interfaces/dev + _setup_writeSingleOption "$SETUP_NETMASK" "$cfgdir"/interfaces/mask + _setup_writeSingleOption "$SETUP_NETPREFIX" "$cfgdir"/interfaces/prefix + + local idx=0 + for i in "${SETUP_INTERFACES[@]}"; do + _setup_writeInterface "$cfgdir" $idx "$i" + let ++idx + done + + test -z "$SETUP_FLAGS" || for i in "${SETUP_FLAGS[@]}"; do + echo "$i" + done >"$cfgdir"/flags + + ln -s "$SETUP_LOCKFILE" "$cfgdir/run" + ln -s "$SETUP_LOCKREVDIR" "$cfgdir/run.rev" +} + +function setup_writeInitialFstab +{ + cat <$SETUP_CONFDIR/fstab +none /proc proc defaults 0 0 +none /tmp tmpfs size=16m,mode=1777 0 0 +none /dev/pts devpts gid=5,mode=620 0 0 +EOF +} + +function setup_test +{ + SETUP_INTERFACES=() + + setup_setOption2 --interface foo0=eth0:1.2.3.4/1 + setup_setOption2 --interface foo1=eth0:1.2.3.4/255.255.248.0 + setup_setOption2 --interface foo2=eth0:1.2.3.4 + setup_setOption2 --interface foo3=1.2.3.4 + setup_setOption2 --interface foo4=1.2.3.4/1 + setup_setOption2 --interface eth0:1.2.3.4 + setup_setOption2 --interface eth0:1.2.3.4/1 + setup_setOption2 --interface 1.2.3.4 + setup_setOption2 --interface 1.2.3.4/1 + + setup_writeOption xx +} -- 1.8.1.5