wok-current view slitaz-dev-tools/stuff/tazdev @ rev 3499

tazdev: Add command gen-chroot and clean-chroot + tiny fix
author Christophe Lincoln <pankso@slitaz.org>
date Fri Jun 19 02:43:44 2009 +0200 (2009-06-19)
parents 65ca24a53b62
children e4b53fbe493e
line source
1 #!/bin/sh
2 # Tazdev - SliTaz developers and build host tool.
3 # System wide config file: /etc/slitaz/tazdev.conf
4 #
5 # (c) 2009 SliTaz GNU/Linux - GNU gpl v3
6 #
7 # Authors : Christophe Lincoln (Pankso) <pankso@slitaz.org>
8 #
10 if [ -f /etc/slitaz/tazdev.conf ]; then
11 . /etc/slitaz/tazdev.conf
12 if [ -f $PWD/tazdev.conf ]; then
13 . $PWD/tazdev.conf
14 fi
15 else
16 echo -e "\nNo config file found in /etc/slitaz or the current dir...\n"
17 exit 0
18 fi
20 usage()
21 {
22 echo -e "\nSliTaz developers and build host tool\n
23 \033[1mUsage: \033[0m `basename $0` [command] [user] [stable|cooking|path]
24 \033[1mCommands: \033[0m\n
25 usage Print this short usage and command list.
26 cmplog Log 'tazwok cmp' result (or use tazbb).
27 update-wok Update Hg wok and copy it to the chroot wok.
28 update-www Update SliTaz Website repo from Hg.
29 chroot Mount virtual fs if needed and chroot into the build env.
30 gen-chroot Generate a chroot environment using the last cooking base rootf.
31 clean-chroot Clean a chroot environment (skip root/ and home/)
32 push Upload new packages to the main mirror.
33 dry-push Show what will be uploaded to the mirror. Does nothing.
34 pull Download new packages from the main mirror.
35 dry-pull Show what will be downloaded from the mirror. Does nothing.\n"
36 }
38 # Exit if user is not root.
39 check_root()
40 {
41 if test $(id -u) != 0 ; then
42 echo -e "\nThis program requires being run as root.\n"
43 exit 0
44 fi
45 }
47 status()
48 {
49 local CHECK=$?
50 if [ $CHECK = 0 ]; then
51 echo " Done"
52 else
53 echo " Failed"
54 fi
55 return $CHECK
56 }
58 get_version()
59 {
60 if [ "$2" = "stable" ]; then
61 VERSION=stable
62 ROOTFS=$STABLE/chroot
63 elif [ -n "$2" ]; then
64 ROOTFS=$2
65 else
66 VERSION=cooking
67 ROOTFS=$COOKING/chroot
68 fi
69 }
71 check_mirror()
72 {
73 # ping -c 1 $MIRROR
74 if [ -n "$2" ]; then
75 USER=$2
76 else
77 echo -e "\nPlease specify a user.\n" && exit 0
78 fi
79 if [ "$3" = "stable" ]; then
80 REMOTE_DIR=$MIRROR_DIR/stable/
81 LOCAL_DIR=$STABLE/packages/
82 else
83 REMOTE_DIR=$MIRROR_DIR/cooking/
84 LOCAL_DIR=$COOKING/packages/
85 fi
86 }
88 # Mount virtual Kernel file systems and chroot but check that nobody
89 # else has done mounts
90 mount_chroot()
91 {
92 if [ ! -d $ROOTFS/proc/1 ]; then
93 echo -n "Mounting virtual filesystem..."
94 mount -t proc proc $ROOTFS/proc
95 mount -t sysfs sysfs $ROOTFS/sys
96 mount -t devpts devpts $ROOTFS/dev/pts
97 mount -t tmpfs shm $ROOTFS/dev/shm
98 status
99 fi
100 }
102 # Unmount virtual Kernel file systems on exit and ensure we are the last
103 # user before unmounting !
104 umount_chroot()
105 {
106 # Not working. Buggy ps ?
107 #sleep 6
108 ps=$(ps | grep `basename $0` | grep -v grep | wc -l)
109 if [ "$ps" == "1" ]; then
110 echo -ne "\Unmounting virtual filesystem..."
111 umount $ROOTFS/dev/shm
112 umount $ROOTFS/dev/pts
113 umount $ROOTFS/sys
114 umount $ROOTFS/proc
115 status
116 else
117 echo -e "\nProcess: $ps\n"
118 ps | grep `basename $0` | grep -v grep
119 echo -e "\nLeaving virtual filesystem unmounted (`pidof tazdev`)...\n"
120 fi
121 }
123 # Get the last cooking base rootfs, extract and configure.
124 gen_new_chroot()
125 {
126 echo -e "\nGenerating new chroot in : $ROOTFS"
127 echo "================================================================================"
128 mkdir -p $ROOTFS && cd $ROOTFS
129 wget $DL_URL/boot/cooking/rootfs-base.gz
130 echo -n "Extracting the rootfs..."
131 lzma d rootfs-base.gz -so | cpio -id
132 rm rootfs-base.gz
133 echo -n "Creating resolv.conf..."
134 cat /etc/resolv.conf > etc/resolv.conf
135 status
136 echo "================================================================================"
137 echo -e "Ready to chroot. Use 'tazdev chroot [version|path]'"
138 echo -e "Example: tazdev chroot $ROOTFS\n"
139 }
141 case "$1" in
142 cmplog)
143 # Log 'tazwok cmp' for the web interface (can be used via a cron job).
144 check_root
145 echo -e "Starting 'tazwok cmp' (can be long)...\n"
146 tazwok cmp | grep ^[A-Z] | tee $CMP_LOG
147 echo "Date: `date`" >> $CMP_LOG ;;
148 update-wok)
149 # Update the Hg wok and copy it to the chroot env. Hg wok is
150 # copied to the chroot wok to avoid messing with build result
151 # file and so we can also modify receipt directly without affecting
152 # the Hg wok.
153 check_root
154 if [ "$2" = "stable" ]; then
155 HG_WOK=$STABLE/wok
156 BUILD_WOK=$STABLE/chroot/home/slitaz
157 else
158 HG_WOK=$COOKING/wok
159 BUILD_WOK=$COOKING/chroot/home/slitaz
160 fi
161 echo ""
162 echo "Hg wok : $HG_WOK"
163 echo "Build wok : $BUILD_WOK"
164 cd $HG_WOK
165 hg pull && hg update
166 echo -n "Copying Hg wok to the build wok... "
167 cp -a $HG_WOK/* $BUILD_WOK
168 cp -a $HG_WOK/.hg $BUILD_WOK
169 status && echo "" ;;
170 update-www)
171 # Update website from repo.
172 echo ""
173 cd $WEBSITE && hg pull && hg update
174 echo "" ;;
175 chroot)
176 # Chroot into a build env. Default to cooking configured in
177 # tazdev.conf
178 check_root
179 get_version
180 mount_chroot
181 echo -e "\nChrooting in $ROOTFS...\n"
182 chroot $ROOTFS /bin/sh --login
183 umount_chroot
184 echo -e "Exiting $ROOTFS chroot environment...\n" ;;
185 gen-chroot)
186 check_root
187 get_version
188 # Dont break another env.
189 if [ -d $ROOTFS/bin ]; then
190 echo -e "\nA chroot environment already exist in : $ROOTFS\n"
191 exit 1
192 fi
193 gen_new_chroot ;;
194 clean-chroot)
195 # Keep root/ and /home they may have a build wok, custom scripts, etc.
196 check_root
197 if [ -z "$2" ]; then
198 echo -e "\nPlease specify the path to the chroot environment to clean.\n"
199 exit 0
200 else
201 ROOTFS=$2
202 if [ ! -d "$ROOTFS" ]; then
203 echo -e "\nWarning : $ROOTFS dont exit!\n"
204 exit 1
205 fi
206 fi
207 if [ -d $ROOTFS/proc/1 ]; then
208 echo -e "\nWarning : $ROOTFS/proc mounted!\n"
209 exit 1
210 fi
211 cd $ROOTFS || exit 1
212 echo -e "\nCleaning chroot in: $ROOTFS"
213 echo "================================================================================"
214 for i in bin dev etc init lib media mnt proc sbin sys tmp usr var
215 do
216 echo -n "Removing: $i (`du -sh $i | awk '{ print $1 }'`)... "
217 rm -rf $i
218 status
219 done
220 echo "================================================================================"
221 echo "" ;;
222 push)
223 check_mirror
224 rsync -r -t -l -v -z --delete \
225 $LOCAL_DIR -e ssh $USER@$MIRROR:$REMOTE_DIR ;;
226 dry-push)
227 check_mirror
228 rsync -r -t -l -v -z --delete --dry-run \
229 $LOCAL_DIR -e ssh $USER@$MIRROR:$REMOTE_DIR ;;
230 pull)
231 check_mirror
232 rsync -r -t -l -v -z --delete \
233 -e ssh $USER@$MIRROR:$REMOTE_DIR $LOCAL_DIR ;;
234 dry-pull)
235 check_mirror
236 rsync -r -t -l -v -z --delete --dry-run \
237 -e ssh $USER@$MIRROR:$REMOTE_DIR $LOCAL_DIR ;;
238 usage|*)
239 usage ;;
240 esac
242 exit 0