Inserting VZGOT development tree within GIT
[safe/jmp/vzgot] / shell / vzgot.create
1 #! /bin/sh
2 #----------------------------------------------------
3 #shell used to create the root file system for the
4 #container, incoming file is a tar file to
5 #insert within the vzgot directory structure
6 #
7 #----------------------------------------------------
8 #Syntaxe
9 #vzgot.create VE_name tpl_tar_filename dist arch base_dir
10 #       VE_name is the container dir name
11 #       tpl_tar_filename is the template file name to be used
12 #       dist is the distribution name (fc12, rh7.3, el5.4, etc...)
13 #       arch is the architecture base (i386, x86_64)
14 #       base_dir is this application root directory
15 #       used for devel only.
16 #----------------------------------------------------
17 CONTNAME=$1
18 TARFILE=$2
19 DIST=$3
20 ARCH=$4
21 BASE=$5
22
23 #-------------------------------------------------------------------
24 #abort procedure
25 abort ()
26
27 {
28 echo "$1"
29 exit 1;
30 }
31 #----------------------------------------------------
32 #default GETAR value, during the container create 
33 #CAN BE overriden within /etc/vzgot/vzgot_config
34 #if your template are somewhere else
35 GETAR="cat /var/lib/vzgot/vztemplate/$TARFILE"
36 #----------------------------------------------------
37 if ! [ -f $BASE/etc/vzgot/vzgot_config ] ; then
38   abort "Missing vzgot config file"
39   fi
40 . $BASE/etc/vzgot/vzgot_config
41
42 #specific container configuration override (if needed)
43 if [ -f $BASE/$VZLIB/vzdir/$CONTNAME/vzgot_config ] ; then
44   $BASE/$VZLIB/vzdir/$CONTNAME/vzgot_config
45   fi
46 #----------------------------------------------------
47 if [ -d $BASE/$VZLIB/vzdir/$CONTNAME ] ; then
48   echo "Container $CONTNAME already existing, must be destroyed first"
49   abort "Use command \"vzgot destroy $CONTNAME\" to do it"
50   fi
51
52 #----------------------------------------------------
53 #creating Container directory
54 mkdir -p $BASE/$VZLIB/vzdir/$CONTNAME
55 if [ ! -z "$LVM" ] ; then
56   if [ -z "$LVM_SIZE" ] ; then
57     LVM_SIZE="2G"
58     fi
59   VZCONTNAME=`echo $CONTNAME | sed 's/\.//g' | sed 's/-//g'`
60   lvcreate -L$LVM_SIZE -n$VZCONTNAME $LVM > /dev/null 2>&1
61   ret=$?
62   if [ $ret -ne 0 ]; then
63     abort "vzgot.create, unable to create LVM $VZCONTNAME on volume $LVM"
64     fi
65   mke2fs -q -t ext4 -j -L$VZCONTNAME /dev/$LVM/$VZCONTNAME
66   ret=$?
67   if [ $ret -ne 0 ]; then
68     abort "vzgot.create, unable to format LVM $VZCONTNAME with mke2fs"
69     fi
70   mount /dev/$LVM/$VZCONTNAME $BASE/$VZLIB/vzdir/$CONTNAME
71   ret=$?
72   if [ $ret -ne 0 ]; then
73     abort "vzgot.create, unable to mount LVM $VZCONTNAME on BASE/$VZLIB/vzdir/$CONTNAME"
74     fi
75   echo -e "/dev/$LVM/$VZCONTNAME\t$BASE/$VZLIB/vzdir/$CONTNAME\text4\tdefaults\t0 0" >> /etc/fstab
76   fi
77 mkdir -p $BASE/$VZLIB/vzdir/$CONTNAME/rootfs
78
79 #installing template
80 $GETAR | tar zxCf $BASE/$VZLIB/vzdir/$CONTNAME/rootfs -
81 ret=$?
82 if [ $ret -ne 0 ]; then
83   abort "vzgot.create, Unable to install template file $TPL!"
84   fi
85 #----------------------------------------------------
86 #duplicating cgroup HOST setting (the limits you
87 #want to be set to all created container)
88 cp -ap $BASE/$VZLIB/cgroup.d $BASE/$VZLIB/vzdir/$CONTNAME
89 #----------------------------------------------------
90 #setting the container architecture 
91 echo $ARCH > $BASE/$VZLIB/vzdir/$CONTNAME/arch
92 #setting the container distribution 
93 echo $DIST > $BASE/$VZLIB/vzdir/$CONTNAME/dist
94 #----------------------------------------------------
95 #cleaning mtab
96 rm -fr $BASE/$VZLIB/vzdir/$CONTNAME/rootfs/etc/mtab*
97 #----------------------------------------------------
98 #specific configuration according distribution
99 case "$DIST" in 
100   "rh9" )       
101      #setting variable  LD_ASSUME_KERNEL to help RPM utility
102      export ENVRPM="LD_ASSUME_KERNEL=2.4.1"
103      echo "export $ENVRPM" >> $BASE/$VZLIB/vzdir/$CONTNAME/rootfs/etc/bashrc
104      ;;
105   "*"   )       #nothing to do
106      ;;
107   esac
108 #----------------------------------------------------
109 #installing needed RPM
110 if [ -d $BASE/$VZLIB/RPM/RPMS/$ARCH/$DIST ] ; then
111   (
112   #do we have specific rpm to add to template
113   cd $BASE/$VZLIB/RPM/RPMS/$ARCH/$DIST
114   num=`ls -1 *.rpm | wc -l`
115   if [ $num -ge 1 ] ; then
116
117     VZROOT=$BASE/$VZLIB/vzdir/$CONTNAME/rootfs
118     CHROOT="/usr/bin/setarch $ARCH /usr/sbin/chroot"
119
120     mkdir $VZROOT/tmp/rpm
121     cp *.rpm $VZROOT/tmp/rpm
122     $CHROOT $VZROOT mount -t proc proc /proc
123     $CHROOT $VZROOT mount -t sysfs sysfs /sys
124     $CHROOT $VZROOT /bin/bash -c "$ENVRPM rpm -U --quiet tmp/rpm/*.rpm"
125     $CHROOT $VZROOT umount /sys
126     $CHROOT $VZROOT umount /proc
127     fi
128   )
129   fi
130 #----------------------------------------------------
131 #final exit
132 exit $ret