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