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

tazdev/tazdev: mount /dev to avoid errors with /dev/null, etc.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Sep 18 20:22:49 2017 +0300 (2017-09-18)
parents 0b423b5392bc
children
line source
1 #!/bin/sh
2 #
3 # Tazdev - SliTaz developers and build host tool. System-wide config file
4 # is located at /etc/slitaz/tazdev.conf. Please, keep the code clear and
5 # well commented. Also keep in mind: configuration variables are $UPPERCASE
6 # and variables initialized by tazdev itself are $lowercase.
7 #
8 # (c) 2014-2017 SliTaz GNU/Linux - GNU GPL v3
9 #
10 # AUTHORS
11 # Christophe Lincoln <pankso@slitaz.org>
12 # Pascal Bellard <bellard@slitaz.org>
13 # Eric Joseph-Alexandre <erjo@slitaz.org>
14 #
16 VERSION=2.1
18 . /lib/libtaz.sh
20 [ -f /etc/slitaz/tazdev.conf ] && . /etc/slitaz/tazdev.conf
21 [ -f ~/.config/slitaz/tazdev.conf ] && . ~/.config/slitaz/tazdev.conf
22 [ -f tazdev.conf ] && . ./tazdev.conf
24 [ -n "$SLITAZ_HOME" ] || die "No config file found"
27 # Remove all --parameters from cmdline
28 IFS=$'\n'
29 set -- $(echo "$*" | sed '/^--/d')
30 unset IFS
33 usage() {
34 cat <<EOT
36 $(boldify "Usage:") $(basename $0) <command> [<options>] [--options]
38 SliTaz developers and build host tool v$VERSION
40 $(boldify "Commands:")
41 usage Print this short usage and command list
42 help <tool> Give help on a SliTaz tool or library
43 -s stats Display statistics for your projects
44 -c chroot [<ver>|<rootfs>] Mount virtual FS and chroot into the build env
45 -gc gen-chroot [<ver>] Generate a chroot using packages from config
46 file
47 -uc umchroot [<rootfs>] Unmount chroot specified on cmdline
48 -cc clean-chroot [<ver>] Clean a chroot environment (skip root/ and
49 home/)
50 -p push [<user> [<ver>]] Upload new packages to the main mirror
51 -dp dry-push [<user> [<ver>]] Show what will be uploaded to the mirror
52 pull [<user> [<ver>]] Download new packages from the main mirror
53 dry-pull [<user> [<ver>]] Show what will be downloaded from the mirror
54 -ur up-repos Update all your SliTaz projects repos in one
55 command
56 -rp relpkg <pkg> [<version>] Archive and upload new package/project version
57 -sc setup-cgi Setup your CGI environment
59 $(boldify "Options:")
60 --rootfs= Path to the chroot to generate or clean
61 --arch= Specify the architecture type for cross-chroot, push/pull
62 --clean or --forced Clean chroot before generating a new one
64 $(boldify "Examples:")
65 $(basename $0) gen-chroot undigest --clean
66 $(basename $0) -c --arch=arm
68 EOT
69 }
71 get_version() {
72 # Stable, undigest or custom.
73 version=${2:-cooking}
74 slitaz="$SLITAZ_HOME/$version"
75 if [ -n "$arch" ]; then
76 rootfs="$slitaz/$arch/chroot"
77 else
78 rootfs="$slitaz/chroot"
79 fi
80 }
82 check_mirror() {
83 [ -n "$2" ] && user="$2"
84 local repo="$3"
85 case $repo in
86 stable|backports|undigest)
87 remote="$MIRROR_PKGS/$repo/"
88 local="$SLITAZ_HOME/$repo/packages/"
89 ;;
90 rpi)
91 remote="$MIRROR_PKGS/cross/rpi/"
92 local="$SLITAZ_HOME/cooking/arm/packages/"
93 ;;
94 *)
95 remote="$MIRROR_PKGS/cooking/"
96 local="$SLITAZ_HOME/cooking/packages/"
97 ;;
98 esac
100 if [ -n "$arch" ]; then
101 remote="$remote$arch/"
102 local="$local$arch/"
103 fi
104 }
106 # Bind a directory into the chroot
107 bind_chroot_dir()
108 {
109 mkdir -p $1 $2
110 mount -o bind $1 $2
111 }
113 # Mount virtual Kernel file systems and chroot, but check that nobody
114 # else has done mounts
115 mount_chroot()
116 {
117 if [ ! -d "$rootfs/proc/1" ]; then
118 newline
119 action 'Mounting virtual filesystems...'
120 mount -n -t tmpfs none $rootfs/dev
121 mknod -m 622 $rootfs/dev/console c 5 1
122 mknod -m 666 $rootfs/dev/null c 1 3
123 mknod -m 666 $rootfs/dev/zero c 1 5
124 mknod -m 666 $rootfs/dev/ptmx c 5 2
125 mknod -m 666 $rootfs/dev/tty c 5 0
126 mknod -m 444 $rootfs/dev/random c 1 8
127 mknod -m 444 $rootfs/dev/urandom c 1 9
128 chown root:tty $rootfs/dev/console
129 chown root:tty $rootfs/dev/ptmx
130 chown root:tty $rootfs/dev/tty
132 ln -s /proc/self/fd $rootfs/dev/fd
133 ln -s /proc/self/fd/0 $rootfs/dev/stdin
134 ln -s /proc/self/fd/1 $rootfs/dev/stdout
135 ln -s /proc/self/fd/2 $rootfs/dev/stderr
136 ln -s /proc/kcore $rootfs/dev/core
137 mkdir $rootfs/dev/pts
138 mkdir $rootfs/dev/shm
140 mount -t devpts -o gid=4,mode=620 none $rootfs/dev/pts
141 mount -t tmpfs none $rootfs/dev/shm
143 mount -t proc proc $rootfs/proc
144 mount -t sysfs sysfs $rootfs/sys
145 status
146 fi
147 # Mount source, so they can be shared between cooking/stable/undigest.
148 # But do it only if it's a SliTaz development chroot.
149 fs="$rootfs/home/slitaz"
150 if [ -d "$slitaz" ]; then
151 bind_chroot_dir $SLITAZ_HOME/src $fs/src
152 # Now mount package dir, so they are in /home/slitaz/$version
153 # We may not mount cache wok or others: it has no point and if
154 # one wants to use a shared wok, one can bind it manually.
155 [ -n "$arch" ] && slitaz="$slitaz/$arch"
156 mkdir -p "$fs/packages" "$slitaz/packages"
157 bind_chroot_dir "$slitaz/packages" "$fs/packages"
158 fi
159 }
161 # Unmount virtual Kernel file systems.
162 umount_chroot() {
163 [ -n "$1" ] && rootfs="$1"
164 fs="$rootfs/home/slitaz"
165 newline
166 action 'Unmounting virtual filesystems...'
167 umount $rootfs/dev/shm
168 umount $rootfs/dev/pts
169 umount $rootfs/dev
170 umount $rootfs/sys
171 umount $rootfs/proc
172 if mount | fgrep -q $fs/src; then
173 umount $fs/src
174 umount $fs/packages
175 fi
176 status
177 newline
178 }
180 # Get the last cooking base rootfs, extract and configure.
181 gen_chroot() {
182 title "Generating new chroot in $rootfs"
184 mkdir -p $rootfs
185 # We may gen cooking chroot from a stable version or vice versa
186 case "$version" in
187 stable*|4.0*|backports)
188 url="http://$MIRROR/packages/stable/" ;;
189 *)
190 url="http://$MIRROR/packages/cooking/" ;;
191 esac
193 # --mirror=
194 [ -n "$mirror" ] && url="$mirror"
195 mpath="/var/lib/tazpkg/mirror"
196 mkdir -p $(dirname $rootfs$mpath)
197 echo "$url" > $rootfs$mpath
199 action "Mirror URL: ${url#http://}"
200 tazpkg recharge --root="$rootfs" --quiet >/dev/null
201 status
203 for pkg in $CHROOT_PKGS; do
204 action "Installing: $pkg $vers"
205 tazpkg -gi $pkg --root="$rootfs" --quiet
206 status
207 done
209 action "Creating resolv.conf..."
210 cat /etc/resolv.conf > $rootfs/etc/resolv.conf
211 status
213 action "Creating TZ..."
214 cat /etc/TZ > $rootfs/etc/TZ
215 status
217 case "$version" in
218 cooking) version="" ;;
219 esac
220 [ "$arch" ] && version="$version --arch=$arch"
221 footer "Ready to chroot with: $(colorize 34 "tazdev -c $version")"
222 }
224 #
225 # Commands
226 #
228 case "$1" in
229 stats|-s)
230 title "Statistics for $PROJECTS"
231 printf "%-23s %-13s %-15s %s\n" 'Project' 'Revision' 'Version' 'Files'
232 separator '-'
233 cd $PROJECTS
234 for proj in *; do
235 rev=""
236 if [ -d "$PROJECTS/$proj/.hg" ]; then
237 cd $PROJECTS/$proj
238 rev=$(hg head --template '{rev};' | sed 's|;$||') # we have multiple heads in some projects
239 vers=$(hg tags | grep -v tip | head -n1 | cut -d" " -f1) # some projects don't have any version tags
240 files=$(find . -type f | wc -l)
241 printf "%-23s %-13s %-15s %s\n" "$proj" "$rev" "${vers:--}" "$files"
242 fi
243 done
244 footer
245 ;;
247 chroot|-c)
248 # Chroot into a build env. Default to cooking configured in tazdev.conf
249 check_root $1
250 get_version $@
251 [ -d "$2" ] && rootfs="$2"
252 mount_chroot
253 echo -e "Chrooting to: $rootfs\n"
254 chroot $rootfs /bin/sh --login
255 umount_chroot
256 ;;
258 umchroot|-uc)
259 check_root $1
260 get_version $@
261 [ -d "$2" ] && rootfs="$2"
262 umount_chroot $rootfs
263 ;;
265 gen-chroot|-gc)
266 check_root $1
267 # We can use: --rootfs=/path/to/chroot
268 [ -n "$rootfs" ] || get_version $@
270 # If --clean or --forced option given
271 [ -n "$clean$forced" ] && $0 -cc --rootfs=$rootfs
273 # Don't break another env.
274 [ -d "$rootfs/bin" ] &&
275 die "A chroot environment already exists in $rootfs.\nUse --clean or --forced to clean this chroot."
277 gen_chroot
278 ;;
280 clean-chroot|-cc)
281 check_root $1
282 # We can use: --rootfs=/path/to/chroot
283 [ -n "$rootfs" ] || get_version $@
285 [ ! -d "$rootfs" ] && die "Chroot '$rootfs' doesn't exist"
287 [ -d "$rootfs/proc/1" ] && die "WARNING: $rootfs/proc mounted!"
289 cd $rootfs || exit 1
291 title "Cleaning chroot $rootfs"
292 # Keep root/ and /home they may have a build wok, custom scripts, etc.
293 for i in boot bin dev etc lib media mnt proc sbin sys tmp usr var run; do
294 if [ -d "$i" ]; then
295 action "Removing: $i ($(du -sh $i | awk '{ print $1 }'))..."
296 rm -rf $i
297 status
298 fi
299 done
300 rm -f init
301 footer
302 ;;
304 push|-p)
305 check_mirror $@
306 rsync -r -t -O -l -v -z --delete \
307 $local -e ssh $user@$MIRROR:$remote
308 ;;
310 dry-push|-dp)
311 check_mirror $@
312 rsync -r -t -O -l -v -z --delete --dry-run \
313 $local -e ssh $user@$MIRROR:$remote
314 ;;
316 pull)
317 check_mirror $@
318 rsync -r -t -l -v -z --delete \
319 -e ssh $user@$MIRROR:$remote $local
320 ;;
322 dry-pull)
323 check_mirror $@
324 rsync -r -t -l -v -z --delete --dry-run \
325 -e ssh $user@$MIRROR:$remote $local
326 ;;
328 up-repos|-ur)
329 # Update all at once.
330 title "Update all SliTaz Hg repos"
331 for p in $(ls $PROJECTS); do
332 title "Project $p"
333 cd $PROJECTS/$p
334 hg pull -u
335 done
336 footer
337 ;;
339 relpkg|-rp)
340 # Release a slitaz sub-project and upload tarball to mirror
341 [ -z "$MIRROR_SOURCES" ] && MIRROR_SOURCES="/var/www/slitaz/mirror/sources"
342 [ -z "$2" ] && die "Usage: $0 relpkg package [version]"
344 pkg=$2
345 version="$3"
347 # We can get the last found version in .hgtags
348 [ -n "$version" ] ||
349 version=$(awk 'END{print $2}' $PROJECTS/$pkg/.hgtags)
351 newline
352 cd $PROJECTS/$pkg
354 # Sanity check
355 grep -q $version$ .hgtags ||
356 die "Missing Hg tag for version $version\nYou may want to: hg tag $version; hg push\n"
358 # Archive
359 action "Creating tarball and md5sum for $pkg-$version..."
360 hg archive -t tgz $SOURCE/$pkg-$version.tar.gz
361 ( cd $SOURCE; md5sum $pkg-$version.tar.gz > $pkg-$version.md5 )
362 status
364 # Upload
365 confirm 'Do you wish to upload tarball to the mirror?' 'n' &&
366 {
367 cd $SOURCE
368 echo "Uploading to $MIRROR/sources/${pkg#slitaz-}"
369 scp "$pkg-$version.tar.gz" "$pkg-$version.md5" \
370 $MIRROR:$MIRROR_SOURCES/${pkg#slitaz-}
371 }
373 # Update pkg in wok
374 confirm "Do you wish to update $pkg in wok?" 'n' &&
375 {
376 action "Updating $pkg: $version"
377 cd $PROJECTS/wok
378 sed -i s"/VERSION=.*/VERSION=\"$version\"/" $pkg*/receipt
379 status
380 }
381 ;;
383 setup-cgi|-sc)
384 . /etc/slitaz/slitaz.conf
385 echo "$(boldify 'Public:') $PUBLIC"
386 mkdir -p $PUBLIC/cgi-bin
387 cd $PUBLIC/cgi-bin
388 for proj in tazbug tinycm mediabox; do
389 [ -d "$proj" ] || hg clone http://hg.slitaz.org/$proj
390 done
391 [ -d "$INSTALLED/lighttpd" ] || sudo tazpkg -gi lighttpd
393 echo
394 echo "TODO: setup SCN (tinycm install + plugins from slitaz-forge)"
395 echo
396 echo "$(boldify 'URL :') http://localhost/~$USER/cgi-bin/"
397 ;;
399 help)
400 doc='/usr/share/doc/slitaz/'
401 topics="Available help topics: $(cd $doc; ls *.txt | sed 's|.txt$||' | tr '\n' ' ')"
402 [ -n "$2" ] ||
403 die "Missing tool/library name\nExample: tazdev help httphelper\n\n$topics"
405 if [ -f "$doc$2.txt" ]; then
406 {
407 output='raw' title "Help for $2"
408 cat $doc$2.txt
409 footer
410 } | less -M
411 else
412 echo "No help found for '$2'"
413 longline "$topics"
414 fi
415 ;;
417 usage|*)
418 usage
419 ;;
421 esac
423 exit 0