Release.
[safe/jmp/vzgot] / shell / vzgot.fboot.debian
1 #! /bin/sh
2 #----------------------------------------------------
3 #shell used to prepare container structure, this script
4 #is called by the vzgot before any root pivot action.
5 #It is used on the firstboot to prepare all needed file.
6 #
7 #----------------------------------------------------
8 #Syntaxe
9 #vzgot.fistboot VE_name node domain ip_list [Base]
10 #       VE_name is the container dir name
11 #       Node is the container hostname
12 #       Domain is the container domainname 
13 #       IP_LIST is an ip list (ip,ip1,ip2,..) used
14 #               to prepare container network
15 #       base_dir is this application root directory
16 #               (used for devel only).
17 #----------------------------------------------------
18 CONTNAME=$1
19 NODE=$2
20 DOMAIN=$3
21 LSTIP=$4
22 BASE=$5
23 #----------------------------------------------------
24 if ! [ -f $BASE/etc/vzgot/vzgot_config ] ; then
25   echo "Missing vzgot config file"
26   exit 1        #trouble trouble
27   fi
28 . $BASE/etc/vzgot/vzgot_config
29
30 #specific container configuration override (if needed)
31 if [ -f $BASE/$VZLIB/vzdir/$CONTNAME/vzgot_config ] ; then
32   $BASE/$VZLIB/vzdir/$CONTNAME/vzgot_config
33   fi
34 #----------------------------------------------------
35 VZROOT=$BASE/$VZLIB/vzdir/$CONTNAME/rootfs
36 #====================================================
37 #procedure to implement redhat distribution file
38 #----------------------------------------------------
39 #Prepare /etc/hosts file
40 #----------------------------------------------------
41
42 HOSTS="$BASE/$VZLIB/vzdir/$CONTNAME/rootfs/etc/hosts"
43 (
44 echo "#----------------------------------------------------"
45 echo "#done by vzgot.fboot"
46 echo "#----------------------------------------------------"
47 echo -e "127.0.0.1\tlocalhost.localdomain\tlocalhost"
48 ) > $HOSTS
49
50 if [ -n $LSTIP ] ; then
51   (
52   echo -n `echo $LSTIP | tr ',' ' ' | cut -d' ' -f1`
53   echo -e "\t$NODE.$DOMAIN\t\t$NODE"
54   echo "#----------------------------------------------------"
55   ) >> $HOSTS
56   fi
57
58 #----------------------------------------------------
59 #Prepare /etc/hostname
60 #----------------------------------------------------
61 HST="$BASE/$VZLIB/vzdir/$CONTNAME/rootfs/etc/hostname"
62 (
63 echo "$NODE.$DOMAIN"
64 ) > $HST
65
66 #----------------------------------------------------
67 #Prepare /etc/network/interface
68 #----------------------------------------------------
69 #need directory to have /etc/init.d/networking to work
70 mkdir -p $BASE/$VZLIB/vzdir/$CONTNAME/rootfs/var/run/network
71
72 #defining interface
73 INT="$BASE/$VZLIB/vzdir/$CONTNAME/rootfs/etc/network/interfaces"
74 (
75 echo "# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or"
76 echo "# /usr/share/doc/ifupdown/examples for more information."
77 echo
78 echo "auto lo"
79 echo "iface lo inet loopback"
80 ) > $INT
81 if [ -n $LSTIP ] ; then
82   ETH0=`echo $LSTIP | tr ',' ' ' | cut -d' ' -f1`
83   (
84   echo
85   echo "auto eth0"
86   echo "iface eth0 inet static"
87   echo "address $ETH0"
88   echo "netmask 255.255.255.255"
89   echo "up route add default dev eth0"
90   ) >> $INT
91   ETHX=`echo $LSTIP | tr ',' ' ' | cut -s -d' ' -f2-`
92   aliasnum=1
93   for eth in $ETHX 
94     do
95     (
96     echo "auto eth0:$aliasnum"
97     echo "address $eth"
98     echo "netmask 255.255.255.255"
99     ) >> $INT
100     let aliasnum=aliasnum+1
101     done
102   fi
103 #----------------------------------------------------
104 #Prepare /dev directoy contents
105 #----------------------------------------------------
106 DEV="$VZROOT/dev"
107
108 rm -fr $DEV
109 mkdir -p $DEV
110 mknod -m 666 $DEV/null c 1 3
111 mknod -m 666 $DEV/zero c 1 5
112 mknod -m 666 $DEV/random c 1 8
113 mknod -m 666 $DEV/urandom c 1 9
114 mkdir -m 755 $DEV/pts
115 mkdir -m 1777 $DEV/shm
116 mknod -m 666 $DEV/tty c 5 0
117 mknod -m 600 $DEV/console p
118 mknod -m 666 $DEV/tty0 c 4 0
119 mknod -m 666 $DEV/full c 1 7
120 mknod -m 600 $DEV/initctl p
121 mknod -m 666 $DEV/ptmx c 5 2
122
123 #----------------------------------------------------
124 #Prepare /dev directoy fstab
125 #----------------------------------------------------
126 FSTAB="$VZROOT/etc/fstab"
127 (
128 echo -e "/vzgot\t\t/\t\text3\tdefaults\t0 0"
129 echo -e "proc\t\t/proc\t\tproc\tdefaults\t0 0"
130 echo -e "sysfs\t\t/sys\t\tsysfs\tdefaults\t0 0"
131 echo -e "devpts\t\t/dev/pts\t\tdevpts\tdefaults\t0 0"
132 echo -e "tmpfs\t\t/dev/shm\t\ttmpfs\tdefaults\t0 0"
133 ) > $FSTAB
134
135 #====================================================
136 #starting common container configuration process
137 (
138
139 cd $BASE/$VZLIB
140 #----------------------------------------------------
141 #insert a common root password within the vz
142
143 if [ -f etc/shadow ] ; then
144   SHAD="$VZROOT/etc/shadow"
145   sed 's/#.*//g' etc/shadow | grep -v '^[[:space:]]*$' |        \
146   while read -r LINE
147     do
148     NAME=`echo $LINE | cut -d':' -f1`
149     (echo $LINE ; grep -v "^$NAME:" $SHAD) > $SHAD.new
150     mv -f $SHAD.new $SHAD
151     done
152   chmod 440 $SHAD
153   fi
154
155 #setting the DNS resolver
156 if [ -f etc/resolv.conf ] ; then
157   cp -a etc/resolv.conf $VZROOT/etc
158   fi
159
160 #duplicating ssh Key if we want to have all container even.
161 if [ -d etc/ssh ] ; then
162   #removing the hack to make original KEY
163   rm -fr $VZROOT/etc/rc2.d/S11sshhack
164   rm -fr $VZROOT/etc/init.d/ssh_key_hack.sh
165   for k in `ls etc/ssh/*key* 2> /dev/null` 
166     do
167     cp -a $k $VZROOT/etc/ssh
168     done
169   fi
170
171 #insert iptables script
172 if [ -f etc/sysconfig/iptables ] ; then
173   cp -a etc/sysconfig/iptables $VZROOT/etc/sysconfig
174   fi
175
176 #adding sudoer informations
177 if [ -f etc/sudoers ] ; then
178   cat etc/sudoers >> $VZROOT/etc/sudoers
179   fi
180
181 #adding snmpd.conf specific to ALL container
182 if [ -d $VZROOT/etc/snmp/ ] && [ -f etc/snmp/snmpd.conf ] ; then
183   cat etc/snmp/snmpd.conf>> $VZROOT/etc/snmp/snmpd.conf
184   fi
185
186 )
187 #end common container configuration  process
188 #====================================================
189 #lets create common users and groups
190 ARCH=`cat $VZLIB/vzdir/$CONTNAME/arch`
191 CHROOT="/usr/bin/setarch $ARCH /usr/sbin/chroot"
192
193 $CHROOT $VZROOT mount -t proc proc /proc
194 $CHROOT $VZROOT mount -t sysfs sysfs /sys
195
196 #creating common group
197 if [ -f $VZLIB/etc/groups ] ; then
198   sed 's/#.*//g' $VZLIB/etc/groups | grep -v '^[[:space:]]*$' | \
199   while read -r GRPNAME
200     do
201     $CHROOT $VZROOT groupadd $GRPNAME
202     done
203   fi
204
205 #creating common users
206 if [ -f $VZLIB/etc/users ] ; then
207   sed 's/#.*//g' $VZLIB/etc/users | grep -v '^[[:space:]]*$' |  \
208   while read -r NAME CRYPTED GRPNAME
209     do
210     if [ -d $VZLIB/home/$NAME ] ; then
211       cp -a $VZLIB/home/$NAME $VZROOT/tmp
212       SKEL="-k /tmp/$NAME"
213       fi
214     $CHROOT $VZROOT useradd -m $SKEL -p "$CRYPTED" -g $GRPNAME $NAME
215     rm -fr $VZROOT/tmp/$NAME
216     done
217   fi
218
219 #removing udev family auto start
220 $CHROOT $VZROOT update-rc.d -f udev remove > /dev/null 2>&1
221 $CHROOT $VZROOT update-rc.d -f udev-finish remove > /dev/null 2>&1
222
223 $CHROOT $VZROOT umount /sys
224 $CHROOT $VZROOT umount /proc
225 #====================================================
226 exit 0