slitaz-tools view installer/tazinst @ rev 678

tazinst: let user choose language on first boot and be quiet
author Christophe Lincoln <pankso@slitaz.org>
date Wed Feb 22 16:39:16 2012 +0100 (2012-02-22)
parents 2b69f8b15b5a
children be257190624e
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: Setup 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 # 7: Another instance is running
21 # 8: Internal error
23 VERSION=3.30
25 # Internationalization
26 . /usr/bin/gettext.sh
27 TEXTDOMAIN='tazinst'
28 export TEXTDOMAIN
30 SOURCE_ROOT=/media/source
31 TARGET_ROOT=/mnt/target
32 LOG=/var/log/tazinst.log
33 LOCK=/run/tazinst.pid
34 BACKLIST="SliTaz GNU/Linux installer"
36 # DEBUG=1: Enable debug msgs
37 DEBUG=0
39 # Predefined urls
40 URL_STABLE="http://mirror.slitaz.org/iso/stable/slitaz-4.0.iso"
41 URL_COOKING="http://mirror.slitaz.org/iso/cooking/slitaz-cooking.iso"
42 URL_ROLLING="http://mirror.slitaz.org/iso/rolling/slitaz-core.iso"
44 # Tazinst conf
45 [ -r /etc/slitaz/tazinst.conf ] && . /etc/slitaz/tazinst.conf
47 # Print a short help
48 usage()
49 {
50 cat <<EOT
51 $(echo -e "\n$(gettext "Tazinst - SliTaz installer - Version"): $VERSION")
53 $(echo -e "\033[1m$(gettext "Usage"):\033[0m $(gettext "tazinst [command] [setup-file|url-shortcut]")")
55 $(echo -e "\033[1m$(gettext "Commands"): \033[0m")
56 usage|help $(gettext "Print this short usage.")
57 install <file> $(gettext "Install SliTaz on HDD using setup file contents.")
58 upgrade <file> $(gettext "Upgrade SliTaz on HDD using setup file contents.")
59 new <file> $(gettext "Create a new setup file.")
60 check <file> $(gettext "Check validity of settings in a setup file.")
61 showurl <shortcut> $(gettext "Show full URL of a predefined shortcut (stable|cooking|rolling).")
62 log $(gettext "Display log file contents and exit.")
63 version $(gettext "Print version and exit.")
64 EOT
65 exit 1
66 }
68 # Print an error msg & exit
69 # $1: exit code
70 # $@: err msg
71 abort()
72 {
73 # unmouting source & target
74 if mount | grep -q $SOURCE_ROOT; then
75 umount $SOURCE_ROOT 2>>$LOG
76 fi
77 if mount | grep -q $TARGET_ROOT; then
78 umount $TARGET_ROOT 2>>$LOG
79 fi
80 # rm lock
81 rm -f $LOCK
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 [ $DEBUG -gt 0 ] && echo "DEBUG: $1" >>$LOG
99 }
101 # Print a simple msg
102 msg()
103 {
104 STEP=$(($STEP+1))
105 echo "$STEP. $@" | tee -a $LOG
106 sleep 1
107 }
109 #######################
110 # New setup functions #
111 #######################
113 # Generate a setup file
114 # $1: Setup file
115 gen_setup()
116 {
117 SETUP=$1
118 touch $SETUP || abort 2 $(gettext "Can't write setup file")
119 if [ -r "$SETUP" ]; then
120 cat > $SETUP << _EOF_
121 # SliTaz Installer setup 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 "Setup file not found")
178 fi
179 }
181 ######################
182 # Checking functions #
183 ######################
185 # def values and start log
186 # $@ :
187 init()
188 {
189 # Check if another instance of tazinst is running
190 if [ -e "$LOCK" ]; then
191 echo $(gettext "Another instance of tazinst is running.")
192 exit 7
193 else
194 echo $$ > $LOCK
195 fi
197 echo "=== Tazinst: start at `date` ===" >$LOG
198 echo "Command: $0 $@" >>$LOG
199 debug $(fdisk -l | grep \/dev)
201 # Default Type
202 INST_TYPE=cdrom
203 # Default Hostname.
204 TGT_HOSTNAME=slitaz
205 # Default root passwd
206 TGT_ROOT_PWD=root
207 # Default user
208 TGT_USER=tux
209 # Default Grub Install
210 TGT_GRUB=no
211 }
213 # Read setup
214 # $1: setup file
215 read_setup_file()
216 {
217 SETUP=$1
218 if [ -n "$SETUP" ]; then
219 if [ -r "$SETUP" ]; then
220 debug "Using setup-file=$SETUP"
221 # source doesn't like file without a path
222 [ $(echo "$SETUP" | grep -c "/") == "0" ] && SETUP="./$SETUP"
223 source $SETUP || abort 2 $(gettext "Unable to read setup file")
224 else
225 abort 2 $(gettext "Setup file not found")
226 fi
227 else
228 abort 2 $(gettext "No setup file provided")
229 fi
230 }
232 # check main vars
233 check_vars()
234 {
235 # error handling
236 local error=no
237 local found=no
238 local partition=""
240 debug "--- Tazinst main options ---"
241 debug "action=$INST_ACTION"
242 debug "type=$INST_TYPE"
243 debug "source=$SRC_FILE"
244 debug "/ partition=$TGT_PARTITION"
245 debug "/ filesystem=$TGT_FS"
246 debug "/home partition=$TGT_HOME"
247 debug "/home filesystem=$TGT_HOME_FS"
248 debug "hostname=$TGT_HOSTNAME"
249 debug "root-pwd=$TGT_ROOT_PWD"
250 debug "user=$TGT_USER"
251 debug "user-pwd=$TGT_USER_PWD"
252 debug "grub=$TGT_GRUB"
253 debug "winboot=$TGT_WINBOOT"
254 debug "--------------------------------------"
256 # Check Action
257 case $INST_ACTION in
258 install|upgrade|check) ;;
259 *) msg "$INST_ACTION: $(gettext "Unknown install mode")"; error=yes ;;
260 esac
262 # Check Type
263 case $INST_TYPE in
264 cdrom|weboot) ;;
265 usb|iso|web)
266 # We need a valid source
267 if [ -z "$SRC_FILE" ]; then
268 msg "$INST_TYPE: $(gettext "No source file provided")"; error=yes
269 fi ;;
270 *) msg "$INST_TYPE: $(gettext "Unknown source type")"; error=yes ;;
271 esac
273 # Check Source file
274 # 1. assign predefs
275 if [ "$INST_TYPE" == "web" ]; then
276 [ "$SRC_FILE" == "stable" ] && SRC_FILE=$URL_STABLE
277 [ "$SRC_FILE" == "cooking" ] && SRC_FILE=$URL_COOKING
278 [ "$SRC_FILE" == "rolling" ] && SRC_FILE=$URL_ROLLING
279 fi
280 # 2. check avail.
281 case $INST_TYPE in
282 iso)
283 if [ ! -r "$SRC_FILE" ]; then
284 msg "$SRC_FILE: $(gettext "Source file not found")"; error=yes
285 fi ;;
286 web)
287 if ! wget -sq "$SRC_FILE" 2> /dev/null ; then
288 msg "$SRC_FILE: $(gettext "URL not found")"; error=yes
289 fi ;;
290 esac
292 # Check Target Partition
293 found=no
294 LIST_PARTITION=$(fdisk -l | awk '/^\/dev/{printf "%s ",$1}')
295 for partition in $LIST_PARTITION; do
296 [ "$partition" == "$TGT_PARTITION" ] && found="yes"
297 done
298 if [ "$found" != "yes" ]; then
299 msg "$TGT_PARTITION: $(gettext "Partition for / not found")"; error=yes
300 fi
301 if [ "$TGT_PARTITION" == "$SRC_FILE" ]; then
302 msg $(gettext "Target and source partitions should be different"); error=yes
303 fi
305 # Check Filesystem
306 case $TGT_FS in
307 "") ;;
308 btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
309 found=no
310 for xdir in /sbin /usr/sbin /usr/bin; do
311 [ -x "$xdir/mkfs.$TGT_FS" ] && found=yes
312 done
313 if [ "$found" == "no" ]; then
314 msg "$TGT_FS: mkfs.$TGT_FS $(gettext "is not installed")"; error=yes
315 fi ;;
316 *) msg "$TGT_FS: $(gettext "Unknown filesystem (/)")"; error=yes ;;
317 esac
319 # Check Home partition
320 if [ -n "$TGT_HOME" ]; then
321 found=no
322 for partition in $LIST_PARTITION; do
323 [ "$partition" == "$TGT_HOME" ] && found=yes
324 done
325 if [ "$found" != "yes" ]; then
326 msg "$TGT_HOME: $(gettext "Partition for /home not found")"; error=yes
327 fi
328 if [ "$TGT_HOME" == "$SRC_FILE" ]; then
329 msg $(gettext "/home and source partitions should be different"); error=yes
330 fi
331 if [ "$TGT_HOME" == "$TGT_PARTITION" ]; then
332 msg $(gettext "/ and /home partitions should be different"); error=yes
333 fi
334 fi
336 # Check Home Filesystem
337 case $TGT_HOME_FS in
338 "") ;;
339 btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|jfs|ntfs|reiser4|reiserfs|ufs|xfs)
340 found=no
341 for xdir in /sbin /usr/sbin /usr/bin; do
342 [ -x "$xdir/mkfs.$TGT_FS" ] && found=yes
343 done
344 if [ "$found" == "no" ]; then
345 msg "$TGT_FS: mkfs.$TGT_FS $(gettext "is not installed")"; error=yes
346 fi ;;
347 *) msg "$TGT_HOME_FS: $(gettext "Unknown filesystem (/home)")"; error=yes ;;
348 esac
350 # Check Grub
351 case $TGT_GRUB in
352 yes|no) ;;
353 *) msg $(gettext "Bootloader (grub): Invalid settings"); error=yes ;;
354 esac
356 # Check Winboot
357 case $TGT_WINBOOT in
358 "") ;;
359 auto) ;;
360 hd[[:digit:]],[[:digit:]]) ;;
361 *) msg $(gettext "Windows Dual-Boot: Invalid settings"); error=yes ;;
362 esac
364 # Stop on error
365 [ "$error" == "yes" ] && abort 1
366 }
368 # Exit install if user is not root.
369 check_root()
370 {
371 if test $(id -u) != 0 ; then
372 gettext "You must be the root user (system administrator) to install SliTaz, \
373 please use 'su' to get a root SHell and restart installation."
374 exit 0
375 fi
376 }
378 # Mount cdrom
379 check_cdrom()
380 {
381 # Set device name
382 DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3` [ -n "$DRIVE_NAME" ] || DRIVE_NAME=cdrom
383 CDROM=/dev/$DRIVE_NAME
384 # Try to mount a cdrom
385 if mount -t iso9660 $CDROM $SOURCE_ROOT 2>>$LOG; then
386 debug "Using files from cdrom ($CDROM)..."
387 sleep 2
388 else
389 warning "$CDROM: $(gettext "Mount failed")"
390 fi
391 }
393 # Link LiveUSB
394 check_usb()
395 {
396 # /home is on USB dev
397 if [ -d /home/boot ]; then
398 debug "Using files from USB device..."
399 ln -s /home/boot $SOURCE_ROOT/boot
400 SOURCE_STATUS="link"
401 sleep 2
402 else
403 # Try to mount LiveUSB
404 if mount $SRC_FILE $SOURCE_ROOT 2>>$LOG; then
405 debug "Using files from USB device ($SRC_FILE)..."
406 SOURCE_STATUS="mount"
407 else
408 warning "$SRC_FILE: $(gettext "Failed to mount USB device")"
409 fi
410 fi
411 }
413 # Mount ISO file
414 check_iso()
415 {
416 local src_md5
417 # Integrity check
418 src_md5=$(echo $SRC_FILE | sed 's/.iso$/.md5/')
419 if [ -r "$src_md5" ]; then
420 [ $(md5sum $SRC_FILE | cut -d' ' -f1) == $(cat "$src_md5" | cut -d' ' -f1) ] || \
421 abort 3 "$SRC-FILE: $(gettext "md5sum mismatch, file corrupted")"
422 else
423 warning "$SRC_FILE: $(gettext "md5 file not found, unable to check integrity.")"
424 fi
425 # Try to mount ISO
426 if mount -o loop -t iso9660 $SRC_FILE $SOURCE_ROOT 2>>$LOG; then
427 debug "Using files from ISO ($SRC_FILE)..."
428 sleep 2
429 else
430 warning "$SRC_FILE: $(gettext "Failed to mount ISO.")"
431 fi
432 }
434 # Source is on the web
435 check_web()
436 {
437 local src_md5
438 debug "Downloading $SRC_FILE"
439 if wget $SRC_FILE -P /tmp; then
440 debug "Download completed."
441 else
442 warning "$SRC_FILE: $(gettext "File download failed.")"
443 fi
444 src_md5=$(echo $SRC_FILE | sed 's/.iso$/.md5/')
445 debug "Downloading $src_md5"
446 wget $src_md5 -P /tmp || warning "$src_md5: $(gettext "File download failed.")"
447 tmpfile=$(echo $SRC_FILE | awk 'BEGIN{RS="/"}{out=$1}END{printf"%s",out}')
448 SRC_FILE="/tmp/$tmpfile"
449 check_iso
450 }
452 # We may be in Tiny Web boot mode
453 check_weboot()
454 {
455 if [ -d $SRC_FILE/boot ]; then
456 debug "Using files from HTTP device..."
457 ln -s $SRC_FILE/boot $SOURCE_ROOT/boot
458 sleep 2
459 else
460 abort 3 $(gettext "Web boot files not found")
461 fi
462 }
464 # set up source and check Slitaz' content
465 check_source()
466 {
467 debug "Creating mount point ($SOURCE_ROOT)..."
468 mkdir -p $SOURCE_ROOT
469 sleep 1
470 case $INST_TYPE in
471 cdrom)
472 check_cdrom ;;
473 usb)
474 check_usb ;;
475 iso)
476 check_iso ;;
477 web)
478 check_web ;;
479 weboot)
480 check_cdrom
481 check_web ;;
482 *)
483 abort 8 $(gettext "Internal") ;;
484 esac
486 # Exit with error msg if no rootfs.gz found.
487 debug "Checking installation media..."
488 if [ ! -f $SOURCE_ROOT/boot/rootfs.gz -a \
489 ! -f $SOURCE_ROOT/boot/rootfs1.gz ]; then
490 abort 3 $(gettext "Invalid source")
491 else
492 debug "Installation media checked ok"
493 fi
494 }
496 #######################
497 # Installer functions #
498 #######################
500 # Mount and mkfs with progress.
501 prepare_install()
502 {
503 debug "Preparing target partition..."
504 # Target may be used
505 mount | grep -q $TGT_PARTITION && \
506 abort 5 "$TGT_PARTITION: $(gettext "Partition in use")"
507 # Mount point can be already used.
508 if mount | grep -q $TARGET_ROOT; then
509 umount $TARGET_ROOT 2>>$LOG
510 fi
511 sleep 2
513 # Formatting root partition
514 case $TGT_FS in
515 "")
516 debug "The partition ($TGT_PARTITION) will be cleaned..."
517 # ROOT_FS=$(parted /dev/hda5 print -m | grep "^1:" | cut -d':' -f5) ;;
518 ROOT_FS=auto ;;
519 *)
520 msg "$(gettext "Formatting / partition:") $TGT_PARTITION ($TGT_FS)"
521 mkfs.$TGT_FS $TGT_PARTITION >>$LOG 2>>$LOG
522 ROOT_FS=$TGT_FS ;;
523 esac
524 sleep 2
526 # Formatting /home
527 if [ -n "$TGT_HOME" ]; then
528 case $TGT_HOME_FS in
529 "")
530 debug "The partition ($TGT_HOME) will be kept..." ;;
531 *)
532 msg "$(gettext "Formatting /home partition:") $TGT_HOME ($TGT_HOME_FS)"
533 mkfs.$TGT_HOME_FS -L "Home" $TGT_HOME >>$LOG 2>>$LOG ;;
534 esac
535 sleep 2
536 fi
538 # Mount target.
539 debug "Creating mount point: $TARGET_ROOT"
540 mkdir -p $TARGET_ROOT >>$LOG
541 sleep 2
543 mount -t $ROOT_FS $TGT_PARTITION $TARGET_ROOT >>$LOG 2>>$LOG
544 if [ $(mount | grep -c "mnt/target") == "0" ]; then
545 abort 5 "$TGT_PARTITION: $(gettext "Unable to mount partition")"
546 fi
547 }
549 # Get a clean target device (15%).
550 clean_target()
551 {
552 if [ -z "$TGT_FS" ]; then
553 # partition was not formatted
554 debug "Cleaning the root partition ($TGT_PARTITION)..."
555 # Keep /home in case of reinstall.
556 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
557 for dir in *
558 do
559 case "$dir" in
560 home)
561 debug "keeping /home found on: $TGT_PARTITION"
562 mv home home.bak ;;
563 lost+found)
564 continue ;;
565 *)
566 debug "removing target: $dir"
567 rm -rf $dir 2>>$LOG ;;
568 esac
569 done
570 if [ -d mklost+found ]; then
571 mklost+found 2>>$LOG
572 fi
573 fi
574 sleep 2
575 }
577 # Kernel is renamed to standard vmlinuz-$VERSION.
578 install_kernel()
579 {
580 if [ -d /$TARGET_ROOT/lib/modules ]; then
581 KERNEL=$(ls /$TARGET_ROOT/lib/modules | tail -1)
582 KERNEL="vmlinuz-$KERNEL"
583 else
584 KERNEL=vmlinuz-`uname -r`
585 warning "$(gettext "Kernel name not found, falling back to:") $(uname -r)"
586 fi
587 mkdir -p $TARGET_ROOT/boot
588 cp $SOURCE_ROOT/boot/bzImage $TARGET_ROOT/boot/$KERNEL
589 debug "install_kernel: $KERNEL"
590 sleep 2
591 }
593 # Copy isolinux r/w files (not syslinux, some files are read only).
594 copy_bootloaders()
595 {
596 if [ -d "$SOURCE/ROOT/boot/isolinux" ]; then
597 debug "Copy isolinux r/w files"
598 mkdir -p $TARGET_ROOT/boot/isolinux
599 cp -a $SOURCE_ROOT/boot/isolinux/*.cfg $TARGET_ROOT/boot/isolinux
600 cp -a $SOURCE_ROOT/boot/isolinux/*.kbd $TARGET_ROOT/boot/isolinux
601 cp -a $SOURCE_ROOT/boot/isolinux/*.txt $TARGET_ROOT/boot/isolinux
602 cp -a $SOURCE_ROOT/boot/isolinux/*.bin $TARGET_ROOT/boot/isolinux
603 cp -a $SOURCE_ROOT/boot/isolinux/*.msg $TARGET_ROOT/boot/isolinux
604 cp -a $SOURCE_ROOT/boot/isolinux/*.lss $TARGET_ROOT/boot/isolinux
605 cp -a $SOURCE_ROOT/boot/isolinux/*.c32 $TARGET_ROOT/boot/isolinux
606 fi
607 }
609 need_package()
610 {
611 [ -d /var/lib/tazpkg/installed/$1 ] || tazpkg get-install $1
612 }
614 # extract packed rootfs: squashfs or cromfs
615 extract_loramfs()
616 {
617 local i
618 for i in $(cpio -idvum 2> /dev/null); do
619 case "$i" in
620 rootfs*)
621 need_package squashfs
622 if ! unsquashfs $i ; then
623 need_package cromfs
624 unmkcromfs $i squashfs-root
625 fi
626 mv -f squashfs-root/* .
627 rmdir squashfs-root
628 rm -f $i
629 esac
630 done
631 }
633 # This is a loram rootfs.gz, skip loram bootstrap and extract
634 extract_first_loramfs()
635 {
636 (zcat $1 || unlzma -c $1) | cpio -i extractfs.cpio 2> /dev/null &&
637 ( cd / ; cpio -id ) < extractfs.cpio && rm -f extractfs.cpio
638 ofs=$(awk '/07070100/ { o+=index($0,"07070100"); printf "%d\n",o/4 ; exit } { o+=1+length() }' < $1)
639 dd if=$1 skip=$(($ofs / 1024)) bs=4k count=1 2> /dev/null | \
640 ( dd skip=$(($ofs % 1024)) bs=4 2> /dev/null ; \
641 dd if=$1 skip=$((1 + ($ofs / 1024) )) bs=4k ) | extract_loramfs
642 }
644 # Extract lzma'ed or gziped rootfs.
645 extract_rootfs()
646 {
647 local isloramfs
648 isloramfs=
649 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
650 if [ -d $1/../fs/etc ]; then
651 # This is a tazlitobox loram (cdrom)
652 cp -a $1/../fs/. .
653 else
654 for i in $(ls $1/rootfs* | sort -r); do
655 if [ ! -d etc ]; then
656 if [ $( (zcat $i 2>/dev/null || lzma d $i -so) | wc -c) \
657 -lt $(stat -c %s $i) ]; then
658 # This is a tazlitobox loram (ram)
659 isloramfs=$i
660 extract_first_loramfs $i
661 continue
662 fi
663 fi
664 if [ -n "$isloramfs" ]; then
665 extract_loramfs < $i
666 continue
667 fi
668 ( zcat $i 2>/dev/null || lzma d $i -so || \
669 cat $i ) 2>>$LOG | cpio -idu
670 done 2>>$LOG > /dev/null
671 fi
672 cp /etc/keymap.conf etc
673 # unpack /usr (double check...)
674 if ls etc/tazlito | grep -q ".extract"; then
675 for i in etc/tazlito/*.extract; do
676 [ -f "$i" ] && . $i /media/cdrom
677 done
678 fi
679 }
681 # Pre configure freshly installed system (60 - 80%).
682 pre_config_system()
683 {
684 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
685 # Restore backup of existing /home if exists.
686 # (created by prepare_target_dev)
687 if [ -d home.bak ]; then
688 debug "Restoring directory: /home..."
689 rm -rf home
690 mv home.bak home
691 sleep 1
692 fi
693 # Add root device to CHECK_FS in rcS.conf to check filesystem
694 # on each boot.
695 debug "Adding $TGT_PARTITION and CHECK_FS to file /etc/rcS.conf..."
696 sed -i s#'CHECK_FS=\"\"'#"CHECK_FS=\"$TGT_PARTITION\""# etc/rcS.conf
697 sleep 2
698 # Set hostname.
699 msg "$(gettext "Configuring host name:") $TGT_HOSTNAME"
700 echo $TGT_HOSTNAME > etc/hostname
701 }
703 # Set root passwd and create user after rootfs extraction.
704 users_settings()
705 {
706 cat > $TARGET_ROOT/users.sh << _EOF_
707 #!/bin/sh
708 echo "root:$TGT_ROOT_PWD" | chpasswd
709 adduser -D -H $TGT_USER
711 for grp in audio cdrom floppy dialout disk kmem tape tty video; do
712 if ! grep \$grp /etc/group | grep -q $TGT_USER ; then
713 grep -q \$grp /etc/group && addgroup $TGT_USER \$grp
714 fi
715 done
717 echo "$TGT_USER:$TGT_USER_PWD" | chpasswd
718 if [ ! -d /home/$TGT_USER ]; then
719 cp -a /etc/skel /home/$TGT_USER
720 [ -e /root/.xinitrc ] && cp /root/.xinitrc /home/$TGT_USER
721 mkdir -p /home/$TGT_USER/.config/slitaz
722 cp -a /etc/slitaz/applications.conf /home/$TGT_USER/.config/slitaz
723 # Set ownership
724 if grep -q ^users: /etc/group; then
725 chown -R $TGT_USER:users /home/$TGT_USER
726 else
727 chown -R $TGT_USER:$TGT_USER /home/$TGT_USER
728 fi
729 # Path for user desktop files.
730 for i in /home/$TGT_USER/.local/share/applications/*.desktop
731 do
732 [ -e "$i" ] && sed -i s/"user_name"/"$TGT_USER"/g \$i
733 done
734 fi
735 # Slim default user.
736 if [ -f /etc/slim.conf ]; then
737 sed -i s/"default_user .*"/"default_user $TGT_USER"/ \
738 /etc/slim.conf
739 fi
740 _EOF_
741 chmod +x $TARGET_ROOT/users.sh
742 chroot $TARGET_ROOT ./users.sh
743 rm $TARGET_ROOT/users.sh
744 sleep 2
745 }
747 # /home can be on a separate partition. If default user exists in /home
748 # we remove default file created by users_settings().
749 home_config()
750 {
751 debug "home_config: $TGT_HOME"
752 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
753 mv home/$TGT_USER tmp
754 mount $TGT_HOME home
755 if [ -d $TARGET_ROOT/home/$TGTUSER ]; then
756 rm -rf tmp/$TGT_USER
757 else
758 mv tmp/$TGT_USER home
759 fi
760 echo "$TGT_HOME /home ext3 defaults 0 2" \
761 >> etc/fstab
762 umount home
763 }
765 # Search for a Windows partition
766 win_partition()
767 {
768 debug "Searching for Windows"
769 if [ "$TGT_WINBOOT" == "auto" ];then
770 WINBOOT=$(fdisk -l | awk '
771 BEGIN{disk=-1, found=-1, winboot=""}
772 {
773 # Counting disk
774 if ($1=="Disk"){disk++, part=-1}
775 # Counting partition
776 if (substr($1,1,4)=="/dev"){part++}
777 # Read partition Id
778 if ($2=="*"){Id=$6} else {Id=$5}
779 # Detect Windows type
780 if (Id=="7" || Id=="b"){
781 if (found){
782 # record 1st Windows partition found
783 winboot=sprintf("hd%d,%d",disk,part),found++}
784 }
785 }
786 END{printf "%s", winboot}')
787 if [ -z "$WINBOOT" ]; then
788 warning $(gettext "No windows partition found. Dual-boot disabled")
789 TGT_WINBOOT=""
790 fi
791 fi
792 }
794 # Determine GRUB partition number and GRUB disk number.
795 grub_partition()
796 {
797 DISK_LETTER=${TGT_PARTITION#/dev/[h-s]d}
798 DISK_LETTER=${DISK_LETTER%[0-9]}
799 GRUB_PARTITION=$((${TGT_PARTITION#/dev/[h-s]d[a-z]}-1))
800 for disk in a b c d e f g h
801 do
802 nb=$(($nb+1))
803 if [ "$disk" = "$DISK_LETTER" ]; then
804 GRUB_DISK=$(($nb-1))
805 break
806 fi
807 done
808 GRUB_ROOT="(hd${GRUB_DISK},${GRUB_PARTITION})"
809 }
811 # Create grub conf
812 grub_config()
813 {
814 grub_partition
815 if [ "$TGT_GRUB" == "yes" ]; then
816 win_partition
817 fi
818 # Create the target GRUB configuration.
819 mkdir -p $TARGET_ROOT/boot/grub
820 cat > $TARGET_ROOT/boot/grub/menu.lst << _EOF_
821 # /boot/grub/menu.lst: GRUB boot loader configuration.
822 #
824 # By default, boot the first entry.
825 default 0
827 # Boot automatically after 8 secs.
828 timeout 8
830 # Graphical splash image.
831 splashimage=/boot/grub/splash.xpm.gz
833 # Change the colors.
834 #color yellow/brown light-green/black
836 # For booting SliTaz from : $TGT_PARTITION
837 #
838 title SliTaz GNU/Linux (cooking) (Kernel $KERNEL)
839 root $GRUB_ROOT
840 kernel /boot/$KERNEL root=$TGT_PARTITION quiet
842 _EOF_
843 if [ -n "$TGT_WINBOOT" ]; then
844 msg $(gettext "Enabling Windows dual-boot")
845 cat >> $TARGET_ROOT/boot/grub/menu.lst << _EOF_
846 # For booting Windows :
847 #
848 title Microsoft Windows
849 rootnoverify ($WINBOOT)
850 chainloader +1
852 _EOF_
853 fi
854 # log
855 debug "grub_config: $TARGET_ROOT/boot/grub/menu.lst"
856 echo "--- menu.lst -------------" >>$LOG
857 cat $TARGET_ROOT/boot/grub/menu.lst >>$LOG
858 echo "--- menu.lst -------------" >>$LOG
859 sleep 2
860 }
862 # Files install, calling for functions or with cmds.
863 install_files()
864 {
865 msg "$(gettext "Installing SliTaz on:") $TGT_PARTITION"
866 # saving pwd
867 local save_pwd=$(pwd)
869 debug "Cleaning the root partition if necessary..."
870 clean_target
872 debug "Extracting the root system..."
873 extract_rootfs $SOURCE_ROOT/boot
875 debug "Installing the kernel..."
876 install_kernel
878 debug "Copying the bootloader syslinux/isolinux..."
879 copy_bootloaders
881 debug "Preconfiguring the system..."
882 pre_config_system
884 msg "$(gettext "Configuring root and default user account:") $TGT_USER"
885 users_settings
887 if [ "$TGT_HOME" != "" ]; then
888 msg "$(gettext "Configuring partition to be used as /home:") $TGT_HOME"
889 home_config
890 sleep 2
891 fi
893 debug "Creating configuration file for GRUB (menu.lst)..."
894 grub_config
896 debug "Files installation completed"
897 sleep 2
898 # restoring pwd
899 cd $save_pwd
900 }
902 # GRUB info with disk name used for grub-install.
903 grub_install()
904 {
905 if [ "$TGT_GRUB" == "yes" ]; then
906 TARGET_DISK=`echo $TGT_PARTITION | sed s/"[0-9]"/''/`
907 msg "$(gettext "Running grub-install on:") $TARGET_DISK"
908 grub-install --no-floppy \
909 --root-directory=$TARGET_ROOT $TARGET_DISK 2>>$LOG
910 debug "Grub installation done..."
911 else
912 debug "Grub not installed"
913 fi
914 }
916 # Copy log file, umount target and eject cdrom.
917 umount_devices()
918 {
919 # Umount target
920 if mount | grep -q $TARGET_ROOT; then
921 echo "$(gettext "Unmounting target partition:") $TGT_PARTITION"
922 umount $TARGET_ROOT 2>>$LOG
923 fi
925 # Umount source
926 if mount | grep -q $SOURCE_ROOT; then
927 echo "$(gettext "Unmounting:") $SOURCE_ROOT"
928 umount $SOURCE_ROOT
929 fi
931 # Eject cd
932 if [ "$INST_TYPE" == "cdrom" ]; then
933 gettext "Ejecting cdrom..."
934 eject
935 fi
936 # Remove lock file
937 rm -f $LOCK
938 sleep 2
939 }
941 # End of installation.
942 end_of_install()
943 {
944 msg $(gettext "Installation complete. You can now restart (reboot)")
945 echo " $(gettext "from your SliTaz GNU/Linux system.")"
946 echo "=== Tazinst end at `date` ===" >>$LOG
947 # Log files
948 echo "$(gettext "Copying log files") ($LOG)..."
949 cp -a $LOG $TARGET_ROOT/var/log
950 sleep 2
951 # umount
952 umount_devices
953 }
955 #####################
956 # Upgrade functions #
957 #####################
959 # Mount.
960 prepare_upgrade()
961 {
962 debug "Preparing the target partition..."
963 # Target may be used
964 mount | grep -q $TGT_PARTITION && \
965 abort 5 "$TGT_PARTITION: $(gettext "Partition in use")"
966 # Mount point can be already used.
967 if mount | grep -q $TARGET_ROOT; then
968 umount $TARGET_ROOT 2>>$LOG
969 fi
970 mkdir -p $TARGET_ROOT && sleep 2
971 # Mount target.
972 mount $TGT_PARTITION $TARGET_ROOT >>$LOG 2>>$LOG
973 if [ $(mount | grep -c "mnt/target") == "0" ]; then
974 abort 5 "$TGT_PARTITION $(gettext "Unable to mount partition")"
975 fi
976 }
978 # Check for a valid SliTaz
979 check_release()
980 {
981 if [ -f $TARGET_ROOT/etc/slitaz-release ]; then
982 release=`cat $TARGET_ROOT/etc/slitaz-release`
983 msg "$(gettext "Preparing upgrade of SliTaz release:") $release"
984 else
985 abort 6 "$TGT_PARTITION: $(gettext "This partition doesn't appear to contain \
986 a valid SliTaz system, the file: /etc/slitaz-release doesn't exist.")"
987 fi && sleep 2
988 }
990 # Backup target packages list.
991 backup_files()
992 {
993 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
994 ls -1 var/lib/tazpkg/installed > home/packages-selection.list
995 for dir in *
996 do
997 case "$dir" in
998 boot)
999 rm -rf boot/vmlinuz-* ;;
1000 home)
1001 mv home home.bak
1002 debug "keeping /home found on: $TGT_PARTITION" ;;
1003 etc)
1004 tar czf etc.tar.gz etc
1005 mv etc etc.bak
1006 debug "keeping /etc found on: $TGT_PARTITION" ;;
1007 var)
1008 if [ -d var/www ]; then
1009 mv var/www www.bak
1010 debug "keeping /var/www found on: $TGT_PARTITION"
1011 fi
1012 rm -rf var 2>>$LOG ;;
1013 lost+found)
1014 continue ;;
1015 *)
1016 debug "removing target: $dir"
1017 rm -rf $dir 2>>$LOG ;;
1018 esac
1019 done
1020 if [ -d mklost+found ]; then
1021 mklost+found 2>>$LOG
1022 fi
1023 sleep 2
1026 # Restore backups.
1027 restore_files()
1029 rm -rf $TARGET_ROOT/home
1030 mv $TARGET_ROOT/home.bak $TARGET_ROOT/home
1031 rm -rf $TARGET_ROOT/etc
1032 mv $TARGET_ROOT/etc.bak $TARGET_ROOT/etc
1033 if [ -d $TARGET_ROOT/www.bak ]; then
1034 rm -rf $TARGET_ROOT/var/www
1035 mv $TARGET_ROOT/www.bak $TARGET_ROOT/var/www
1036 fi
1037 debug "backups restored: `date`"
1039 # /var/lib/slitaz-installer
1040 mkdir -p $TARGET_ROOT/var/lib/tazinst
1041 mv $TARGET_ROOT/etc.tar.gz $TARGET_ROOT/var/lib/tazinst
1042 mv $TARGET_ROOT/home/packages-selection.list $TARGET_ROOT/var/lib/tazinst
1045 # Added pkgs
1046 install_pkgs()
1048 # Check if the pkg is on the mirror.
1049 debug "Checking the availability of packages..."
1050 touch packages-to-install.list
1051 packages=0
1052 diff=`cat packages-selection.diff | sort`
1053 for pkg in $diff
1054 do
1055 if grep -q ^$pkg-[0-9] /var/lib/tazpkg/packages.list; then
1056 packages=$(($packages+1))
1057 echo "$pkg" >> packages-to-install.list
1058 fi
1059 done
1061 # Calculate the percent for one package and install.
1062 debug "Installing any packages..."
1063 sleep 2
1064 if [ "$packages" == "0" ]; then
1065 debug "packages to install: 0"
1066 else
1067 onepkg=$((48/$packages))
1068 pct=50
1069 # Get-install all missing pkgs.
1070 for pkg in `cat packages-to-install.list`
1071 do
1072 pct=$(($pct+$onepkg))
1073 echo $pct
1074 debug "Installing: $pkg..."
1075 # Get install package and answer yes in case of dependencies.
1076 pkgname=`grep ^$pkg /var/lib/tazpkg/packages.list`
1077 tazpkg get $pkg >/dev/null 2>/dev/null
1078 yes "" | tazpkg install $pkgname.tazpkg --root=$TARGET_ROOT >/dev/null 2>/dev/null
1079 rm -f $pkgname.tazpkg
1080 done
1081 fi
1082 echo 100
1083 debug "Installation of packages complete..."
1084 sleep 2
1087 # Search for added pkgs
1088 update_pkgs()
1090 cd $TARGET_ROOT/var/lib/tazinst || abort 8 $(gettext "Internal")
1091 # LiveCD packages list.
1092 debug "Creating package lists..."
1093 ls -1 $TARGET_ROOT/var/lib/tazpkg/installed > packages-source.list || exit 1
1094 debug "packages-source.list: done"
1095 # Diff
1096 diff packages-source.list packages-selection.list | \
1097 grep ^+[a-z] | sed s/^+// > packages-selection.diff
1098 debug "packages-selection.diff: done"
1099 # Get mirror list.
1100 tazpkg recharge >>$LOG 2>>$LOG
1101 if [ -f /var/lib/tazpkg/packages.list ]; then
1102 install_pkgs
1103 else
1104 touch packages-to-install.list
1105 warning $(gettext "The list of available packages on the mirror could not be \
1106 downloaded. No missing packages will be reinstalled now, but \
1107 you can do so later by looking at the following list: \
1108 /var/lib/tazinst/packages-selection.diff")
1109 fi
1110 sleep 2
1113 # Update grub conf
1114 grub_update()
1116 # Backup and create a new grub menu.lst.
1117 if [ "$TGT_GRUB" == "yes" ]; then
1118 msg $(gettext "Grub update")
1119 mv $TARGET_ROOT/boot/grub/menu.lst \
1120 $TARGET_ROOT/boot/grub/menu.lst.bak 2>/dev/null
1121 grub_config
1122 fi
1125 # Prepare the partition to upgrade, backup, install, restore configs
1126 # and reinstall pkgs.
1127 upgrade_files()
1129 # saving pwd
1130 local save_pwd=$(pwd)
1131 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
1133 debug "Searching for /etc/slitaz-release"
1134 check_release
1136 msg $(gettext "Backup /etc, /home and the packages list...")
1137 backup_files
1139 msg "$(gettext "Upgrading SliTaz on:") $TGT_PARTITION"
1141 debug "Copying the bootloader syslinux/isolinux..."
1142 copy_bootloaders
1144 debug "Extracting the root system..."
1145 extract_rootfs $SOURCE_ROOT/boot
1147 msg $(gettext "Restoring configuration files...")
1148 restore_files
1150 debug "Installing the kernel..."
1151 install_kernel
1153 msg $(gettext "Upgrading added packages...")
1154 update_pkgs
1156 # restoring pwd
1157 cd $save_pwd
1160 # End of system upgrade.
1161 end_of_upgrade()
1163 pkgscd=`cat $TARGET_ROOT/var/lib/tazinst/packages-source.list | wc -l`
1164 pkginst=`cat $TARGET_ROOT/var/lib/tazinst/packages-to-install.list | wc -l`
1165 msg $(gettext "Upgrade finished. You can now restart (reboot)")
1166 echo $(gettext "from your SliTaz GNU/Linux system.")
1167 echo "$(gettext "Packages on the cdrom :") $pkgscd"
1168 echo "$(gettext "Packages installed from the mirror :") $pkginst"
1169 echo "=== Tazinst end at `date` ===" >>$LOG
1170 umount_devices
1173 ######################
1174 # Installer sequence #
1175 ######################
1177 case $1 in
1178 install)
1179 INST_ACTION=install
1180 check_root
1181 init $@
1182 read_setup_file $2
1183 check_vars
1184 check_source
1185 prepare_install
1186 install_files
1187 grub_install
1188 end_of_install ;;
1189 upgrade)
1190 INST_ACTION=upgrade
1191 check_root
1192 init $@
1193 read_setup_file $2
1194 check_vars
1195 check_source
1196 prepare_upgrade
1197 upgrade_files
1198 grub_update
1199 end_of_upgrade ;;
1200 new)
1201 gen_setup $2 ;;
1202 showurl)
1203 case $2 in
1204 stable)
1205 echo $URL_STABLE ;;
1206 cooking)
1207 echo $URL_COOKING ;;
1208 rolling)
1209 echo $URL_ROLLING ;;
1210 esac ;;
1211 check)
1212 LOG="/dev/null"
1213 INST_ACTION=check
1214 check_root
1215 init $@
1216 read_setup_file $2
1217 check_vars
1218 rm -f $LOCK ;;
1219 log)
1220 [ -r "$LOG" ] && cat $LOG ;;
1221 version)
1222 echo $VERSION ;;
1223 usage|*)
1224 usage ;;
1225 esac
1227 exit 0