Removing etch compatibility.
[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         udevadm settle
195
196         for loopdev in ${pattern}
197         do
198                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
199                 then
200                         dev=$(sys2dev "${loopdev}")
201                         options=''
202
203                         if [ -n "${readonly}" ]
204                         then
205                                 if losetup --help 2>&1 | grep -q -- "-r\b"
206                                 then
207                                         options="${options} -r"
208                                 fi
209                         fi
210
211                         if [ 0 -lt "${offset}" ]
212                         then
213                                 options="${options} -o ${offset}"
214                         fi
215
216                         if [ -z "${encryption}" ]
217                         then
218                                 losetup ${options} "${dev}" "${fspath}"
219                         else
220                                 # Loop AES encryption
221                                 while true
222                                 do
223                                         load_keymap
224
225                                         echo -n "Enter passphrase for root filesystem: " >&6
226                                         read -s passphrase
227                                         echo "${passphrase}" > /tmp/passphrase
228                                         unset passphrase
229                                         exec 9</tmp/passphrase
230                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
231                                         error=${?}
232                                         exec 9<&-
233                                         rm -f /tmp/passphrase
234
235                                         if [ 0 -eq ${error} ]
236                                         then
237                                                 unset error
238                                                 break
239                                         fi
240
241                                         echo
242                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
243                                         read answer
244
245                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
246                                         then
247                                                 unset answer
248                                                 break
249                                         fi
250                                 done
251                         fi
252
253                         echo "${dev}"
254                         return 0
255                 fi
256         done
257
258         panic "No loop devices available"
259 }
260
261 try_mount ()
262 {
263         dev="${1}"
264         mountp="${2}"
265         opts="${3}"
266         fstype="${4}"
267
268         old_mountp="$(where_is_mounted ${dev})"
269
270         if [ -n "${old_mountp}" ]
271         then
272                 if [ "${opts}" != "ro" ]
273                 then
274                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
275                 fi
276
277                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
278         else
279                 if [ -z "${fstype}" ]
280                 then
281                         fstype=$(get_fstype "${dev}")
282                 fi
283                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
284                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live.log && return 0 )
285         fi
286 }
287
288 find_cow_device ()
289 {
290         # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
291         #  in the latter case the partition containing the file is left mounted
292         #  if is not in black_listed_devices
293         pers_label="${1}"
294         cow_backing="/${pers_label}-backing"
295         black_listed_devices="${2}"
296
297         if [ -z "${PERSISTENT_PATH}" ]
298         then
299                 pers_fpath=${cow_backing}/${pers_label}
300         else
301                 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
302         fi
303
304         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
305         do
306                 for dev in $(subdevices "${sysblock}")
307                 do
308                         devname=$(sys2dev "${dev}")
309
310                         if echo "${black_listed_devices}" | grep -q "${devname}"
311                         then
312                                 # skip this device enterely
313                                 break
314                         fi
315
316                         # Checking for a luks device
317                         if [ "${PERSISTENT}" = "cryptsetup" ] && [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
318                         then
319                                 while true
320                                 do
321                                         load_keymap
322
323                                         /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${devname}: " | /sbin/cryptsetup -T 1 luksOpen ${devname} $(basename ${devname}) --key-file=-
324                                         error=${?}
325
326                                         devname="/dev/mapper/$(basename ${devname})"
327
328                                         if [ 0 -eq ${error} ]
329                                         then
330                                                 unset error
331                                                 break
332                                         fi
333
334                                         echo
335                                         echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
336                                         read answer
337
338                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
339                                         then
340                                                 unset answer
341                                                 break
342                                         fi
343                                 done
344                         fi
345
346                         # udev >=146-1 no longer provides vol_id:
347                         if [ -x /lib/udev/vol_id ]
348                         then
349                                 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
350                                 then
351                                         echo "${devname}"
352                                         return 0
353                                 fi
354                         else
355                                 eval $(blkid -o udev "${devname}")
356                                 if [ "$ID_FS_LABEL" = "${pers_label}" ]
357                                 then
358                                         echo "${devname}"
359                                         return 0
360                                 fi
361                         fi
362
363                         if [ "${PERSISTENT}" = "nofiles" ]
364                         then
365                                 # do not mount the device to find for image files
366                                 # just skip this
367                                 continue
368                         fi
369
370                         case "$(get_fstype ${devname})" in
371                                 vfat|ext2|ext3|ext4|jffs2)
372                                         mkdir -p "${cow_backing}"
373                                         if try_mount "${devname}" "${cow_backing}" "rw"
374                                         then
375                                                 if [ -f "${pers_fpath}" ]
376                                                 then
377                                                         echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
378                                                         return 0
379                                                 else
380                                                         umount ${cow_backing} > /dev/null 2>&1 || true
381                                                 fi
382                                         fi
383                                         ;;
384                                 *)
385                                         ;;
386                         esac
387                 done
388         done
389         return 1
390 }
391
392 find_files ()
393 {
394         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
395         # FIXME: merge with above function
396
397         filenames="${1}"
398         snap_backing="/snap-backing"
399         black_listed_devices="${2}"
400
401         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
402         do
403                 for dev in $(subdevices "${sysblock}")
404                 do
405                         devname=$(sys2dev "${dev}")
406                         devfstype="$(get_fstype ${devname})"
407
408                         if echo "${black_listed_devices}" | grep -q "${devname}"
409                         then
410                                 # skip this device enterely
411                                 break
412                         fi
413
414                         if is_supported_fs ${devfstype}
415                         then
416                                 mkdir -p "${snap_backing}"
417
418                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
419                                 then
420                                         for filename in ${filenames}
421                                         do
422                                                 if [ -f "${snap_backing}/${filename}" ]
423                                                 then
424                                                         echo "${devname} ${snap_backing} ${filename}"
425                                                         umount ${snap_backing}
426                                                         return 0
427                                                 fi
428                                         done
429                                 fi
430
431                                 umount ${snap_backing}
432                         fi
433                 done
434         done
435 }
436
437 get_mac ()
438 {
439         mac=""
440
441         for adaptor in /sys/class/net/*
442         do
443                 status="$(cat ${adaptor}/iflink)"
444
445                 if [ "${status}" -eq 2 ]
446                 then
447                         mac="$(cat ${adaptor}/address)"
448                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
449                 fi
450         done
451
452         echo ${mac}
453 }
454
455 is_luks()
456 {
457     devname="${1}"
458     if [ -x /sbin/cryptsetup ]
459     then
460         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
461         return ${ret}
462     else
463         return 1
464     fi
465
466 }