flavors-stable view core/rootfs/usr/bin/tazinst @ rev 105

core: add tazinst doc in /usr/share/doc/slitaz
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 31 00:30:56 2011 +0200 (2011-05-31)
parents
children
line source
1 #!/bin/sh
2 # slitaz-installer - SliTaz GNU/Linux installer.
3 #
4 # So this is the SliTaz installer. The script starts with a
5 # few main variables, then all the functions and then the
6 # full sequence of functions.
7 #
8 # (C) 2007-2011 SliTaz - GNU General Public License v3.
9 #
10 # Authors : Christophe Lincoln <pankso@slitaz.org>
11 #
13 VERSION=2.99
15 #
16 SOURCE_ROOT=/media/source
17 TARGET_ROOT=/mnt/target
18 LOG=/var/log/tazinst.log
19 BACKLIST="SliTaz GNU/Linux installer"
21 # Default debug
22 DEBUG=0
24 # Mirror urls
25 URL_STABLE="http://mirror.slitaz.org/iso/stable/slitaz-3.0.iso"
26 URL_COOKING="http://mirror.slitaz.org/iso/cooking/slitaz-cooking.iso"
27 URL_ROLLING="http://mirror.slitaz.org/iso/rolling/slitaz-core.iso"
29 # Print a short help
30 usage()
31 {
32 echo -e "\n`gettext \"Tazinst - SliTaz installer - Version\"`: $VERSION\n"
33 echo -e "\033[1m`gettext \"Usage\"`:"
34 echo -e "\033[0m `gettext \"tazinst [command] [config-file|options]\n\"`"
35 echo -e "\033[1m`gettext \"Commands\"`: \033[0m"
36 echo -e "usage Print this short usage."
37 echo -e "install Install SliTaz on HDD using a configuration file."
38 echo -e "upgrade Upgrade SLiTaz on HDD using a configuration file."
39 echo -e "config Generate a configuration file."
40 echo -e "cli Install or upgrade using command line options:"
41 echo -e " -i `gettext \"Full Install (not upgrading, all present data will be lost).\"`"
42 echo -e " -u `gettext \"Upgrade (Needs an active internet connection).\"`"
43 echo -e " -t <type> `gettext \"Install type (cdrom|usb|iso|web|weboot).\"`"
44 echo -e " -m <source> `gettext \"Source media (ex: file.iso|usb partition|web url)\"`"
45 echo -e " -p <partition> `gettext \"Partition where SliTaz will be installed (ex:/dev/hda3).\"`"
46 echo -e " -f <fs> `gettext \"Partition will be formatted (fs=ext2|ext3|ext4|etc..).\"`"
47 echo -e " -g `gettext \"Install Grub.\"`"
48 echo -e " -w `gettext \"Dual-boot a Windows partition (auto|hd<disk>,<part> ex:hd0,0).\"`"
49 echo -e " -d `gettext \"Debug mode.\"`"
50 exit 0
51 }
53 # Print an error msg & exit
54 # $1: exit code
55 # $@: err msg
56 abort()
57 {
58 # unmouting source & target
59 if mount | grep -q $SOURCE_ROOT; then
60 umount $SOURCE_ROOT 2>>$LOG
61 fi
62 if mount | grep -q $TARGET_ROOT; then
63 umount $TARGET_ROOT 2>>$LOG
64 fi
65 echo "Error $@"
66 echo "Installation canceled"
67 test $(id -u) = 0 && echo "Installation canceled on error $@" >> $LOG
68 exit $1
69 }
71 # Print a warning msg
72 warning()
73 {
74 echo "Warning: $@" | tee -a $LOG
75 }
77 # Print a debug msg
78 debug()
79 {
80 [ $DEBUG -gt 0 ] && echo -e "\033[1mDEBUG: \033[0m$1"
81 echo "DEBUG: $1" >>$LOG
82 }
84 # print a msg
85 msg()
86 {
87 STEP=$(($STEP+1))
88 echo "$STEP. $@" | tee -a $LOG
89 }
91 ########################
92 # Gen-config functions #
93 ########################
95 # Generate a config file
96 # $1: Configuration file
97 gen_config()
98 {
99 CONFILE=$1
100 touch $CONFILE || error 1 "Unable to write $CONFILE"
101 if [ -r "$CONFILE" ]; then
102 cat > $CONFILE << _EOF_
103 # tazinst.conf: SliTaz Installer configuration file.
104 #
106 # Install type : [cdrom|usb|iso|web|weboot]
107 INST_TYPE="cdrom"
109 # Install source
110 # usb:/dev/xxx, ex: SRC_FILE=/dev/sdb1
111 # iso:file.iso, ex: SRC_FILE=~/slitaz.3.0.iso
112 # web: url, ex: SRC_FILE=http://mirror.slitaz.org/iso/cooking/slitaz-cooking.iso
113 # web: predefinites (stable|cooking|rolling), ex: SRC_FILE=cooking
114 SRC_FILE=""
116 # Install Target (Root Partition).
117 TGT_PARTITION="/dev/hda5"
119 # Target File system.
120 # SliTaz uses ext3 by default but another filesystem can be used if wanted,
121 # for this please adjust your /etc/fstab after installation. Valid options are:
122 # (btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
123 TGT_FS="ext3"
125 # Home partition.
126 # On most GNU/Linux systems users personal files are stored in the directory
127 # /home. Home can be on a separate partition or another hard disk.
128 TGT_HOME=""
129 # Home File system (if /home is on a separate partition)
130 TGT_HOME_FS=""
132 # Hostname
133 TGT_HOSTNAME="slitaz"
135 # roor password
136 # The root administrator privilege lets you manage and configure the full
137 # system. A root user can damage your system so you should always setup a
138 # strong password with special characters and/or numbers.
139 TGT_ROOT_PWD="root"
141 # The default user for the system will have his personal files stored
142 # in /home/*user* (and will be automatically added to the audio group).
143 TGT_USER="tux"
144 TGT_USER_PWD=""
146 # Grub bootloader
147 # install grub [yes|no]
148 TGT_GRUB="no"
149 # Where to find menu.lst (dedicated grub part. or multi-os install)
150 # If you don't know what to do, it's safe to leave it blank
151 TGT_MENU_PARTITION=""
153 # Windows dual-boot
154 # Dual boot is disabled if WINBOOT is empty: TGT_WINBOOT=""
155 # You may let tazinst find your win partition, mode=auto: TGT_WINBOOT="auto"
156 # or set manual setting: "hd[disk],[partition]" ex:TGT_WINBOOT=hd0,0
157 TGT_WINBOOT=""
159 _EOF_
161 else
162 abort 2 "Configuration file $CONFILE not found"
163 fi
165 }
167 ######################
168 # Checking functions #
169 ######################
172 # def values and start log
173 # $@ :
174 init()
175 {
176 echo "=== Tazinst start at `date` ===" >$LOG
177 echo "Command: $0 $@" >>$LOG
179 # Default Type
180 INST_TYPE=cdrom
181 # Default Hostname.
182 TGT_HOSTNAME=slitaz
183 # Default root passwd
184 TGT_ROOT_PWD=root
185 # Default user
186 TGT_USER=tux
187 # Default Grub Install
188 TGT_GRUB=no
189 }
191 # Read Conf
192 # $1: conf file
193 read_configuration_file()
194 {
195 CONFILE=$1
196 if [ -n "$CONFILE" ]; then
197 if [ -r "$CONFILE" ]; then
198 debug "Using config-file=$CONFILE"
199 # source doesn't like file without a path
200 [ $(echo "$CONFILE" | grep -c "/") == "0" ] && CONFILE="./$CONFILE"
201 source $CONFILE || abort 2 "Unable to read $CONFILE"
202 else
203 abort 2 "Configuration file not found"
204 fi
205 else
206 abort 2 "No configuration file provided"
207 fi
208 }
210 # Cli options
211 # $@: tazinst parms
212 check_cli_options()
213 {
214 shift
215 while getopts "c:df:ghip:s:t:uw:" opt ; do
216 case $opt in
217 d) DEBUG=1 ;;
218 f) TGT_FS=$OPTARG ;;
219 g) TGT_GRUB="yes" ;;
220 h) usage ;;
221 i) INSTALL="install" ;;
222 p) TGT_PARTITION=$OPTARG ;;
223 s) SRC_FILE=$OPTARG ;;
224 t) INST_TYPE=$OPTARG ;;
225 u) UPGRADE="upgrade" ;;
226 w) TGT_WINBOOT=$OPTARG ;;
227 [?]) abort 1 ;;
228 esac
229 done
231 # Duplicate install modes are forbidden
232 [ -n "$INSTALL" ] && [ -n "$UPGRADE" ] && abort 1 "Mismatched parameters"
233 INST_ACTION="$INSTALL$UPGRADE"
234 # Don't allow formatting on upgrades
235 [ "$INST_ACTION" == "upgrade" ] && [ -n "$TGT_FS" ] && \
236 warning "Partition $TGT_PARTITION will not be formatted during upgrade"
237 }
239 # check main vars
240 check_vars()
241 {
242 # error handling
243 local error=no
244 local found=no
245 local partition=""
247 debug "--- Tazinst main options ---"
248 debug "action=$INST_ACTION"
249 debug "type=$INST_TYPE"
250 debug "source=$SRC_FILE"
251 debug "/ partition=$TGT_PARTITION"
252 debug "/ filesystem=$TGT_FS"
253 debug "/home partition=$TGT_HOME"
254 debug "/home filesystem=$TGT_HOME_FS"
255 debug "hostname=$TGT_HOSTNAME"
256 debug "root-pwd=$TGT_ROOT_PWD"
257 debug "user=$TGT_USER"
258 debug "user-pwd=$TGT_USER_PWD"
259 debug "grub=$TGT_GRUB"
260 debug "winboot=$TGT_WINBOOT"
261 debug "--------------------------------------"
263 # Check Action
264 case $INST_ACTION in
265 install|upgrade) ;;
266 *) msg "INST_ACTION=$INST_ACTION: Invalid setting"; error=yes ;;
267 esac
269 # Check Type
270 case $INST_TYPE in
271 cdrom|weboot) ;;
272 usb|iso|web)
273 # We need a valid source
274 if [ -z "$SRC_FILE" ]; then
275 echo "Type is $INST_TYPE, but INST_SOURCE is not set"; error=yes
276 fi ;;
277 *) msg "INST_TYPE=$INST_TYPE: Invalid setting"; error=yes ;;
278 esac
280 # Check Source file
281 # 1. assign predefs
282 if [ "$INST_TYPE" == "web" ]; then
283 [ "$SRC_FILE" == "stable" ] && SRC_FILE=$URL_STABLE
284 [ "$SRC_FILE" == "cooking" ] && SRC_FILE=$URL_COOKING
285 [ "$SRC_FILE" == "rolling" ] && SRC_FILE=$URL_ROLLING
286 fi
287 # 2. check avail.
288 case $INST_TYPE in
289 iso)
290 if [ ! -r "$SRC_FILE" ]; then
291 msg "SRC_FILE=$SRC_FILE: file not found" ; error=yes
292 fi ;;
293 web)
294 if [ $(wget -sq "$SRC_FILE") ]; then
295 msg "SRC_FILE=$SRC_FILE: file not found" ; error=yes
296 fi ;;
297 esac
299 # Check Target Partition
300 found=no
301 LIST_PARTITION=$(fdisk -l | awk '/^\/dev/{printf "%s ",$1}')
302 for partition in $LIST_PARTITION; do
303 [ "$partition" == "$TGT_PARTITION" ] && found="yes"
304 done
305 if [ "$found" != "yes" ]; then
306 msg "TGT_PARTITION=$TGT_PARTITION Partition not found"; error=yes
307 fi
309 # Check Filesystem
310 case $TGT_FS in
311 "") ;;
312 btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
313 found=no
314 for xdir in /sbin /usr/sbin /usr/bin; do
315 [ -x "$xdir/mkfs.$TGT_FS" ] && found=yes
316 done
317 if [ "$found" == "no" ]; then
318 msg "$TGT_FS: mkfs.$TGT_FS is not installed."; error=yes
319 fi ;;
320 *) msg "TGT_FS=$TGT_FS: Invalid setting"; error=yes ;;
321 esac
323 # Check Home partition
324 if [ -n "$TGT_HOME" ]; then
325 found=no
326 for partition in $LIST_PARTITION; do
327 [ "$partition" == "$TGT_HOME" ] && found=yes
328 done
329 if [ "$found" != "yes" ]; then
330 msg "TGT_HOME=$TGT_HOME: Not found"; error=yes
331 fi
332 fi
334 # Check Home Filesystem
335 case $TGT_HOME_FS in
336 "") ;;
337 btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
338 found=no
339 for xdir in /sbin /usr/sbin /usr/bin; do
340 [ -x "$xdir/mkfs.$TGT_FS" ] && found=yes
341 done
342 if [ "$found" == "no" ]; then
343 msg "$TGT_FS: mkfs.$TGT_FS is not installed."; error=yes
344 fi ;;
345 *) msg "TGT_HOME_FS=$TGT_HOME_FS: Invalid setting"; error=yes ;;
346 esac
348 # Check Grub
349 case $TGT_GRUB in
350 yes|no) ;;
351 *) msg "TGT_GRUB=$TGT_GRUB: Invalid setting"; error=yes ;;
352 esac
354 # Check Winboot
355 case $TGT_WINBOOT in
356 "") ;;
357 auto) ;;
358 hd[[:digit:]],[[:digit:]]) ;;
359 *) msg "TGT_WINBOOT=$TGT_WINBOOT: Invalid format"; error=yes ;;
360 esac
362 # Stop on error
363 [ "$error" == "yes" ] && abort 1
364 }
366 # Exit install if user is not root.
367 check_root()
368 {
369 if test $(id -u) != 0 ; then
370 echo "You must be the root user (system administrator) to install SliTaz, \
371 please use 'su' to get a root SHell and restart installation."
372 exit 0
373 fi
374 }
376 # Mount cdrom
377 check_cdrom()
378 {
379 # Set device name
380 DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3` [ -n "$DRIVE_NAME" ] || DRIVE_NAME=cdrom
381 CDROM=/dev/$DRIVE_NAME
382 # Try to mount a cdrom
383 if mount -t iso9660 $CDROM $SOURCE_ROOT 2>>$LOG; then
384 debug "Using files from cdrom ($CDROM)..."
385 sleep 2
386 else
387 warning "Failed to mount $CDROM"
388 fi
389 }
391 # Link LiveUSB
392 check_usb()
393 {
394 # /home is on USB dev
395 if [ -d /home/boot ]; then
396 debug "Using files from USB device..."
397 ln -s /home/boot $SOURCE_ROOT/boot
398 SOURCE_STATUS="link"
399 sleep 2
400 else
401 # Try to mount LiveUSB
402 if mount $SRC_FILE $SOURCE_ROOT 2>>$LOG; then
403 debug "Using files from USB device ($SRC_FILE)..."
404 SOURCE_STATUS="mount"
405 else
406 warning "Failed to mount USB device $SRC_FILE"
407 fi
408 fi
409 }
411 # Mount ISO file
412 check_iso()
413 {
414 local src_md5
415 # Integrity check
416 src_md5=$(echo $SRC_FILE | sed 's/.iso$/.md5/')
417 if [ -r "$src_md5" ]; then
418 [ $(md5sum $SRC_FILE | cut -d' ' -f1) == $(cat "$src_md5" | cut -d' ' -f1) ] || \
419 abort 3 "$SRC-FILE corrupted"
420 else
421 warning "$src_md5 not found, unable to check integrity of $SRC_FILE"
422 fi
423 # Try to mount ISO
424 if mount -o loop -t iso9660 $SRC_FILE $SOURCE_ROOT 2>>$LOG; then
425 debug "Using files from ISO ($SRC_FILE)..."
426 sleep 2
427 else
428 warning "Failed to mount ISO '$SRC_FILE'"
429 fi
430 }
432 # Source is on the web
433 check_web()
434 {
435 local src_md5
436 debug "Downloading $SRC_FILE"
437 if wget $SRC_FILE -P /tmp; then
438 debug "Download completed."
439 else
440 warning "Failed to download '$SRC_FILE'"
441 fi
442 src_md5=$(echo $SRC_FILE | sed 's/.iso$/.md5/')
443 debug "Downloading $src_md5"
444 wget $src_md5 -P /tmp || warning "Failed to download '$src_md5'"
445 tmpfile=$(echo $SRC_FILE | awk 'BEGIN{RS="/"}{out=$1}END{printf"%s",out}')
446 SRC_FILE="/tmp/$tmpfile"
447 check_iso
448 }
450 # We may be in Tiny Web boot mode
451 check_weboot()
452 {
453 if [ -d $SRC_FILE/boot ]; then
454 debug "Using files from HTTP device..."
455 ln -s $SRC_FILE/boot $SOURCE_ROOT/boot
456 sleep 2
457 else
458 abort 3 "Error: Web boot files not found"
459 fi
460 }
462 # set up source and check Slitaz' content
463 check_source()
464 {
465 debug "Creating mount point ($SOURCE_ROOT)..."
466 mkdir -p $SOURCE_ROOT
467 sleep 1
468 case $INST_TYPE in
469 cdrom)
470 check_cdrom ;;
471 usb)
472 check_usb ;;
473 iso)
474 check_iso ;;
475 web)
476 check_web ;;
477 weboot)
478 check_cdrom
479 check_web ;;
480 *)
481 abort 8 "Internal" ;;
482 esac
484 # Exit with error msg if no rootfs.gz found.
485 debug "Checking installation media..."
486 if [ ! -f $SOURCE_ROOT/boot/rootfs.gz -a \
487 ! -f $SOURCE_ROOT/boot/rootfs1.gz ]; then
488 abort 3 "Invalid source"
489 else
490 debug "Installation media checked ok"
491 fi
492 }
494 # Tiny summary
495 summary()
496 {
497 echo "Installation settings summary:"
498 STEP=0
500 if [ "$INST_ACTION" == "install" ]; then
501 if [ -n "$TGT_FS" ]; then
502 msg "Format root partition '$TGT_PARTITION' ($TGT_FS)"
503 fi
504 if [ -n "$TGT_HOME_FS" ]; then
505 msg "Format /home partition '$TGT_HOME' ($TGT_HOME_FS)"
506 fi
507 msg "Install SliTaz on '$TGT_PARTITION' from $INST_TYPE"
508 [ -n "$TGT_HOME" ] && msg "Set Home partition to '$TGT_HOME'"
509 msg "Set Hostname as '$TGT_HOSTNAME'"
510 msg "Set Default user as '$TGT_USER'"
511 else
512 msg "Check '$TGT_PARTITION'"
513 msg "Backup /etc, /home and the packages list"
514 msg "Upgrade SliTaz on '$TGT_PARTITION' from $INST_TYPE"
515 msg "Restore /etc, /home"
516 msg "Upgrade additional packages."
517 fi
518 if [ -n "$TGT_WINBOOT" ]; then
519 msg "Enable Windows dual-boot ($TGT_WINBOOT)"
520 fi
521 if [ "$TGT_GRUB" == "yes" ]; then
522 msg "Install Grub"
523 fi
525 echo -n "Continue:(y/n)"
526 read answer
527 case $answer in
528 [yY]) echo "Running $BACKLIST.." ;;
529 *) abort 4 "Cancelled by user" ;;
530 esac
531 STEP=0
532 }
534 #######################
535 # Installer functions #
536 #######################
538 # Mount and mkfs with progress.
539 prepare_install()
540 {
541 debug "Preparing target partition..."
542 # Mount point can be already used.
543 if mount | grep -q $TARGET_ROOT; then
544 umount $TARGET_ROOT 2>>$LOG
545 fi
546 sleep 2
548 # Formatting root partition
549 case $TGT_FS in
550 "")
551 debug "The partition ($TGT_PARTITION) will be cleaned..."
552 # ROOT_FS=$(parted /dev/hda5 print -m | grep "^1:" | cut -d':' -f5) ;;
553 ROOT_FS=auto ;;
554 *)
555 msg "Formatting $TGT_PARTITION ($TGT_FS)"
556 mkfs.$TGT_FS $TGT_PARTITION >>$LOG 2>>$LOG
557 ROOT_FS=$TGT_FS ;;
558 esac
559 sleep 2
561 # Formatting /home
562 if [ -n "$TGT_HOME" ]; then
563 case $TGT_HOME_FS in
564 "")
565 debug "The partition ($TGT_HOME) will be kept..." ;;
566 *)
567 msg "Formatting /home on $TGT_HOME ($TGT_HOME_FS)"
568 mkfs.$TGT_HOME_FS -L "Home" $TGT_HOME >>$LOG 2>>$LOG ;;
569 esac
570 sleep 2
571 fi
573 # Mount target.
574 debug "Creating mount point: $TARGET_ROOT"
575 mkdir -p $TARGET_ROOT >>$LOG
576 sleep 2
578 mount -t $ROOT_FS $TGT_PARTITION $TARGET_ROOT >>$LOG 2>>$LOG
579 if [ $(mount | grep -c "mnt/target") == "0" ]; then
580 abort 5 "Can't mount $TGT_PARTITION"
581 fi
582 }
584 # Get a clean target device (15%).
585 clean_target()
586 {
587 if [ -z "$TGT_FS" ]; then
588 # partition was not formatted
589 debug "Cleaning the root partition ($TGT_PARTITION)..."
590 # Keep /home in case of reinstall.
591 cd $TARGET_ROOT || abort 8 "Internal"
592 for dir in *
593 do
594 case "$dir" in
595 home)
596 debug "keeping /home found on: $TGT_PARTITION"
597 mv home home.bak ;;
598 lost+found)
599 continue ;;
600 *)
601 debug "removing target: $dir"
602 rm -rf $dir 2>>$LOG ;;
603 esac
604 done
605 if [ -d mklost+found ]; then
606 mklost+found 2>>$LOG
607 fi
608 fi
609 sleep 2
610 }
612 # Kernel is renamed to standard vmlinuz-$VERSION.
613 install_kernel()
614 {
615 if [ -d /$TARGET_ROOT/lib/modules ]; then
616 KERNEL=$(ls /$TARGET_ROOT/lib/modules | tail -1)
617 KERNEL="vmlinuz-$KERNEL"
618 else
619 warning "Falling back to running kernel name.."
620 KERNEL=vmlinuz-`uname -r`
621 fi
622 mkdir -p $TARGET_ROOT/boot
623 cp $SOURCE_ROOT/boot/bzImage $TARGET_ROOT/boot/$KERNEL
624 debug "install_kernel: $KERNEL"
625 sleep 2
626 }
628 # Copy isolinux r/w files (not syslinux, some files are read only).
629 copy_bootloaders()
630 {
631 if [ -d "$SOURCE/ROOT/boot/isolinux" ]; then
632 debug "Copy isolinux r/w files"
633 mkdir -p $TARGET_ROOT/boot/isolinux
634 cp -a $SOURCE_ROOT/boot/isolinux/*.cfg $TARGET_ROOT/boot/isolinux
635 cp -a $SOURCE_ROOT/boot/isolinux/*.kbd $TARGET_ROOT/boot/isolinux
636 cp -a $SOURCE_ROOT/boot/isolinux/*.txt $TARGET_ROOT/boot/isolinux
637 cp -a $SOURCE_ROOT/boot/isolinux/*.bin $TARGET_ROOT/boot/isolinux
638 cp -a $SOURCE_ROOT/boot/isolinux/*.msg $TARGET_ROOT/boot/isolinux
639 cp -a $SOURCE_ROOT/boot/isolinux/*.lss $TARGET_ROOT/boot/isolinux
640 cp -a $SOURCE_ROOT/boot/isolinux/*.c32 $TARGET_ROOT/boot/isolinux
641 fi
642 }
644 need_package()
645 {
646 [ -d /var/lib/tazpkg/installed/$1 ] || tazpkg get-install $1
647 }
649 # extract packed rootfs: squashfs or cromfs
650 extract_loramfs()
651 {
652 local i
653 for i in $(cpio -idvum 2> /dev/null); do
654 case "$i" in
655 rootfs*)
656 need_package squashfs
657 if ! unsquashfs $i ; then
658 need_package cromfs
659 unmkcromfs $i squashfs-root
660 fi
661 mv -f squashfs-root/* .
662 rmdir squashfs-root
663 rm -f $i
664 esac
665 done
666 }
668 # This is a loram rootfs.gz, skip loram bootstrap and extract
669 extract_first_loramfs()
670 {
671 (zcat $1 || unlzma -c $1) | cpio -i extractfs.cpio 2> /dev/null &&
672 ( cd / ; cpio -id ) < extractfs.cpio && rm -f extractfs.cpio
673 ofs=$(awk '/07070100/ { o+=index($0,"07070100"); printf "%d\n",o/4 ; exit } { o+=1+length() }' < $1)
674 dd if=$1 skip=$(($ofs / 1024)) bs=4k count=1 2> /dev/null | \
675 ( dd skip=$(($ofs % 1024)) bs=4 2> /dev/null ; \
676 dd if=$1 skip=$((1 + ($ofs / 1024) )) bs=4k ) | extract_loramfs
677 }
679 # Extract lzma'ed or gziped rootfs.
680 extract_rootfs()
681 {
682 local isloramfs
683 isloramfs=
684 cd $TARGET_ROOT || abort 8 "Internal"
685 if [ -d $1/../fs/etc ]; then
686 # This is a tazlitobox loram (cdrom)
687 cp -a $1/../fs/. .
688 else
689 for i in $(ls $1/rootfs* | sort -r); do
690 if [ ! -d etc ]; then
691 if [ $( (zcat $i 2>/dev/null || lzma d $i -so) | wc -c) \
692 -lt $(stat -c %s $i) ]; then
693 # This is a tazlitobox loram (ram)
694 isloramfs=$i
695 extract_first_loramfs $i
696 continue
697 fi
698 fi
699 if [ -n "$isloramfs" ]; then
700 extract_loramfs < $i
701 continue
702 fi
703 ( zcat $i 2>/dev/null || lzma d $i -so || \
704 cat $i ) 2>>$LOG | cpio -idu
705 done 2>>$LOG > /dev/null
706 fi
707 cp /etc/keymap.conf etc
708 # unpack /usr (double check...)
709 if ls etc/tazlito | grep -q ".extract"; then
710 for i in etc/tazlito/*.extract; do
711 [ -f "$i" ] && . $i /media/cdrom
712 done
713 fi
714 }
716 # Pre configure freshly installed system (60 - 80%).
717 pre_config_system()
718 {
719 cd $TARGET_ROOT || abort 8 "Internal"
720 # Restore backup of existing /home if exists.
721 # (created by prepare_target_dev)
722 if [ -d home.bak ]; then
723 debug "Restoring directory: /home..."
724 rm -rf home
725 mv home.bak home
726 sleep 1
727 fi
728 # Add root device to CHECK_FS in rcS.conf to check filesystem
729 # on each boot.
730 debug "Adding $TGT_PARTITION and CHECK_FS to file /etc/rcS.conf..."
731 sed -i s#'CHECK_FS=\"\"'#"CHECK_FS=\"$TGT_PARTITION\""# etc/rcS.conf
732 sleep 2
733 # Set hostname.
734 msg "Configuring host name: $TGT_HOSTNAME"
735 echo $TGT_HOSTNAME > etc/hostname
736 }
738 # Set root passwd and create user after rootfs extraction.
739 users_settings()
740 {
741 cat > $TARGET_ROOT/users.sh << _EOF_
742 #!/bin/sh
743 echo "root:$TGT_ROOT_PWD" | chpasswd
744 adduser -D -H $TGT_USER
746 for grp in audio cdrom floppy dialout disk kmem tape tty video; do
747 if ! grep \$grp /etc/group | grep -q $TGT_USER ; then
748 grep -q \$grp /etc/group && addgroup $TGT_USER \$grp
749 fi
750 done
752 echo "$TGT_USER:$TGT_USER_PWD" | chpasswd
753 if [ ! -d /home/$TGT_USER ]; then
754 cp -a /etc/skel /home/$TGT_USER
755 cp /root/.xinitrc /home/$TGT_USER
756 mkdir -p /home/$TGT_USER/.config/slitaz
757 cp -a /etc/slitaz/applications.conf /home/$TGT_USER/.config/slitaz
758 chown -R $TGT_USER.users /home/$TGT_USER
759 # Path for user desktop files.
760 for i in /home/$TGT_USER/.local/share/applications/*.desktop
761 do
762 [ -e "$i" ] && sed -i s/"user_name"/"$TGT_USER"/g \$i
763 done
764 fi
765 # Slim default user.
766 if [ -f /etc/slim.conf ]; then
767 sed -i s/"default_user .*"/"default_user $TGT_USER"/ \
768 /etc/slim.conf
769 fi
770 _EOF_
771 chmod +x $TARGET_ROOT/users.sh
772 chroot $TARGET_ROOT ./users.sh
773 rm $TARGET_ROOT/users.sh
774 sleep 2
775 }
777 # /home can be on a separate partition. If default user exist in /home
778 # we remove default file crated by users_settings().
779 home_config()
780 {
781 debug "home_config: $TGT_HOME"
782 cd $TARGET_ROOT || abort 8 "Internal"
783 mv home/$TGT_USER tmp
784 mount $TGT_HOME home
785 if [ -d $TARGET_ROOT/home/$TGTUSER ]; then
786 rm -rf tmp/$TGT_USER
787 else
788 mv tmp/$TGT_USER home
789 fi
790 echo "$TGT_HOME /home ext3 defaults 0 2" \
791 >> etc/fstab
792 umount home
793 }
795 # Search for a Windows partition
796 win_partition()
797 {
798 msg "Enabling Windows dual-boot"
799 if [ "$TGT_WINBOOT" == "auto" ];then
800 WINBOOT=$(fdisk -l | awk '
801 BEGIN{disk=-1, found=-1, winboot=""}
802 {
803 # Counting disk
804 if ($1=="Disk"){disk++, part=-1}
805 # Counting partition
806 if (substr($1,1,4)=="/dev"){part++}
807 # Read partition Id
808 if ($2=="*"){Id=$6} else {Id=$5}
809 # Detect Windows type
810 if (Id=="7" || Id=="b"){
811 if (found){
812 # record 1st Windows partition found
813 winboot=sprintf("hd%d,%d",disk,part),found++}
814 }
815 }
816 END{printf "%s", winboot}')
817 if [ -z "$WINBOOT" ]; then
818 warning "No windows partition found. Dual-boot disabled"
819 TGT_WINBOOT=""
820 fi
821 fi
822 }
824 # Determine GRUB partition number and GRUB disk number.
825 grub_partition()
826 {
827 DISK_LETTER=${TGT_PARTITION#/dev/[h-s]d}
828 DISK_LETTER=${DISK_LETTER%[0-9]}
829 GRUB_PARTITION=$((${TGT_PARTITION#/dev/[h-s]d[a-z]}-1))
830 for disk in a b c d e f g h
831 do
832 nb=$(($nb+1))
833 if [ "$disk" = "$DISK_LETTER" ]; then
834 GRUB_DISK=$(($nb-1))
835 break
836 fi
837 done
838 GRUB_ROOT="(hd${GRUB_DISK},${GRUB_PARTITION})"
839 }
841 # Create grub conf
842 grub_config()
843 {
844 grub_partition
845 win_partition
846 # Create the target GRUB configuration.
847 mkdir -p $TARGET_ROOT/boot/grub
848 . /etc/locale.conf
849 cat > $TARGET_ROOT/boot/grub/menu.lst << _EOF_
850 # /boot/grub/menu.lst: GRUB boot loader configuration.
851 #
853 # By default, boot the first entry.
854 default 0
856 # Boot automatically after 8 secs.
857 timeout 8
859 # Graphical splash image.
860 splashimage=/boot/grub/splash.xpm.gz
862 # Change the colors.
863 #color yellow/brown light-green/black
865 # For booting SliTaz from : $TGT_PARTITION
866 #
867 title SliTaz GNU/Linux (cooking) (Kernel $KERNEL)
868 root $GRUB_ROOT
869 kernel /boot/$KERNEL root=$TGT_PARTITION lang=$LANG
871 _EOF_
872 if [ -n "$TGT_WINBOOT" ]; then
873 cat >> $TARGET_ROOT/boot/grub/menu.lst << _EOF_
874 # For booting Windows :
875 #
876 title Microsoft Windows
877 rootnoverify ($WINBOOT)
878 chainloader +1
880 _EOF_
881 fi
882 # log
883 debug "grub_config: $TARGET_ROOT/boot/grub/menu.lst"
884 echo "--- menu.lst -------------" >>$LOG
885 cat $TARGET_ROOT/boot/grub/menu.lst >>$LOG
886 echo "--- menu.lst -------------" >>$LOG
887 sleep 2
888 }
890 # Files install, calling for functions or with cmds.
891 install_files()
892 {
893 msg "Installing SliTaz on $TGT_PARTITION"
894 # saving pwd
895 local save_pwd=$(pwd)
897 debug "Cleaning the root partition if necessary..."
898 clean_target
900 debug "Extracting the root system..."
901 extract_rootfs $SOURCE_ROOT/boot
903 debug "Installing the kernel..."
904 install_kernel
906 debug "Copying the bootloader syslinux/isolinux..."
907 copy_bootloaders
909 debug "Preconfiguring the system..."
910 pre_config_system
912 msg "Configuring root and default $USER account..."
913 users_settings
915 if [ "$TGT_HOME" != "" ]; then
916 msg "Configuring $TGT_HOME to be used as /home..."
917 home_config
918 sleep 2
919 fi
921 debug "Creating configuration file for GRUB (menu.lst)..."
922 grub_config
924 debug "Files installation completed"
925 sleep 2
926 # restoring pwd
927 cd $save_pwd
928 }
930 # GRUB info with disk name used for grub-install.
931 grub_install()
932 {
933 if [ "$TGT_GRUB" == "yes" ]; then
934 TARGET_DISK=`echo $TGT_PARTITION | sed s/"[0-9]"/''/`
935 msg "Running grub-install on : $TARGET_DISK"
936 grub-install --no-floppy \
937 --root-directory=$TARGET_ROOT $TARGET_DISK 2>>$LOG
938 debug "Grub installation done..."
939 else
940 debug "Grub not installed"
941 fi
942 }
944 # Copy log file, umount target and eject cdrom.
945 umount_devices()
946 {
947 # Log files
948 debug "Copying log files ($LOG)..."
949 cp -a $LOG $TARGET_ROOT/var/log
950 sleep 2
952 # Umount target
953 if mount | grep -q $TARGET_ROOT; then
954 echo "Unmounting target ($TGT_PARTITION)..."
955 umount $TARGET_ROOT 2>>$LOG
956 fi
958 # Umount source
959 if mount | grep -q $SOURCE_ROOT; then
960 echo "Unmounting $SOURCE_ROOT"
961 umount $SOURCE_ROOT
962 fi
964 # Eject cd
965 if [ "$INST_TYPE" == "cdrom" ]; then
966 echo "Ejecting cdrom..."
967 eject
968 fi
969 sleep 2
970 }
972 # End of installation.
973 end_of_install()
974 {
975 echo "Installation complete. You can now restart (reboot) \
976 from your SliTaz GNU/Linux system."
977 echo "=== Tazinst end at `date` ===" >>$LOG
978 umount_devices
979 }
981 #####################
982 # Upgrade functions #
983 #####################
985 # Mount.
986 prepare_upgrade()
987 {
988 debug "Preparing the target partition..."
989 # Mount point can be already used.
990 if mount | grep -q $TARGET_ROOT; then
991 umount $TARGET_ROOT 2>>$LOG
992 fi
993 mkdir -p $TARGET_ROOT && sleep 2
994 # Mount target.
995 mount $TGT_PARTITION $TARGET_ROOT >>$LOG 2>>$LOG
996 if [ $(mount | grep -c "mnt/target") == "0" ]; then
997 abort 5 "Can't mount $TGT_PARTITION"
998 fi
999 }
1001 # Check for a valid SliTaz
1002 check_release()
1004 if [ -f $TARGET_ROOT/etc/slitaz-release ]; then
1005 release=`cat $TARGET_ROOT/etc/slitaz-release`
1006 msg "Preparing upgrade of SliTaz release: $release"
1007 else
1008 abort 6 "The partition '$TGT_PARTITION' doesn't appear to contain \
1009 a SliTaz system, the file: /etc/slitaz-release doesn't exist."
1010 fi && sleep 2
1013 # Backup target packages list.
1014 backup_files()
1016 cd $TARGET_ROOT || abort 8 "Internal"
1017 ls -1 var/lib/tazpkg/installed > home/packages-selection.list
1018 for dir in *
1019 do
1020 case "$dir" in
1021 boot)
1022 rm -rf boot/vmlinuz-* ;;
1023 home)
1024 mv home home.bak
1025 debug "keeping /home found on: $TGT_PARTITION" ;;
1026 etc)
1027 tar czf etc.tar.gz etc
1028 mv etc etc.bak
1029 debug "keeping /etc found on: $TGT_PARTITION" ;;
1030 var)
1031 if [ -d var/www ]; then
1032 mv var/www www.bak
1033 debug "keeping /var/www found on: $TGT_PARTITION"
1034 fi
1035 rm -rf var 2>>$LOG ;;
1036 lost+found)
1037 continue ;;
1038 *)
1039 debug "removing target: $dir"
1040 rm -rf $dir 2>>$LOG ;;
1041 esac
1042 done
1043 if [ -d mklost+found ]; then
1044 mklost+found 2>>$LOG
1045 fi
1046 sleep 2
1049 # Restore backups.
1050 restore_files()
1052 rm -rf $TARGET_ROOT/home
1053 mv $TARGET_ROOT/home.bak $TARGET_ROOT/home
1054 rm -rf $TARGET_ROOT/etc
1055 mv $TARGET_ROOT/etc.bak $TARGET_ROOT/etc
1056 if [ -d $TARGET_ROOT/www.bak ]; then
1057 rm -rf $TARGET_ROOT/var/www
1058 mv $TARGET_ROOT/www.bak $TARGET_ROOT/var/www
1059 fi
1060 debug "backups restored: `date`"
1062 # /var/lib/slitaz-installer
1063 mkdir -p $TARGET_ROOT/var/lib/tazinst
1064 mv $TARGET_ROOT/etc.tar.gz $TARGET_ROOT/var/lib/tazinst
1065 mv $TARGET_ROOT/home/packages-selection.list $TARGET_ROOT/var/lib/tazinst
1068 # Added pkgs
1069 install_pkgs()
1071 # Check if the pkg is on the mirror.
1072 debug "Checking the availability of packages..."
1073 touch packages-to-install.list
1074 packages=0
1075 diff=`cat packages-selection.diff | sort`
1076 for pkg in $diff
1077 do
1078 if grep -q ^$pkg-[0-9] /var/lib/tazpkg/packages.list; then
1079 packages=$(($packages+1))
1080 echo "$pkg" >> packages-to-install.list
1081 fi
1082 done
1084 # Calculate the percent for one package and install.
1085 debug "Installing any packages..."
1086 sleep 2
1087 if [ "$packages" == "0" ]; then
1088 debug "packages to install: 0"
1089 else
1090 onepkg=$((48/$packages))
1091 pct=50
1092 # Get-install all missing pkgs.
1093 for pkg in `cat packages-to-install.list`
1094 do
1095 pct=$(($pct+$onepkg))
1096 echo $pct
1097 debug "Installing: $pkg..."
1098 # Get install package and answer yes in case of dependencies.
1099 pkgname=`grep ^$pkg /var/lib/tazpkg/packages.list`
1100 tazpkg get $pkg >/dev/null 2>/dev/null
1101 yes "" | tazpkg install $pkgname.tazpkg --root=$TARGET_ROOT >/dev/null 2>/dev/null
1102 rm -f $pkgname.tazpkg
1103 done
1104 fi
1105 echo 100
1106 debug "Installation of packages complete..."
1107 sleep 2
1110 # Search for added pkgs
1111 update_pkgs()
1113 cd $TARGET_ROOT/var/lib/tazinst || abort 8 "Internal"
1114 # LiveCD packages list.
1115 debug "Creating package lists..."
1116 ls -1 $TARGET_ROOT/var/lib/tazpkg/installed > packages-source.list || exit 1
1117 debug "packages-source.list: done"
1118 # Diff
1119 diff packages-source.list packages-selection.list | \
1120 grep ^+[a-z] | sed s/^+// > packages-selection.diff
1121 debug "packages-selection.diff: done"
1122 # Get mirror list.
1123 tazpkg recharge >>$LOG 2>>$LOG
1124 if [ -f /var/lib/tazpkg/packages.list ]; then
1125 install_pkgs
1126 else
1127 touch packages-to-install.list
1128 warning "The list of available packages on the mirror could not be \
1129 downloaded. No missing packages will be reinstalled now, but \
1130 you can do so later by looking at the following list: \
1131 /var/lib/tazinst/packages-selection.diff"
1132 fi
1133 sleep 2
1136 # Update grub conf
1137 grub_update()
1139 # Backup and create a new grub menu.lst.
1140 if [ "$TGT_GRUB" == "yes" ]; then
1141 msg "Grub update"
1142 mv $TARGET_ROOT/boot/grub/menu.lst \
1143 $TARGET_ROOT/boot/grub/menu.lst.bak 2>/dev/null
1144 grub_config
1145 fi
1148 # Prepare the partition to upgrade, backup, install, restore configs
1149 # and reinstall pkgs.
1150 upgrade_files()
1152 # saving pwd
1153 local save_pwd=$(pwd)
1154 cd $TARGET_ROOT || abort 8 "Internal"
1156 debug "Searching for /etc/slitaz-release"
1157 check_release
1159 msg "Backup /etc, /home and the packages list..."
1160 backup_files
1162 msg "Upgrading SliTaz on $TGT_PARTITION"
1164 debug "Copying the bootloader syslinux/isolinux..."
1165 copy_bootloaders
1167 debug "Extracting the root system..."
1168 extract_rootfs $SOURCE_ROOT/boot
1170 msg "Restoring configuration files..."
1171 restore_files
1173 debug "Installing the kernel..."
1174 install_kernel
1176 msg "Upgrading added packages..."
1177 update_pkgs
1179 # restoring pwd
1180 cd $save_pwd
1183 # End of system upgrade.
1184 end_of_upgrade()
1186 pkgscd=`cat $TARGET_ROOT/var/lib/tazinst/packages-source.list | wc -l`
1187 pkginst=`cat $TARGET_ROOT/var/lib/tazinst/packages-to-install.list | wc -l`
1188 echo -e "Upgrade finished. You can now restart (reboot) \
1189 from your SliTaz GNU/Linux system.
1190 Packages on the cdrom : $pkgscd
1191 Packages installed from the mirror : $pkginst"
1192 echo "=== Tazinst end at `date` ===" >>$LOG
1193 umount_devices
1196 ######################
1197 # Installer sequence #
1198 ######################
1200 case $1 in
1201 install)
1202 INST_ACTION=install
1203 check_root
1204 init $@
1205 read_configuration_file $2
1206 check_vars
1207 check_source
1208 prepare_install
1209 install_files
1210 grub_install
1211 end_of_install ;;
1212 upgrade)
1213 INST_ACTION=upgrade
1214 check_root
1215 init $@
1216 read_configuration_file $2
1217 check_vars
1218 check_source
1219 prepare_upgrade
1220 upgrade_files
1221 grub_update
1222 end_of_upgrade ;;
1223 config)
1224 gen_config $2 ;;
1225 cli)
1226 check_root
1227 init $@
1228 check_cli_options $@
1229 check_vars
1230 check_source
1231 summary
1232 case $INST_ACTION in
1233 upgrade)
1234 prepare_upgrade
1235 upgrade_files
1236 grub_update
1237 end_of_upgrade ;;
1238 install)
1239 prepare_install
1240 install_files
1241 grub_install
1242 end_of_install ;;
1243 esac ;;
1244 usage|*)
1245 usage ;;
1246 esac
1248 exit 0