tazinst view tazinst @ rev 100
Remove cat process
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sun Oct 20 12:17:10 2019 +0200 (2019-10-20) |
parents | 5c34d77a6168 |
children | 37246fc2557b |
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-2016 SliTaz - GNU General Public License v3.
9 #
10 # Authors : Christophe Lincoln <pankso@slitaz.org>
11 # Dominique Corbex <domcox@slitaz.org>
13 # Exit codes:
14 # 1: Parameters error
15 # 2: Install file error
16 # 3: Source error
17 # 4: Target error
18 # 5: Missing resource
19 # 6: SliTaz system to upgrade not found
20 # 7: Another instance is running
21 # 8: Internal error
22 # 9: User cancellation
25 # path
26 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
27 umask 0177
29 # read SliTaz conf
30 [ -r /etc/slitaz/slitaz.conf ] && . /etc/slitaz/slitaz.conf
32 # read Tazinst conf
33 [ -r /etc/slitaz/tazinst.conf ] && . /etc/slitaz/tazinst.conf
35 # version
36 readonly VERSION=3.95
38 # i18n
39 . /lib/libtaz.sh
40 export TEXTDOMAIN='tazinst'
43 # files
44 readonly DEFAULT_INSTALL_FILE=./tazinst.rc
45 readonly SOURCE_ROOT=/media/source
46 readonly TARGET_ROOT=/mnt/target
47 readonly LOG=/var/log/tazinst.log
48 readonly LOCK=/run/tazinst.pid
49 MIRRORS="${MIRRORS:-$LOCALSTATE/mirrors}"
51 # settings
52 readonly SETTINGS="mode media source \
53 root_uuid root_format home_uuid home_format \
54 hostname root_pwd user_login user_pwd \
55 bootloader winboot"
57 # modes (key:help)
58 readonly LST_MODE="
59 install:$(_ 'Fresh install on a HDD')
60 upgrade:$(_ 'Upgrade an existing system')"
62 # media (key:help)
63 readonly LST_MEDIA="
64 cdrom:$(_ 'LiveCD')
65 usb:$(_ 'LiveUSB')
66 iso:$(_ 'ISO image on a local drive')
67 web:$(_ 'ISO image on the Internet')"
69 # formats (key:help)
70 readonly LST_FORMAT="
71 btrfs:$(_ 'B-tree file system (Oracle)')
72 ext2:$(_ 'Second extended filesystem (Linux)')
73 ext3:$(_ 'Third extended filesystem (Linux)')
74 ext4:$(_ 'Fourth extended file system (Linux)')
75 jfs:$(_ 'Journaled File System (IBM)')
76 minix:$(_ 'File system of the MINIX operating system')
77 reiser4:$(_ 'Journaled computer file system (Namesys)')
78 xfs:$(_ 'Journaling file system (Silicon Graphics, Inc.)')"
80 # bootloaders (key:help)
81 readonly LST_BOOTLOADER="
82 auto:$(_ 'Automatic selection')
83 grub:$(_ 'GRUB legacy bootloader')
84 syslinux:$(_ 'Lightweight bootloader')"
86 # predefined iso (key:url:help)
87 SLITAZ_VERSION="${SLITAZ_VERSION:-cooking}"
88 [ "$SLITAZ_VERSION" = 'cooking' ] && SLITAZ_VERSION="$(($(date "+%y")-8)).0"
89 [ -n "$URL_ISO" ] && readonly LST_WEB="$URL_ISO" || readonly LST_WEB="
90 stable:iso/stable/slitaz-$SLITAZ_VERSION.iso \
91 :$(_ 'Stable release') $SLITAZ_VERSION
92 core:iso/stable/flavors/slitaz-$SLITAZ_VERSION-core.iso \
93 :$(_ 'Stable version without nested subsets')
94 base:iso/stable/flavors/slitaz-$SLITAZ_VERSION-base.iso \
95 :$(_ 'Stable text-only version (8.1MB)')
96 justx:iso/stable/flavors/slitaz-$SLITAZ_VERSION-justx.iso \
97 :$(_ 'Stable basic graphic version without graphic apps')
98 gtkonly:iso/stable/flavors/slitaz-$SLITAZ_VERSION-gtkonly.iso \
99 :$(_ 'Stable basic graphic version with only GTK')
100 cooking:iso/cooking/slitaz-cooking.iso \
101 :$(_ 'Development version for testing latest features')
102 rolling:iso/rolling/slitaz-rolling.iso \
103 :$(_ 'Bleeding edge development version updated every day')
104 "
107 #-------
108 # usage
109 #-------
111 # print a short help
112 usage()
113 {
114 _ 'SliTaz GNU/Linux Installer - Version: %s' "$VERSION"
115 newline; boldify "$(_ 'Usage:')"
116 echo -n ' '; _ '%s [command] <setting> <value> <file>' 'tazinst'
117 newline; boldify "$(_ 'Commands:')"
118 optlist "\
119 new $(_ 'Create a new install file.')
120 set $(_ 'Change value of a setting.')
121 unset $(_ 'Clear a setting.')
122 get $(_ 'Get the value of a setting.')
123 check $(_ 'Check settings.')
124 help $(_ 'Print a short help on settings')
125 list $(_ 'List system resources.')
126 execute $(_ 'Execute a SliTaz installation.')
127 log $(_ 'Display log file contents.')
128 clean $(_ 'Clean install and log files.')
129 version $(_ 'Print version and exit.')
130 usage $(_ 'Print this short usage.')
131 "
132 exit 0
133 }
135 usage_error()
136 {
137 local cmd="$1" script="$(basename $0)"
138 _ 'SliTaz GNU/Linux Installer - Version: %s' "$VERSION"
139 newline
140 _ "'%s': Unknown command!" "$cmd"
142 _ "Run: '%s' to get a list of available commands." "$script help"
143 exit 1
144 }
147 option_error()
148 {
149 local option="$1" list="$2"
150 _ 'SliTaz GNU/Linux Installer - Version: %s' "$VERSION"
151 newline
152 _ "'%s': Unknown option!" "$option"
154 _ 'Please select one of these options:'
155 printf " %s\n\n" "$list"
156 exit 1
157 }
159 #---------------------
160 # 1. settings section
161 #---------------------
163 #----------
164 # 1.1 file
165 #----------
167 # create a new install file
168 new_file()
169 {
170 local install_file=$1
171 [ -z "$install_file" ] && install_file="$DEFAULT_INSTALL_FILE"
172 if [ -e "$install_file" ]; then
173 _ 'Warning: file already exists.' 1>&2
174 exit 0
175 fi
176 [ -n "$install_file" ] && touch "$install_file"
177 if [ -w "$install_file" ]; then
178 write_file "$install_file"
179 else
180 _ 'Error: Cannot create file.' 1>&2
181 exit 2
182 fi
183 }
185 # fill up the install file
186 write_file()
187 {
188 local install_file="$1"
189 cat > "$install_file" <<EOT
190 # SliTaz GNU/Linux Installer - Version: $VERSION
191 #
192 # Install file.
193 #
195 # Mode of installation:
196 # install: Full install of SliTaz on a disk, all previous info will be erased
197 # upgrade: Upgrade an existing SliTaz installation to a new version
198 # run 'tazinst list mode' to have a full list.
199 MODE="$MODE"
201 # Media to install from:
202 # Options are cdrom usb iso web.
203 # run 'tazinst list media' to see available options on your system.
204 MEDIA="$MEDIA"
206 # Install source:
207 # it depends on the media used:
208 # usb: partition, run 'tazinst list uuid' to list your partitions
209 # iso: file.iso, ex: SOURCE=~/slitaz.5.0.iso
210 # web: url, ex: SOURCE=http://mirror.slitaz.org/../slitaz-cooking.iso
211 # web: iso names, ex: SOURCE=cooking
212 # run 'tazinst list <MEDIA>' to list source values. ex: tazinst list iso.
213 SOURCE="$SOURCE"
215 # root partition that SliTaz will be Installed on:
216 # Enter the UUID of the partition.
217 # run 'tazinst list uuid' to list your partitions
218 ROOT_UUID="$ROOT_UUID"
220 # Formatting the root partition:
221 # Let ROOT_FORMAT empty if you do not want to format the root partition.
222 # SliTaz uses ext2 ext3 ext4 by default but another filesystem can be
223 # installed if wanted, for this please adjust your /etc/fstab after
224 # installation.
225 # run 'tazinst list format' to list installed filesystems on your system.
226 ROOT_FORMAT="$ROOT_FORMAT"
228 # Home partition:
229 # On most GNU/Linux systems users personal files are stored in the directory
230 # /home. /home can be on another hard disk or on a separate partition.
231 # Leave HOME_UUID empty if you do not intend to use a specific partition for /home
232 # or enter the UUID of the partition to be used.
233 # run 'tazinst list uuid' to list your partitions.
234 HOME_UUID="$HOME_UUID"
236 # Formatting the /home partition (if /home is on a separate partition):
237 # Leave HOME_FORMAT empty if you do not want to format the /home partition.
238 # For options, see comments on 'Formatting the root partition' above.
239 HOME_FORMAT="$HOME_FORMAT"
241 # Hostname of the new system:
242 HOSTNAME="$HOSTNAME"
244 # root password:
245 # The root administrator privilege lets you manage and configure the full
246 # system. A root user can damage your system so you should always setup a
247 # strong password with special characters and/or numbers.
248 ROOT_PWD="$ROOT_PWD"
250 # Default user:
251 # The default user for the system will have his personal files stored
252 # in /home/<USER_LOGIN> (and will be automatically added to the audio group).
253 USER_LOGIN="$USER_LOGIN"
254 USER_PWD="$USER_PWD"
256 # Install bootloader:
257 # If you do not want to install a bootloader, leave this field empty.
258 # It's generally safe to set it up as 'auto'.
259 # Run 'tazinst list bootloader' to list all options.
260 BOOTLOADER="$BOOTLOADER"
262 # Windows dual boot:
263 # If you do not want enable Dual boot, leave WINBOOT empty (WINBOOT="").
264 # You may let tazinst automatically find your win partition by specifying auto
265 # (WINBOOT="auto"), otherwise enter the UUID of the partition to be used.
266 # Run 'tazinst list winboot' to see the Windows partitions found by tazinst.
267 WINBOOT="$WINBOOT"
269 EOT
270 return "$?"
271 }
273 read_file()
274 {
275 local install_file="$1"
276 [ -z "$install_file" ] && install_file="$DEFAULT_INSTALL_FILE"
277 if ! [ -r "$install_file" ]; then
278 _ 'Error: Unable to read install file.' 1>&2
279 exit 2
280 fi
281 #
282 if ! CONTENTS="$(cat "$install_file")"; then
283 _ 'Error: Unable to read install file.' 1>&2
284 exit 2
285 fi
286 }
288 # read value of a setting
289 get_value()
290 {
291 local setting="$1"
292 printf "%s" "$CONTENTS" | /bin/busybox awk -v setting="$setting" 'BEGIN{
293 setting="^" toupper(setting) "="
294 }
295 {
296 if (match($0,setting)){
297 n=index($0,"=")
298 value=substr($0,n+1)
299 gsub(/[\t\s]*$/,"",value)
300 sub(/^"/,"",value)
301 sub(/"$/,"",value)
302 }
303 }
304 END{
305 print value
306 }'
307 }
309 # list of settings
310 get_settings()
311 {
312 local "mode=$(get_value mode)"
313 case "$mode" in
314 upgrade)
315 echo "mode media source root_uuid bootloader winboot" ;;
316 *)
317 printf "%s\n" "$SETTINGS" ;;
318 esac
319 }
321 # get command
322 get()
323 {
324 local setting="$1"
325 [ -z "$setting" ] && setting="all"
326 # setting is valid: display value
327 if printf "%s" "$setting" | \
328 egrep -q "$(regex "$SETTINGS")"; then
329 get_value "$setting"
330 else
331 case "$setting" in
332 all)
333 for i in mode media source root_uuid root_format home_uuid home_format \
334 hostname root_pwd user_login user_pwd bootloader winboot; do
335 printf "%-15s: %s\n" "$i" "$(get_value $i)"
336 done
337 ;;
338 settings)
339 get_settings
340 ;;
341 *)
342 option_error "$1" "$SETTINGS"
343 ;;
344 esac
345 fi
346 }
348 # set command
349 change()
350 {
351 local setting="$1" value="$2" install_file="$3"
352 # validate setting
353 if ! printf "%s" "$setting" | \
354 egrep -q "$(regex "$SETTINGS")"; then
355 _ "Error: '%s' unknown setting." "$setting" 1>&2
356 exit 1
357 fi
358 # and file
359 [ -z "$install_file" ] && install_file="$DEFAULT_INSTALL_FILE"
360 # write changes to file
361 if [ -w "$install_file" ]; then
362 printf "%s" "$CONTENTS" | \
363 /bin/busybox awk -v setting="$setting" -v value="$value" '
364 BEGIN{
365 set=0
366 }
367 {
368 if (match($0,"^" toupper(setting) "=")){
369 printf toupper(setting) "=\"" value "\"\n"
370 set++
371 }
372 else
373 printf $0 "\n"
374 }
375 END{
376 if (! set)
377 printf toupper(setting) "=\"" value "\"\n"
378 }' > "$install_file"
379 # check new value
380 read_file "$install_file"
381 check "$setting"
382 else
383 _ 'Error: Unable to write to install file.' 1>&2
384 exit 2
385 fi
386 }
388 #
389 load_settings()
390 {
391 MODE="$(get mode)"
392 local settings="$(get settings)"
393 MEDIA="$(get media)"
394 echo "source" | egrep -q "$(regex "$settings")" \
395 && SOURCE="$(get source)" \
396 || unset SOURCE
397 ROOT_UUID="$(get root_uuid)"
398 echo "root_format" | egrep -q "$(regex "$settings")" \
399 && ROOT_FORMAT="$(get root_format)" \
400 || unset ROOT_FORMAT
401 echo "home_uuid" | egrep -q "$(regex "$settings")" \
402 && HOME_UUID="$(get home_uuid)" \
403 || unset HOME_UUID
404 echo "home_format" | egrep -q "$(regex "$settings")" \
405 && HOME_FORMAT="$(get home_format)" \
406 || unset HOME_FORMAT
407 echo "hostname" | egrep -q "$(regex "$settings")" \
408 && HOSTNAME="$(get hostname)" \
409 || unset HOSTNAME
410 echo "root_pwd" | egrep -q "$(regex "$settings")" \
411 && ROOT_PWD="$(get root_pwd)" \
412 || unset ROOT_PWD
413 echo "user_login" | egrep -q "$(regex "$settings")" \
414 && USER_LOGIN="$(get user_login)" \
415 || unset USER_LOGIN
416 echo "user_pwd" | egrep -q "$(regex "$settings")" \
417 && USER_PWD="$(get user_pwd)" \
418 || unset USER_PWD
419 echo "bootloader" | egrep -q "$(regex "$settings")" \
420 && BOOTLOADER="$(get bootloader)" \
421 || unset BOOTLOADER
422 echo "winboot" | egrep -q "$(regex "$settings")" \
423 && WINBOOT="$(get winboot)" \
424 || unset WINBOOT
425 }
433 # clean command
434 clean()
435 {
436 # rm LOG
437 [ -r "$LOG" ] && rm -f "$LOG"
438 # rm temp files
439 rm -rf /tmp/tazinst
440 # rm install file
441 local install_file="$1"
442 [ -z "$install_file" ] && install_file="$DEFAULT_INSTALL_FILE"
443 _ 'Deleting install file: %s' "$install_file"
444 if ! [ -w "$install_file" ]; then
445 _ 'Error: Unable to delete install file.' 1>&2
446 exit 2
447 else
448 rm -f "$install_file"
449 fi
450 }
452 #-----------
453 # 1.2 check
454 #-----------
456 # exit if user is not root.
457 check_root()
458 {
459 if [ $(id -u) -ne 0 ]; then
460 _ "You must be the root user (system administrator) \
461 to install SliTaz, please use 'su' to get a root SHell and restart \
462 installation." 1>&2
463 exit 1
464 fi
465 }
467 # exit if another instance of tazinst is running
468 check_instance()
469 {
470 if [ -e "$LOCK" ]; then
471 _ 'Another instance of tazinst is running.' 1>&2
472 exit 7
473 else
474 printf "%s" "$$" > $LOCK
475 fi
476 }
478 # exit if the setting is not in a list of keywords
479 check_key()
480 {
481 local setting="$1" keylist="$2" keyword="$(get $1)" msg
482 if ! printf "%s" "$keyword" | \
483 egrep -q "$(regex "$keylist")"; then
484 msg="$setting=$keyword
485 $(_ "Error: '%s' Invalid keyword." "$keyword")
486 $(_ 'Select one of these options: %s.' "$keylist")
487 $(_ 'For more information, see tazinst Manual.')"
488 printf "%s\n" "$msg" 1>&2
489 exit 1
490 fi
491 }
493 # exit if the partition does not exist
494 check_uuid()
495 {
496 local setting="$1" value="$(get $1)" found=0 partition msg
497 for partition in $(list_uuid); do
498 [ "$partition" = "$value" ] && found="$(($found + 1))"
499 done
500 if [ "$found" != "1" ]; then
501 msg="$(gettext "$setting")=$value
502 $(_ 'Error: Partition not found.')
503 $(_ "To see available partitions, run '%s'." 'tazinst list uuid')"
504 printf "%s\n" "$msg" 1>&2
505 exit 1
506 fi
507 }
509 # exit if the source does not exist
510 check_source()
511 {
512 local media="$(get media)" source="$(get source)"
513 case $media in
514 usb)
515 check_uuid source ;;
516 iso)
517 if [ ! -r "$source" ]; then
518 _ 'Error: Source file not found.' 1>&2
519 exit 1
520 fi ;;
521 web)
522 local valid=0
523 # check full url (http://...)
524 local regexp="^(https?|ftp):\/\/([a-z0-9\-]+\.)?[a-z0-9\-]+\.\
525 [a-z0-9]{2,4}(\.[a-z0-9]{2,4})?(\/.*)?iso$"
526 printf "%s" "$source" | \
527 egrep -q "$regexp" && valid=$(($valid+1))
528 # check iso names (stable cooking...)
529 regexp="$(regex "$(list web)")"
530 printf "%s" "$source" | \
531 egrep -q "$regexp" && valid=$(($valid+1))
532 if [ "$valid" -le "0" ]; then
533 _ 'Error: invalid URL.' 1>&2
534 exit 1
535 fi
536 esac
537 }
539 # exit if a partition is selected more than once
540 check_uuid_mix()
541 {
542 local list all nodup
543 list="$(get root_uuid) $(get source) $(get home_uuid) $(get winboot)"
544 all="$(printf "%s" "$list" | wc -w)"
545 nodup="$(printf "%s" "$list" | /bin/busybox awk 'BEGIN{RS=" "}{print $0}' \
546 | sort | uniq | wc -w)"
547 if [ "$all" != "$nodup" ]; then
548 _ 'Error: multiple assignations for a disk.' 1>&2
549 exit 1
550 fi
551 }
554 # exit if a password is invalid
555 check_password()
556 {
557 local pass="$(get "$1")"
558 local invalid="^[A-Za-z0-9!@#$%^&*()_]{0,40}$"
559 local errcode=0
560 # too long
561 if [ "${#pass}" -ge 40 ]; then
562 _ 'Error: password too long.' 1>&2
563 exit 1
564 fi
565 # bad chars
566 if ! (printf "%s" "$pass" | egrep -q "$invalid"); then
567 _ 'Error: Unallowed characters in password.' 1>&2
569 exit 1
570 fi
571 # short pwd
572 [ "${#pass}" -le 4 ] && errcode=128
573 # empty pwd
574 [ -z "$pass" ] && errcode=129
575 case "$errcode" in
576 128)
577 _ 'Warning: short password!' 1>&2 ;;
578 129)
579 _ 'Warning: no password!' 1>&2 ;;
580 esac
581 return "$errcode"
582 }
584 # exit if a name is invalid
585 check_name()
586 {
587 local name="$1" value="$(get "$1")" msg
588 msg="$name=$value"
589 if [ "${#value}" -lt 2 ]; then
590 _ '%s Error: Too short.' "$msg" 1>&2
591 exit 1
592 fi
593 if [ "${#value}" -gt 32 ]; then
594 _ '%s Error: Too long.' "$msg" 1>&2
595 exit 1
596 fi
597 if printf "%s" "$value" | \
598 grep -q "[[:space:]\&\"\'\(\)\|\*\\#\`\+\:/;<>]"; then
599 _ '%s Error: Invalid chars.' "$msg" 1>&2
600 exit 1
601 fi
602 }
604 # check bootloader + winboot
605 check_boot_mix()
606 {
607 local bootloader=$(get bootloader)
608 local winboot=$(get winboot)
609 if [ -z "$bootloader" ] && [ -n "$winboot" ]; then
611 _ 'Error: Dualboot set with no bootloader.' 1>&2
612 exit 1
613 fi
614 }
616 # exit if partition table is not in list
617 check_table()
618 {
619 local pt_list="gpt msdos"
620 # get root uuid
621 local uuid="$(get root_uuid)"
622 if [ "$(/sbin/blkid | grep -c "$uuid")" = "1" ]; then
623 if ! printf "%s" "$(p_table $uuid)" | \
624 egrep -q "$(regex "$pt_list")"; then
625 _ 'Error: Unsupported Partition Table.' 1>&2
626 exit 1
627 fi
628 else
630 _ 'Error: No disk selected, cannot install any bootloader.' 1>&2
631 exit 1
632 fi
633 }
636 # check all settings()
637 check_all()
638 {
639 # check only settings we need
640 for key in $(get settings); do
641 case "$key" in
642 mode)
643 printf "%-15s: " "mode"
644 check_key mode "$(key "$LST_MODE")" && echo "ok" ;;
645 media)
646 printf "%-15s: " "media"
647 check_key media "$(key "$LST_MEDIA")" && echo "ok" ;;
648 source)
649 printf "%-15s: " "source"
650 check_source && echo "ok" ;;
651 root_uuid)
652 printf "%-15s: " "root_uuid"
653 check_uuid root_uuid && echo "ok" ;;
654 root_format)
655 printf "%-15s: " "root_format"
656 [ -n "$(get root_format)" ] && \
657 { check_key root_format "$(key "$LST_FORMAT")" && echo "ok"; } \
658 || echo "ok" ;;
659 home_uuid)
660 printf "%-15s: " "home_uuid"
661 [ -n "$(get home_uuid)" ] && \
662 { check_uuid home_uuid && check_uuid_mix && echo "ok"; } \
663 || echo "ok" ;;
664 home_format)
665 printf "%-15s: " "home_format"
666 [ -n "$(get home_format)" ] && \
667 { check_key home_format "$(key "$LST_FORMAT")" && echo "ok"; } \
668 || echo "ok" ;;
669 hostname)
670 printf "%-15s: " "hostname"
671 check_name hostname && echo "ok" ;;
672 root_pwd)
673 printf "%-15s: " "root_password"
674 check_password root_pwd && echo "ok" ;;
675 user_login)
676 printf "%-15s: " "user_login"
677 check_name user_login && echo "ok" ;;
678 user_pwd)
679 printf "%-15s: " "user_password"
680 check_password user_pwd && echo "ok" ;;
681 bootloader)
682 printf "%-15s: " "bootloader"
683 [ -n "$(get bootloader)" ] \
684 && { check_key bootloader "$(list_bootloader)" \
685 && check_table && echo "ok" ; } \
686 || echo "ok" ;;
687 winboot)
688 printf "%-15s: " "winboot"
689 [ -n "$(get winboot)" ] && [ "$(get winboot)" != "auto" ] \
690 && { check_uuid winboot && check_boot_mix && echo "ok"; } \
691 || echo "ok" ;;
692 esac
693 done
694 }
696 # check command
697 check()
698 {
699 local setting="$1"
700 case "$setting" in
701 mode)
702 check_key mode "$(key "$LST_MODE")" ;;
703 media)
704 check_key media "$(key "$LST_MEDIA")" ;;
705 source)
706 check_source ;;
707 root_uuid)
708 check_uuid root_uuid
709 check_uuid_mix ;;
710 home_uuid)
711 [ -z "$(get home_uuid)" ] || check_uuid home_uuid
712 check_uuid_mix ;;
713 root_format)
714 [ -z "$(get root_format)" ] \
715 || check_key root_format "$(key "$LST_FORMAT")" ;;
716 home_format)
717 [ -z "$(get home_format)" ] \
718 || check_key home_format "$(key "$LST_FORMAT")" ;;
719 hostname)
720 check_name hostname ;;
721 root_pwd)
722 check_password root_pwd ;;
723 user_login)
724 check_name user_login ;;
725 user_pwd)
726 check_password user_pwd ;;
727 bootloader)
728 [ -z "$(get bootloader)" ] \
729 || (check_key bootloader "$(list_bootloader)" \
730 && check_table ; ) ;;
731 winboot)
732 ([ -z "$(get winboot)" ] || [ "$(get winboot)" = "auto" ]) \
733 || check_uuid winboot && check_boot_mix ;;
734 ""|all)
735 check_all ;;
736 *)
737 option_error "$setting" "$SETTINGS" ;;
738 esac
739 }
741 #----------
742 # 1.3 help
743 #----------
745 help_source()
746 {
748 _ 'The Source setting depends on the type of media:'
749 printf "%-12s%s\n" "cdrom" "$(help cdrom)"
750 printf "%-12s%s\n" "usb" "$(help usb)"
751 printf "%-12s%s\n" "iso" "$(help iso)"
752 printf "%-12s%s" "web" "$(_ 'Name or URL of the image on the web.')"
753 printf "%s\n" "$(_ 'Type: %s' 'tazinst help web')"
754 }
756 help_all()
757 {
758 _ 'List of settings:'
759 optlist "\
760 mode $(_ 'Mode of install')
761 media $(_ 'Media containing the SliTaz source files')
762 source $(_ 'Source file containing SliTaz')
763 root_uuid $(_ 'The name of the target partition')
764 root_format $(_ 'Format of the target partition')
765 home_uuid $(_ 'Separate home partition')
766 home_format $(_ 'Format of the root partition')
767 hostname $(_ 'Name of the system')
768 root_pwd $(_ 'Superuser password')
769 user_login $(_ 'First user name')
770 user_pwd $(_ 'First user password')
771 bootloader $(_ 'Install a bootloader')
772 winboot $(_ 'Partition to dualboot Windows from')
773 "
774 }
776 # help command
777 help()
778 {
779 local setting="$1"
780 case "$setting" in
781 mode)
782 printf "%s" "$LST_MODE" | \
783 /bin/busybox awk -F: '/..*/{printf "%-12s%s\n", $1, $2}' ;;
784 media)
785 printf "%s" "$LST_MEDIA" | \
786 /bin/busybox awk -F: '/..*/{printf "%-12s%s\n", $1, $2}' ;;
787 source)
788 help_source ;;
789 cdrom)
790 _ 'CD. Automatically set' ;;
791 usb)
793 _ 'USB partition. For a list, type: %s' 'tazinst list usb' ;;
794 iso)
796 _ 'ISO file name. For a list, type: %s' 'tazinst list iso' ;;
797 web)
798 printf "%s" "$LST_WEB" | \
799 /bin/busybox awk -F: '/..*/{printf "%-12s%s\n", $1, $3}' ;;
800 root_uuid)
801 /sbin/blkid -s TYPE -s LABEL | sort ;;
802 root_format)
803 printf "%s" "$LST_FORMAT" | \
804 /bin/busybox awk -F: '/..*/{printf "%-12s%s\n", $1, $2}' ;;
805 home_uuid)
806 /sbin/blkid -s TYPE -s LABEL | sort ;;
807 home_format)
808 printf "%s" "$LST_FORMAT" | \
809 /bin/busybox awk -F: '/..*/{printf "%-12s%s\n", $1, $2}' ;;
810 hostname)
811 _ 'Name of the system' ;;
812 root_pwd)
813 _ 'Superuser password' ;;
814 user_login)
815 _ 'First user name' ;;
816 user_pwd)
817 _ 'First user password' ;;
818 bootloader)
819 printf "%s" "$LST_BOOTLOADER" | \
820 /bin/busybox awk -F: '/..*/{printf "%-12s%s\n",$1,$2}' ;;
821 winboot)
822 _ "Partition containing Windows, or 'auto'" ;;
823 ""|all)
824 help_all ;;
825 *)
826 option_error "$setting" "$SETTINGS" ;;
827 esac
828 }
830 #---------
831 # 2. list
832 #---------
834 list_media()
835 {
836 local key media
837 for key in $(key "$LST_MEDIA") ; do
838 case "$key" in
839 cdrom)
840 [ -e "/dev/cdrom" ] && media="cdrom" ;;
841 usb)
842 [ -e "/sys/bus/usb" ] && media="$media usb" ;;
843 web)
844 [ "$(ifconfig -a | grep -c Link)" -gt 2 ] \
845 && media="$media web" ;;
846 *)
847 media="$media $key"
848 esac
849 done
850 printf "%s\n" "$media" | sed 's/^\s//'
851 }
853 list_usb()
854 {
855 # List plugged USB disks
856 if [ -d /proc/scsi/usb-storage ]; then
857 for DEV in /sys/block/sd* ; do
858 if readlink $DEV | grep -q usb; then
859 DEV=$(basename $DEV)
860 if [ -d /sys/block/${DEV}/${DEV}1 ]; then
861 /sbin/blkid /dev/$DEV* | tr ' ' '\n' | /bin/busybox awk '
862 /^\/dev\// {
863 DEV=$1
864 gsub(/:/,"",DEV)
865 }
866 /^UUID/ {
867 UUID=$1
868 gsub(/"/,"",UUID)
869 printf "%s %s\n",UUID,DEV}'
870 fi
871 fi
872 done
873 fi
874 }
876 list_iso()
877 {
878 for i in /root/*.iso /home/*/*.iso /home/*/*/*.iso ; do
879 printf "%s" $i | grep -v "*"
880 done
881 }
883 list_format()
884 {
885 local fs
886 type mkfs.btrfs > /dev/null && fs="btrfs"
887 type mkfs.ext2 > /dev/null && fs="$fs ext2"
888 type mkfs.ext3 > /dev/null && fs="$fs ext3"
889 type mkfs.ext4 > /dev/null && fs="$fs ext4"
890 type mkfs.jfs > /dev/null && fs="$fs jfs"
891 type mkfs.minix > /dev/null && fs="$fs minix"
892 type mkfs.reiser4 > /dev/null && fs="$fs reiser4"
893 type mkfs.xfs > /dev/null && fs="$fs xfs"
894 printf "%s" "$fs" | sed 's/^\s//'
895 }
897 # list partitions
898 list_uuid()
899 {
900 # list all drives but cdroms
901 /sbin/blkid -o export| /bin/busybox awk '
902 BEGIN{FS="="}
903 /DEVNAME/{dev=$2, type="unknown", label=""}
904 /LABEL/{label=$2"Â "}
905 /TYPE/{type=$2}
906 /PARTUUID/{if (type != "iso9660"){
907 printf "%s %s(%s)\n", dev, label, type}}' | sort -k 2
908 }
910 list_partition_table()
911 {
912 /usr/sbin/parted -lms 2>&1 | \
913 /bin/busybox awk -F: '/^\/dev\//{printf "%s: %s\n", $1,$6}'
914 }
916 list_web()
917 {
918 local key="$1"
919 # print url of a given iso
920 if printf "%s" "$LST_WEB" | egrep -q "^$key:"; then
921 printf "%s" "$LST_WEB" | egrep "^$key:" | \
922 /bin/busybox awk -F: '{print $2}'
923 fi
924 # print all key
925 if [ -z "$key" ]; then
926 key "$LST_WEB"
927 fi
928 }
930 list_bootloader()
931 {
932 local btlr
933 type grub-install > /dev/null && btlr=" grub"
934 type syslinux > /dev/null && btlr="$btlr syslinux"
935 [ -n "$btlr" ] && printf "%s\n" "auto$btlr"
936 }
938 # list Windows partitions
939 list_winboot()
940 {
941 /usr/sbin/parted -lms 2>&1 | /bin/busybox awk '
942 BEGIN{
943 FS=":"
944 disknum=-1
945 found=0
946 winboot=""
947 printf "auto"
948 }
949 {
950 # Count disks
951 if (match($1,"^/dev")){
952 disknum++
953 part=0
954 disk=substr($1,1,8)
955 dev=substr($1,6,3)
956 # get removable status
957 file="/sys/block/"dev"/removable"
958 "cat " file | getline removable
959 close("cat ")
960 }
961 # Count partitions
962 if (match($1,"[0-9][0-9]?")){
963 # List fixed drives only
964 if (removable==0){
965 part++
966 # Read partition Id
967 if (match($7,"boot")){
968 fs=$5
969 # Detect Windows Partition Type: ntfs vfat
970 WPT="ntfs|vfat"
971 if (fs ~ WPT){
972 found++
973 # record 1st Windows partition found
974 if (found==1){
975 printf(" %s%d",disk,part)
976 }
977 }
978 }
979 }
980 }
981 }
982 END{printf "\n"}'
983 }
985 # list command
986 list()
987 {
988 local ressource="$1"
989 case "$ressource" in
990 mode)
991 printf "%s\n" "$(key "$LST_MODE")" ;;
992 media)
993 list_media ;;
994 iso)
995 list_iso ;;
996 usb)
997 list_usb ;;
998 web)
999 list_web "$2" ;;
1000 uuid)
1001 list_uuid ;;
1002 format)
1003 list_format ;;
1004 bootloader)
1005 list_bootloader ;;
1006 winboot)
1007 list_winboot ;;
1008 partition_table)
1009 list_partition_table ;;
1010 ""|all)
1011 printf "* mode:\n%s\n\n" "$(key "$LST_MODE")"
1012 printf "* media:\n%s\n\n" "$(list_media)"
1013 printf "* usb:\n%s\n\n" "$(list usb)"
1014 printf "* iso:\n%s\n\n" "$(list_iso)"
1015 printf "* web:\n%s\n\n" "$(list_web)"
1016 printf "* uuid:\n%s\n\n" "$(list_uuid)"
1017 printf "* format:\n%s\n\n" "$(list_format)"
1018 printf "* bootloader:\n%s\n\n" "$(list_bootloader)"
1019 printf "* partition_table:\n%s\n\n" "$(list_partition_table)"
1020 printf "* winboot:\n%s\n" "$(list_winboot)"
1021 ;;
1022 *)
1023 local options="mode media usb iso web uuid format bootloader \
1024 partition_table winboot"
1025 option_error "$1" "$options" ;;
1026 esac
1027 }
1029 #----------
1030 # 3. tools
1031 #----------
1033 # list indexes from a list
1034 key()
1035 {
1036 printf "%s" "$1" | /bin/busybox awk -F: 'BEGIN{
1037 other=-1
1038 }
1039 !/^#|^$/{
1040 if(other){
1041 printf "%s", $1
1042 other++}
1043 else
1044 printf " %s", $1
1045 }
1046 END{printf "\n"}'
1047 }
1049 # convert a list of words to a regex
1050 regex()
1051 {
1052 printf "%s" "^$1$" | sed s'/ /$|^/g'
1053 }
1055 # print dev from uuid
1056 uuid2dev()
1057 {
1058 local uuid="$1" id
1059 if [ "$(printf "%s" $uuid | cut -d '=' -f1)" = "UUID" ]; then
1060 id="$(printf "%s" $uuid | cut -d'=' -f2)"
1061 printf "$(/sbin/blkid -U $id)"
1062 else
1063 printf "%s" "$uuid"
1064 fi
1065 }
1067 # print disk from uuid
1068 uuid2disk()
1069 {
1070 local uuid="$1"
1071 printf "%s" "$(uuid2dev $uuid | /bin/busybox awk '{print substr($0,1,8)}')"
1072 }
1074 dev2uuid()
1075 {
1076 local uuid="$1"
1077 if printf "%s" "$uuid" | grep -q dev; then
1078 printf "UUID=%s" "$(/sbin/blkid -p -i -o udev "$uuid" \
1079 | grep ID_FS_UUID= | cut -d '=' -f2)"
1080 else
1081 printf "%s" "$uuid"
1082 fi
1083 }
1085 # print partition scheme from uuid
1086 p_table()
1087 {
1088 local uuid="$1" device
1089 device="$(uuid2disk $uuid)"
1090 printf "%s" "$(/usr/sbin/parted -lms 2>&1 | grep "$device" | \
1091 cut -d':' -f6)"
1092 }
1094 # print filesystem from uuid
1095 filesys()
1096 {
1097 local uuid="$1"
1098 local fs="$(/sbin/blkid -s UUID -s TYPE | \
1099 grep "$(uuid2dev $uuid)" | cut -d' ' -f3)"
1100 fs="${fs#TYPE=\"}"
1101 fs="${fs%\"}"
1102 printf "%s" "$fs"
1103 }
1105 # return removable status from uuid
1106 is_removable()
1107 {
1108 local uuid="$1" removable=1
1109 local disk="$(uuid2disk $uuid | /bin/busybox awk '{print substr($0,6,3)}')"
1110 if [ "$(printf "%s" $disk | wc -w)" -eq "1" ]; then
1111 [ "$(cat /sys/block/"$disk"/removable)" -gt "0" ] \
1112 && removable=0
1113 fi
1114 return "$removable"
1115 }
1117 randomize_mirrors()
1118 {
1119 local list_mirrors
1120 if [ -r "$MIRRORS" ]; then
1121 # randomize list of mirrors
1122 list_mirrors="$(cat $MIRRORS | \
1123 /bin/busybox awk 'BEGIN {srand()}{
1124 printf "%05.0f %s \n",rand()*99999, $0;
1125 }' | sort -n | sed 's/^[0-9]* //' )"
1126 else
1127 log "$(_ 'No mirror list found, run %s.' 'tazpkg recharge')"
1128 list_mirrors="http://mirror.slitaz.org/"
1129 fi
1130 MIRRORS="$list_mirrors"
1131 }
1133 # download a file
1134 dnl()
1135 {
1136 local file="$1" mirror transfer=0 oldfile
1137 mkdir -p /tmp/tazinst
1138 for mirror in $MIRRORS; do
1139 log "$(_ 'Downloading: %s' "$mirror$file")"
1140 oldfile="$(printf "%s" $file | \
1141 /bin/busybox awk 'BEGIN{RS="/"}{text=$1}END{print text}')"
1142 [ -e "/tmp/tazinst/$oldfile" ] && rm -f "/tmp/tazinst/$oldfile"
1143 /bin/busybox wget $mirror$file -P /tmp/tazinst && transfer=1 && break
1144 done
1145 if [ "$transfer" -gt "0" ];then
1146 log "$(_ 'Download completed.')"
1147 else
1148 error 5 "(_ '%s: Download failed.' "$file")"
1149 fi
1150 }
1152 # install pkg in current system
1153 need_package()
1154 {
1155 local pkg="$1"
1156 if [ ! -d /var/lib/tazpkg/installed/$pkg ]; then
1157 log "$(_ 'Installing package %s to the current system.' "$pkg")"
1158 [ -z "$RECHARGE" ] && RECHARGE=true && /usr/bin/tazpkg recharge
1159 /usr/bin/tazpkg get-install "$pkg" || error 5 "$(_ 'Cannot install %s.' "$pkg")"
1160 fi
1161 }
1163 # install pkg in target system
1164 add_pkg()
1165 {
1166 local pkg="$1"
1167 log "$(_ 'Adding package %s to the target system...' "$pkg")"
1168 [ -z "$RECHARGE" ] && RECHARGE=true && /usr/bin/tazpkg recharge
1169 /usr/bin/tazpkg get "$pkg" >> "$LOG" 2>> "$LOG"
1170 yes "" | /usr/bin/tazpkg install $pkg.tazpkg \
1171 --root=$TARGET_ROOT >> "$LOG" 2>> "$LOG"
1172 rm -f $pkg.tazpkg
1173 }
1175 #---------
1176 # 3.1 log
1177 #---------
1179 # start log
1180 log_open()
1181 {
1182 _ '=== Tazinst: started on %s ===' "$(date "+%x %X")" > "$LOG"
1183 LOGGING="true"
1184 }
1186 # print and log a comment
1187 log(){
1188 # for front-ends, sleep 1 if $1 is num
1189 printf "%s" "$1" | awk '{
1190 num=$1+0
1191 if(num>0 && num<=100)
1192 exit 0
1193 else
1194 exit 1
1195 }' && sleep 1
1196 # log
1197 printf "%s\n" "$1"
1198 [ -n "$LOGGING" ] && printf "%s\n" "$1" >> $LOG
1199 }
1201 #--------------------
1202 # 3.2 error handling
1203 #--------------------
1205 # print an error msg & exit
1206 error()
1207 {
1208 local error="$1" msg="$2" cancel="$(_ 'Process not completed')"
1209 _ 'Error: %s' "$msg" >&2; echo "$cancel" >&2
1210 if [ -n "$LOGGING" ]; then
1211 # 1st pattern
1212 echo "-x-x- " >> "$LOG"
1213 _ 'Error: %s' "$msg" >> "$LOG"; echo "$cancel" >> "$LOG"
1214 # 2nd pattern
1215 echo "x-x-x " >> "$LOG"
1216 _ '=== Tazinst error on %s ===' "$(date "+%x %X")" >> "$LOG"
1217 fi
1218 unset LOGGING
1219 umount_devices
1220 exit "$error"
1221 }
1223 error8()
1224 {
1225 error 8 "$(_ 'Internal error')"
1226 }
1228 error9()
1229 {
1230 error 9 "$(_ 'Cancelled by user')"
1231 }
1233 #------------------
1234 # 4. disks section
1235 #------------------
1237 #------------
1238 # 4.1 source
1239 #------------
1241 # liveCD
1242 mount_cdrom()
1243 {
1244 # set device name
1245 local drive="$(grep "drive name" < \
1246 /proc/sys/dev/cdrom/info | cut -f 3)"
1247 [ -n "$drive" ] || drive=cdrom
1248 local cdrom=/dev/$drive
1249 # mount cdrom
1250 if mount -t iso9660 "$cdrom" "$SOURCE_ROOT" 2>>"$LOG"; then
1251 log "$(_ 'Using files from %s.' "$cdrom")"
1252 else
1253 error 3 "$(_ '%s: Mount failed.' "$cdrom")"
1254 fi
1255 }
1257 # liveUSB
1258 mount_usb()
1259 {
1260 # /home is on LiveUSB
1261 if [ -d /home/boot ]; then
1262 log "$(_ 'Using files from USB device...')"
1263 ln -s /home/boot $SOURCE_ROOT/boot
1264 else
1265 # mount LiveUSB
1266 if mount "$SOURCE" "$SOURCE_ROOT" 2>>"$LOG"; then
1267 log "$(_ 'Using files from USB device %s.' "$SOURCE")"
1268 else
1269 error 3 "$(_ '%s: Failed to mount USB device.' "$SOURCE")"
1270 fi
1271 fi
1272 }
1274 # ISO file on HDD
1275 mount_iso()
1276 {
1277 # check integrity
1278 local md5file=$(printf "%s" $SOURCE | sed 's/.iso$/.md5/')
1279 if [ -r "$md5file" ]; then
1280 local md5ref="$(cut -d' ' -f1 < "$md5file")"
1281 local md5calc="$(md5sum $SOURCE | cut -d' ' -f1)"
1282 if [ ! "$md5calc" = "$md5ref" ]; then
1283 log "md5sum iso=$md5ref md5sum tazinst=$md5calc"
1284 error 3 "$(_ 'md5sum error, file corrupted.')"
1285 fi
1286 else
1287 log "$(_ '%s: md5 file not found, cannot check integrity.' "$SOURCE")"
1288 fi
1289 # mount ISO
1290 if mount -o loop -t iso9660 "$SOURCE" \
1291 "$SOURCE_ROOT" 2>>"$LOG"; then
1292 log "$(_ 'Using files from ISO %s.' "$SOURCE")"
1293 else
1294 error 3 "$(_ '%s: Failed to mount ISO.' "$SOURCE")"
1295 fi
1296 }
1298 # ISO file on the web
1299 mount_web()
1300 {
1301 if (printf "%s" "$SOURCE" | egrep -q "$(regex "$(list web)")"); then
1302 SOURCE="$(list web $SOURCE)"
1303 fi
1304 dnl "$SOURCE"
1305 local md5file="$(printf "%s" $SOURCE | sed 's/.iso$/.md5/')"
1306 dnl "$md5file"
1307 local webiso="$(printf "%s" $SOURCE | \
1308 /bin/busybox awk 'BEGIN{RS="/"}{out=$1} END{printf "%s",out}')"
1309 SOURCE="/tmp/tazinst/$webiso"
1310 mount_iso
1311 }
1313 # set up source and check SliTaz' content
1314 mount_source()
1315 {
1316 log "$(_ 'Creating mount point: %s...' "$SOURCE_ROOT")"
1317 mkdir -p "$SOURCE_ROOT"
1318 case "$MEDIA" in
1319 cdrom)
1320 mount_cdrom ;;
1321 usb)
1322 mount_usb ;;
1323 iso)
1324 mount_iso ;;
1325 web)
1326 mount_web ;;
1327 *)
1328 error8 ;;
1329 esac
1331 # exit if no rootfs.gz found.
1332 log "$(_ 'Checking installation media...')"
1333 if [ ! -f $SOURCE_ROOT/boot/rootfs.gz -a \
1334 ! -f $SOURCE_ROOT/boot/rootfs1.gz ]; then
1335 error 3 "$(_ 'Invalid source')"
1336 else
1337 log "$(_ 'Installation media checked ok')"
1338 fi
1339 }
1341 #------------
1342 # 4.2 target
1343 #------------
1345 # format a partition
1346 format()
1347 {
1348 local uuid="$1" fmt="$2" dest="$3" dev format
1349 log "$(_ 'Format %s (%s)' "$uuid" "$fmt")"
1350 format="mkfs.$fmt"
1351 if (printf "%s" "$uuid" | grep -q "UUID="); then
1352 # case UUID=
1353 dev="$(uuid2dev $uuid)"
1354 "$format" "$dev" >>"$LOG" 2>>"$LOG" || error 4 "$(_ 'Formatting has failed')"
1355 # uuid has changed
1356 case "$dest" in
1357 root_uuid)
1358 ROOT_UUID="$(/sbin/blkid -p "$dev" -o export | \
1359 grep ^UUID=)" ;;
1360 home_uuid)
1361 HOME_UUID="$(/sbin/blkid -p "$dev" -o export | \
1362 grep ^UUID=)" ;;
1363 esac
1364 else
1365 # case /dev/xxxx
1366 "$format" $uuid >>"$LOG" 2>>"$LOG" || error 4 "$(_ 'Formatting has failed')"
1367 fi
1368 }
1370 # prepare partitions
1371 prepare_uuid()
1372 {
1373 log "$(_ 'Preparing target partition...')"
1374 local uuid
1375 # target may be in use
1376 if mount | grep -q "$ROOT_UUID" ;then
1377 log "$(_ 'Partition is already mounted, unmounting.')"
1378 umount "$ROOT_UUID"
1379 fi
1380 mount | grep -q "$ROOT_UUID" && \
1381 error 4 "$ROOT_UUID: $(_ 'Partition is already in use.')"
1382 # Mount point can be already used.
1383 mount | grep -q "$TARGET_ROOT" && \
1384 umount "$TARGET_ROOT" 2>>"$LOG"
1386 # Formatting root partition
1387 case "$ROOT_FORMAT" in
1388 "")
1389 log "$(_ '%s: The partition will be cleaned...' "$ROOT_UUID")" ;;
1390 *)
1391 format "$ROOT_UUID" "$ROOT_FORMAT" root_uuid
1392 esac
1394 # Formatting /home
1395 if [ -n "$HOME_UUID" ]; then
1396 case "$HOME_FORMAT" in
1397 "")
1398 log "$(_ '%s: The partition will be kept...' "$HOME_UUID")" ;;
1399 *)
1400 format "$HOME_UUID" "$HOME_FORMAT" home_uuid
1401 esac
1402 fi
1404 log "$(_ 'Creating mount point: %s...' "$TARGET_ROOT")"
1405 mkdir -p "$TARGET_ROOT" >> "$LOG" || error8
1406 # Mount target.
1407 local mount_fs="$ROOT_FORMAT"
1408 [ -z "$mount_fs" ] && mount_fs="$(filesys $ROOT_UUID)"
1409 mount -t "$mount_fs" "$ROOT_UUID" \
1410 "$TARGET_ROOT" >>"$LOG" 2>>"$LOG"
1411 if [ $(mount | \
1412 grep -c "mnt/target") = "0" ]; then
1413 error 4 "$(_ '%s: Unable to mount partition' "$ROOT_UUID")"
1414 fi
1415 }
1418 #------------
1419 # 4.3 umount
1420 #------------
1422 # copy log file, umount target and eject cdrom.
1423 umount_devices()
1424 {
1425 # umount target
1426 if mount | grep -q "$TARGET_ROOT"; then
1427 log "$(_ 'Unmounting target partition: %s' "$ROOT_UUID")"
1428 umount -l "$TARGET_ROOT" 2>>$LOG
1429 fi
1431 # umount source
1432 if mount | grep -q "$SOURCE_ROOT"; then
1433 log "$(_ 'Unmounting: %s' "$SOURCE_ROOT")"
1434 umount -l "$SOURCE_ROOT"
1435 fi
1436 if [ -h $SOURCE_ROOT/boot ]; then
1437 log "$(_ 'Unlinking: %s' "$SOURCE_ROOT")"
1438 rm -f $SOURCE_ROOT/boot
1439 fi
1441 # eject cd
1442 if [ "$SOURCE" = "cdrom" ]; then
1443 _ 'Ejecting CD-ROM...'
1444 eject
1445 fi
1446 # remove lock file
1447 rm -f "$LOCK"
1448 }
1450 end_of_install()
1451 {
1452 log "$(_ 'Process completed. You can now restart (reboot) from your SliTaz GNU/Linux system.' |\
1453 fold -s)"
1454 _ '=== Tazinst ended on %s ===' "$(date "+%x %X")" >> "$LOG"
1455 unset LOGGING
1456 # saving log
1457 log "$(_ 'Copying log to %s' '/var/log/tazinst.log')"
1458 cp -a "$LOG" $TARGET_ROOT/var/log
1459 umount_devices
1460 }
1463 #---------------
1464 # 5. bootloader
1465 #---------------
1467 #------------
1468 # 5.1 common
1469 #------------
1471 # selection
1472 bootloader()
1473 {
1474 if [ "$BOOTLOADER" = "auto" ]; then
1475 # use syslinux, but if p_table=msdos take grub (if available)
1476 unset BOOTLOADER
1477 printf "%s" "$(list_bootloader)" | \
1478 grep -q "syslinux" && BOOTLOADER=syslinux
1479 if [ "$(p_table $ROOT_UUID)" = "msdos" ]; then
1480 printf "%s" "$(list_bootloader)" | \
1481 grep -q " grub" && BOOTLOADER=grub
1482 fi
1483 fi
1484 case "$BOOTLOADER" in
1485 grub)
1486 grub_config
1487 grub_install ;;
1488 syslinux)
1489 syslinux_config
1490 syslinux_install ;;
1491 *)
1492 syslinux_config
1493 log "$(_ 'No bootloader to install.')" ;;
1494 esac
1495 }
1497 # print disk num
1498 disknum()
1499 {
1500 local partition="$(uuid2dev $1)"
1501 partition="${partition%%[0-9]*}"
1502 /usr/sbin/parted -lms 2>&1 | grep "^/dev" | \
1503 /bin/busybox awk -v PART="$partition" '{if (match($0,PART)) print NR-1}'
1504 }
1506 # print partition num
1507 partnum()
1508 {
1509 local partition="$(uuid2dev $1)"
1510 printf "%s\n" "$((${partition#/dev/[hs]d[a-z]}-1))"
1511 }
1513 # print root device
1514 rootdev()
1515 {
1516 local partition="$1"
1517 case "$(p_table $partition)" in
1518 msdos)
1519 # print device
1520 printf "%s" "$(uuid2dev $partition)" ;;
1521 gpt)
1522 # print PARTUUID (different from UUID)
1523 printf "%s" "PARTUUID="
1524 /sbin/blkid -p -i -o udev $(uuid2dev $partition) \
1525 | grep ENTRY_UUID | cut -d '=' -f2 ;;
1526 esac
1527 }
1529 # add rootdelay for removable devices
1530 rootdelay()
1531 {
1532 is_removable "$ROOT_UUID" && printf "%s" "rootdelay=9"
1533 }
1535 # print winboot uuid
1536 winboot_uuid()
1537 {
1538 if [ "$WINBOOT" = "auto" ]; then
1539 # get the first uuid if any
1540 [ $(list_winboot | wc -w) -gt 1 ] \
1541 && printf "%s" "$(list_winboot | cut -d' ' -f2)"
1542 else
1543 printf "%s" "$WINBOOT"
1544 fi
1545 }
1548 #-----------
1549 # 5.1 grub1
1550 #-----------
1552 # create grub legacy conf
1553 grub_config()
1554 {
1555 # backup an existing conf
1556 [ -e "$TARGET_ROOT/boot/grub/menu.lst" ] && \
1557 cp $TARGET_ROOT/boot/grub/menu.lst \
1558 $TARGET_ROOT/boot/grub/menu.bak
1559 # create the target GRUB configuration.
1560 mkdir -p $TARGET_ROOT/boot/grub || error8
1561 cat > $TARGET_ROOT/boot/grub/menu.lst <<_EOF_
1562 # /boot/grub/menu.lst: GRUB boot loader configuration.
1563 #
1565 # By default, boot the first entry.
1566 default 0
1568 # Boot automatically after 8 secs.
1569 timeout 8
1571 # Graphical splash image.
1572 splashimage=/boot/grub/splash.xpm.gz
1574 # Change the colors.
1575 #color yellow/brown light-green/black
1577 # For booting SliTaz from : $ROOT_UUID
1578 #
1579 title SliTaz GNU/Linux $(cat $TARGET_ROOT/etc/slitaz-release) (Kernel $KERNEL)
1580 root (hd$(disknum $ROOT_UUID),$(partnum $ROOT_UUID))
1581 kernel /boot/$KERNEL root=$(rootdev $ROOT_UUID) video=-32 quiet $(rootdelay)
1583 _EOF_
1585 local winpart="$(winboot_uuid)"
1586 if [ -n "$winpart" ]; then
1587 log "$(_ 'Enabling Windows dual-boot')"
1588 cat >> $TARGET_ROOT/boot/grub/menu.lst <<_EOF_
1589 # For booting Windows :
1590 #
1591 title Microsoft Windows
1592 rootnoverify (hd$(disknum $winpart),$(partnum $winpart))
1593 chainloader +1
1595 _EOF_
1596 fi
1598 # log
1599 printf "%s\n" "/boot/grub/menu.lst:" >> "$LOG"
1600 printf "%s\n" "--- menu.lst -------------" >> "$LOG"
1601 cat $TARGET_ROOT/boot/grub/menu.lst >> "$LOG"
1602 printf "%s\n\n" "--- menu.lst -------------" >> "$LOG"
1603 }
1605 # GRUB info with disk name used for grub
1606 grub_install()
1607 {
1608 # install grub
1609 local target_disk="$(uuid2disk $ROOT_UUID)"
1610 log "$(_ 'Installing GRUB on: %s' "$target_disk")"
1611 /usr/sbin/grub-install --version >> "$LOG" || error 5 "$(_ 'GRUB not found')"
1612 /usr/sbin/grub-install --no-floppy \
1613 --root-directory=$TARGET_ROOT $target_disk >>"$LOG" 2>>"$LOG"
1614 # set boot flag
1615 log "$(_ 'Setting the boot flag')"
1616 /usr/sbin/parted "$(uuid2disk $ROOT_UUID)" \
1617 set "$(($(partnum $ROOT_UUID)+1))" boot on 2>> "$LOG"
1618 # splash image
1619 if [ -f "$SOURCE_ROOT/boot/grub/splash.xpm.gz" ]; then
1620 log "$(_ 'Copying splash image')"
1621 mkdir -p $TARGET_ROOT/boot/grub
1622 cp $SOURCE_ROOT/boot/grub/splash.xpm.gz \
1623 $TARGET_ROOT/boot/grub
1624 fi
1625 }
1628 #--------------
1629 # 5.2 syslinux
1630 #--------------
1632 # create syslinux conf
1633 syslinux_config()
1634 {
1635 local version="[$(cat $TARGET_ROOT/etc/slitaz-release)]"
1636 version="$version\(Kernel $KERNEL)"
1637 # backup an existing conf
1638 [ -e "$TARGET_ROOT/boot/syslinux/syslinux.cfg" ] && \
1639 cp $TARGET_ROOT/boot/syslinux/syslinux.cfg \
1640 $TARGET_ROOT/boot/syslinux/syslinux.bak
1641 # create the target syslinux configuration.
1642 mkdir -p $TARGET_ROOT/boot/syslinux || error8
1643 cat > $TARGET_ROOT/boot/syslinux/syslinux.cfg <<EOF
1644 # use a boot menu
1645 UI vesamenu.c32
1647 # display the boot
1648 PROMPT 1
1650 # how long to pause at the boot
1651 TIMEOUT 50
1653 # default label
1654 DEFAULT slitaz
1656 # Menu settings
1657 MENU TITLE SliTaz GNU/Linux boot menu
1658 MENU BACKGROUND splash.jpg
1659 MENU WIDTH 78
1660 MENU MARGIN 6
1661 MENU ROW 10
1662 MENU VSHIFT 2
1663 MENU TIMEOUTROW 18
1664 MENU TABMSGROW 16
1665 MENU CMDLINEROW 16
1667 # Menu colors
1668 MENU COLOR border * #00000000 #00000000 none
1669 MENU COLOR title * #ffffffff #00000000 *
1670 MENU COLOR sel 0 #ffffffff #00000000 none
1671 MENU COLOR unsel 0 #50ffffff #00000000 none
1672 MENU COLOR help * #ffffffff #00000000 *
1673 MENU COLOR timeout_msg 37;40 #80ffffff #00000000 std
1674 MENU COLOR timeout 1;37;40 #c0ffffff #00000000 std
1675 MENU COLOR msg07 37;40 #90ffffff #a0000000 std
1676 MENU COLOR tabmsg 31;40 #30ffffff #00000000 std
1678 # Labels
1679 LABEL slitaz
1680 MENU LABEL SliTaz GNU/Linux $version
1681 LINUX /boot/$KERNEL
1682 APPEND root=$(rootdev $ROOT_UUID) video=-32 quiet $(rootdelay)
1684 LABEL cmdline
1685 MENU LABEL Command Line
1686 MENU QUIT
1688 EOF
1689 local winpart="$(winboot_uuid)"
1690 if [ -n "$winpart" ]; then
1691 log "$(_ 'Enabling Windows dual-boot')"
1692 cat >> $TARGET_ROOT/boot/syslinux/syslinux.cfg <<EOF
1693 LABEL windows
1694 MENU LABEL Windows
1695 COM32 chain.c32
1696 APPEND hd$(disknum $winpart) $(($(partnum $winpart)+1))
1698 EOF
1699 fi
1700 # log
1701 printf "%s\n" "/boot/syslinux/syslinux.cfg:" >> "$LOG"
1702 printf "%s\n" "--- syslinux.cfg -------------" >> "$LOG"
1703 cat $TARGET_ROOT/boot/syslinux/syslinux.cfg >> "$LOG"
1704 printf "%s\n\n" "--- syslinux.cfg -------------" >> "$LOG"
1705 }
1707 # install syslinux
1708 syslinux_install()
1709 {
1710 log "$(_ 'Installing Syslinux')"
1711 # needed tools
1712 local dir disk="$(uuid2disk $ROOT_UUID)"
1713 for dir in /home/boot/extlinux /home/boot/syslinux /boot/syslinux; do
1714 [ -d "$dir" ] && cp $dir/*.c32 "$TARGET_ROOT/boot/syslinux/"
1715 [ -d "$dir" ] && cp $dir/*.sys "$TARGET_ROOT/boot/syslinux/"
1716 done
1717 /usr/bin/lzma d /usr/share/boot/chain.c32.lzma \
1718 $TARGET_ROOT/boot/syslinux/chain.c32 2>> "$LOG"
1719 # install syslinux
1720 /bin/syslinux -version >> "$LOG" || error 5 "$(_ 'Syslinux not found')"
1721 /bin/extlinux --install $TARGET_ROOT/boot/syslinux >> "$LOG" 2>> "$LOG"
1722 case "$(p_table $ROOT_UUID)" in
1723 msdos)
1724 log "$(_ 'Setting the boot flag on')"
1725 /usr/sbin/parted "$disk" \
1726 set "$(($(partnum $ROOT_UUID)+1))" boot on 2>> "$LOG"
1727 [ -r /usr/share/boot/mbr.bin ] || error 5 "$(_ 'mbr.bin not found')"
1728 log "$(_ 'Installing MBR')"
1729 dd bs=440 count=1 conv=notrunc \
1730 if=/usr/share/boot/mbr.bin of=$disk >> "$LOG" ;;
1731 gpt)
1732 log "$(_ 'Setting the legacy_boot flag on')"
1733 # remove old boot flag
1734 local oldboot="$(parted $disk print \
1735 | grep boot | /bin/busybox awk '{print $1}')"
1736 /usr/sbin/parted "$disk" \
1737 set "$oldboot" legacy_boot off 2>> "$LOG"
1738 # set boot flag
1739 /usr/sbin/parted "$disk" \
1740 set "$(($(partnum $ROOT_UUID)+1))" legacy_boot on 2>> "$LOG"
1741 log "$(_ 'Installing GPTMBR')"
1742 [ -r /usr/share/boot/gptmbr.bin ] || error 5 "$(_ 'gptmbr.bin not found')"
1743 dd bs=440 conv=notrunc count=1 \
1744 if=/usr/share/boot/gptmbr.bin of=$disk >> "$LOG" ;;
1745 esac
1746 # splash image
1747 if [ -f "$SOURCE_ROOT/boot/isolinux/splash.jpg" ]; then
1748 log "$(_ 'Copying splash image')"
1749 cp -a $SOURCE_ROOT/boot/isolinux/splash.jpg \
1750 $TARGET_ROOT/boot/syslinux
1751 fi
1752 }
1755 #--------------------
1756 # 6. execute section
1757 #--------------------
1759 execute()
1760 {
1761 local mode="$(get mode)" sighup=1 sigint=2 sigquit=3
1762 randomize_mirrors
1763 trap "error9" "$sighup" "$sigint" "$sigquit"
1764 case "$mode" in
1765 install)
1766 install ;;
1767 upgrade)
1768 upgrade ;;
1769 esac
1770 }
1772 #-------------
1773 # 6.1 install
1774 #-------------
1776 # get a clean target device
1777 clean_target()
1778 {
1779 if [ -z "$ROOT_FORMAT" ]; then
1780 # partition was not formatted
1781 log "$(_ 'Cleaning the root partition (%s)...' "$ROOT_UUID")"
1782 # keep /home in case of reinstall.
1783 local path="$(pwd)"
1784 cd "$TARGET_ROOT" || error8
1785 local dir
1786 for dir in *
1787 do
1788 case "$dir" in
1789 home)
1790 log "$(_ 'keeping /home found on: %s' "$ROOT_UUID")"
1791 mv home home.bak ;;
1792 lost+found)
1793 continue ;;
1794 *)
1795 log "$(_ 'removing target: %s' "$dir")"
1796 rm -rf "$dir" 2>>"$LOG" ;;
1797 esac
1798 done
1799 if [ ! -d lost+found ]; then
1800 mklost+found 2>>"$LOG"
1801 fi
1802 cd "$path" || error8
1803 fi
1804 }
1806 # kernel is renamed to standard vmlinuz-$VERSION.
1807 install_kernel()
1808 {
1809 if [ -d /$TARGET_ROOT/lib/modules ]; then
1810 KERNEL="vmlinuz-$(ls /$TARGET_ROOT/lib/modules | tail -1)"
1811 else
1812 KERNEL="vmlinuz-$(uname -r)"
1813 log "$(_ 'Kernel name not found, falling back to: %s' "$(uname -r)")"
1814 fi
1815 mkdir -p $TARGET_ROOT/boot || error8
1816 for i in $SOURCE_ROOT/boot/bzImage* ; do
1817 cp $i $TARGET_ROOT/boot/${KERNEL%slitaz*}slitaz${i#*bzImage}
1818 done
1819 log "$(_ 'install_kernel: %s' "$KERNEL")"
1820 }
1822 # extract packed rootfs: squashfs or cromfs
1823 extract_loramfs()
1824 {
1825 local i
1826 for i in $(/bin/busybox cpio -idvum 2> /dev/null); do
1827 case "$i" in
1828 rootfs*)
1829 need_package squashfs
1830 if ! unsquashfs $i ; then
1831 need_package cromfs
1832 unmkcromfs $i squashfs-root
1833 fi
1834 mv -f squashfs-root/* .
1835 rmdir squashfs-root
1836 rm -f $i
1837 esac
1838 done
1839 }
1841 # this is a loram rootfs.gz, skip loram bootstrap and extract
1842 extract_first_loramfs()
1843 {
1844 local path="$(pwd)"
1845 (zcat $1 || /usr/bin/unlzma < $1) | \
1846 /bin/busybox cpio -i extractfs.cpio 2> /dev/null &&
1847 ( cd / ; /bin/busybox cpio -id ) < extractfs.cpio && \
1848 rm -f extractfs.cpio
1849 ofs=$(/bin/busybox awk '/07070100/ { o+=index($0,"07070100"); printf "%d\n",o/4 ; exit } { o+=1+length() }' < $1)
1850 dd if=$1 skip=$(($ofs / 1024)) bs=4k count=1 2> /dev/null | \
1851 ( dd skip=$(($ofs % 1024)) bs=4 2> /dev/null ; \
1852 dd if=$1 skip=$((1 + ($ofs / 1024) )) bs=4k ) | \
1853 extract_loramfs
1854 cd "$path" || error8
1855 }
1857 # extract lzma'ed or gziped rootfs.
1858 extract_rootfs()
1859 {
1860 local path="$(pwd)"
1861 local isloramfs
1862 cd "$TARGET_ROOT" || error8
1863 if [ -d "$1"/../fs/etc ]; then
1864 # this is a tazlitobox loram (cdrom)
1865 cp -a "$1"/../fs/. .
1866 else
1867 for i in $(ls "$1"/rootfs* | sort -r); do
1868 if [ ! -d etc ]; then
1869 if [ $( (zcat "$i" 2>/dev/null \
1870 || /usr/bin/lzma d "$i" -so) | \
1871 wc -c) -lt $(stat -c %s "$i") ]; then
1872 # this is a tazlitobox loram (ram)
1873 isloramfs="$i"
1874 extract_first_loramfs "$i"
1875 continue
1876 fi
1877 fi
1878 if [ -n "$isloramfs" ]; then
1879 extract_loramfs < "$i"
1880 continue
1881 fi
1882 ( zcat "$i" 2>/dev/null || /usr/bin/lzma d "$i" -so || \
1883 cat "$i" ) 2>>"$LOG" | /bin/busybox cpio -idu
1884 done 2>>"$LOG" > /dev/null
1885 fi
1886 cp /etc/keymap.conf etc
1887 # unpack /usr (double check...)
1888 if ls etc/tazlito | grep -q ".extract"; then
1889 for i in etc/tazlito/*.extract; do
1890 [ -f "$i" ] && . "$i" /media/cdrom
1891 done
1892 fi
1893 cd "$pwd" || error8
1894 }
1897 # pre configure freshly installed system (60 - 80%).
1898 pre_config_system()
1899 {
1900 local path="$(pwd)"
1901 cd "$TARGET_ROOT" || error8
1902 # restore backup of existing /home if exists.
1903 # (created by prepare_target_dev)
1904 if [ -d home.bak ]; then
1905 log "$(_ 'Restoring directory: /home...')"
1906 rm -rf home
1907 mv home.bak home
1908 fi
1909 # add root device to CHECK_FS in rcS.conf to check filesystem
1910 # on each boot.
1911 log "$(_ 'Adding / partition and CHECK_FS to file %s...' '/etc/rcS.conf')"
1912 sed -i s#'CHECK_FS=\"\"'#"CHECK_FS=\"$(dev2uuid $ROOT_UUID)\""# etc/rcS.conf
1913 # set hostname.
1914 log "$(_ 'Configuring host name: %s' "$HOSTNAME")"
1915 printf "%s\n" "$HOSTNAME" > etc/hostname
1916 printf "%s\n" "127.0.0.1 localhost $HOSTNAME tazpanel" > etc/hosts
1917 cd "$path" || error8
1918 }
1920 # set root passwd and create user after rootfs extraction.
1921 users_settings()
1922 {
1923 # create file
1924 cat > "$TARGET_ROOT/users.sh" << _EOF_
1925 #!/bin/sh
1926 umask 0022
1927 printf "root:%s" "$ROOT_PWD" | chpasswd -m
1928 adduser -D -H $USER_LOGIN
1930 for grp in audio cdrom floppy dialout disk kmem tape tty video; do
1931 if ! grep \$grp /etc/group | grep -q $USER_LOGIN ; then
1932 grep -q \$grp /etc/group && addgroup $USER_LOGIN \$grp
1933 fi
1934 done
1936 printf "%s:%s" "$USER_LOGIN" "$USER_PWD" | chpasswd -m
1937 if [ ! -d /home/$USER_LOGIN ]; then
1938 cp -a /etc/skel /home/$USER_LOGIN
1939 [ -e /root/.xinitrc ] && cp /root/.xinitrc /home/$USER_LOGIN
1940 mkdir -p /home/$USER_LOGIN/.config/slitaz
1941 cp -a /etc/slitaz/applications.conf /home/$USER_LOGIN/.config/slitaz
1942 # Set ownership
1943 if grep -q ^users: /etc/group; then
1944 chown -R $USER_LOGIN:users /home/$USER_LOGIN
1945 else
1946 chown -R $USER_LOGIN:$USER_LOGIN /home/$USER_LOGIN
1947 fi
1948 # Path for user desktop files.
1949 for i in /home/$USER_LOGIN/.local/share/applications/*.desktop
1950 do
1951 [ -e "$i" ] && sed -i s/"user_name"/"$USER_LOGIN"/g \$i
1952 done
1953 fi
1954 # Slim default user.
1955 if [ -f /etc/slim.conf ]; then
1956 sed -i s/"default_user .*"/"default_user $USER_LOGIN"/ \
1957 /etc/slim.conf
1958 fi
1959 _EOF_
1960 chmod o+x "$TARGET_ROOT/users.sh"
1961 chroot "$TARGET_ROOT" ./users.sh 2>>"$LOG" >> "$LOG"
1962 rm "$TARGET_ROOT/users.sh"
1963 umask 0177
1964 }
1966 # /home can be on a separate partition. If default user exists in /home
1967 # we remove default file created by users_settings().
1968 home_config()
1969 {
1970 if [ -n "$HOME_UUID" ]; then
1971 local path="$(pwd)" uuid
1972 cd "$TARGET_ROOT" || error8
1973 # detect fs
1974 local home_fs="$HOME_FORMAT"
1975 [ -z "$home_fs" ] && home_fs="$(filesys $HOME_UUID)"
1976 # manage /home
1977 log "$(_ 'Configuring partition to be used as /home: %s' "$HOME_UUID")"
1978 mv home/$USER_LOGIN tmp
1979 mount -t "$home_fs" "$HOME_UUID" home >> "$LOG" 2>> "$LOG"
1980 if [ -d $TARGET_ROOT/home/$USER_LOGIN ]; then
1981 rm -rf tmp/home/$USER_LOGIN
1982 else
1983 mv tmp/$USER_LOGIN home
1984 fi
1985 # write entry in fstab, force uuid
1986 uuid="$(dev2uuid "$HOME_UUID")"
1987 printf "%b\n" "$uuid /home $home_fs defaults \t0 \t2" >> etc/fstab
1988 umount home >> "$LOG" 2>> "$LOG"
1989 cd "$path" || error8
1990 fi
1991 }
1993 install()
1994 {
1995 log_open
1996 log "1 $(_ 'Installing SliTaz on: %s' "$(get root_uuid)")"
1997 log "5 $(_ 'Checking settings...')"
1998 check all >> "$LOG" 2>> "$LOG"
1999 load_settings
2001 log "10 $(_ 'Preparing source media...')"
2002 mount_source
2004 log "20 $(_ 'Preparing target disk...')"
2005 prepare_uuid
2007 log "30 $(_ 'Cleaning the root partition if necessary...')"
2008 clean_target
2010 log "40 $(_ 'Extracting the root system...')"
2011 extract_rootfs $SOURCE_ROOT/boot
2013 log "50 $(_ 'Installing the Kernel...')"
2014 install_kernel
2016 log "60 $(_ 'Preconfiguring the system...')"
2017 pre_config_system
2019 log "70 $(_ 'Configuring root and default user account...')"
2020 users_settings
2022 log "80 $(_ 'Configuring /home...')"
2023 home_config
2025 log "90 $(_ 'Checking bootloader installation...')"
2026 bootloader
2028 log "100 $(_ 'Files installation completed')"
2029 end_of_install
2030 }
2033 #-------------
2034 # 6.2 upgrade
2035 #-------------
2037 # search for SliTaz
2038 check_release()
2039 {
2040 if [ -f $TARGET_ROOT/etc/slitaz-release ]; then
2041 local release="$(cat $TARGET_ROOT/etc/slitaz-release)"
2042 log "$(_ 'Preparing upgrade of SliTaz release: %s' "$release")"
2043 else
2044 error 6 "$(_ "%s: This partition doesn't appear to contain a valid \
2045 SliTaz system, the file: %s doesn't exist." "$ROOT_UUID" '/etc/slitaz-release')"
2046 fi
2047 }
2049 # backup packages list.
2050 backup_files()
2051 {
2052 local path="$(pwd)"
2053 cd "$TARGET_ROOT" || error8
2054 ls -1 var/lib/tazpkg/installed > home/packages-selection.list
2055 local dir
2056 for dir in *
2057 do
2058 case "$dir" in
2059 boot)
2060 rm -rf boot/vmlinuz-* ;;
2061 home)
2062 mv home home.bak
2063 log "$(_ 'keeping /home found on: %s' "$ROOT_UUID")" ;;
2064 etc)
2065 /bin/busybox tar czf etc.tar.gz etc
2066 mv etc etc.bak
2067 log "$(_ 'keeping /etc found on: %s' "$ROOT_UUID")" ;;
2068 var)
2069 if [ -d var/www ]; then
2070 mv var/www www.bak
2071 log "$(_ 'keeping /var/www found on: %s' "$ROOT_UUID")"
2072 fi
2073 rm -rf var 2>>"$LOG" ;;
2074 lost+found)
2075 continue ;;
2076 *)
2077 log "$(_ 'removing target: %s' "$dir")"
2078 rm -rf "$dir" 2>>"$LOG" ;;
2079 esac
2080 done
2081 if [ -d mklost+found ]; then
2082 mklost+found 2>>"$LOG"
2083 fi
2084 cd "$path" || error8
2085 }
2087 # restore backups.
2088 restore_files()
2089 {
2090 rm -rf $TARGET_ROOT/home
2091 mv $TARGET_ROOT/home.bak $TARGET_ROOT/home
2092 rm -rf $TARGET_ROOT/etc
2093 mv $TARGET_ROOT/etc.bak $TARGET_ROOT/etc
2094 if [ -d $TARGET_ROOT/www.bak ]; then
2095 rm -rf $TARGET_ROOT/var/www
2096 mv $TARGET_ROOT/www.bak $TARGET_ROOT/var/www
2097 fi
2098 log "$(_ 'backups restored: %s' "$(date)")"
2100 # /var/lib/slitaz-installer
2101 mkdir -p $TARGET_ROOT/var/lib/tazinst && \
2102 mv $TARGET_ROOT/etc.tar.gz $TARGET_ROOT/var/lib/tazinst && \
2103 mv $TARGET_ROOT/home/packages-selection.list \
2104 $TARGET_ROOT/var/lib/tazinst \
2105 && log "$(_ 'backups saved in %s' '/var/lib/tazinst')"
2106 }
2108 # upgrade added pkgs
2109 install_pkgs()
2110 {
2111 # check if the pkg is on the mirror.
2112 log "$(_ 'Checking the availability of packages...')"
2113 touch packages-to-install.list
2114 packages=0
2115 diff="$(sort < packages-selection.diff)"
2116 for pkg in $diff
2117 do
2118 if grep -q ^$pkg-[0-9] /var/lib/tazpkg/packages.list; then
2119 packages="$(($packages+1))"
2120 printf "%s\n" "$pkg" >> packages-to-install.list
2121 fi
2122 done
2124 # install packages.
2125 log "$(_ 'Installing packages...')"
2126 if [ "$packages" = "0" ]; then
2127 log "$(_ 'packages to install: 0')"
2128 else
2129 # get-install all missing pkgs.
2130 while read pkg
2131 do
2132 log "$(_ 'Installing: %s...' "$pkg")"
2133 # get install package and answer yes in case of dependencies.
2134 pkgname="$(grep ^$pkg /var/lib/tazpkg/packages.list)"
2135 /usr/bin/tazpkg get "$pkg" >/dev/null 2>/dev/null
2136 yes "" | /usr/bin/tazpkg install $pkgname.tazpkg \
2137 --root=$TARGET_ROOT >/dev/null 2>/dev/null
2138 rm -f $pkgname.tazpkg
2139 done < packages-to-install.list
2140 fi
2141 log "$(_ 'Installation of packages complete...')"
2142 }
2144 # search for added pkgs
2145 update_pkgs()
2146 {
2147 local path="$(pwd)"
2148 cd $TARGET_ROOT/var/lib/tazinst || error8
2149 # LiveCD packages list.
2150 log "$(_ 'Creating package lists...')"
2151 ls -1 $TARGET_ROOT/var/lib/tazpkg/installed > packages-source.list || error8
2152 log "$(_ '%s: done' 'packages-source.list')"
2153 # diff
2154 /bin/busybox diff packages-source.list packages-selection.list | \
2155 grep ^+[a-z] | sed s/^+// > packages-selection.diff
2156 log "$(_ '%s: done' 'packages-selection.diff')"
2157 # get mirror list.
2158 /usr/bin/tazpkg recharge >>$LOG 2>>$LOG
2159 if [ -f /var/lib/tazpkg/packages.list ]; then
2160 install_pkgs
2161 else
2162 touch packages-to-install.list
2163 longline "$(_ "The list of available packages on the mirror could not \
2164 be downloaded. No missing packages will be reinstalled now, but you can do so \
2165 later by looking at the following list:")"
2166 echo '/var/lib/tazinst/packages-selection.diff'
2167 fi
2168 cd "$path" || error8
2169 }
2171 # upgrade command
2172 upgrade()
2173 {
2174 log_open
2175 log "1 $(_ 'Upgrading SliTaz on: %s' "$(get root_uuid)")"
2176 log "5 $(_ 'Checking settings...')"
2177 check all >> "$LOG"
2178 load_settings
2180 log "10 $(_ 'Preparing source media...')"
2181 mount_source
2183 log "20 $(_ 'Preparing target disk...')"
2184 prepare_uuid
2186 log "30 $(_ 'Searching for %s...' '/etc/slitaz-release')"
2187 check_release
2189 log "40 $(_ 'Backup /etc, /home and the packages list...')"
2190 backup_files
2192 log "50 $(_ 'Extracting the root system...')"
2193 extract_rootfs $SOURCE_ROOT/boot
2195 log "60 $(_ 'Restoring configuration files...')"
2196 restore_files
2198 log "70 $(_ 'Installing the Kernel...')"
2199 install_kernel
2201 log "80 $(_ 'Upgrading added packages...')"
2202 update_pkgs
2204 log "90 $(_ 'Bootloader...')"
2205 bootloader
2207 log "100 $(_ 'Files installation completed')"
2208 end_of_install
2209 }
2212 #--------------
2213 # tazinst main
2214 #--------------
2216 case $1 in
2217 new)
2218 new_file "$2" ;;
2219 set)
2220 read_file "$4"
2221 change "$2" "$3" "$4" ;;
2222 unset)
2223 read_file "$3"
2224 change "$2" "" "$3" "$4" ;;
2225 get)
2226 read_file "$3"
2227 get "$2" ;;
2228 check)
2229 read_file "$3"
2230 check "$2" ;;
2231 list)
2232 list "$2" "$3" ;;
2233 execute)
2234 check_root
2235 read_file "$2"
2236 execute "$2" ;;
2237 log)
2238 [ -r "$LOG" ] && cat "$LOG" ;;
2239 clean)
2240 clean "$2" ;;
2241 version)
2242 printf "%s\n" "$VERSION" ;;
2243 ""|usage)
2244 usage ;;
2245 help)
2246 help "$2" ;;
2247 *)
2248 usage_error "$1" ;;
2249 esac