slitaz-tools view installer/tazinst @ rev 615

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