Clarifying what happens when you don't retry decrypting a device.
[live-boot-grml.git] / scripts / live-helpers
1 # live-boot helper functions, used by live-boot on boot and by live-snapshot
2
3 if [ ! -x "/bin/fstype" ]
4 then
5         # klibc not in path -> not in initramfs
6         export PATH="${PATH}:/usr/lib/klibc/bin"
7 fi
8
9 # handle upgrade path from old udev (using udevinfo) to
10 # recent versions of udev (using udevadm info)
11 if [ -x /sbin/udevadm ]
12 then
13         udevinfo='/sbin/udevadm info'
14 else
15         udevinfo='udevinfo'
16 fi
17
18 sys2dev ()
19 {
20         sysdev=${1#/sys}
21         echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
22 }
23
24 subdevices ()
25 {
26         sysblock=${1}
27         r=""
28
29         for dev in "${sysblock}"/* "${sysblock}"
30         do
31                 if [ -e "${dev}/dev" ]
32                 then
33                         r="${r} ${dev}"
34                 fi
35         done
36
37         echo ${r}
38 }
39
40 is_supported_fs ()
41 {
42         fstype="${1}"
43
44         # Validate input first
45         if [ -z "${fstype}" ]
46         then
47                 return 1
48         fi
49
50         # Try to look if it is already supported by the kernel
51         if grep -q ${fstype} /proc/filesystems
52         then
53                 return 0
54         else
55                 # Then try to add support for it the gentle way using the initramfs capabilities
56                 modprobe ${fstype}
57                 if grep -q ${fstype} /proc/filesystems
58                 then
59                         return 0
60                 # Then try the hard way if /root is already reachable
61                 else
62                         kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
63                         if [ -e "${kmodule}" ]
64                         then
65                                 insmod "${kmodule}"
66                                 if grep -q ${fstype} /proc/filesystems
67                                 then
68                                         return 0
69                                 fi
70                         fi
71                 fi
72         fi
73
74         return 1
75 }
76
77 get_fstype ()
78 {
79         /sbin/blkid -s TYPE -o value $1 2>/dev/null
80 }
81
82 where_is_mounted ()
83 {
84         device=${1}
85
86         if grep -q "^${device} " /proc/mounts
87         then
88                 # return the first found
89                 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
90         fi
91 }
92
93 lastline ()
94 {
95         while read lines
96         do
97                 line=${lines}
98         done
99
100         echo "${line}"
101 }
102
103 base_path ()
104 {
105         testpath="${1}"
106         mounts="$(awk '{print $2}' /proc/mounts)"
107         testpath="$(busybox realpath ${testpath})"
108
109         while true
110         do
111                 if echo "${mounts}" | grep -qs "^${testpath}"
112                 then
113                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
114                         echo ${1}
115                         break
116                 else
117                         testpath=$(dirname $testpath)
118                 fi
119         done
120 }
121
122 fs_size ()
123 {
124         # Returns used/free fs kbytes + 5% more
125         # You could pass a block device as ${1} or the mount point as ${2}
126
127         dev="${1}"
128         mountp="${2}"
129         used="${3}"
130
131         if [ -z "${mountp}" ]
132         then
133                 mountp="$(where_is_mounted ${dev})"
134
135                 if [ -z "${mountp}" ]
136                 then
137                         mountp="/mnt/tmp_fs_size"
138
139                         mkdir -p "${mountp}"
140                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
141
142                         doumount=1
143                 fi
144         fi
145
146         if [ "${used}" = "used" ]
147         then
148                 size=$(du -ks ${mountp} | cut -f1)
149                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
150         else
151                 # free space
152                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
153         fi
154
155         if [ -n "${doumount}" ]
156         then
157                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
158                 rmdir "${mountp}"
159         fi
160
161         echo "${size}"
162 }
163
164 load_keymap ()
165 {
166         # Load custom keymap
167         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
168         then
169                 loadkeys /etc/boottime.kmap.gz
170         fi
171 }
172
173 setup_loop ()
174 {
175         local fspath=${1}
176         local module=${2}
177         local pattern=${3}
178         local offset=${4}
179         local encryption=${5}
180         local readonly=${6}
181
182         # the output of setup_loop is evaluated in other functions,
183         # modprobe leaks kernel options like "libata.dma=0"
184         # as "options libata dma=0" on stdout, causing serious
185         # problems therefor, so instead always avoid output to stdout
186         modprobe -q -b "${module}" 1>/dev/null
187
188         udevadm settle
189
190         for loopdev in ${pattern}
191         do
192                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
193                 then
194                         dev=$(sys2dev "${loopdev}")
195                         options=''
196
197                         if [ -n "${readonly}" ]
198                         then
199                                 if losetup --help 2>&1 | grep -q -- "-r\b"
200                                 then
201                                         options="${options} -r"
202                                 fi
203                         fi
204
205                         if [ 0 -lt "${offset}" ]
206                         then
207                                 options="${options} -o ${offset}"
208                         fi
209
210                         if [ -z "${encryption}" ]
211                         then
212                                 losetup ${options} "${dev}" "${fspath}"
213                         else
214                                 # Loop AES encryption
215                                 while true
216                                 do
217                                         load_keymap
218
219                                         echo -n "Enter passphrase for root filesystem: " >&6
220                                         read -s passphrase
221                                         echo "${passphrase}" > /tmp/passphrase
222                                         unset passphrase
223                                         exec 9</tmp/passphrase
224                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
225                                         error=${?}
226                                         exec 9<&-
227                                         rm -f /tmp/passphrase
228
229                                         if [ 0 -eq ${error} ]
230                                         then
231                                                 unset error
232                                                 break
233                                         fi
234
235                                         echo
236                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
237                                         read answer
238
239                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
240                                         then
241                                                 unset answer
242                                                 break
243                                         fi
244                                 done
245                         fi
246
247                         echo "${dev}"
248                         return 0
249                 fi
250         done
251
252         panic "No loop devices available"
253 }
254
255 try_mount ()
256 {
257         dev="${1}"
258         mountp="${2}"
259         opts="${3}"
260         fstype="${4}"
261
262         old_mountp="$(where_is_mounted ${dev})"
263
264         if [ -n "${old_mountp}" ]
265         then
266                 if [ "${opts}" != "ro" ]
267                 then
268                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
269                 fi
270
271                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
272         else
273                 if [ -z "${fstype}" ]
274                 then
275                         fstype=$(get_fstype "${dev}")
276                 fi
277                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
278                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
279         fi
280 }
281
282 find_cow_device ()
283 {
284         # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
285         #  in the latter case the partition containing the file is left mounted
286         #  if is not in black_listed_devices.
287         #  Additionally, if the white_listed_devices list is non-empty, the
288         #  parent block device of the returned device must be part of this list.
289         pers_label="${1}"
290         cow_backing="/${pers_label}-backing"
291         black_listed_devices="${2}"
292         white_listed_devices="${3}"
293
294         if [ -z "${PERSISTENT_PATH}" ]
295         then
296                 pers_fpath=${cow_backing}/${pers_label}
297         else
298                 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
299         fi
300
301         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
302         do
303                 fulldevname=$(sys2dev "${sysblock}")
304
305                 if echo "${black_listed_devices}" | grep -q -w "${fulldevname}"
306                 then
307                         # skip this device entirely
308                         break
309                 fi
310
311                 if [ -n "${white_listed_devices}" ]
312                 then
313                         if echo "${white_listed_devices}" | grep -v -q -w "${fulldevname}"
314                         then
315                                 # skip this device entirely
316                                 break
317                         fi
318                 fi
319
320                 for dev in $(subdevices "${sysblock}")
321                 do
322                         devname=$(sys2dev "${dev}")
323
324                         if echo "${black_listed_devices}" | grep -q -w "${devname}"
325                         then
326                                 # skip this subdevice
327                                 continue
328                         fi
329
330                         # Checking for a luks device
331                         if [ "${PERSISTENT_ENCRYPTION}" = "luks" ] && [ -e /sbin/cryptsetup ]
332                         then
333                                 if ! modprobe dm-crypt
334                                 then
335                                         log_warning_msg "Unable to load module dm-crypt"
336                                         continue
337                                 fi
338
339                                 if ! /sbin/cryptsetup isLuks ${devname}
340                                 then
341                                         # we only look for encrypted subdevices
342                                         continue
343                                 fi
344
345                                 while true
346                                 do
347                                         load_keymap
348
349                                         /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${devname}: " | /sbin/cryptsetup -T 1 luksOpen ${devname} $(basename ${devname}) --key-file=-
350                                         error=${?}
351
352                                         devname="/dev/mapper/$(basename ${devname})"
353
354                                         if [ 0 -eq ${error} ]
355                                         then
356                                                 unset error
357                                                 break
358                                         fi
359
360                                         echo >&6
361                                         echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
362                                         read answer
363
364                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
365                                         then
366                                                 unset answer
367                                                 # skip to next subdevice
368                                                 continue 2
369                                         fi
370                                 done
371                         fi
372
373                         if echo ${PERSISTENT_STORAGE} | grep -qw filesystem && [ "$(/sbin/blkid -s LABEL -o value $devname 2>/dev/null)" = "${pers_label}" ]
374                         then
375                                 echo "${devname}"
376                                 return 0
377                         fi
378
379                         if ! echo ${PERSISTENT_STORAGE} | grep -qw file
380                         then
381                                 # do not mount the device to find for image files
382                                 # just skip this
383                                 continue
384                         fi
385
386                         case "$(get_fstype ${devname})" in
387                                 vfat|ext2|ext3|ext4|jffs2)
388                                         mkdir -p "${cow_backing}"
389                                         if try_mount "${devname}" "${cow_backing}" "rw"
390                                         then
391                                                 if [ -f "${pers_fpath}" ]
392                                                 then
393                                                         echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
394                                                         return 0
395                                                 else
396                                                         umount ${cow_backing} > /dev/null 2>&1 || true
397                                                 fi
398                                         fi
399                                         ;;
400                                 *)
401                                         ;;
402                         esac
403                 done
404         done
405         return 1
406 }
407
408 find_files ()
409 {
410         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
411         #  if is not in black_listed_devices.
412         #  Additionally, if the white_listed_devices list is non-empty, the
413         #  parent block device of the returned device must be part of this list.
414         # FIXME: merge with above function
415
416         filenames="${1}"
417         snap_backing="/snap-backing"
418         black_listed_devices="${2}"
419         white_listed_devices="${3}"
420
421         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
422         do
423                 fulldevname=$(sys2dev "${sysblock}")
424
425                 if echo "${black_listed_devices}" | grep -q -w "${fulldevname}"
426                 then
427                         # skip this device entirely
428                         break
429                 fi
430
431                 if [ -n "${white_listed_devices}" ]
432                 then
433                         if echo "${white_listed_devices}" | grep -v -q -w "${fulldevname}"
434                         then
435                                 # skip this device entirely
436                                 break
437                         fi
438                 fi
439
440                 for dev in $(subdevices "${sysblock}")
441                 do
442                         devname=$(sys2dev "${dev}")
443                         devfstype="$(get_fstype ${devname})"
444
445                         if echo "${black_listed_devices}" | grep -q -w "${devname}"
446                         then
447                                 # skip this subdevice
448                                 break
449                         fi
450
451                         if is_supported_fs ${devfstype}
452                         then
453                                 mkdir -p "${snap_backing}"
454
455                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
456                                 then
457                                         for filename in ${filenames}
458                                         do
459                                                 if [ -f "${snap_backing}/${filename}" ]
460                                                 then
461                                                         echo "${devname} ${snap_backing} ${filename}"
462                                                         umount ${snap_backing}
463                                                         return 0
464                                                 fi
465                                         done
466                                 fi
467
468                                 umount ${snap_backing}
469                         fi
470                 done
471         done
472 }
473
474 get_mac ()
475 {
476         mac=""
477
478         for adaptor in /sys/class/net/*
479         do
480                 status="$(cat ${adaptor}/iflink)"
481
482                 if [ "${status}" -eq 2 ]
483                 then
484                         mac="$(cat ${adaptor}/address)"
485                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
486                 fi
487         done
488
489         echo ${mac}
490 }
491
492 is_luks()
493 {
494     devname="${1}"
495     if [ -x /sbin/cryptsetup ]
496     then
497         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
498         return ${ret}
499     else
500         return 1
501     fi
502
503 }
504
505 removable_dev ()
506 {
507         output_format="${1}"
508         want_usb="${2}"
509         ret=
510
511         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
512         do
513                 dev_ok=
514                 if [ "$(cat ${sysblock}/removable)" = "1" ]
515                 then
516                         if [ -z "${want_usb}" ]
517                         then
518                                 dev_ok="yes"
519                         else
520                                 if readlink ${sysblock} | grep -q usb
521                                 then
522                                         dev_ok="yes"
523                                 fi
524                         fi
525                 fi
526
527                 if [ "${dev_ok}" = "yes" ]
528                 then
529                         case "${output_format}" in
530                                 sys)
531                                         ret="${ret} ${sysblock}"
532                                         ;;
533                                 *)
534                                         devname=$(sys2dev "${sysblock}")
535                                         ret="${ret} ${devname}"
536                                         ;;
537                         esac
538                 fi
539         done
540
541         echo "${ret}"
542 }
543
544 removable_usb_dev ()
545 {
546         output_format="${1}"
547
548         removable_dev "${output_format}" "want_usb"
549 }
550
551 non_removable_dev ()
552 {
553         output_format="${1}"
554         ret=
555
556         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
557         do
558                 if [ "$(cat ${sysblock}/removable)" = "0" ]
559                 then
560                         case "${output_format}" in
561                                 sys)
562                                         ret="${ret} ${sysblock}"
563                                         ;;
564                                 *)
565                                         devname=$(sys2dev "${sysblock}")
566                                         ret="${ret} ${devname}"
567                                         ;;
568                         esac
569                 fi
570         done
571
572         echo "${ret}"
573 }