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