Two return code consistency fix.
[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 "^${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 [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
332                         then
333                                 while true
334                                 do
335                                         load_keymap
336
337                                         echo -n "Enter passphrase for ${pers_label} on ${devname}: " >&6
338                                         read -s passphrase
339                                         echo "${passphrase}" > /tmp/passphrase
340                                         unset passphrase
341                                         exec 9</tmp/passphrase
342                                         /sbin/cryptsetup luksOpen ${devname} $(basename ${devname})
343                                         error=${?}
344                                         exec 9<&-
345                                         rm -f /tmp/passphrase
346
347                                         devname="/dev/mapper/$(basename ${devname})"
348
349                                         if [ 0 -eq ${error} ]
350                                         then
351                                                 unset error
352                                                 break
353                                         fi
354
355                                         echo
356                                         echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
357                                         read answer
358
359                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
360                                         then
361                                                 unset answer
362                                                 break
363                                         fi
364                                 done
365                         fi
366
367                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
368                         then
369                                 echo "${devname}"
370                                 return 0
371                         fi
372
373                         if [ "${PERSISTENT}" = "nofiles" ]
374                         then
375                                 # do not mount the device to find for image files
376                                 # just skip this
377                                 break
378                         fi
379
380                         case "$(get_fstype ${devname})" in
381                                 vfat|ext2|ext3|ext4|jffs2)
382                                         mkdir -p "${cow_backing}"
383                                         if ! try_mount "${devname}" "${cow_backing}" "rw"
384                                         then
385                                                 break
386                                         fi
387
388                                         if [ -f "${pers_fpath}" ]
389                                         then
390                                                 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
391                                                 return 0
392                                         else
393                                                 umount ${cow_backing}
394                                         fi
395                                         ;;
396                                 *)
397                                         ;;
398                         esac
399                 done
400         done
401         return 1
402 }
403
404 find_files ()
405 {
406         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
407         # FIXME: merge with above function
408
409         filenames="${1}"
410         snap_backing="/snap-backing"
411         black_listed_devices="${2}"
412
413         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
414         do
415                 for dev in $(subdevices "${sysblock}")
416                 do
417                         devname=$(sys2dev "${dev}")
418                         devfstype="$(get_fstype ${devname})"
419
420                         if echo "${black_listed_devices}" | grep -q "${devname}"
421                         then
422                                 # skip this device enterely
423                                 break
424                         fi
425
426                         if is_supported_fs ${devfstype}
427                         then
428                                 mkdir -p "${snap_backing}"
429
430                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
431                                 then
432                                         for filename in ${filenames}
433                                         do
434                                                 if [ -f "${snap_backing}/${filename}" ]
435                                                 then
436                                                         echo "${devname} ${snap_backing} ${filename}"
437                                                         umount ${snap_backing}
438                                                         return 0
439                                                 fi
440                                         done
441                                 fi
442
443                                 umount ${snap_backing}
444                         fi
445                 done
446         done
447 }
448
449 get_mac ()
450 {
451         mac=""
452
453         for adaptor in /sys/class/net/*
454         do
455                 status="$(cat ${adaptor}/iflink)"
456
457                 if [ "${status}" -eq 2 ]
458                 then
459                         mac="$(cat ${adaptor}/address)"
460                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
461                 fi
462         done
463
464         echo ${mac}
465 }
466
467 is_luks()
468 {
469     devname="${1}"
470     if [ -x /sbin/cryptsetup ]
471     then
472         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
473         return ${ret}
474     else
475         return 1
476     fi
477
478 }