tazlito view tazlito @ rev 8

Add support for 'addgroup hacker audio' + fixe clean-distro
author Christophe Lincoln <pankso@slitaz.org>
date Mon Dec 03 12:30:15 2007 +0100 (2007-12-03)
parents 159c6264d500
children 8f27e43358c0
line source
1 #!/bin/sh
2 # TazLito - SliTaz Live Tool.
3 #
4 # Tazlito is a tool to help generating and configuring SliTaz LiveCD
5 # ISO images. You can creat a custom distro in one command from a list of
6 # packages, extract a existing ISO image to hack it, creat new initramfs
7 # and/or new ISO. Most commands must be run by root, expect the stats
8 # and the configuration file manipulation.
9 #
10 # (C) 2007 SliTaz - GNU General Public License.
11 # Author : <pankso@slitaz.org>
12 #
13 VERSION=1.2pre
15 # Tazlito configuration variables to be shorter
16 # and to use words rater than numbers.
17 COMMAND=$1
18 LIST_NAME=$2
19 TMP_DIR=/tmp/tazlito-$$-$RANDOM
20 TOP_DIR=`pwd`
21 INITRAMFS=rootfs.gz
22 LZMA_INITRAMFS=rootfs.lz
24 # Try to include config file, continue if command is gen-config or exit.
25 # The main config used by default is in /etc/tazlito.
26 if [ -f "/etc/tazlito/tazlito.conf" ] ; then
27 CONFIG_FILE="/etc/tazlito/tazlito.conf"
28 fi
29 # Specific distro config file can be put in a distro tree.
30 if [ -f "$TOP_DIR/tazlito.conf" ] ; then
31 CONFIG_FILE="$TOP_DIR/tazlito.conf"
32 fi
33 if [ ! "$CONFIG_FILE" = "" ] ; then
34 . $CONFIG_FILE
35 else
36 if [ "$COMMAND" = "gen-config" ] ; then
37 continue
38 else
39 echo "Unable to find any configuration file. Please read the doc"
40 echo "or run '`basename $0` gen-config' to get an empty config file."
41 exit 0
42 fi
43 fi
45 # Set the rootfs and rootcd path with $DISTRO
46 # configuration variable.
47 ROOTFS=$DISTRO/rootfs
48 ROOTCD=$DISTRO/rootcd
50 #####################
51 # Tazlito functions #
52 #####################
54 # Print the usage.
55 usage ()
56 {
57 echo -e "\nSliTaz Live Tool - Version: $VERSION\n
58 \033[1mUsage: \033[0m `basename $0` [command] [list|iso] [dir]
59 \033[1mCommands: \033[0m\n
60 usage Print this short usage.
61 stats View Tazlito and distro configuration statistics.
62 gen-config Generate a new configuration file for a distro.
63 configure Configure the main config file or a specific tazlito.conf.
64 gen-iso Generate a new ISO from a distro tree.
65 gen-initiso Generate a new initramfs and ISO from the distro tree.
66 extract-distro Extract and ISO to a directory and rebuild LiveCD tree.
67 gen-distro Generated a Live distro and ISO from a list of packages.
68 clean-distro Remove all files generated by gen-distro.
69 addhacker Add Linux User Hacker to the current distro.
70 check-distro Help to check if distro is ready to release.
71 burn-iso Burn ISO image to a cdrom using Wodim.\n"
72 }
74 # Status function.
75 status()
76 {
77 local CHECK=$?
78 echo -en "\\033[70G[ "
79 if [ $CHECK = 0 ]; then
80 echo -en "\\033[1;33mOK"
81 else
82 echo -en "\\033[1;31mFailed"
83 fi
84 echo -e "\\033[0;39m ]"
85 }
86 todomsg()
87 {
88 echo -e "\\033[70G[ \\033[1;31mTODO\\033[0;39m ]"
89 }
91 # Check if user is root.
92 check_root()
93 {
94 if test $(id -u) != 0 ; then
95 echo -e "\nYou must be root to run `basename $0` with this option."
96 echo -e "Please type 'su' and root password to become super-user.\n"
97 exit 0
98 fi
99 }
101 # Check for the boot dir into the root CD tree.
102 verify_rootcd()
103 {
104 if [ ! -d "$ROOTCD/boot" ] ; then
105 echo -e "\nUnable to find the rootcd boot directory...\n"
106 exit 0
107 fi
108 }
110 # Gen a new ISO image using isolinux.
111 gen_livecd_isolinux()
112 {
113 if [ ! -f "$ROOTCD/boot/isolinux/isolinux.bin" ] ; then
114 echo -e "\nUnable to find isolinux binary.\n"
115 exit 0
116 fi
117 cd $DISTRO
118 echo ""
119 echo -e "\033[1mGenerating ISO image\033[0m"
120 echo "================================================================================"
121 genisoimage -R -o $ISO_NAME.iso -b boot/isolinux/isolinux.bin \
122 -c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
123 -V "$VOLUM_NAME" -p "$PREPARED" -input-charset iso8859-1 \
124 -boot-info-table $ROOTCD
125 echo "================================================================================"
126 }
128 # Gen a new initramfs from the root file system.
129 gen_initramfs()
130 {
131 cd $ROOTFS
132 echo ""
133 # Use lzma if installed
134 if [ -x /usr/bin/lzma ]; then
135 echo -n "Generating cpio archive... "
136 find . -print | cpio -o -H newc > ../rootfs.cpio
137 cd $DISTRO
138 lzma e $ROOTFS.cpio $LZMA_INITRAMFS
139 rm rootfs.cpio
140 cp $LZMA_INITRAMFS $ROOTCD/boot
141 # Remove eventual gziped initramfs and sed isolinux.cfg for the path
142 # to lzma rootfs ($LZMA_INITRAMFS)
143 rm -f $ROOTCD/boot/$INITRAMFS
144 sed -i s/$INITRAMFS/$LZMA_INITRAMFS/g $ROOTCD/boot/isolinux/isolinux.cfg
145 else
146 echo -n "Generating gziped initramfs... "
147 find . -print | cpio -o -H newc | gzip -9 > $DISTRO/$INITRAMFS
148 cd $DISTRO
149 cp $INITRAMFS $ROOTCD/boot
150 fi
151 }
153 # Print ISO and rootfs size.
154 distro_stats()
155 {
156 echo ""
157 echo -e "\033[1mDistro statistics\033[0m"
158 echo "================================================================================"
159 echo "Build date : `date +%Y%m%d\ \at\ \%H:%M:%S`"
160 echo "Packages : `ls -1 $ROOTFS/var/lib/tazpkg/installed | wc -l`"
161 echo "Rootfs size : `du -sh $ROOTFS`"
162 if [ -f $LZMA_INITRAMFS ]; then
163 echo "Initramfs size : `du -sh $LZMA_INITRAMFS`"
164 else
165 echo "Initramfs size : `du -sh $INITRAMFS`"
166 fi
167 echo "ISO image size : `du -sh $ISO_NAME.iso`"
168 echo "================================================================================"
169 echo ""
170 }
172 # Creat an empty configuration file.
173 empty_config_file()
174 {
175 cat >> tazlito.conf << "EOF"
176 # tazlito.conf: Tazlito (SliTaz Live Tool)
177 # configuration file.
178 #
180 # Name of the ISO image to generate.
181 ISO_NAME=""
183 # ISO image volum name.
184 VOLUM_NAME="SliTaz"
186 # Name of the preparator.
187 PREPARED="$USER"
189 # Path to the packages repository and the packages.list.
190 PACKAGES_REPOSITORY=""
192 # Path to the distro tree to gen-distro from a
193 # list of packages.
194 DISTRO=""
196 # Path to the directory contening additional files
197 # to copy into the rootfs and rootcd of the LiveCD.
198 ADDFILES="$DISTRO/addfiles"
199 EOF
200 }
202 ####################
203 # Tazlito commands #
204 ####################
206 case "$COMMAND" in
207 stats)
208 # Tazlito general statistics from the config file.
209 #
210 echo ""
211 echo -e "\033[1mTazlito statistics\033[0m
212 ===============================================================================
213 Config file : $CONFIG_FILE
214 ISO name : $ISO_NAME.iso
215 Volum name : $VOLUM_NAME
216 Prepared : $PREPARED
217 Packages repository : $PACKAGES_REPOSITORY
218 Distro directory : $DISTRO"
219 if [ ! "$ADDFILES" = "" ] ; then
220 echo -e "Additional files : $ADDFILES"
221 fi
222 echo "================================================================================"
223 echo ""
224 ;;
225 gen-config)
226 # Gen a new config file in the current dir or the specified
227 # directory by $2.
228 #
229 if [ -n "$2" ] ; then
230 mkdir -p $2 && cd $2
231 fi
232 echo -n "Generating empty tazlito.conf..."
233 empty_config_file
234 status
235 echo ""
236 if [ -f "tazlito.conf" ] ; then
237 echo "Configuration file is ready to edit."
238 echo "File location : `pwd`/tazlito.conf"
239 echo ""
240 fi
241 ;;
242 configure)
243 # Configure a tazlito.conf config file. Start by getting
244 # a empty config file and sed it.
245 #
246 if [ -f "tazlito.conf" ] ; then
247 continue
248 rm tazlito.conf
249 else
250 if test $(id -u) = 0 ; then
251 cd /etc
252 else
253 echo "You must be root to configure the main config file or in"
254 echo "the same directory of the file you want to configure."
255 exit 0
256 fi
257 fi
258 empty_config_file
259 echo""
260 echo -e "\033[1mConfiguring :\033[0m `pwd`/tazlito.conf"
261 echo "================================================================================"
262 # ISO name.
263 echo -n "ISO name : " ; read anser
264 sed -i s#'ISO_NAME=\"\"'#"ISO_NAME=\"$anser\""# tazlito.conf
265 # Volum name.
266 echo -n "Volum name : " ; read anser
267 sed -i s/'VOLUM_NAME=\"SliTaz\"'/"VOLUM_NAME=\"$anser\""/ tazlito.conf
268 # Packages repository.
269 echo -n "Packages repository : " ; read anser
270 sed -i s#'PACKAGES_REPOSITORY=\"\"'#"PACKAGES_REPOSITORY=\"$anser\""# tazlito.conf
271 # Distro path.
272 echo -n "Distro path : " ; read anser
273 sed -i s#'DISTRO=\"\"'#"DISTRO=\"$anser\""# tazlito.conf
274 echo "================================================================================"
275 echo "Config file is ready to use."
276 echo "You can now extract an ISO or generate a distro."
277 echo ""
278 ;;
279 gen-iso)
280 # Simply generated a new iso.
281 #
282 check_root
283 verify_rootcd
284 gen_livecd_isolinux
285 distro_stats
286 ;;
287 gen-initiso)
288 # Simply generated a new initramfs with a new iso.
289 #
290 check_root
291 verify_rootcd
292 gen_initramfs
293 gen_livecd_isolinux
294 distro_stats
295 ;;
296 extract-distro)
297 # Extract a ISO image to a directory and rebuild the LiveCD tree.
298 #
299 check_root
300 ISO_IMAGE=$2
301 if [ -z "$ISO_IMAGE" ] ; then
302 echo -e "\nPlease specify the path to the ISO image."
303 echo -e "Example : `basename $0` image.iso /path/target\n"
304 exit 0
305 fi
306 # Set the distro path by checking for $3 on cmdline.
307 if [ -n "$3" ] ; then
308 TARGET=$3
309 else
310 TARGET=$DISTRO
311 fi
312 # Exit if existing distro is found.
313 if [ -d "$TARGET/rootfs" ] ; then
314 echo -e "\nA rootfs exist in : $TARGET"
315 echo -e "Please clean the distro tree or change directory path.\n"
316 exit 0
317 fi
318 echo ""
319 echo -e "\033[1mTazlito extracting :\033[0m $ISO_IMAGE"
320 echo "================================================================================"
321 # Start to mount the ISO.
322 echo ""
323 echo "Mounting ISO image..."
324 mkdir -p $TMP_DIR
325 # Get ISO file size.
326 isosize=`du -sh $ISO_IMAGE`
327 mount -o loop $ISO_IMAGE $TMP_DIR
328 sleep 2
329 # Prepare target dir, copy the kernel and the rootfs.
330 mkdir -p $TARGET/rootfs
331 mkdir -p $TARGET/rootcd/boot
332 echo -n "Copying the Linux kernel..."
333 cp $TMP_DIR/boot/bzImage $TARGET/rootcd/boot
334 status
335 echo -n "Copying isolinux files..."
336 cp -a $TMP_DIR/boot/isolinux $TARGET/rootcd/boot
337 status
338 echo -n "Copying the rootfs..."
339 cp $TMP_DIR/boot/rootfs.gz $TARGET/rootcd/boot
340 cp $TMP_DIR/boot/rootfs.gz $TARGET/rootfs
341 status
342 # Exract initramfs.
343 cd $TARGET/rootfs
344 echo -n "Extracting the rootfs... "
345 gzip -d rootfs.gz && cpio -id < rootfs
346 rm rootfs
347 # Umount and remove temp directory and cd to $TARGET to get stats.
348 umount $TMP_DIR && rm -rf $TMP_DIR
349 cd ..
350 echo ""
351 echo "================================================================================"
352 echo "Extracted : $ISO_IMAGE ($isosize)"
353 echo "Distro tree : `pwd`"
354 echo "Rootfs size : `du -sh rootfs`"
355 echo "Rootcd size : `du -sh rootcd`"
356 echo "================================================================================"
357 echo ""
358 ;;
359 gen-distro)
360 # Generate a live distro tree with a set of packages.
361 #
362 check_root
363 if [ -d $ROOTFS ] ; then
364 echo "A rootfs exist in : $DISTRO"
365 echo -e "Please clean the distro tree or change directory path.\n"
366 exit 0
367 fi
368 # First check for the main packages list then
369 # for a distro-packages.list in the current directory
370 # if none, check if a package list was specified on cmdline.
371 if [ -f "/etc/slitaz-tools/distro-packages.list" ] ; then
372 LIST_NAME="distro-packages.list"
373 fi
374 if [ -f "distro-packages.list" ] ; then
375 LIST_NAME="distro-packages.list"
376 fi
377 if [ -n "$2" ] ; then
378 if [ ! -f "$2" ] ; then
379 echo -e "\nUnable to find the specified packages list."
380 echo -e "List name : $2\n"
381 exit 0
382 else
383 LIST_NAME=$2
384 fi
385 fi
386 # Exit if no list name.
387 if [ "$LIST_NAME" = "" ] ; then
388 echo -e "\nNo packages list found or specified. Please read the doc.\n"
389 exit 0
390 fi
391 # Start generation.
392 echo ""
393 echo -e "\033[1mTazlito generating a distro\033[0m"
394 echo "================================================================================"
395 # Get the list of packages using cat for a file list.
396 LIST=`cat $LIST_NAME`
397 # Verify if all packages in list are presents in $PACKAGES_REPOSITORY.
398 for pkg in $LIST
399 do
400 pkg=${pkg%.tazpkg}
401 if [ ! -f $PACKAGES_REPOSITORY/$pkg.tazpkg ] ; then
402 echo -e "\nUnable to find $pkg in the repository."
403 echo -e "Path : $PACKAGES_REPOSITORY\n"
404 exit 0
405 fi
406 done
407 # Root fs stuff.
408 echo "Preparing the rootfs directory..."
409 mkdir -p $ROOTFS
410 sleep 2
411 for pkg in $LIST
412 do
413 # First copy and extract the package in tmp dir.
414 pkg=${pkg%.tazpkg}
415 mkdir -p $TMP_DIR
416 cp $PACKAGES_REPOSITORY/$pkg.tazpkg $TMP_DIR && cd $TMP_DIR
417 echo ""
418 echo -n "Installing $PACKAGE... "
419 cpio -id < $pkg.tazpkg && rm -f $pkg.tazpkg
420 gzip -d fs.cpio.gz
421 echo -n "Extracting the file system... "
422 cpio -id < fs.cpio && rm fs.cpio
423 # Copy all packages fs and receipt to the rootfs.
424 echo -n "Copying files to the rootfs..."
425 cp -a fs/* $ROOTFS
426 . $PWD/receipt
427 mkdir -p $ROOTFS/var/lib/tazpkg/installed/$PACKAGE
428 # Copy the receipt, list of files and description if exist.
429 cp receipt $ROOTFS/var/lib/tazpkg/installed/$PACKAGE
430 cp files.list $ROOTFS/var/lib/tazpkg/installed/$PACKAGE
431 if [ -f "description.txt" ] ; then
432 cp description.txt $ROOTFS/var/lib/tazpkg/installed/$PACKAGE
433 fi
434 status
435 # Remove tmp dir to get a empty one for the next package.
436 rm -rf $TMP_DIR
437 done
438 echo ""
439 cd $DISTRO
440 # Copy all files from $ADDFILES/rootfs to the rootfs.
441 if [ -d "$ADDFILES/rootfs" ] ; then
442 echo -n "Copying addfiles content to the rootfs... "
443 cp -a $ADDFILES/rootfs/* $ROOTFS && status
444 fi
445 echo "Root file system is generated..."
446 # Root CD part.
447 echo -n "Preparing the rootcd directory..."
448 mkdir -p $ROOTCD
449 status
450 # Move the boot dir with the Linux kernel from rootfs.
451 # The boot dir goes directly on the CD.
452 if [ -d "$ROOTFS/boot" ] ; then
453 echo -n "Moving the boot directory..."
454 mv $ROOTFS/boot $ROOTCD
455 cd $ROOTCD/boot
456 mv vmlinuz-* bzImage
457 status
458 fi
459 cd $DISTRO
460 # Copy all files from $ADDFILES/rootcd to the rootcd.
461 if [ -d "$ADDFILES/rootcd" ] ; then
462 echo -n "Copying addfiles content to the rootcd... "
463 cp -a $ADDFILES/rootcd/* $ROOTCD && status
464 fi
465 # Initramfs and ISO image stuff.
466 gen_initramfs
467 gen_livecd_isolinux
468 distro_stats
469 ;;
470 clean-distro)
471 # Remove old distro tree.
472 #
473 check_root
474 echo ""
475 echo -e "\033[1mCleaning :\033[0m $DISTRO"
476 echo "================================================================================"
477 if [ -d "$DISTRO" ] ; then
478 if [ -d "$ROOTFS" ] ; then
479 echo -n "Removing the rootfs..."
480 rm -f $DISTRO/$INITRAMFS
481 rm -f $DISTRO/$LZMA_INITRAMFS
482 rm -rf $ROOTFS && status
483 fi
484 if [ -d "$ROOTCD" ] ; then
485 echo -n "Removing the rootcd..."
486 rm -rf $ROOTCD && status
487 fi
488 echo -n "Removing eventual ISO image..."
489 rm -f $DISTRO/$ISO_NAME.iso && status
490 fi
491 echo "================================================================================"
492 echo ""
493 ;;
494 addhacker)
495 # Without /etc/passwd...
496 #
497 check_root
498 echo ""
499 echo -e "\033[1mAdduser hacker to :\033[0m $ROOTFS"
500 echo "================================================================================"
501 if [ ! -d "$ROOTFS/etc" ] ; then
502 echo -e "\nUnable to find : $ROOTFS/etc"
503 echo -e "Users and passwords config files will not be found.\n"
504 exit 0
505 fi
506 # Go for echoing on configuration files if any hacker was found.
507 #
508 if [ ! "`cat $ROOTFS/etc/passwd | grep hacker`" ] ; then
509 echo -n "Configuring $ROOTFS/etc..."
510 echo 'hacker:x:500:500:Linux User,,,:/home/hacker:/bin/ash' >> $ROOTFS/etc/passwd
511 echo 'hacker::13646:0:99999:7:::' >> $ROOTFS/etc/shadow
512 echo 'hacker:x:500:' >> $ROOTFS/etc/group
513 echo 'hacker:!::' >> $ROOTFS/etc/gshadow
514 status
515 else
516 echo "Hacker is already in : $ROOTFS/etc/passwd"
517 fi
518 # Hacker can listen to music
519 #
520 if grep -q audio $ROOTFS/etc/group; then
521 sed -i s/'audio:x:20:'/'audio:x:20:hacker'/ $ROOTFS/etc/group
522 fi
523 # /home/hacker files.
524 #
525 echo -n "Creating default directories... "
526 mkdir -p $ROOTFS/home/hacker/Documents \
527 $ROOTFS/home/hacker/Downloads \
528 $ROOTFS/home/hacker/Images \
529 $ROOTFS/home/hacker/Public \
530 $ROOTFS/home/hacker/Templates
531 status
532 # Change permissions.
533 #
534 echo -n "Chmodig all files in /home/hacker..."
535 chown -R 500.500 $ROOTFS/home/hacker
536 status
537 echo "================================================================================"
538 echo "Linux User Hacker have an account in the distro."
539 echo ""
540 ;;
541 check-distro)
542 # Check for a few LiveCD needed files not installed by packages.
543 #
544 if [ ! -d "$ROOTFS" ] ; then
545 echo -e "\nUnable to find a distro rootfs to check.\n"
546 exit 0
547 fi
548 echo ""
549 echo -e "\033[1mChecking distro :\033[0m $ROOTFS"
550 echo "================================================================================"
551 # SliTaz release info.
552 if [ ! -f "$ROOTFS/etc/slitaz-release" ]; then
553 echo "Missing release info : /var/lib/tazpkg/mirror"
554 else
555 release=`cat $ROOTFS/etc/slitaz-release`
556 echo -n "Release : $release"
557 status
558 fi
559 # Tazpkg mirror.
560 if [ ! -f "$ROOTFS/var/lib/tazpkg/mirror" ]; then
561 echo -n "Mirror URL : Missing /var/lib/tazpkg/mirror"
562 todomsg
563 else
564 echo -n "Mirror configuration exist..."
565 status
566 fi
567 # Isolinux msg
568 if grep -q "cooking-XXXXXXXX" /$ROOTCD/boot/isolinux/isolinux.msg; then
569 echo -n "Isolinux msg : Missing cooking date XXXXXXXX (ex `date +%Y%m%d`)"
570 todomsg
571 else
572 echo -n "Isolinux message seems good..."
573 status
574 fi
575 echo "================================================================================"
576 echo ""
577 ;;
578 burn-iso)
579 # Guess cdrom device, ask user and burn the ISO.
580 #
581 check_root
582 DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3`
583 DRIVE_SPEED=`cat /proc/sys/dev/cdrom/info | grep "drive speed" | cut -f 3`
584 # We can specify an alternative ISO from the cmdline.
585 if [ -n "$2" ] ; then
586 iso=$2
587 else
588 iso=$DISTRO/$ISO_NAME.iso
589 fi
590 if [ ! -f "$iso" ]; then
591 echo -e "\nUnable to find ISO : $iso\n"
592 exit 0
593 fi
594 echo ""
595 echo -e "\033[1mTazlito burn ISO\033[0m "
596 echo "================================================================================"
597 echo "Cdrom device : /dev/$DRIVE_NAME"
598 echo "Drive speed : $DRIVE_SPEED"
599 echo "ISO image : $iso"
600 echo "================================================================================"
601 echo ""
602 echo -n "Burn ISO image (y/N) ? "; read anser
603 if [ "$anser" == "y" ]; then
604 echo ""
605 echo "Starting Wodim to burn the iso..." && sleep 2
606 echo "================================================================================"
607 wodim speed=$DRIVE_SPEED dev=/dev/$DRIVE_NAME $DISTRO/$ISO_NAME.iso
608 echo "================================================================================"
609 echo "ISO image is burned to cdrom."
610 else
611 echo -e "\nExiting. No ISO burned."
612 fi
613 echo ""
614 ;;
615 usage|*)
616 # Clear and print usage also for all unknow commands.
617 #
618 clear
619 usage
620 ;;
622 esac
624 exit 0