slitaz-tools view installer/tazinst @ rev 660

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