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