slitaz-tools view installer/tazinst @ rev 661

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