6edd51cd189bd7a660388a9f30291b0662f987dd
[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}" || \
291                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live.log && return 0 )
292         fi
293 }
294
295 find_cow_device ()
296 {
297         # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
298         #  in the latter case the partition containing the file is left mounted
299         #  if is not in black_listed_devices
300         pers_label="${1}"
301         cow_backing="/${pers_label}-backing"
302         black_listed_devices="${2}"
303
304         if [ -z "${PERSISTENT_PATH}" ]
305         then
306                 pers_fpath=${cow_backing}/${pers_label}
307         else
308                 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
309         fi
310
311         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
312         do
313                 for dev in $(subdevices "${sysblock}")
314                 do
315                         devname=$(sys2dev "${dev}")
316
317                         if echo "${black_listed_devices}" | grep -q "${devname}"
318                         then
319                                 # skip this device enterely
320                                 break
321                         fi
322
323                         # Checking for a luks device
324                         if [ "${PERSISTENT}" = "cryptsetup" ] && [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
325                         then
326                                 while true
327                                 do
328                                         load_keymap
329
330                                         /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${devname}: " | /sbin/cryptsetup -T 1 luksOpen ${devname} $(basename ${devname}) --key-file=-
331                                         error=${?}
332
333                                         devname="/dev/mapper/$(basename ${devname})"
334
335                                         if [ 0 -eq ${error} ]
336                                         then
337                                                 unset error
338                                                 break
339                                         fi
340
341                                         echo
342                                         echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
343                                         read answer
344
345                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
346                                         then
347                                                 unset answer
348                                                 break
349                                         fi
350                                 done
351                         fi
352
353                         # udev >=146-1 no longer provides vol_id:
354                         if [ -x /lib/udev/vol_id ]
355                         then
356                                 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
357                                 then
358                                         echo "${devname}"
359                                         return 0
360                                 fi
361                         else
362                                 eval $(blkid -o udev "${devname}")
363                                 if [ "$ID_FS_LABEL" = "${pers_label}" ]
364                                 then
365                                         echo "${devname}"
366                                         return 0
367                                 fi
368                         fi
369
370                         if [ "${PERSISTENT}" = "nofiles" ]
371                         then
372                                 # do not mount the device to find for image files
373                                 # just skip this
374                                 continue
375                         fi
376
377                         case "$(get_fstype ${devname})" in
378                                 vfat|ext2|ext3|ext4|jffs2)
379                                         mkdir -p "${cow_backing}"
380                                         if try_mount "${devname}" "${cow_backing}" "rw"
381                                         then
382                                                 if [ -f "${pers_fpath}" ]
383                                                 then
384                                                         echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
385                                                         return 0
386                                                 else
387                                                         umount ${cow_backing} > /dev/null 2>&1 || true
388                                                 fi
389                                         fi
390                                         ;;
391                                 *)
392                                         ;;
393                         esac
394                 done
395         done
396         return 1
397 }
398
399 find_files ()
400 {
401         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
402         # FIXME: merge with above function
403
404         filenames="${1}"
405         snap_backing="/snap-backing"
406         black_listed_devices="${2}"
407
408         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
409         do
410                 for dev in $(subdevices "${sysblock}")
411                 do
412                         devname=$(sys2dev "${dev}")
413                         devfstype="$(get_fstype ${devname})"
414
415                         if echo "${black_listed_devices}" | grep -q "${devname}"
416                         then
417                                 # skip this device enterely
418                                 break
419                         fi
420
421                         if is_supported_fs ${devfstype}
422                         then
423                                 mkdir -p "${snap_backing}"
424
425                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
426                                 then
427                                         for filename in ${filenames}
428                                         do
429                                                 if [ -f "${snap_backing}/${filename}" ]
430                                                 then
431                                                         echo "${devname} ${snap_backing} ${filename}"
432                                                         umount ${snap_backing}
433                                                         return 0
434                                                 fi
435                                         done
436                                 fi
437
438                                 umount ${snap_backing}
439                         fi
440                 done
441         done
442 }
443
444 get_mac ()
445 {
446         mac=""
447
448         for adaptor in /sys/class/net/*
449         do
450                 status="$(cat ${adaptor}/iflink)"
451
452                 if [ "${status}" -eq 2 ]
453                 then
454                         mac="$(cat ${adaptor}/address)"
455                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
456                 fi
457         done
458
459         echo ${mac}
460 }
461
462 is_luks()
463 {
464     devname="${1}"
465     if [ -x /sbin/cryptsetup ]
466     then
467         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
468         return ${ret}
469     else
470         return 1
471     fi
472
473 }