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