slitaz-dev-tools view tazdev/tazdev @ rev 77

tazdev: fix gen-chroot if chrrot path is given in argument and add umount-chroot cmd
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 03 14:52:07 2011 +0200 (2011-05-03)
parents 9b6516f3edc0
children bfce85497514
line source
1 #!/bin/sh
2 #
3 # Tazdev - SliTaz developers and build host tool. System wide config
4 # file is located at: /etc/slitaz/tazdev.conf
5 #
6 # (c) 2011 SliTaz GNU/Linux - GNU gpl v3
7 #
8 # AUTHORS
9 # Christophe Lincoln <pankso@slitaz.org>
10 # Pascal Bellard <bellard@slitaz.org>
11 #
13 [ -f /etc/slitaz/tazdev.conf ] && . /etc/slitaz/tazdev.conf
14 [ -f $PWD/tazdev.conf ] && . $PWD/tazdev.conf
16 if [ -z "$SLITAZ" ]; then
17 echo -e "\nNo config file found\n" && exit 1
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|undigest|path]
24 \033[1mCommands: \033[0m\n
25 usage Print this short usage and command list
26 projects-stats|-ps Display statistics about your projects
27 update-wok Update Hg wok and copy it to the chroot wok
28 chroot Mount virtual fs if needed and chroot into the build env
29 gen-chroot|-gs Generate a chroot using the last cooking base rootfs
30 umount-chroot|-uc Unmount chroot specified on cmdline (--forced to force)
31 clean-chroot Clean a chroot environment (skip root/ and home/)
32 purge Remove obsolete packages and obsolete source tarballs
33 dry-purge Show obsolete packages and obsolete source tarballs
34 push|-p Upload new packages to the main mirror.
35 dry-push|-dp Show what will be uploaded to the mirror. Does nothing
36 pull Download new packages from the main mirror.
37 dry-pull Show what will be downloaded from the mirror. Does nothing
38 relpkg|-rp Archive and upload new package/project version
39 reliso|-ri Create all main flavors (not loram)\n"
40 }
42 # Exit if user is not root.
43 check_root()
44 {
45 if test $(id -u) != 0 ; then
46 echo -e "\nThis program requires being run as root.\n"
47 exit 0
48 fi
49 }
51 status()
52 {
53 local CHECK=$?
54 echo -en "\033[70G"
55 if [ $CHECK = 0 ]; then
56 echo "Done"
57 else
58 echo "Failed"
59 fi
60 return $CHECK
61 }
63 get_version()
64 {
65 if [ "$2" = "stable" ]; then
66 VERSION=stable
67 SLITAZ=$STABLE
68 elif [ -n "$2" ]; then
69 # Undigest - custom ?
70 VERSION=$2
71 SLITAZ=/home/slitaz/$2
72 else
73 VERSION=cooking
74 SLITAZ=$COOKING
75 fi
76 ROOTFS=$SLITAZ/chroot
77 HG_WOK=$SLITAZ/wok
78 BUILD_WOK=$SLITAZ/chroot/home/slitaz/wok
79 }
81 check_mirror()
82 {
83 # ping -c 1 $MIRROR
84 if [ -n "$2" ]; then
85 USER=$2
86 else
87 USER=$USER
88 fi
89 if [ "$2" = "stable" ] || [ "$3" = "stable" ]; then
90 REMOTE_DIR=$MIRROR_PKGS/stable/
91 LOCAL_DIR=$STABLE/packages/
92 elif [ "$2" = "undigest" ] || [ "$3" = "undigest" ]; then
93 REMOTE_DIR=$MIRROR_PKGS/undigest/
94 LOCAL_DIR=$UNDIGEST/packages/
95 else
96 REMOTE_DIR=$MIRROR_PKGS/cooking/
97 LOCAL_DIR=$COOKING/packages/
98 fi
99 }
101 # Mount virtual Kernel file systems and chroot but check that nobody
102 # else has done mounts
103 mount_chroot()
104 {
105 if [ ! -d $ROOTFS/proc/1 ]; then
106 echo -n "Mounting virtual filesystems..."
107 mount -t proc proc $ROOTFS/proc
108 mount -t sysfs sysfs $ROOTFS/sys
109 mount -t devpts devpts $ROOTFS/dev/pts
110 mount -t tmpfs shm $ROOTFS/dev/shm
111 status
112 fi
113 }
115 # Unmount virtual Kernel file systems on exit and ensure we are the last
116 # user before unmounting !
117 umount_chroot()
118 {
119 # Not working. Buggy ps ?
120 #sleep 6
121 ps=$(ps | grep `basename $0` | grep -v grep | wc -l)
122 if [ "$ps" == "1" ] || [ "$2" == "--forced" ]; then
123 [ "$1" ] && ROOTF=$1
124 echo -e "\nUnmounting virtual filesystems...\n"
125 umount $ROOTFS/dev/shm
126 umount $ROOTFS/dev/pts
127 umount $ROOTFS/sys
128 umount $ROOTFS/proc
129 echo ""
130 else
131 echo -e "\nProcess: $ps\n"
132 ps | grep `basename $0` | grep -v grep
133 echo -e "\nLeaving virtual filesystems unmounted (`pidof tazdev`)...\n"
134 fi
135 }
137 # Get the last cooking base rootfs, extract and configure.
138 gen_new_chroot()
139 {
140 echo -e "\nGenerating new chroot in : $ROOTFS"
141 echo "================================================================================"
142 mkdir -p $ROOTFS && cd $ROOTFS
143 wget $DL_URL/boot/cooking/rootfs-base.gz
144 echo -n "Extracting the rootfs..."
145 lzma d rootfs-base.gz -so | cpio -id
146 rm rootfs-base.gz
147 echo -n "Creating resolv.conf..."
148 cat /etc/resolv.conf > etc/resolv.conf
149 status
150 echo "================================================================================"
151 echo -e "Ready to chroot. Use 'tazdev chroot [version|path]'"
152 echo -e "Example: tazdev chroot $ROOTFS\n"
153 }
155 # Remove obsolate slitaz packages
156 purge_packages()
157 {
158 arg=$1
159 TMP_FILE=/tmp/tazdev.$$
160 ls $BUILD_WOK | while read pkg; do
161 [ -f $BUILD_WOK/$pkg/taz/*/receipt ] || continue
162 EXTRAVERSION=""
163 . $BUILD_WOK/$pkg/taz/*/receipt
164 echo $PACKAGE-$VERSION$EXTRAVERSION.tazpkg
165 done > $TMP_FILE
166 ls $SLITAZ/chroot/home/slitaz/packages | while read pkg; do
167 case "$pkg" in
168 *.tazpkg)
169 grep -q ^$pkg$ $TMP_FILE && continue
170 echo Remove $pkg
171 [ "$arg" == "purge" ] &&
172 rm -f $SLITAZ/chroot/home/slitaz/packages/$pkg ;;
173 esac
174 done
175 rm -f $TMP_FILE
176 }
178 # Remove obsolate source tarballs
179 purge_sources()
180 {
181 arg=$1
182 TMP_FILE=/tmp/tazdev.$$
183 ls $BUILD_WOK | while read pkg; do
184 [ -f $BUILD_WOK/$pkg/receipt ] || continue
185 TARBALL=""
186 . $BUILD_WOK/$pkg/receipt
187 [ -n "$TARBALL" ] && echo $TARBALL
188 grep SOURCES_REPOSITORY/ $BUILD_WOK/$pkg/receipt | sed \
189 -e 's|.*SOURCES_REPOSITORY/\([^ ]*\)\( .*\)$|\1|' \
190 -e 's|.*SOURCES_REPOSITORY/\([^ ]*\)$|\1|' | sort | uniq | \
191 sed "s|['\"/]||g" | while read file ; do
192 eval echo $file 2> /dev/null
193 done
194 done > $TMP_FILE
195 ls $SLITAZ/chroot/home/slitaz/src | while read pkg; do
196 grep -q ^$pkg$ $TMP_FILE && continue
197 echo Remove $pkg
198 [ "$arg" == "purge" ] &&
199 rm -f $SLITAZ/chroot/home/slitaz/src/$pkg
200 done
201 rm -f $TMP_FILE
202 }
204 case "$1" in
205 projects-stats|-ps)
206 echo -e "\nStatistics for: $PROJECTS\n"
207 echo -n "Project" && echo -ne "\033[24G Size" && echo -ne "\033[38G Revision"
208 echo -ne "\033[48G Version" && echo -e "\033[64G Files"
209 echo "================================================================================"
210 cd $PROJECTS
211 for proj in *
212 do
213 rev=""
214 echo -n "$proj"
215 size=`du -sh $proj | awk '{ print $1 }'`
216 echo -ne "\033[24G $size"
217 if [ -d $proj/.hg ]; then
218 cd $proj
219 rev=`hg head --template '{rev}\n'`
220 vers=`hg tags | head -n 2 | tail -n 1 | cut -d " " -f 1`
221 echo -ne "\033[38G $rev"
222 echo -ne "\033[48G $vers" && cd ..
223 fi
224 files=`find $proj -type f | wc -l`
225 echo -e "\033[64G $files"
226 done
227 echo "================================================================================"
228 echo "" ;;
229 update-wok)
230 # Update the Hg wok and copy it to the chroot env. Hg wok is
231 # copied to the chroot wok to avoid messing with build result
232 # file and so we can also modify receipt directly without affecting
233 # the Hg wok.
234 check_root
235 get_version $@
236 echo ""
237 echo "Hg wok : $HG_WOK"
238 echo "Build wok : $BUILD_WOK"
239 cd $HG_WOK
240 hg pull && hg update
241 echo -n "Copying Hg wok to the build wok... "
242 cp -a $HG_WOK/* $BUILD_WOK
243 cp -a $HG_WOK/.hg $BUILD_WOK
244 status && echo "" ;;
245 chroot)
246 # Chroot into a build env. Default to cooking configured in
247 # tazdev.conf
248 check_root
249 get_version $@
250 mount_chroot
251 echo -e "\nChrooting in $ROOTFS...\n"
252 chroot $ROOTFS /bin/sh --login
253 umount_chroot
254 echo -e "Exiting $ROOTFS chroot environment...\n" ;;
255 umount-chroot|-uc)
256 check_root
257 [ "$2" ] && ROOTFS=$2
258 [ "$3" ] && opt=$3
259 umount_chroot $ROOTFS $opt ;;
260 gen-chroot|-gc)
261 check_root
262 [ "$2" ] && ROOTFS=$2
263 [ "$2" ] || ROOTFS=$SLITAZ/cooking/chroot
264 # Dont break another env.
265 if [ -d $ROOTFS/bin ]; then
266 echo -e "\nA chroot environment already exists in : $ROOTFS\n"
267 exit 1
268 fi
269 gen_new_chroot ;;
270 clean-chroot)
271 # Keep root/ and /home they may have a build wok, custom scripts, etc.
272 check_root
273 if [ -z "$2" ]; then
274 echo -e "\nPlease specify the path to the chroot environment to clean.\n"
275 exit 0
276 else
277 ROOTFS=$2
278 if [ ! -d "$ROOTFS" ]; then
279 echo -e "\nWarning : $ROOTFS doesn't exist!\n"
280 exit 1
281 fi
282 fi
283 if [ -d $ROOTFS/proc/1 ]; then
284 echo -e "\nWarning : $ROOTFS/proc mounted!\n"
285 exit 1
286 fi
287 cd $ROOTFS || exit 1
288 echo -e "\nCleaning chroot in: $ROOTFS"
289 echo "================================================================================"
290 for i in bin dev etc init lib media mnt proc sbin sys tmp usr var
291 do
292 echo -n "Removing: $i (`du -sh $i | awk '{ print $1 }'`)... "
293 rm -rf $i
294 status
295 done
296 echo "================================================================================"
297 echo "" ;;
298 push|-p)
299 check_mirror $@
300 rsync -r -t -l -v -z --delete \
301 $LOCAL_DIR -e ssh $USER@$MIRROR:$REMOTE_DIR ;;
302 dry-push|-dp)
303 check_mirror $@
304 rsync -r -t -l -v -z --delete --dry-run \
305 $LOCAL_DIR -e ssh $USER@$MIRROR:$REMOTE_DIR ;;
306 pull)
307 check_mirror $@
308 rsync -r -t -l -v -z --delete \
309 -e ssh $USER@$MIRROR:$REMOTE_DIR $LOCAL_DIR ;;
310 dry-pull)
311 check_mirror $@
312 rsync -r -t -l -v -z --delete --dry-run \
313 -e ssh $USER@$MIRROR:$REMOTE_DIR $LOCAL_DIR ;;
314 purge|dry-purge)
315 check_root
316 get_version $@
317 purge_packages $1
318 purge_sources $1 ;;
319 relpkg|-rp)
320 [ -z "$MIRROR_SOURCES" ] && MIRROR_SOURCES="/var/www/slitaz/mirror/sources"
321 if [ -z $2 ] || [ -z $3 ]; then
322 echo -e "\nUsage: $0 relpkg package version\n"
323 exit 0
324 fi
325 PACKAGE=$2
326 VERSION=$3
327 echo ""
328 cd $PROJECTS/$PACKAGE
329 # Sanity check
330 if ! grep -q $VERSION$ .hgtags; then
331 echo "Missing Hg tag for version: $VERSION"
332 echo -e "You may want to: hg tag $VERSION && hg push\n"
333 exit 0
334 fi
335 # Archive
336 echo -n "Creating tarball and md5sum for: $PACKAGE-$VERSION... "
337 hg archive -t tgz $PACKAGE-$VERSION.tar.gz
338 md5sum $PACKAGE-$VERSION.tar.gz > $PACKAGE-$VERSION.md5
339 echo "Done"
340 # Upload
341 echo -n "Do you wish to upload tarball to the mirror [N/y] ? "
342 read upload
343 if [ "$upload" = "y" ]; then
344 echo "Uploading to: $MIRROR/sources/${PACKAGE#slitaz-}"
345 scp $PACKAGE-$VERSION.tar.gz $PACKAGE-$VERSION.md5 \
346 $USER@$MIRROR:$MIRROR_SOURCES/${PACKAGE#slitaz-}
347 fi ;;
348 reliso|-ri)
349 # Generate all official images iso at once.
350 version=$2
351 mkdir -p $ISO
352 for flavor in base core justx firefox
353 do
354 cd /home/slitaz/$version
355 tazlito pack-flavor $flavor
356 tazlito get-flavor $flavor
357 tazlito gen-distro
358 if [ "$flavor" == "core" ]; then
359 cp distro/slitaz-$flavor.iso $ISO/slitaz-$version.iso
360 cd $ISO && md5sum slitaz-$version.iso > slitaz-$version.md5
361 else
362 cp distro/slitaz-$flavor.iso $ISO/slitaz-$version-$flavor.iso
363 cd $ISO && md5sum slitaz-$version-$flavor.iso slitaz-$version-$flavor.md5
364 fi
365 done ;;
366 usage|*)
367 usage ;;
368 esac
370 exit 0