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