Switch from vol_id to blkid if using udev >=146-1 (Closes: #555529).
[live-boot-grml.git] / scripts / live-helpers
1 # live-initramfs helper functions, used by live-initramfs 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         # udev >=146-1 no longer provides vol_id:
80         if [ -x /lib/udev/vol_id ]
81         then
82                 /lib/udev/vol_id -t ${1} 2>/dev/null
83         else
84                 eval $(blkid -o udev "${1}")
85                 if [ -n "$ID_FS_TYPE" ]
86                 then
87                         echo "${ID_FS_TYPE}"
88                 fi
89         fi
90 }
91
92 where_is_mounted ()
93 {
94         device=${1}
95
96         if grep -q "^${device} " /proc/mounts
97         then
98                 # return the first found
99                 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
100         fi
101 }
102
103 lastline ()
104 {
105         while read lines
106         do
107                 line=${lines}
108         done
109
110         echo "${line}"
111 }
112
113 base_path ()
114 {
115         testpath="${1}"
116         mounts="$(awk '{print $2}' /proc/mounts)"
117         testpath="$(busybox realpath ${testpath})"
118
119         while true
120         do
121                 if echo "${mounts}" | grep -qs "^${testpath}"
122                 then
123                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
124                         echo ${1}
125                         break
126                 else
127                         testpath=$(dirname $testpath)
128                 fi
129         done
130 }
131
132 fs_size ()
133 {
134         # Returns used/free fs kbytes + 5% more
135         # You could pass a block device as ${1} or the mount point as ${2}
136
137         dev="${1}"
138         mountp="${2}"
139         used="${3}"
140
141         if [ -z "${mountp}" ]
142         then
143                 mountp="$(where_is_mounted ${dev})"
144
145                 if [ -z "${mountp}" ]
146                 then
147                         mountp="/mnt/tmp_fs_size"
148
149                         mkdir -p "${mountp}"
150                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
151
152                         doumount=1
153                 fi
154         fi
155
156         if [ "${used}" = "used" ]
157         then
158                 size=$(du -ks ${mountp} | cut -f1)
159                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
160         else
161                 # free space
162                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
163         fi
164
165         if [ -n "${doumount}" ]
166         then
167                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
168                 rmdir "${mountp}"
169         fi
170
171         echo "${size}"
172 }
173
174 load_keymap ()
175 {
176         # Load custom keymap
177         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
178         then
179                 loadkeys /etc/boottime.kmap.gz
180         fi
181 }
182
183 setup_loop ()
184 {
185         local fspath=${1}
186         local module=${2}
187         local pattern=${3}
188         local offset=${4}
189         local encryption=${5}
190         local readonly=${6}
191
192         modprobe -q -b "${module}"
193
194         if [ -x /sbin/udevadm ]
195         then
196                 # lenny
197                 udevadm settle
198         else
199                 # etch
200                 udevsettle
201         fi
202
203         for loopdev in ${pattern}
204         do
205                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
206                 then
207                         dev=$(sys2dev "${loopdev}")
208                         options=''
209
210                         if [ -n "${readonly}" ]
211                         then
212                                 if losetup --help 2>&1 | grep -q -- "-r\b"
213                                 then
214                                         options="${options} -r"
215                                 fi
216                         fi
217
218                         if [ 0 -lt "${offset}" ]
219                         then
220                                 options="${options} -o ${offset}"
221                         fi
222
223                         if [ -z "${encryption}" ]
224                         then
225                                 losetup ${options} "${dev}" "${fspath}"
226                         else
227                                 # Loop AES encryption
228                                 while true
229                                 do
230                                         load_keymap
231
232                                         echo -n "Enter passphrase for root filesystem: " >&6
233                                         read -s passphrase
234                                         echo "${passphrase}" > /tmp/passphrase
235                                         unset passphrase
236                                         exec 9</tmp/passphrase
237                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
238                                         error=${?}
239                                         exec 9<&-
240                                         rm -f /tmp/passphrase
241
242                                         if [ 0 -eq ${error} ]
243                                         then
244                                                 unset error
245                                                 break
246                                         fi
247
248                                         echo
249                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
250                                         read answer
251
252                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
253                                         then
254                                                 unset answer
255                                                 break
256                                         fi
257                                 done
258                         fi
259
260                         echo "${dev}"
261                         return 0
262                 fi
263         done
264
265         panic "No loop devices available"
266 }
267
268 try_mount ()
269 {
270         dev="${1}"
271         mountp="${2}"
272         opts="${3}"
273         fstype="${4}"
274
275         old_mountp="$(where_is_mounted ${dev})"
276
277         if [ -n "${old_mountp}" ]
278         then
279                 if [ "${opts}" != "ro" ]
280                 then
281                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
282                 fi
283
284                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
285         else
286                 if [ -z "${fstype}" ]
287                 then
288                         fstype=$(get_fstype "${dev}")
289                 fi
290                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}"
291         fi
292 }
293
294 find_cow_device ()
295 {
296         # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
297         #  in the latter case the partition containing the file is left mounted
298         #  if is not in black_listed_devices
299         pers_label="${1}"
300         cow_backing="/${pers_label}-backing"
301         black_listed_devices="${2}"
302
303         if [ -z "${PERSISTENT_PATH}" ]
304         then
305                 pers_fpath=${cow_backing}/${pers_label}
306         else
307                 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
308         fi
309
310         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
311         do
312                 for dev in $(subdevices "${sysblock}")
313                 do
314                         devname=$(sys2dev "${dev}")
315
316                         if echo "${black_listed_devices}" | grep -q "${devname}"
317                         then
318                                 # skip this device enterely
319                                 break
320                         fi
321
322                         # Checking for a luks device
323                         if [ "${PERSISTENT}" = "cryptsetup" ] && [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
324                         then
325                                 while true
326                                 do
327                                         load_keymap
328
329                                         /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${devname}: " | /sbin/cryptsetup -T 1 luksOpen ${devname} $(basename ${devname}) --key-file=-
330                                         error=${?}
331
332                                         devname="/dev/mapper/$(basename ${devname})"
333
334                                         if [ 0 -eq ${error} ]
335                                         then
336                                                 unset error
337                                                 break
338                                         fi
339
340                                         echo
341                                         echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
342                                         read answer
343
344                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
345                                         then
346                                                 unset answer
347                                                 break
348                                         fi
349                                 done
350                         fi
351
352                         # udev >=146-1 no longer provides vol_id:
353                         if [ -x /lib/udev/vol_id ]
354                         then
355                                 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
356                                 then
357                                         echo "${devname}"
358                                         return 0
359                                 fi
360                         else
361                                 eval $(blkid -o udev "${devname}")
362                                 if [ "$ID_FS_LABEL" = "${pers_label}" ]
363                                 then
364                                         echo "${devname}"
365                                         return 0
366                                 fi
367                         fi
368
369                         if [ "${PERSISTENT}" = "nofiles" ]
370                         then
371                                 # do not mount the device to find for image files
372                                 # just skip this
373                                 continue
374                         fi
375
376                         case "$(get_fstype ${devname})" in
377                                 vfat|ext2|ext3|ext4|jffs2)
378                                         mkdir -p "${cow_backing}"
379                                         if ! try_mount "${devname}" "${cow_backing}" "rw"
380                                         then
381                                                 break
382                                         fi
383
384                                         if [ -f "${pers_fpath}" ]
385                                         then
386                                                 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
387                                                 return 0
388                                         else
389                                                 umount ${cow_backing}
390                                         fi
391                                         ;;
392                                 *)
393                                         ;;
394                         esac
395                 done
396         done
397         return 1
398 }
399
400 find_files ()
401 {
402         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
403         # FIXME: merge with above function
404
405         filenames="${1}"
406         snap_backing="/snap-backing"
407         black_listed_devices="${2}"
408
409         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
410         do
411                 for dev in $(subdevices "${sysblock}")
412                 do
413                         devname=$(sys2dev "${dev}")
414                         devfstype="$(get_fstype ${devname})"
415
416                         if echo "${black_listed_devices}" | grep -q "${devname}"
417                         then
418                                 # skip this device enterely
419                                 break
420                         fi
421
422                         if is_supported_fs ${devfstype}
423                         then
424                                 mkdir -p "${snap_backing}"
425
426                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
427                                 then
428                                         for filename in ${filenames}
429                                         do
430                                                 if [ -f "${snap_backing}/${filename}" ]
431                                                 then
432                                                         echo "${devname} ${snap_backing} ${filename}"
433                                                         umount ${snap_backing}
434                                                         return 0
435                                                 fi
436                                         done
437                                 fi
438
439                                 umount ${snap_backing}
440                         fi
441                 done
442         done
443 }
444
445 get_mac ()
446 {
447         mac=""
448
449         for adaptor in /sys/class/net/*
450         do
451                 status="$(cat ${adaptor}/iflink)"
452
453                 if [ "${status}" -eq 2 ]
454                 then
455                         mac="$(cat ${adaptor}/address)"
456                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
457                 fi
458         done
459
460         echo ${mac}
461 }
462
463 is_luks()
464 {
465     devname="${1}"
466     if [ -x /sbin/cryptsetup ]
467     then
468         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
469         return ${ret}
470     else
471         return 1
472     fi
473
474 }