slitaz-tools view installer/tazinst @ rev 735

tazinst: back improved ms win detection
author Dominique Corbex <domcox@slitaz.org>
date Wed Apr 25 22:37:48 2012 +0200 (2012-04-25)
parents e436b1a8acad
children ac3b66b7cd05
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-2012 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.33
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, ex /dev/hda5).
135 TGT_PARTITION=""
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_HOME_FS" ] && found=yes
343 done
344 if [ "$found" == "no" ]; then
345 msg "$TGT_FS: mkfs.$TGT_HOME_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 # GRUB splash image
608 if [ -f "$SOURCE_ROOT/boot/grub/splash.xpm.gz" ]; then
609 debug "Copy GRUB splash image"
610 mkdir -p $TARGET_ROOT/boot/grub
611 $SOURCE_ROOT/boot/grub/splash.xpm.gz $TARGET_ROOT/boot/grub
612 fi
613 }
615 need_package()
616 {
617 [ -d /var/lib/tazpkg/installed/$1 ] || tazpkg get-install $1
618 }
620 # extract packed rootfs: squashfs or cromfs
621 extract_loramfs()
622 {
623 local i
624 for i in $(cpio -idvum 2> /dev/null); do
625 case "$i" in
626 rootfs*)
627 need_package squashfs
628 if ! unsquashfs $i ; then
629 need_package cromfs
630 unmkcromfs $i squashfs-root
631 fi
632 mv -f squashfs-root/* .
633 rmdir squashfs-root
634 rm -f $i
635 esac
636 done
637 }
639 # This is a loram rootfs.gz, skip loram bootstrap and extract
640 extract_first_loramfs()
641 {
642 (zcat $1 || unlzma -c $1) | cpio -i extractfs.cpio 2> /dev/null &&
643 ( cd / ; cpio -id ) < extractfs.cpio && rm -f extractfs.cpio
644 ofs=$(awk '/07070100/ { o+=index($0,"07070100"); printf "%d\n",o/4 ; exit } { o+=1+length() }' < $1)
645 dd if=$1 skip=$(($ofs / 1024)) bs=4k count=1 2> /dev/null | \
646 ( dd skip=$(($ofs % 1024)) bs=4 2> /dev/null ; \
647 dd if=$1 skip=$((1 + ($ofs / 1024) )) bs=4k ) | extract_loramfs
648 }
650 # Extract lzma'ed or gziped rootfs.
651 extract_rootfs()
652 {
653 local isloramfs
654 isloramfs=
655 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
656 if [ -d $1/../fs/etc ]; then
657 # This is a tazlitobox loram (cdrom)
658 cp -a $1/../fs/. .
659 else
660 for i in $(ls $1/rootfs* | sort -r); do
661 if [ ! -d etc ]; then
662 if [ $( (zcat $i 2>/dev/null || lzma d $i -so) | wc -c) \
663 -lt $(stat -c %s $i) ]; then
664 # This is a tazlitobox loram (ram)
665 isloramfs=$i
666 extract_first_loramfs $i
667 continue
668 fi
669 fi
670 if [ -n "$isloramfs" ]; then
671 extract_loramfs < $i
672 continue
673 fi
674 ( zcat $i 2>/dev/null || lzma d $i -so || \
675 cat $i ) 2>>$LOG | cpio -idu
676 done 2>>$LOG > /dev/null
677 fi
678 cp /etc/keymap.conf etc
679 # unpack /usr (double check...)
680 if ls etc/tazlito | grep -q ".extract"; then
681 for i in etc/tazlito/*.extract; do
682 [ -f "$i" ] && . $i /media/cdrom
683 done
684 fi
685 }
687 # Pre configure freshly installed system (60 - 80%).
688 pre_config_system()
689 {
690 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
691 # Restore backup of existing /home if exists.
692 # (created by prepare_target_dev)
693 if [ -d home.bak ]; then
694 debug "Restoring directory: /home..."
695 rm -rf home
696 mv home.bak home
697 sleep 1
698 fi
699 # Add root device to CHECK_FS in rcS.conf to check filesystem
700 # on each boot.
701 debug "Adding $TGT_PARTITION and CHECK_FS to file /etc/rcS.conf..."
702 sed -i s#'CHECK_FS=\"\"'#"CHECK_FS=\"$TGT_PARTITION\""# etc/rcS.conf
703 sleep 2
704 # Set hostname.
705 msg "$(gettext "Configuring host name:") $TGT_HOSTNAME"
706 echo $TGT_HOSTNAME > etc/hostname
707 }
709 # Set root passwd and create user after rootfs extraction.
710 users_settings()
711 {
712 cat > $TARGET_ROOT/users.sh << _EOF_
713 #!/bin/sh
714 echo "root:$TGT_ROOT_PWD" | chpasswd -m
715 adduser -D -H $TGT_USER
717 for grp in audio cdrom floppy dialout disk kmem tape tty video; do
718 if ! grep \$grp /etc/group | grep -q $TGT_USER ; then
719 grep -q \$grp /etc/group && addgroup $TGT_USER \$grp
720 fi
721 done
723 echo "$TGT_USER:$TGT_USER_PWD" | chpasswd -m
724 if [ ! -d /home/$TGT_USER ]; then
725 cp -a /etc/skel /home/$TGT_USER
726 [ -e /root/.xinitrc ] && cp /root/.xinitrc /home/$TGT_USER
727 mkdir -p /home/$TGT_USER/.config/slitaz
728 cp -a /etc/slitaz/applications.conf /home/$TGT_USER/.config/slitaz
729 # Set ownership
730 if grep -q ^users: /etc/group; then
731 chown -R $TGT_USER:users /home/$TGT_USER
732 else
733 chown -R $TGT_USER:$TGT_USER /home/$TGT_USER
734 fi
735 # Path for user desktop files.
736 for i in /home/$TGT_USER/.local/share/applications/*.desktop
737 do
738 [ -e "$i" ] && sed -i s/"user_name"/"$TGT_USER"/g \$i
739 done
740 fi
741 # Slim default user.
742 if [ -f /etc/slim.conf ]; then
743 sed -i s/"default_user .*"/"default_user $TGT_USER"/ \
744 /etc/slim.conf
745 fi
746 _EOF_
747 chmod +x $TARGET_ROOT/users.sh
748 chroot $TARGET_ROOT ./users.sh
749 rm $TARGET_ROOT/users.sh
750 sleep 2
751 }
753 # /home can be on a separate partition. If default user exists in /home
754 # we remove default file created by users_settings().
755 home_config()
756 {
757 debug "home_config: $TGT_HOME"
758 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
759 mv home/$TGT_USER tmp
760 mount $TGT_HOME home
761 if [ -d $TARGET_ROOT/home/$TGT_USER ]; then
762 rm -rf tmp/$TGT_USER
763 else
764 mv tmp/$TGT_USER home
765 fi
766 echo "$TGT_HOME /home ext3 defaults 0 2" \
767 >> etc/fstab
768 umount home
769 }
771 # Search for a Windows partition
772 win_partition()
773 {
774 debug "Searching for Windows"
775 if [ "$TGT_WINBOOT" == "auto" ];then
776 WINBOOT=$(fdisk -l | awk '
777 BEGIN{
778 disk=-1
779 found=0
780 winboot=""}
781 {
782 # Count disks
783 if ($1=="Disk"){
784 disk++
785 part=-1
786 dev=substr($2,6,3)
787 # get removable status
788 file="/sys/block/"dev"/removable"
789 "cat " file | getline removable
790 close("cat ")
791 }
792 # Count partitions
793 if (substr($1,1,4)=="/dev"){
794 # List fixed drives only
795 if (removable==0){
796 part++
797 # Read partition Id
798 if ($2=="*"){Id=$6} else {Id=""}
799 # Detect Windows Partition Type: 7,b,c,e,f
800 WPT="[7bcef]"
801 if (Id ~ WPT){
802 found++
803 # record 1st Windows partition found
804 if (found==1){
805 winboot=sprintf("hd%d,%d",disk,part)
806 }
807 }
808 }
809 }
810 }
811 END{printf "%s", winboot}')
812 if [ -z "$WINBOOT" ]; then
813 warning $(gettext "No windows partition found. Dual-boot disabled")
814 TGT_WINBOOT=""
815 fi
816 fi
817 }
819 # Determine GRUB partition number and GRUB disk number.
820 grub_partition()
821 {
822 DISK_LETTER=${TGT_PARTITION#/dev/[h-s]d}
823 DISK_LETTER=${DISK_LETTER%[0-9]}
824 GRUB_PARTITION=$((${TGT_PARTITION#/dev/[h-s]d[a-z]}-1))
825 for disk in a b c d e f g h
826 do
827 nb=$(($nb+1))
828 if [ "$disk" = "$DISK_LETTER" ]; then
829 GRUB_DISK=$(($nb-1))
830 break
831 fi
832 done
833 GRUB_ROOT="(hd${GRUB_DISK},${GRUB_PARTITION})"
834 }
836 # Create grub conf
837 grub_config()
838 {
839 grub_partition
840 if [ "$TGT_GRUB" == "yes" ]; then
841 win_partition
842 fi
843 # Create the target GRUB configuration.
844 mkdir -p $TARGET_ROOT/boot/grub
845 cat > $TARGET_ROOT/boot/grub/menu.lst << _EOF_
846 # /boot/grub/menu.lst: GRUB boot loader configuration.
847 #
849 # By default, boot the first entry.
850 default 0
852 # Boot automatically after 8 secs.
853 timeout 8
855 # Graphical splash image.
856 splashimage=/boot/grub/splash.xpm.gz
858 # Change the colors.
859 #color yellow/brown light-green/black
861 # For booting SliTaz from : $TGT_PARTITION
862 #
863 title SliTaz GNU/Linux (cooking) (Kernel $KERNEL)
864 root $GRUB_ROOT
865 kernel /boot/$KERNEL root=$TGT_PARTITION quiet
867 _EOF_
868 if [ -n "$TGT_WINBOOT" ]; then
869 msg $(gettext "Enabling Windows dual-boot")
870 cat >> $TARGET_ROOT/boot/grub/menu.lst << _EOF_
871 # For booting Windows :
872 #
873 title Microsoft Windows
874 rootnoverify ($WINBOOT)
875 chainloader +1
877 _EOF_
878 fi
879 # log
880 debug "grub_config: $TARGET_ROOT/boot/grub/menu.lst"
881 echo "--- menu.lst -------------" >>$LOG
882 cat $TARGET_ROOT/boot/grub/menu.lst >>$LOG
883 echo "--- menu.lst -------------" >>$LOG
884 sleep 2
885 }
887 # Files install, calling for functions or with cmds.
888 install_files()
889 {
890 msg "$(gettext "Installing SliTaz on:") $TGT_PARTITION"
891 # saving pwd
892 local save_pwd=$(pwd)
894 debug "Cleaning the root partition if necessary..."
895 clean_target
897 debug "Extracting the root system..."
898 extract_rootfs $SOURCE_ROOT/boot
900 debug "Installing the kernel..."
901 install_kernel
903 debug "Copying the bootloader syslinux/isolinux..."
904 copy_bootloaders
906 debug "Preconfiguring the system..."
907 pre_config_system
909 msg "$(gettext "Configuring root and default user account:") $TGT_USER"
910 users_settings
912 if [ "$TGT_HOME" != "" ]; then
913 msg "$(gettext "Configuring partition to be used as /home:") $TGT_HOME"
914 home_config
915 sleep 2
916 fi
918 debug "Creating configuration file for GRUB (menu.lst)..."
919 grub_config
921 debug "Files installation completed"
922 sleep 2
923 # restoring pwd
924 cd $save_pwd
925 }
927 # GRUB info with disk name used for grub-install.
928 grub_install()
929 {
930 if [ "$TGT_GRUB" == "yes" ]; then
931 TARGET_DISK=`echo $TGT_PARTITION | sed s/"[0-9]"/''/`
932 msg "$(gettext "Running grub-install on:") $TARGET_DISK"
933 grub-install --no-floppy \
934 --root-directory=$TARGET_ROOT $TARGET_DISK 2>>$LOG
935 debug "Grub installation done..."
936 else
937 debug "Grub not installed"
938 fi
939 }
941 # Copy log file, umount target and eject cdrom.
942 umount_devices()
943 {
944 # Umount target
945 if mount | grep -q $TARGET_ROOT; then
946 echo "$(gettext "Unmounting target partition:") $TGT_PARTITION"
947 umount $TARGET_ROOT 2>>$LOG
948 fi
950 # Umount source
951 if mount | grep -q $SOURCE_ROOT; then
952 echo "$(gettext "Unmounting:") $SOURCE_ROOT"
953 umount $SOURCE_ROOT
954 fi
956 # Eject cd
957 if [ "$INST_TYPE" == "cdrom" ]; then
958 gettext "Ejecting cdrom..."
959 eject
960 fi
961 # Remove lock file
962 rm -f $LOCK
963 sleep 2
964 }
966 # End of installation.
967 end_of_install()
968 {
969 msg $(gettext "Installation complete. You can now restart (reboot)")
970 echo " $(gettext "from your SliTaz GNU/Linux system.")"
971 echo "=== Tazinst end at `date` ===" >>$LOG
972 # Log files
973 echo "$(gettext "Copying log files") ($LOG)..."
974 cp -a $LOG $TARGET_ROOT/var/log
975 sleep 2
976 # umount
977 umount_devices
978 }
980 #####################
981 # Upgrade functions #
982 #####################
984 # Mount.
985 prepare_upgrade()
986 {
987 debug "Preparing the target partition..."
988 # Target may be used
989 mount | grep -q $TGT_PARTITION && \
990 abort 5 "$TGT_PARTITION: $(gettext "Partition in use")"
991 # Mount point can be already used.
992 if mount | grep -q $TARGET_ROOT; then
993 umount $TARGET_ROOT 2>>$LOG
994 fi
995 mkdir -p $TARGET_ROOT && sleep 2
996 # Mount target.
997 mount $TGT_PARTITION $TARGET_ROOT >>$LOG 2>>$LOG
998 if [ $(mount | grep -c "mnt/target") == "0" ]; then
999 abort 5 "$TGT_PARTITION $(gettext "Unable to mount partition")"
1000 fi
1003 # Check for a valid SliTaz
1004 check_release()
1006 if [ -f $TARGET_ROOT/etc/slitaz-release ]; then
1007 release=`cat $TARGET_ROOT/etc/slitaz-release`
1008 msg "$(gettext "Preparing upgrade of SliTaz release:") $release"
1009 else
1010 abort 6 "$TGT_PARTITION: $(gettext "This partition doesn't appear to contain \
1011 a valid SliTaz system, the file: /etc/slitaz-release doesn't exist.")"
1012 fi && sleep 2
1015 # Backup target packages list.
1016 backup_files()
1018 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
1019 ls -1 var/lib/tazpkg/installed > home/packages-selection.list
1020 for dir in *
1021 do
1022 case "$dir" in
1023 boot)
1024 rm -rf boot/vmlinuz-* ;;
1025 home)
1026 mv home home.bak
1027 debug "keeping /home found on: $TGT_PARTITION" ;;
1028 etc)
1029 tar czf etc.tar.gz etc
1030 mv etc etc.bak
1031 debug "keeping /etc found on: $TGT_PARTITION" ;;
1032 var)
1033 if [ -d var/www ]; then
1034 mv var/www www.bak
1035 debug "keeping /var/www found on: $TGT_PARTITION"
1036 fi
1037 rm -rf var 2>>$LOG ;;
1038 lost+found)
1039 continue ;;
1040 *)
1041 debug "removing target: $dir"
1042 rm -rf $dir 2>>$LOG ;;
1043 esac
1044 done
1045 if [ -d mklost+found ]; then
1046 mklost+found 2>>$LOG
1047 fi
1048 sleep 2
1051 # Restore backups.
1052 restore_files()
1054 rm -rf $TARGET_ROOT/home
1055 mv $TARGET_ROOT/home.bak $TARGET_ROOT/home
1056 rm -rf $TARGET_ROOT/etc
1057 mv $TARGET_ROOT/etc.bak $TARGET_ROOT/etc
1058 if [ -d $TARGET_ROOT/www.bak ]; then
1059 rm -rf $TARGET_ROOT/var/www
1060 mv $TARGET_ROOT/www.bak $TARGET_ROOT/var/www
1061 fi
1062 debug "backups restored: `date`"
1064 # /var/lib/slitaz-installer
1065 mkdir -p $TARGET_ROOT/var/lib/tazinst
1066 mv $TARGET_ROOT/etc.tar.gz $TARGET_ROOT/var/lib/tazinst
1067 mv $TARGET_ROOT/home/packages-selection.list $TARGET_ROOT/var/lib/tazinst
1070 # Added pkgs
1071 install_pkgs()
1073 # Check if the pkg is on the mirror.
1074 debug "Checking the availability of packages..."
1075 touch packages-to-install.list
1076 packages=0
1077 diff=`cat packages-selection.diff | sort`
1078 for pkg in $diff
1079 do
1080 if grep -q ^$pkg-[0-9] /var/lib/tazpkg/packages.list; then
1081 packages=$(($packages+1))
1082 echo "$pkg" >> packages-to-install.list
1083 fi
1084 done
1086 # Install packages.
1087 debug "Installing any packages..."
1088 sleep 2
1089 if [ "$packages" == "0" ]; then
1090 debug "packages to install: 0"
1091 else
1092 # Get-install all missing pkgs.
1093 for pkg in `cat packages-to-install.list`
1094 do
1095 debug "Installing: $pkg..."
1096 # Get install package and answer yes in case of dependencies.
1097 pkgname=`grep ^$pkg /var/lib/tazpkg/packages.list`
1098 tazpkg get $pkg >/dev/null 2>/dev/null
1099 yes "" | tazpkg install $pkgname.tazpkg --root=$TARGET_ROOT >/dev/null 2>/dev/null
1100 rm -f $pkgname.tazpkg
1101 done
1102 fi
1103 debug "Installation of packages complete..."
1104 sleep 2
1107 # Search for added pkgs
1108 update_pkgs()
1110 cd $TARGET_ROOT/var/lib/tazinst || abort 8 $(gettext "Internal")
1111 # LiveCD packages list.
1112 debug "Creating package lists..."
1113 ls -1 $TARGET_ROOT/var/lib/tazpkg/installed > packages-source.list || exit 1
1114 debug "packages-source.list: done"
1115 # Diff
1116 diff packages-source.list packages-selection.list | \
1117 grep ^+[a-z] | sed s/^+// > packages-selection.diff
1118 debug "packages-selection.diff: done"
1119 # Get mirror list.
1120 tazpkg recharge >>$LOG 2>>$LOG
1121 if [ -f /var/lib/tazpkg/packages.list ]; then
1122 install_pkgs
1123 else
1124 touch packages-to-install.list
1125 warning $(gettext "The list of available packages on the mirror could not be \
1126 downloaded. No missing packages will be reinstalled now, but \
1127 you can do so later by looking at the following list: \
1128 /var/lib/tazinst/packages-selection.diff")
1129 fi
1130 sleep 2
1133 # Update grub conf
1134 grub_update()
1136 # Backup and create a new grub menu.lst.
1137 if [ "$TGT_GRUB" == "yes" ]; then
1138 msg $(gettext "Grub update")
1139 mv $TARGET_ROOT/boot/grub/menu.lst \
1140 $TARGET_ROOT/boot/grub/menu.lst.bak 2>/dev/null
1141 grub_config
1142 fi
1145 # Prepare the partition to upgrade, backup, install, restore configs
1146 # and reinstall pkgs.
1147 upgrade_files()
1149 # saving pwd
1150 local save_pwd=$(pwd)
1151 cd $TARGET_ROOT || abort 8 $(gettext "Internal")
1153 debug "Searching for /etc/slitaz-release"
1154 check_release
1156 msg $(gettext "Backup /etc, /home and the packages list...")
1157 backup_files
1159 msg "$(gettext "Upgrading SliTaz on:") $TGT_PARTITION"
1161 debug "Copying the bootloader syslinux/isolinux..."
1162 copy_bootloaders
1164 debug "Extracting the root system..."
1165 extract_rootfs $SOURCE_ROOT/boot
1167 msg $(gettext "Restoring configuration files...")
1168 restore_files
1170 debug "Installing the kernel..."
1171 install_kernel
1173 msg $(gettext "Upgrading added packages...")
1174 update_pkgs
1176 # restoring pwd
1177 cd $save_pwd
1180 # End of system upgrade.
1181 end_of_upgrade()
1183 pkgscd=`cat $TARGET_ROOT/var/lib/tazinst/packages-source.list | wc -l`
1184 pkginst=`cat $TARGET_ROOT/var/lib/tazinst/packages-to-install.list | wc -l`
1185 msg $(gettext "Upgrade finished. You can now restart (reboot)")
1186 echo $(gettext "from your SliTaz GNU/Linux system.")
1187 echo "$(gettext "Packages on the cdrom :") $pkgscd"
1188 echo "$(gettext "Packages installed from the mirror :") $pkginst"
1189 echo "=== Tazinst end at `date` ===" >>$LOG
1190 umount_devices
1193 ######################
1194 # Installer sequence #
1195 ######################
1197 case $1 in
1198 install)
1199 INST_ACTION=install
1200 check_root
1201 init $@
1202 read_setup_file $2
1203 check_vars
1204 check_source
1205 prepare_install
1206 install_files
1207 grub_install
1208 end_of_install ;;
1209 upgrade)
1210 INST_ACTION=upgrade
1211 check_root
1212 init $@
1213 read_setup_file $2
1214 check_vars
1215 check_source
1216 prepare_upgrade
1217 upgrade_files
1218 grub_update
1219 end_of_upgrade ;;
1220 new)
1221 gen_setup $2 ;;
1222 showurl)
1223 case $2 in
1224 stable)
1225 echo $URL_STABLE ;;
1226 cooking)
1227 echo $URL_COOKING ;;
1228 rolling)
1229 echo $URL_ROLLING ;;
1230 esac ;;
1231 check)
1232 LOG="/dev/null"
1233 INST_ACTION=check
1234 check_root
1235 init $@
1236 read_setup_file $2
1237 check_vars
1238 rm -f $LOCK ;;
1239 log)
1240 [ -r "$LOG" ] && cat $LOG ;;
1241 version)
1242 echo $VERSION ;;
1243 usage|*)
1244 usage ;;
1245 esac
1247 exit 0