59bdb4fd673c9dc4c4d631b159af394ee5448915
[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                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
331                         then
332                                 echo "${devname}"
333                                 return
334                         fi
335
336                         if [ "${PERSISTENT}" = "nofiles" ]
337                         then
338                                 # do not mount the device to find for image files
339                                 # just skip this
340                                 break
341                         fi
342
343                         case "$(get_fstype ${devname})" in
344                                 vfat|ext2|ext3|ext4|jffs2)
345                                         mkdir -p "${cow_backing}"
346                                         if ! try_mount "${devname}" "${cow_backing}" "rw"
347                                         then
348                                                 break
349                                         fi
350
351                                         if [ -f "${pers_fpath}" ]
352                                         then
353                                                 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
354                                                 return 0
355                                         else
356                                                 umount ${cow_backing}
357                                         fi
358                                         ;;
359                                 *)
360                                         ;;
361                         esac
362                 done
363         done
364 }
365
366 find_files ()
367 {
368         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
369         # FIXME: merge with above function
370
371         filenames="${1}"
372         snap_backing="/snap-backing"
373         black_listed_devices="${2}"
374
375         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
376         do
377                 for dev in $(subdevices "${sysblock}")
378                 do
379                         devname=$(sys2dev "${dev}")
380                         devfstype="$(get_fstype ${devname})"
381
382                         if echo "${black_listed_devices}" | grep -q "${devname}"
383                         then
384                                 # skip this device enterely
385                                 break
386                         fi
387
388                         if is_supported_fs ${devfstype}
389                         then
390                                 mkdir -p "${snap_backing}"
391
392                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
393                                 then
394                                         for filename in ${filenames}
395                                         do
396                                                 if [ -f "${snap_backing}/${filename}" ]
397                                                 then
398                                                         echo "${devname} ${snap_backing} ${filename}"
399                                                         umount ${snap_backing}
400                                                         return 0
401                                                 fi
402                                         done
403                                 fi
404
405                                 umount ${snap_backing}
406                         fi
407                 done
408         done
409 }
410
411 get_mac ()
412 {
413         mac=""
414
415         for adaptor in /sys/class/net/*
416         do
417                 status="$(cat ${adaptor}/iflink)"
418
419                 if [ "${status}" -eq 2 ]
420                 then
421                         mac="$(cat ${adaptor}/address)"
422                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
423                 fi
424         done
425
426         echo ${mac}
427 }
428
429 is_luks()
430 {
431     devname="${1}"
432     if [ -x /sbin/cryptsetup ]
433     then
434         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
435         return ${ret}
436     else
437         return 1
438     fi
439
440 }