tazlito view tazlito @ rev 1

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