Removing dead lang2locale function and associated files, not used anywhere.
[live-boot-grml.git] / scripts / boot.sh
1 #!/bin/sh
2
3 # set -e
4
5 if [ -e /scripts/functions ]
6 then
7         # initramfs-tools specific (FIXME)
8         . /scripts/functions
9 fi
10
11 for _SCRIPT in /lib/live/boot/*
12 do
13         if [ -e "${_SCRIPT}" ]
14         then
15                 . ${_SCRIPT}
16         fi
17 done
18
19 export PATH="/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
20
21 echo "/root/lib" >> /etc/ld.so.conf
22 echo "/root/usr/lib" >> /etc/ld.so.conf
23
24 mountpoint="/live/image"
25 alt_mountpoint="/media"
26 LIVE_MEDIA_PATH="live"
27
28 HOSTNAME="host"
29
30 mkdir -p "${mountpoint}"
31 tried="/tmp/tried"
32
33 # Create /etc/mtab for debug purpose and future syncs
34 if [ ! -d /etc ]
35 then
36         mkdir /etc/
37 fi
38
39 if [ ! -f /etc/mtab ]
40 then
41         touch /etc/mtab
42 fi
43
44 if [ ! -x "/bin/fstype" ]
45 then
46         # klibc not in path -> not in initramfs
47         export PATH="${PATH}:/usr/lib/klibc/bin"
48 fi
49
50 # handle upgrade path from old udev (using udevinfo) to
51 # recent versions of udev (using udevadm info)
52 if [ -x /sbin/udevadm ]
53 then
54         udevinfo='/sbin/udevadm info'
55 else
56         udevinfo='udevinfo'
57 fi
58
59 old_root_overlay_label="live-rw"
60 old_home_overlay_label="home-rw"
61 custom_overlay_label="custom-ov"
62 root_snapshot_label="live-sn"
63 old_root_snapshot_label="live-sn"
64 home_snapshot_label="home-sn"
65 persistence_list="live-persistence.conf"
66
67 if [ ! -f /live.vars ]
68 then
69         touch /live.vars
70 fi
71
72 is_live_path ()
73 {
74         DIRECTORY="${1}"
75
76         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
77         then
78                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs2
79                 do
80                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
81                         then
82                                 return 0
83                         fi
84                 done
85         fi
86
87         return 1
88 }
89
90 matches_uuid ()
91 {
92         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
93         then
94                 return 0
95         fi
96
97         path="${1}"
98         uuid="$(cat /conf/uuid.conf)"
99
100         for try_uuid_file in "${path}/.disk/live-uuid"*
101         do
102                 [ -e "${try_uuid_file}" ] || continue
103
104                 try_uuid="$(cat "${try_uuid_file}")"
105
106                 if [ "${uuid}" = "${try_uuid}" ]
107                 then
108                         return 0
109                 fi
110         done
111
112         return 1
113 }
114
115 get_backing_device ()
116 {
117         case "${1}" in
118                 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
119                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
120                         ;;
121
122                 *.dir)
123                         echo "directory"
124                         ;;
125
126                 *)
127                         panic "Unrecognized live filesystem: ${1}"
128                         ;;
129         esac
130 }
131
132 match_files_in_dir ()
133 {
134         # Does any files match pattern ${1} ?
135         local pattern="${1}"
136
137         if [ "$(echo ${pattern})" != "${pattern}" ]
138         then
139                 return 0
140         fi
141
142         return 1
143 }
144
145 mount_images_in_directory ()
146 {
147         directory="${1}"
148         rootmnt="${2}"
149         mac="${3}"
150
151
152         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
153                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
154                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
155                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext4" ||
156                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
157                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
158         then
159                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
160                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
161         else
162                 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
163         fi
164 }
165
166 is_nice_device ()
167 {
168         sysfs_path="${1#/sys}"
169
170         if [ -e /lib/udev/path_id ]
171         then
172                 # squeeze
173                 PATH_ID="/lib/udev/path_id"
174         else
175                 # wheezy/sid (udev >= 174)
176                 PATH_ID="/sbin/udevadm test-builtin path_id"
177         fi
178
179         if ${PATH_ID} "${sysfs_path}" | egrep -q "ID_PATH=(usb|pci-[^-]*-(ide|sas|scsi|usb|virtio)|platform-sata_mv|platform-orion-ehci|platform-mmc|platform-mxsdhci)"
180         then
181                 return 0
182         elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
183         then
184                 return 0
185         elif echo ${sysfs_path} | grep -q "^/block/dm-"
186         then
187                 return 0
188         elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
189         then
190                 return 0
191         fi
192
193         return 1
194 }
195
196 check_dev ()
197 {
198         sysdev="${1}"
199         devname="${2}"
200         skip_uuid_check="${3}"
201
202         # support for fromiso=.../isofrom=....
203         if [ -n "$FROMISO" ]
204         then
205                 ISO_DEVICE=$(dirname $FROMISO)
206                 if ! [ -b $ISO_DEVICE ]
207                 then
208                         # to support unusual device names like /dev/cciss/c0d0p1
209                         # as well we have to identify the block device name, let's
210                         # do that for up to 15 levels
211                         i=15
212                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
213                         do
214                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
215                                 [ -b "$ISO_DEVICE" ] && break
216                                 i=$(($i -1))
217                         done
218                 fi
219
220                 if [ "$ISO_DEVICE" = "/" ]
221                 then
222                         echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
223                 else
224                         fs_type=$(get_fstype "${ISO_DEVICE}")
225                         if is_supported_fs ${fs_type}
226                         then
227                                 mkdir /live/fromiso
228                                 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
229                                 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
230                                 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
231                                 devname="${loopdevname}"
232                         else
233                                 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
234                         fi
235                 fi
236         fi
237
238         if [ -z "${devname}" ]
239         then
240                 devname=$(sys2dev "${sysdev}")
241         fi
242
243         if [ -d "${devname}" ]
244         then
245                 mount -o bind "${devname}" $mountpoint || continue
246
247                 if is_live_path $mountpoint
248                 then
249                         echo $mountpoint
250                         return 0
251                 else
252                         umount $mountpoint
253                 fi
254         fi
255
256         IFS=","
257         for device in ${devname}
258         do
259                 case "$device" in
260                         *mapper*)
261                                 # Adding lvm support
262                                 if [ -x /scripts/local-top/lvm2 ]
263                                 then
264                                         ROOT="$device" resume="" /scripts/local-top/lvm2
265                                 fi
266                                 ;;
267
268                         /dev/md*)
269                                 # Adding raid support
270                                 if [ -x /scripts/local-top/mdadm ]
271                                 then
272                                         cp /conf/conf.d/md /conf/conf.d/md.orig
273                                         echo "MD_DEVS=$device " >> /conf/conf.d/md
274                                         /scripts/local-top/mdadm
275                                         mv /conf/conf.d/md.orig /conf/conf.d/md
276                                 fi
277                                 ;;
278                 esac
279         done
280         unset IFS
281
282         [ -n "$device" ] && devname="$device"
283
284         [ -e "$devname" ] || continue
285
286         if [ -n "${LIVE_MEDIA_OFFSET}" ]
287         then
288                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
289                 devname="${loopdevname}"
290         fi
291
292         fstype=$(get_fstype "${devname}")
293
294         if is_supported_fs ${fstype}
295         then
296                 devuid=$(blkid -o value -s UUID "$devname")
297                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
298                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
299                 [ -n "$devuid" ] && echo "$devuid" >> $tried
300
301                 if [ -n "${FINDISO}" ]
302                 then
303                         if [ -f ${mountpoint}/${FINDISO} ]
304                         then
305                                 umount ${mountpoint}
306                                 mkdir -p /live/findiso
307                                 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
308                                 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
309                                 devname="${loopdevname}"
310                                 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
311                         else
312                                 umount ${mountpoint}
313                         fi
314                 fi
315
316                 if is_live_path ${mountpoint} && \
317                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
318                 then
319                         echo ${mountpoint}
320                         return 0
321                 else
322                         umount ${mountpoint} 2>/dev/null
323                 fi
324         fi
325
326         if [ -n "${LIVE_MEDIA_OFFSET}" ]
327         then
328                 losetup -d "${loopdevname}"
329         fi
330
331         return 1
332 }
333
334 find_livefs ()
335 {
336         timeout="${1}"
337
338         # don't start autodetection before timeout has expired
339         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
340         then
341                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
342                 then
343                         return 1
344                 fi
345         fi
346
347         # first look at the one specified in the command line
348         case "${LIVE_MEDIA}" in
349                 removable-usb)
350                         for sysblock in $(removable_usb_dev "sys")
351                         do
352                                 for dev in $(subdevices "${sysblock}")
353                                 do
354                                         if check_dev "${dev}"
355                                         then
356                                                 return 0
357                                         fi
358                                 done
359                         done
360                         return 1
361                         ;;
362
363                 removable)
364                         for sysblock in $(removable_dev "sys")
365                         do
366                                 for dev in $(subdevices "${sysblock}")
367                                 do
368                                         if check_dev "${dev}"
369                                         then
370                                                 return 0
371                                         fi
372                                 done
373                         done
374                         return 1
375                         ;;
376
377                 *)
378                         if [ ! -z "${LIVE_MEDIA}" ]
379                         then
380                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
381                                 then
382                                         return 0
383                                 fi
384                         fi
385                         ;;
386         esac
387
388         # or do the scan of block devices
389         # prefer removable devices over non-removable devices, so scan them first
390         devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
391
392         for sysblock in $devices_to_scan
393         do
394                 devname=$(sys2dev "${sysblock}")
395                 [ -e "$devname" ] || continue
396                 fstype=$(get_fstype "${devname}")
397
398                 if /lib/udev/cdrom_id ${devname} > /dev/null
399                 then
400                         if check_dev "null" "${devname}"
401                         then
402                                 return 0
403                         fi
404                 elif is_nice_device "${sysblock}"
405                 then
406                         for dev in $(subdevices "${sysblock}")
407                         do
408                                 if check_dev "${dev}"
409                                 then
410                                         return 0
411                                 fi
412                         done
413                 elif [ "${fstype}" = "squashfs" -o \
414                         "${fstype}" = "btrfs" -o \
415                         "${fstype}" = "ext2" -o \
416                         "${fstype}" = "ext3" -o \
417                         "${fstype}" = "ext4" -o \
418                         "${fstype}" = "jffs2" ]
419                 then
420                         # This is an ugly hack situation, the block device has
421                         # an image directly on it.  It's hopefully
422                         # live-boot, so take it and run with it.
423                         ln -s "${devname}" "${devname}.${fstype}"
424                         echo "${devname}.${fstype}"
425                         return 0
426                 fi
427         done
428
429         return 1
430 }
431
432 integrity_check ()
433 {
434         media_mountpoint="${1}"
435
436         log_begin_msg "Checking media integrity"
437
438         cd ${media_mountpoint}
439         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
440         RC="${?}"
441
442         log_end_msg
443
444         if [ "${RC}" -eq 0 ]
445         then
446                 log_success_msg "Everything ok, will reboot in 10 seconds."
447                 sleep 10
448                 cd /
449                 umount ${media_mountpoint}
450                 sync
451                 echo u > /proc/sysrq-trigger
452                 echo b > /proc/sysrq-trigger
453         else
454                 panic "Not ok, a media defect is likely, switch to VT8 for details."
455         fi
456 }
457
458 mountroot ()
459 {
460         if [ -x /scripts/local-top/cryptroot ]; then
461             /scripts/local-top/cryptroot
462         fi
463
464         exec 6>&1
465         exec 7>&2
466         exec > boot.log
467         exec 2>&1
468         tail -f boot.log >&7 &
469         tailpid="${!}"
470
471         . /live.vars
472
473         Arguments
474
475         # make sure all harddisk devices are read-only
476         # this is important for forensic investigations
477         case "${READ_ONLY}" in
478                 true)
479                         Read_only
480                         ;;
481         esac
482
483         maybe_break live-premount
484         log_begin_msg "Running /scripts/live-premount"
485         run_scripts /scripts/live-premount
486         log_end_msg
487
488         # Needed here too because some things (*cough* udev *cough*)
489         # changes the timeout
490
491         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
492         then
493                 if do_netmount
494                 then
495                         livefs_root="${mountpoint}"
496                 else
497                         panic "Unable to find a live file system on the network"
498                 fi
499         else
500                 if [ -n "${ISCSI_PORTAL}" ]
501                 then
502                         do_iscsi && livefs_root="${mountpoint}"
503                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
504                 then
505                         # Do a local boot from hd
506                         livefs_root=${ROOT}
507                 else
508                         if [ -x /usr/bin/memdiskfind ]
509                         then
510                                 MEMDISK=$(/usr/bin/memdiskfind)
511
512                                 if [ $? -eq 0 ]
513                                 then
514                                         # We found a memdisk, set up phram
515                                         modprobe phram phram=memdisk,${MEMDISK}
516
517                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
518                                         modprobe mtdblock
519                                 fi
520                         fi
521
522                         # Scan local devices for the image
523                         i=0
524                         while [ "$i" -lt 60 ]
525                         do
526                                 livefs_root=$(find_livefs ${i})
527
528                                 if [ -n "${livefs_root}" ]
529                                 then
530                                         break
531                                 fi
532
533                                 sleep 1
534                                 i="$(($i + 1))"
535                         done
536                 fi
537         fi
538
539         if [ -z "${livefs_root}" ]
540         then
541                 panic "Unable to find a medium containing a live file system"
542         fi
543
544         if [ "${INTEGRITY_CHECK}" ]
545         then
546                 integrity_check "${livefs_root}"
547         fi
548
549         if [ "${TORAM}" ]
550         then
551                 live_dest="ram"
552         elif [ "${TODISK}" ]
553         then
554                 live_dest="${TODISK}"
555         fi
556
557         if [ "${live_dest}" ]
558         then
559                 log_begin_msg "Copying live media to ${live_dest}"
560                 copy_live_to "${livefs_root}" "${live_dest}"
561                 log_end_msg
562         fi
563
564         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
565         # because the mountpoint is left behind in /proc/mounts, so let's get
566         # rid of it when running from RAM
567         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
568         then
569                 losetup -d /dev/loop0
570
571                 if is_mountpoint /live/fromiso
572                 then
573                         umount /live/fromiso
574                         rmdir --ignore-fail-on-non-empty /live/fromiso \
575                                 >/dev/null 2>&1 || true
576                 fi
577         fi
578
579         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
580         then
581                 setup_unionfs "${livefs_root}" "${rootmnt}"
582         else
583                 mac="$(get_mac)"
584                 mac="$(echo ${mac} | sed 's/-//g')"
585                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
586         fi
587
588
589         if [ -n "${ROOT_PID}" ] ; then
590                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
591         fi
592
593         log_end_msg
594
595         # unionfs-fuse needs /dev to be bind-mounted for the duration of
596         # live-bottom; udev's init script will take care of things after that
597         if [ "${UNIONTYPE}" = unionfs-fuse ]
598         then
599                 mount -n -o bind /dev "${rootmnt}/dev"
600         fi
601
602         # Move to the new root filesystem so that programs there can get at it.
603         if [ ! -d /root/live/image ]
604         then
605                 mkdir -p /root/live/image
606                 mount --move /live/image /root/live/image
607         fi
608
609         # aufs2 in kernel versions around 2.6.33 has a regression:
610         # directories can't be accessed when read for the first the time,
611         # causing a failure for example when accessing /var/lib/fai
612         # when booting FAI, this simple workaround solves it
613         ls /root/* >/dev/null 2>&1
614
615         # Move findiso directory to the new root filesystem so that programs there can get at it.
616         if [ -d /live/findiso ] && [ ! -d /root/live/findiso ]
617         then
618                 mkdir -p /root/live/findiso
619                 mount -n --move /live/findiso /root/live/findiso
620         fi
621
622         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
623         # because the mountpoint is left behind in /proc/mounts, so let's get
624         # rid of it when running from RAM
625         if [ -n "$FINDISO" ] && [ "${TORAM}" ]
626         then
627                 losetup -d /dev/loop0
628
629                 if is_mountpoint /root/live/findiso
630                 then
631                         umount /root/live/findiso
632                         rmdir --ignore-fail-on-non-empty /root/live/findiso \
633                                 >/dev/null 2>&1 || true
634                 fi
635         fi
636
637         # copy snapshot configuration if exists
638         if [ -f snapshot.conf ]
639         then
640                 log_begin_msg "Copying snapshot.conf to ${rootmnt}/etc/live/boot.d"
641                 if [ ! -d "${rootmnt}/etc/live/boot.d" ]
642                 then
643                         mkdir -p "${rootmnt}/etc/live/boot.d"
644                 fi
645                 cp snapshot.conf "${rootmnt}/etc/live/boot.d/"
646                 log_end_msg
647         fi
648
649         if [ -f /etc/resolv.conf ] && [ ! -s ${rootmnt}/etc/resolv.conf ]
650         then
651                 log_begin_msg "Copying /etc/resolv.conf to ${rootmnt}/etc/resolv.conf"
652                 cp -v /etc/resolv.conf ${rootmnt}/etc/resolv.conf
653                 log_end_msg
654         fi
655
656         maybe_break live-bottom
657         log_begin_msg "Running /scripts/live-bottom\n"
658
659         run_scripts /scripts/live-bottom
660         log_end_msg
661
662         if [ "${UNIONFS}" = unionfs-fuse ]
663         then
664                 umount "${rootmnt}/dev"
665         fi
666
667         exec 1>&6 6>&-
668         exec 2>&7 7>&-
669         kill ${tailpid}
670         [ -w "${rootmnt}/var/log/" ] && mkdir -p /var/log/live && cp boot.log "${rootmnt}/var/log/live" 2>/dev/null
671 }