slitaz-tools view installer/tazinst @ rev 654

tazinst: gettextization
author Dominique Corbex <domcox@slitaz.org>
date Tue Jul 19 22:52:27 2011 +0200 (2011-07-19)
parents 2c63e52fec7d
children b28d4a6bf467
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>
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: No SliTaz system to upgrade found
20 # 8: Internal error
22 VERSION=3.0
24 SOURCE_ROOT=/media/source
25 TARGET_ROOT=/mnt/target
26 LOG=/var/log/tazinst.log
27 BACKLIST="SliTaz GNU/Linux installer"
29 # DEBUG=1: Enable debug msgs
30 DEBUG=0
32 # Predefined urls
33 URL_STABLE="http://mirror.slitaz.org/iso/stable/slitaz-4.0.iso"
34 URL_COOKING="http://mirror.slitaz.org/iso/cooking/slitaz-cooking.iso"
35 URL_ROLLING="http://mirror.slitaz.org/iso/rolling/slitaz-core.iso"
37 # Print a short help
38 usage()
39 {
40 echo -e "\n`gettext \"Tazinst - SliTaz installer - Version\"`: $VERSION\n"
41 echo -e "\033[1m`gettext \"Usage\"`:"
42 echo -e "\033[0m `gettext \"tazinst [command] [config-file]\n\"`"
43 echo -e "\033[1m`gettext \"Commands\"`: \033[0m"
44 echo -e "`gettext \"usage Print this short usage.\"`"
45 echo -e "`gettext \"install Install SliTaz on HDD using a configuration file.\"`"
46 echo -e "`gettext \"upgrade Upgrade SliTaz on HDD using a configuration file.\"`"
47 echo -e "`gettext \"config Generate a configuration file.\"`"
48 echo -e "`gettext \"cli Install or upgrade using command line options:\"`"
49 echo -e " -i `gettext \"Full Install (not upgrading, all present data will be lost).\"`"
50 echo -e " -u `gettext \"Upgrade (Needs an active internet connection).\"`"
51 echo -e " -t <type> `gettext \"Install type (cdrom|usb|iso|web|weboot).\"`"
52 echo -e " -s <source> `gettext \"Source media (ex: file.iso|usb partition|web url).\"`"
53 echo -e " -p <partition> `gettext \"Partition where SliTaz will be installed (ex:/dev/hda3).\"`"
54 echo -e " -f <fs> `gettext \"Partition to be formatted (fs=ext2|ext3|ext4|etc..).\"`"
55 echo -e " -g `gettext \"Install Grub.\"`"
56 echo -e " -w `gettext \"Dual-boot a Windows partition (auto|hd<disk>,<part> ex:hd0,0).\"`"
57 echo -e " -d `gettext \"Debug mode.\"`"
58 exit 0
59 }
61 # Print an error msg & exit
62 # $1: exit code
63 # $@: err msg
64 abort()
65 {
66 # unmouting source & target
67 if mount | grep -q $SOURCE_ROOT; then
68 umount $SOURCE_ROOT 2>>$LOG
69 fi
70 if mount | grep -q $TARGET_ROOT; then
71 umount $TARGET_ROOT 2>>$LOG
72 fi
73 echo -e "`gettext \"Error\"` $@"
74 echo -e "`gettext \"Installation cancelled\"`"
75 test $(id -u) = 0 && echo "Installation cancelled on error $@" >> $LOG
76 exit $1
77 }
79 # Print a warning msg
80 warning()
81 {
82 echo -e "`gettext \"Warning:\"` $@" | tee -a $LOG
83 }
85 # Print a debug msg
86 debug()
87 {
88 [ $DEBUG -gt 0 ] && echo -e "\033[1mDEBUG: \033[0m$1"
89 echo "DEBUG: $1" >>$LOG
90 }
92 # print a msg
93 msg()
94 {
95 STEP=$(($STEP+1))
96 echo "$STEP. $@" | tee -a $LOG
97 }
99 ########################
100 # Gen-config functions #
101 ########################
103 # Generate a config file
104 # $1: Configuration file
105 gen_config()
106 {
107 CONFILE=$1
108 touch $CONFILE || abort 2 `gettext "Unable to write to $CONFILE"`
109 if [ -r "$CONFILE" ]; then
110 cat > $CONFILE << _EOF_
111 # tazinst.conf: SliTaz Installer configuration file.
112 #
114 # Install type : [cdrom|usb|iso|web|weboot]
115 INST_TYPE="cdrom"
117 # Install source
118 # usb:/dev/xxx, ex: SRC_FILE=/dev/sdb1
119 # iso:file.iso, ex: SRC_FILE=~/slitaz.3.0.iso
120 # web: url, ex: SRC_FILE=http://mirror.slitaz.org/iso/cooking/slitaz-cooking.iso
121 # web: predefined mirrors (stable|cooking|rolling), ex: SRC_FILE=cooking
122 SRC_FILE=""
124 # Install Target (Root Partition).
125 TGT_PARTITION="/dev/hda5"
127 # Target File system.
128 # SliTaz uses ext3 by default but another filesystem can be used if wanted,
129 # for this please adjust your /etc/fstab after installation. Valid options are:
130 # (btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
131 TGT_FS="ext3"
133 # Home partition.
134 # On most GNU/Linux systems users personal files are stored in the directory
135 # /home. Home can be on another hard disk or on a separate partition.
136 TGT_HOME=""
137 # Home File system (if /home is on a separate partition)
138 TGT_HOME_FS=""
140 # Hostname
141 TGT_HOSTNAME="slitaz"
143 # root password
144 # The root administrator privilege lets you manage and configure the full
145 # system. A root user can damage your system so you should always setup a
146 # strong password with special characters and/or numbers.
147 TGT_ROOT_PWD="root"
149 # The default user for the system will have his personal files stored
150 # in /home/*user* (and will be automatically added to the audio group).
151 TGT_USER="tux"
152 TGT_USER_PWD=""
154 # Grub bootloader
155 # install grub [yes|no]
156 TGT_GRUB="no"
158 # Windows dual-boot
159 # Dual boot is disabled if WINBOOT is empty: TGT_WINBOOT=""
160 # You may let tazinst find your win partition, mode=auto: TGT_WINBOOT="auto"
161 # or use manual setting: "hd[disk],[partition]" ex:TGT_WINBOOT=hd0,0
162 TGT_WINBOOT=""
164 _EOF_
166 else
167 abort 2 `gettext "Configuration file $CONFILE not found"`
168 fi
169 }
171 ######################
172 # Checking functions #
173 ######################
175 # def values and start log
176 # $@ :
177 init()
178 {
179 echo "=== Tazinst start at `date` ===" >$LOG
180 echo "Command: $0 $@" >>$LOG
182 # Default Type
183 INST_TYPE=cdrom
184 # Default Hostname.
185 TGT_HOSTNAME=slitaz
186 # Default root passwd
187 TGT_ROOT_PWD=root
188 # Default user
189 TGT_USER=tux
190 # Default Grub Install
191 TGT_GRUB=no
192 }
194 # Read Conf
195 # $1: conf file
196 read_configuration_file()
197 {
198 CONFILE=$1
199 if [ -n "$CONFILE" ]; then
200 if [ -r "$CONFILE" ]; then
201 debug "Using config-file=$CONFILE"
202 # source doesn't like file without a path
203 [ $(echo "$CONFILE" | grep -c "/") == "0" ] && CONFILE="./$CONFILE"
204 source $CONFILE || abort 2 `gettext "Unable to read $CONFILE"`
205 else
206 abort 2 `gettext "Configuration file not found"`
207 fi
208 else
209 abort 2 `gettext "No configuration file provided"`
210 fi
211 }
213 # Cli options
214 # $@: tazinst parms
215 check_cli_options()
216 {
217 shift
218 while getopts "c:df:ghip:s:t:uw:" opt ; do
219 case $opt in
220 d) DEBUG=1 ;;
221 f) TGT_FS=$OPTARG ;;
222 g) TGT_GRUB="yes" ;;
223 h) usage ;;
224 i) INSTALL="install" ;;
225 p) TGT_PARTITION=$OPTARG ;;
226 s) SRC_FILE=$OPTARG ;;
227 t) INST_TYPE=$OPTARG ;;
228 u) UPGRADE="upgrade" ;;
229 w) TGT_WINBOOT=$OPTARG ;;
230 [?]) abort 1 ;;
231 esac
232 done
234 # Duplicate install modes are forbidden
235 [ -n "$INSTALL" ] && [ -n "$UPGRADE" ] && abort 1 `gettext "Mismatched parameters"`
236 INST_ACTION="$INSTALL$UPGRADE"
237 # Don't allow formatting on upgrades
238 [ "$INST_ACTION" == "upgrade" ] && [ -n "$TGT_FS" ] && \
239 warning `gettext "Partition $TGT_PARTITION will not be formatted during upgrade"`
240 }
242 # check main vars
243 check_vars()
244 {
245 # error handling
246 local error=no
247 local found=no
248 local partition=""
250 debug "--- Tazinst main options ---"
251 debug "action=$INST_ACTION"
252 debug "type=$INST_TYPE"
253 debug "source=$SRC_FILE"
254 debug "/ partition=$TGT_PARTITION"
255 debug "/ filesystem=$TGT_FS"
256 debug "/home partition=$TGT_HOME"
257 debug "/home filesystem=$TGT_HOME_FS"
258 debug "hostname=$TGT_HOSTNAME"
259 debug "root-pwd=$TGT_ROOT_PWD"
260 debug "user=$TGT_USER"
261 debug "user-pwd=$TGT_USER_PWD"
262 debug "grub=$TGT_GRUB"
263 debug "winboot=$TGT_WINBOOT"
264 debug "--------------------------------------"
266 # Check Action
267 case $INST_ACTION in
268 install|upgrade) ;;
269 *) msg `gettext "INST_ACTION=$INST_ACTION: Invalid setting"`; error=yes ;;
270 esac
272 # Check Type
273 case $INST_TYPE in
274 cdrom|weboot) ;;
275 usb|iso|web)
276 # We need a valid source
277 if [ -z "$SRC_FILE" ]; then
278 msg `gettext "Type is $INST_TYPE, but INST_SOURCE is not set"`; error=yes
279 fi ;;
280 *) msg `gettext "INST_TYPE=$INST_TYPE: Invalid setting"`; error=yes ;;
281 esac
283 # Check Source file
284 # 1. assign predefs
285 if [ "$INST_TYPE" == "web" ]; then
286 [ "$SRC_FILE" == "stable" ] && SRC_FILE=$URL_STABLE
287 [ "$SRC_FILE" == "cooking" ] && SRC_FILE=$URL_COOKING
288 [ "$SRC_FILE" == "rolling" ] && SRC_FILE=$URL_ROLLING
289 fi
290 # 2. check avail.
291 case $INST_TYPE in
292 iso)
293 if [ ! -r "$SRC_FILE" ]; then
294 msg `gettext "SRC_FILE=$SRC_FILE: file not found"`; error=yes
295 fi ;;
296 web)
297 if [ $(wget -sq "$SRC_FILE") ]; then
298 msg `gettext "SRC_FILE=$SRC_FILE: file not found on the web"`; error=yes
299 fi ;;
300 esac
302 # Check Target Partition
303 found=no
304 LIST_PARTITION=$(fdisk -l | awk '/^\/dev/{printf "%s ",$1}')
305 for partition in $LIST_PARTITION; do
306 [ "$partition" == "$TGT_PARTITION" ] && found="yes"
307 done
308 if [ "$found" != "yes" ]; then
309 msg `gettext "TGT_PARTITION=$TGT_PARTITION Partition not found"`; error=yes
310 fi
312 # Check Filesystem
313 case $TGT_FS in
314 "") ;;
315 btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
316 found=no
317 for xdir in /sbin /usr/sbin /usr/bin; do
318 [ -x "$xdir/mkfs.$TGT_FS" ] && found=yes
319 done
320 if [ "$found" == "no" ]; then
321 msg `gettext "$TGT_FS: mkfs.$TGT_FS is not installed."`; error=yes
322 fi ;;
323 *) msg `gettext "TGT_FS=$TGT_FS: Unknown filesystem"`; error=yes ;;
324 esac
326 # Check Home partition
327 if [ -n "$TGT_HOME" ]; then
328 found=no
329 for partition in $LIST_PARTITION; do
330 [ "$partition" == "$TGT_HOME" ] && found=yes
331 done
332 if [ "$found" != "yes" ]; then
333 msg `gettext "TGT_HOME=$TGT_HOME: Partition not found"`; error=yes
334 fi
335 fi
337 # Check Home Filesystem
338 case $TGT_HOME_FS in
339 "") ;;
340 btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
341 found=no
342 for xdir in /sbin /usr/sbin /usr/bin; do
343 [ -x "$xdir/mkfs.$TGT_FS" ] && found=yes
344 done
345 if [ "$found" == "no" ]; then
346 msg `gettext "$TGT_FS: mkfs.$TGT_FS is not installed."`; error=yes
347 fi ;;
348 *) msg `gettext "TGT_HOME_FS=$TGT_HOME_FS: unknown filesystem"`; error=yes ;;
349 esac
351 # Check Grub
352 case $TGT_GRUB in
353 yes|no) ;;
354 *) msg `gettext "TGT_GRUB=$TGT_GRUB: Invalid setting"`; error=yes ;;
355 esac
357 # Check Winboot
358 case $TGT_WINBOOT in
359 "") ;;
360 auto) ;;
361 hd[[:digit:]],[[:digit:]]) ;;
362 *) msg `gettext "TGT_WINBOOT=$TGT_WINBOOT: Invalid format"`; error=yes ;;
363 esac
365 # Stop on error
366 [ "$error" == "yes" ] && abort 1
367 }
369 # Exit install if user is not root.
370 check_root()
371 {
372 if test $(id -u) != 0 ; then
373 echo -e "`gettext \"You must be the root user (system administrator) to install SliTaz, \
374 please use 'su' to get a root SHell and restart installation.\"`"
375 exit 0
376 fi
377 }
379 # Mount cdrom
380 check_cdrom()
381 {
382 # Set device name
383 DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3` [ -n "$DRIVE_NAME" ] || DRIVE_NAME=cdrom
384 CDROM=/dev/$DRIVE_NAME
385 # Try to mount a cdrom
386 if mount -t iso9660 $CDROM $SOURCE_ROOT 2>>$LOG; then
387 debug "Using files from cdrom ($CDROM)..."
388 sleep 2
389 else
390 warning `gettext "Failed to mount $CDROM"`
391 fi
392 }
394 # Link LiveUSB
395 check_usb()
396 {
397 # /home is on USB dev
398 if [ -d /home/boot ]; then
399 debug "Using files from USB device..."
400 ln -s /home/boot $SOURCE_ROOT/boot
401 SOURCE_STATUS="link"
402 sleep 2
403 else
404 # Try to mount LiveUSB
405 if mount $SRC_FILE $SOURCE_ROOT 2>>$LOG; then
406 debug "Using files from USB device ($SRC_FILE)..."
407 SOURCE_STATUS="mount"
408 else
409 warning `gettext "Failed to mount USB device $SRC_FILE"`
410 fi
411 fi
412 }
414 # Mount ISO file
415 check_iso()
416 {
417 local src_md5
418 # Integrity check
419 src_md5=$(echo $SRC_FILE | sed 's/.iso$/.md5/')
420 if [ -r "$src_md5" ]; then
421 [ $(md5sum $SRC_FILE | cut -d' ' -f1) == $(cat "$src_md5" | cut -d' ' -f1) ] || \
422 abort 3 `gettext "$SRC-FILE corrupted"`
423 else
424 warning `gettext "$src_md5 not found, unable to check integrity of $SRC_FILE"`
425 fi
426 # Try to mount ISO
427 if mount -o loop -t iso9660 $SRC_FILE $SOURCE_ROOT 2>>$LOG; then
428 debug "Using files from ISO ($SRC_FILE)..."
429 sleep 2
430 else
431 warning `gettext "Failed to mount ISO '$SRC_FILE'"`
432 fi
433 }
435 # Source is on the web
436 check_web()
437 {
438 local src_md5
439 debug "Downloading $SRC_FILE"
440 if wget $SRC_FILE -P /tmp; then
441 debug "Download completed."
442 else
443 warning `gettext "Failed to download '$SRC_FILE'"`
444 fi
445 src_md5=$(echo $SRC_FILE | sed 's/.iso$/.md5/')
446 debug "Downloading $src_md5"
447 wget $src_md5 -P /tmp || warning `gettext "Failed to download '$src_md5'"`
448 tmpfile=$(echo $SRC_FILE | awk 'BEGIN{RS="/"}{out=$1}END{printf"%s",out}')
449 SRC_FILE="/tmp/$tmpfile"
450 check_iso
451 }
453 # We may be in Tiny Web boot mode
454 check_weboot()
455 {
456 if [ -d $SRC_FILE/boot ]; then
457 debug "Using files from HTTP device..."
458 ln -s $SRC_FILE/boot $SOURCE_ROOT/boot
459 sleep 2
460 else
461 abort 3 `gettext "Web boot files not found"`
462 fi
463 }
465 # set up source and check Slitaz' content
466 check_source()
467 {
468 debug "Creating mount point ($SOURCE_ROOT)..."
469 mkdir -p $SOURCE_ROOT
470 sleep 1
471 case $INST_TYPE in
472 cdrom)
473 check_cdrom ;;
474 usb)
475 check_usb ;;
476 iso)
477 check_iso ;;
478 web)
479 check_web ;;
480 weboot)
481 check_cdrom
482 check_web ;;
483 *)
484 abort 8 `gettext "Internal"` ;;
485 esac
487 # Exit with error msg if no rootfs.gz found.
488 debug "Checking installation media..."
489 if [ ! -f $SOURCE_ROOT/boot/rootfs.gz -a \
490 ! -f $SOURCE_ROOT/boot/rootfs1.gz ]; then
491 abort 3 `gettext "Invalid source"`
492 else
493 debug "Installation media checked ok"
494 fi
495 }
497 # Tiny summary
498 summary()
499 {
500 echo -e "`gettext \"Installation settings summary:\"`"
501 STEP=0
503 if [ "$INST_ACTION" == "install" ]; then
504 if [ -n "$TGT_FS" ]; then
505 msg `gettext "Format root partition '$TGT_PARTITION' ($TGT_FS)"`
506 fi
507 if [ -n "$TGT_HOME_FS" ]; then
508 msg `gettext "Format /home partition '$TGT_HOME' ($TGT_HOME_FS)"`
509 fi
510 msg `gettext "Install SliTaz on '$TGT_PARTITION' from $INST_TYPE"`
511 [ -n "$TGT_HOME" ] && msg `gettext "Set Home partition to '$TGT_HOME'"`
512 msg `gettext "Set Hostname as '$TGT_HOSTNAME'"`
513 msg `gettext "Set Default user as '$TGT_USER'"`
514 else
515 msg `gettext "Check '$TGT_PARTITION'"`
516 msg `gettext "Backup /etc, /home and the packages list"`
517 msg `gettext "Upgrade SliTaz on '$TGT_PARTITION' from $INST_TYPE"`
518 msg `gettext "Restore /etc, /home"`
519 msg `gettext "Upgrade additional packages."`
520 fi
521 if [ -n "$TGT_WINBOOT" ]; then
522 msg `gettext "Enable Windows dual-boot ($TGT_WINBOOT)"`
523 fi
524 if [ "$TGT_GRUB" == "yes" ]; then
525 msg `gettext "Install Grub"`
526 fi
528 echo -ne "`gettext \"Continue:(y/n)\"`"
529 read answer
530 case $answer in
531 [yY]) echo -e "`gettext \"Running $BACKLIST..\"`" ;;
532 *) abort 4 `gettext "Cancelled by user"` ;;
533 esac
534 STEP=0
535 }
537 #######################
538 # Installer functions #
539 #######################
541 # Mount and mkfs with progress.
542 prepare_install()
543 {
544 debug "Preparing target partition..."
545 # Target may be used
546 mount | grep -q $TGT_PARTITION && \
547 abort 5 `gettext $TGT_PARTITION": Partition in use"`
548 # Mount point can be already used.
549 if mount | grep -q $TARGET_ROOT; then
550 umount $TARGET_ROOT 2>>$LOG
551 fi
552 sleep 2
554 # Formatting root partition
555 case $TGT_FS in
556 "")
557 debug "The partition ($TGT_PARTITION) will be cleaned..."
558 # ROOT_FS=$(parted /dev/hda5 print -m | grep "^1:" | cut -d':' -f5) ;;
559 ROOT_FS=auto ;;
560 *)
561 msg `gettext "Formatting $TGT_PARTITION ($TGT_FS)"`
562 mkfs.$TGT_FS $TGT_PARTITION >>$LOG 2>>$LOG
563 ROOT_FS=$TGT_FS ;;
564 esac
565 sleep 2
567 # Formatting /home
568 if [ -n "$TGT_HOME" ]; then
569 case $TGT_HOME_FS in
570 "")
571 debug "The partition ($TGT_HOME) will be kept..." ;;
572 *)
573 msg `gettext "Formatting /home on $TGT_HOME ($TGT_HOME_FS)"`
574 mkfs.$TGT_HOME_FS -L "Home" $TGT_HOME >>$LOG 2>>$LOG ;;
575 esac
576 sleep 2
577 fi
579 # Mount target.
580 debug "Creating mount point: $TARGET_ROOT"
581 mkdir -p $TARGET_ROOT >>$LOG
582 sleep 2
584 mount -t $ROOT_FS $TGT_PARTITION $TARGET_ROOT >>$LOG 2>>$LOG
585 if [ $(mount | grep -c "mnt/target") == "0" ]; then
586 abort 5 `gettext "Can't mount $TGT_PARTITION"`
587 fi
588 }
590 # Get a clean target device (15%).
591 clean_target()
592 {
593 if [ -z "$TGT_FS" ]; then
594 # partition was not formatted
595 debug "Cleaning the root partition ($TGT_PARTITION)..."
596 # Keep /home in case of reinstall.
597 cd $TARGET_ROOT || abort 8 `gettext "Internal"`
598 for dir in *
599 do
600 case "$dir" in
601 home)
602 debug "keeping /home found on: $TGT_PARTITION"
603 mv home home.bak ;;
604 lost+found)
605 continue ;;
606 *)
607 debug "removing target: $dir"
608 rm -rf $dir 2>>$LOG ;;
609 esac
610 done
611 if [ -d mklost+found ]; then
612 mklost+found 2>>$LOG
613 fi
614 fi
615 sleep 2
616 }
618 # Kernel is renamed to standard vmlinuz-$VERSION.
619 install_kernel()
620 {
621 if [ -d /$TARGET_ROOT/lib/modules ]; then
622 KERNEL=$(ls /$TARGET_ROOT/lib/modules | tail -1)
623 KERNEL="vmlinuz-$KERNEL"
624 else
625 warning `gettext "Falling back to running kernel name.."`
626 KERNEL=vmlinuz-`uname -r`
627 fi
628 mkdir -p $TARGET_ROOT/boot
629 cp $SOURCE_ROOT/boot/bzImage $TARGET_ROOT/boot/$KERNEL
630 debug "install_kernel: $KERNEL"
631 sleep 2
632 }
634 # Copy isolinux r/w files (not syslinux, some files are read only).
635 copy_bootloaders()
636 {
637 if [ -d "$SOURCE/ROOT/boot/isolinux" ]; then
638 debug "Copy isolinux r/w files"
639 mkdir -p $TARGET_ROOT/boot/isolinux
640 cp -a $SOURCE_ROOT/boot/isolinux/*.cfg $TARGET_ROOT/boot/isolinux
641 cp -a $SOURCE_ROOT/boot/isolinux/*.kbd $TARGET_ROOT/boot/isolinux
642 cp -a $SOURCE_ROOT/boot/isolinux/*.txt $TARGET_ROOT/boot/isolinux
643 cp -a $SOURCE_ROOT/boot/isolinux/*.bin $TARGET_ROOT/boot/isolinux
644 cp -a $SOURCE_ROOT/boot/isolinux/*.msg $TARGET_ROOT/boot/isolinux
645 cp -a $SOURCE_ROOT/boot/isolinux/*.lss $TARGET_ROOT/boot/isolinux
646 cp -a $SOURCE_ROOT/boot/isolinux/*.c32 $TARGET_ROOT/boot/isolinux
647 fi
648 }
650 need_package()
651 {
652 [ -d /var/lib/tazpkg/installed/$1 ] || tazpkg get-install $1
653 }
655 # extract packed rootfs: squashfs or cromfs
656 extract_loramfs()
657 {
658 local i
659 for i in $(cpio -idvum 2> /dev/null); do
660 case "$i" in
661 rootfs*)
662 need_package squashfs
663 if ! unsquashfs $i ; then
664 need_package cromfs
665 unmkcromfs $i squashfs-root
666 fi
667 mv -f squashfs-root/* .
668 rmdir squashfs-root
669 rm -f $i
670 esac
671 done
672 }
674 # This is a loram rootfs.gz, skip loram bootstrap and extract
675 extract_first_loramfs()
676 {
677 (zcat $1 || unlzma -c $1) | cpio -i extractfs.cpio 2> /dev/null &&
678 ( cd / ; cpio -id ) < extractfs.cpio && rm -f extractfs.cpio
679 ofs=$(awk '/07070100/ { o+=index($0,"07070100"); printf "%d\n",o/4 ; exit } { o+=1+length() }' < $1)
680 dd if=$1 skip=$(($ofs / 1024)) bs=4k count=1 2> /dev/null | \
681 ( dd skip=$(($ofs % 1024)) bs=4 2> /dev/null ; \
682 dd if=$1 skip=$((1 + ($ofs / 1024) )) bs=4k ) | extract_loramfs
683 }
685 # Extract lzma'ed or gziped rootfs.
686 extract_rootfs()
687 {
688 local isloramfs
689 isloramfs=
690 cd $TARGET_ROOT || abort 8 `gettext "Internal"`
691 if [ -d $1/../fs/etc ]; then
692 # This is a tazlitobox loram (cdrom)
693 cp -a $1/../fs/. .
694 else
695 for i in $(ls $1/rootfs* | sort -r); do
696 if [ ! -d etc ]; then
697 if [ $( (zcat $i 2>/dev/null || lzma d $i -so) | wc -c) \
698 -lt $(stat -c %s $i) ]; then
699 # This is a tazlitobox loram (ram)
700 isloramfs=$i
701 extract_first_loramfs $i
702 continue
703 fi
704 fi
705 if [ -n "$isloramfs" ]; then
706 extract_loramfs < $i
707 continue
708 fi
709 ( zcat $i 2>/dev/null || lzma d $i -so || \
710 cat $i ) 2>>$LOG | cpio -idu
711 done 2>>$LOG > /dev/null
712 fi
713 cp /etc/keymap.conf etc
714 # unpack /usr (double check...)
715 if ls etc/tazlito | grep -q ".extract"; then
716 for i in etc/tazlito/*.extract; do
717 [ -f "$i" ] && . $i /media/cdrom
718 done
719 fi
720 }
722 # Pre configure freshly installed system (60 - 80%).
723 pre_config_system()
724 {
725 cd $TARGET_ROOT || abort 8 `gettext "Internal"`
726 # Restore backup of existing /home if exists.
727 # (created by prepare_target_dev)
728 if [ -d home.bak ]; then
729 debug "Restoring directory: /home..."
730 rm -rf home
731 mv home.bak home
732 sleep 1
733 fi
734 # Add root device to CHECK_FS in rcS.conf to check filesystem
735 # on each boot.
736 debug "Adding $TGT_PARTITION and CHECK_FS to file /etc/rcS.conf..."
737 sed -i s#'CHECK_FS=\"\"'#"CHECK_FS=\"$TGT_PARTITION\""# etc/rcS.conf
738 sleep 2
739 # Set hostname.
740 msg `gettext "Configuring host name: $TGT_HOSTNAME"`
741 echo $TGT_HOSTNAME > etc/hostname
742 }
744 # Set root passwd and create user after rootfs extraction.
745 users_settings()
746 {
747 cat > $TARGET_ROOT/users.sh << _EOF_
748 #!/bin/sh
749 echo "root:$TGT_ROOT_PWD" | chpasswd > /etc/null
750 adduser -D -H $TGT_USER
752 for grp in audio cdrom floppy dialout disk kmem tape tty video; do
753 if ! grep \$grp /etc/group | grep -q $TGT_USER ; then
754 grep -q \$grp /etc/group && addgroup $TGT_USER \$grp
755 fi
756 done
758 echo "$TGT_USER:$TGT_USER_PWD" | chpasswd > /etc/null
759 if [ ! -d /home/$TGT_USER ]; then
760 cp -a /etc/skel /home/$TGT_USER
761 [ -e /root/.xinitrc ] && cp /root/.xinitrc /home/$TGT_USER
762 mkdir -p /home/$TGT_USER/.config/slitaz
763 cp -a /etc/slitaz/applications.conf /home/$TGT_USER/.config/slitaz
764 # chmod $TGT_USER:user doesn't work with most ISO
765 if grep -q ^users: /etc/group; then
766 chown -R $TGT_USER:users /home/$TGT_USER
767 else
768 chown -R $TGT_USER:$TGT_USER /home/$TGT_USER
769 fi
770 # Path for user desktop files.
771 for i in /home/$TGT_USER/.local/share/applications/*.desktop
772 do
773 [ -e "$i" ] && sed -i s/"user_name"/"$TGT_USER"/g \$i
774 done
775 fi
776 # Slim default user.
777 if [ -f /etc/slim.conf ]; then
778 sed -i s/"default_user .*"/"default_user $TGT_USER"/ \
779 /etc/slim.conf
780 fi
781 _EOF_
782 chmod +x $TARGET_ROOT/users.sh
783 chroot $TARGET_ROOT ./users.sh
784 rm $TARGET_ROOT/users.sh
785 sleep 2
786 }
788 # /home can be on a separate partition. If default user exists in /home
789 # we remove default file created by users_settings().
790 home_config()
791 {
792 debug "home_config: $TGT_HOME"
793 cd $TARGET_ROOT || abort 8 `gettext "Internal"`
794 mv home/$TGT_USER tmp
795 mount $TGT_HOME home
796 if [ -d $TARGET_ROOT/home/$TGTUSER ]; then
797 rm -rf tmp/$TGT_USER
798 else
799 mv tmp/$TGT_USER home
800 fi
801 echo "$TGT_HOME /home ext3 defaults 0 2" \
802 >> etc/fstab
803 umount home
804 }
806 # Search for a Windows partition
807 win_partition()
808 {
809 # msg `gettext "Searching for Windows"`
810 if [ "$TGT_WINBOOT" == "auto" ];then
811 WINBOOT=$(fdisk -l | awk '
812 BEGIN{disk=-1, found=-1, winboot=""}
813 {
814 # Counting disk
815 if ($1=="Disk"){disk++, part=-1}
816 # Counting partition
817 if (substr($1,1,4)=="/dev"){part++}
818 # Read partition Id
819 if ($2=="*"){Id=$6} else {Id=$5}
820 # Detect Windows type
821 if (Id=="7" || Id=="b"){
822 if (found){
823 # record 1st Windows partition found
824 winboot=sprintf("hd%d,%d",disk,part),found++}
825 }
826 }
827 END{printf "%s", winboot}')
828 if [ -z "$WINBOOT" ]; then
829 warning `gettext "No windows partition found. Dual-boot disabled"`
830 TGT_WINBOOT=""
831 fi
832 fi
833 }
835 # Determine GRUB partition number and GRUB disk number.
836 grub_partition()
837 {
838 DISK_LETTER=${TGT_PARTITION#/dev/[h-s]d}
839 DISK_LETTER=${DISK_LETTER%[0-9]}
840 GRUB_PARTITION=$((${TGT_PARTITION#/dev/[h-s]d[a-z]}-1))
841 for disk in a b c d e f g h
842 do
843 nb=$(($nb+1))
844 if [ "$disk" = "$DISK_LETTER" ]; then
845 GRUB_DISK=$(($nb-1))
846 break
847 fi
848 done
849 GRUB_ROOT="(hd${GRUB_DISK},${GRUB_PARTITION})"
850 }
852 # Create grub conf
853 grub_config()
854 {
855 grub_partition
856 if [ "$TGT_GRUB" == "yes" ]; then
857 win_partition
858 fi
859 # Create the target GRUB configuration.
860 mkdir -p $TARGET_ROOT/boot/grub
861 . /etc/locale.conf
862 cat > $TARGET_ROOT/boot/grub/menu.lst << _EOF_
863 # /boot/grub/menu.lst: GRUB boot loader configuration.
864 #
866 # By default, boot the first entry.
867 default 0
869 # Boot automatically after 8 secs.
870 timeout 8
872 # Graphical splash image.
873 splashimage=/boot/grub/splash.xpm.gz
875 # Change the colors.
876 #color yellow/brown light-green/black
878 # For booting SliTaz from : $TGT_PARTITION
879 #
880 title SliTaz GNU/Linux (cooking) (Kernel $KERNEL)
881 root $GRUB_ROOT
882 kernel /boot/$KERNEL root=$TGT_PARTITION lang=$LANG
884 _EOF_
885 if [ -n "$TGT_WINBOOT" ]; then
886 msg `gettext "Enabling Windows dual-boot"`
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 `gettext "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 `gettext "Configuring root and default $TGT_USER account..."`
927 users_settings
929 if [ "$TGT_HOME" != "" ]; then
930 msg `gettext "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 `gettext "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 -e "`gettext \"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 -e "`gettext \"Unmounting $SOURCE_ROOT\"`"
970 umount $SOURCE_ROOT
971 fi
973 # Eject cd
974 if [ "$INST_TYPE" == "cdrom" ]; then
975 echo -e "`gettext \"Ejecting cdrom...\"`"
976 eject
977 fi
978 sleep 2
979 }
981 # End of installation.
982 end_of_install()
983 {
984 echo -e "`gettext \"Installation complete. You can now restart (reboot) \"`"
985 echo -e "`gettext \"from your SliTaz GNU/Linux system.\"`"
986 echo "=== Tazinst end at `date` ===" >>$LOG
987 # Log files
988 echo -e "`gettext \"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 `gettext "$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 `gettext "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 `gettext "Preparing upgrade of SliTaz release: $release"`
1024 else
1025 abort 6 `gettext "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 `gettext "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 `gettext "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 `gettext "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 `gettext "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 `gettext "Internal"`
1173 debug "Searching for /etc/slitaz-release"
1174 check_release
1176 msg `gettext "Backup /etc, /home and the packages list..."`
1177 backup_files
1179 msg `gettext "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 `gettext "Restoring configuration files..."`
1188 restore_files
1190 debug "Installing the kernel..."
1191 install_kernel
1193 msg `gettext "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 "`gettext \"Upgrade finished. You can now restart (reboot) from your SliTaz GNU/Linux system.\"`"
1206 echo -e "`gettext \"Packages on the cdrom :\"`" $pkgscd
1207 echo -e "`gettext \"Packages installed from the mirror :\"`" $pkginst
1208 echo "=== Tazinst end at `date` ===" >>$LOG
1209 umount_devices
1212 ######################
1213 # Installer sequence #
1214 ######################
1216 case $1 in
1217 install)
1218 INST_ACTION=install
1219 check_root
1220 init $@
1221 read_configuration_file $2
1222 check_vars
1223 check_source
1224 prepare_install
1225 install_files
1226 grub_install
1227 end_of_install ;;
1228 upgrade)
1229 INST_ACTION=upgrade
1230 check_root
1231 init $@
1232 read_configuration_file $2
1233 check_vars
1234 check_source
1235 prepare_upgrade
1236 upgrade_files
1237 grub_update
1238 end_of_upgrade ;;
1239 config)
1240 gen_config $2 ;;
1241 cli)
1242 check_root
1243 init $@
1244 check_cli_options $@
1245 check_vars
1246 check_source
1247 summary
1248 case $INST_ACTION in
1249 upgrade)
1250 prepare_upgrade
1251 upgrade_files
1252 grub_update
1253 end_of_upgrade ;;
1254 install)
1255 prepare_install
1256 install_files
1257 grub_install
1258 end_of_install ;;
1259 esac ;;
1260 usage|*)
1261 usage ;;
1262 esac
1264 exit 0