#! /bin/sh #---------------------------------------------------- #shell used to create the root file system for the #container, incoming file is a tar file to #insert within the vzgot directory structure # #---------------------------------------------------- #Syntaxe #vzgot.create VE_name tpl_tar_filename dist arch base_dir # VE_name is the container dir name # tpl_tar_filename is the template file name to be used # dist is the distribution name (fc12, rh7.3, el5.4, etc...) # arch is the architecture base (i386, x86_64) # base_dir is this application root directory # used for devel only. #---------------------------------------------------- CONTNAME=$1 TARFILE=$2 DIST=$3 ARCH=$4 BASE=$5 #------------------------------------------------------------------- #abort procedure abort () { echo "$1" exit 1; } #---------------------------------------------------- #default GETAR value, during the container create #CAN BE overriden within /etc/vzgot/vzgot_config #if your template are somewhere else GETAR="cat /var/lib/vzgot/vztemplate/$TARFILE" #---------------------------------------------------- if ! [ -f $BASE/etc/vzgot/vzgot_config ] ; then abort "Missing vzgot config file" fi . $BASE/etc/vzgot/vzgot_config #specific container configuration override (if needed) if [ -f $BASE/$VZLIB/vzdir/$CONTNAME/vzgot_config ] ; then $BASE/$VZLIB/vzdir/$CONTNAME/vzgot_config fi #---------------------------------------------------- if [ -d $BASE/$VZLIB/vzdir/$CONTNAME ] ; then echo "Container $CONTNAME already existing, must be destroyed first" abort "Use command \"vzgot destroy $CONTNAME\" to do it" fi #---------------------------------------------------- #creating Container directory mkdir -p $BASE/$VZLIB/vzdir/$CONTNAME if [ ! -z "$LVM" ] ; then if [ -z "$LVM_SIZE" ] ; then LVM_SIZE="2G" fi VZCONTNAME=`echo $CONTNAME | sed 's/\.//g' | sed 's/-//g'` lvcreate -L$LVM_SIZE -n$VZCONTNAME $LVM > /dev/null 2>&1 ret=$? if [ $ret -ne 0 ]; then abort "vzgot.create, unable to create LVM $VZCONTNAME on volume $LVM" fi mke2fs -q -t ext4 -j -L$VZCONTNAME /dev/$LVM/$VZCONTNAME ret=$? if [ $ret -ne 0 ]; then abort "vzgot.create, unable to format LVM $VZCONTNAME with mke2fs" fi mount /dev/$LVM/$VZCONTNAME $BASE/$VZLIB/vzdir/$CONTNAME ret=$? if [ $ret -ne 0 ]; then abort "vzgot.create, unable to mount LVM $VZCONTNAME on BASE/$VZLIB/vzdir/$CONTNAME" fi echo -e "/dev/$LVM/$VZCONTNAME\t$BASE/$VZLIB/vzdir/$CONTNAME\text4\tdefaults\t0 0" >> /etc/fstab fi mkdir -p $BASE/$VZLIB/vzdir/$CONTNAME/rootfs #installing template $GETAR | tar zxCf $BASE/$VZLIB/vzdir/$CONTNAME/rootfs - ret=$? if [ $ret -ne 0 ]; then abort "vzgot.create, Unable to install template file $TPL!" fi #---------------------------------------------------- #duplicating cgroup HOST setting (the limits you #want to be set to all created container) cp -ap $BASE/$VZLIB/cgroup.d $BASE/$VZLIB/vzdir/$CONTNAME #---------------------------------------------------- #setting the container architecture echo $ARCH > $BASE/$VZLIB/vzdir/$CONTNAME/arch #setting the container distribution echo $DIST > $BASE/$VZLIB/vzdir/$CONTNAME/dist #---------------------------------------------------- #cleaning mtab rm -fr $BASE/$VZLIB/vzdir/$CONTNAME/rootfs/etc/mtab* #---------------------------------------------------- #specific configuration according distribution case "$DIST" in "rh9" ) #setting variable LD_ASSUME_KERNEL to help RPM utility export ENVRPM="LD_ASSUME_KERNEL=2.4.1" echo "export $ENVRPM" >> $BASE/$VZLIB/vzdir/$CONTNAME/rootfs/etc/bashrc ;; "*" ) #nothing to do ;; esac #---------------------------------------------------- #installing needed RPM if [ -d $BASE/$VZLIB/RPM/RPMS/$ARCH/$DIST ] ; then ( #do we have specific rpm to add to template cd $BASE/$VZLIB/RPM/RPMS/$ARCH/$DIST num=`ls -1 *.rpm | wc -l` if [ $num -ge 1 ] ; then VZROOT=$BASE/$VZLIB/vzdir/$CONTNAME/rootfs CHROOT="/usr/bin/setarch $ARCH /usr/sbin/chroot" mkdir $VZROOT/tmp/rpm cp *.rpm $VZROOT/tmp/rpm $CHROOT $VZROOT mount -t proc proc /proc $CHROOT $VZROOT mount -t sysfs sysfs /sys $CHROOT $VZROOT /bin/bash -c "$ENVRPM rpm -U --quiet tmp/rpm/*.rpm" $CHROOT $VZROOT umount /sys $CHROOT $VZROOT umount /proc fi ) fi #---------------------------------------------------- #final exit exit $ret