Don't redirect output of fstype detection to standard out, thanks to Ronny Standtke...
[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 sys2dev ()
10 {
11         sysdev=${1#/sys}
12         echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
13 }
14
15 subdevices ()
16 {
17         sysblock=${1}
18         r=""
19
20         for dev in "${sysblock}" "${sysblock}"/*
21         do
22                 if [ -e "${dev}/dev" ]
23                 then
24                         r="${r} ${dev}"
25                 fi
26         done
27
28         echo ${r}
29 }
30
31 is_supported_fs ()
32 {
33         fstype="${1}"
34
35         # Validate input first
36         if [ -z "${fstype}" ]
37         then
38                 return 1
39         fi
40
41         # Try to look if it is already supported by the kernel
42         if grep -q ${fstype} /proc/filesystems
43         then
44                 return 0
45         else
46                 # Then try to add support for it the gentle way using the initramfs capabilities
47                 modprobe ${fstype}
48                 if grep -q ${fstype} /proc/filesystems
49                 then
50                         return 0
51                 # Then try the hard way if /root is already reachable
52                 else
53                         kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
54                         if [ -e "${kmodule}" ]
55                         then
56                                 insmod "${kmodule}"
57                                 if grep -q ${fstype} /proc/filesystems
58                                 then
59                                         return 0
60                                 fi
61                         fi
62                 fi
63         fi
64
65         return 1
66 }
67
68 get_fstype ()
69 {
70         local FSTYPE
71         local FSSIZE
72
73         # fstype misreports LUKS devices
74         if is_luks "${1}"
75         then
76             /lib/udev/vol_id -t ${1} 2>/dev/null
77             return
78         fi
79
80         eval $(fstype ${1} 2>/dev/null)
81
82         if [ "${FSTYPE}" != "unknown" ]
83         then
84                 echo ${FSTYPE}
85                 return 0
86         fi
87
88         /lib/udev/vol_id -t ${1} 2>/dev/null
89 }
90
91 where_is_mounted ()
92 {
93         device=${1}
94
95         if grep -q "^${device} " /proc/mounts
96         then
97                 # return the first found
98                 grep "^${device} " /proc/mounts | cut -f2 -d ' '
99         fi
100 }
101
102 lastline ()
103 {
104         while read lines
105         do
106                 line=${lines}
107         done
108
109         echo "${line}"
110 }
111
112 base_path ()
113 {
114         testpath="${1}"
115         mounts="$(awk '{print $2}' /proc/mounts)"
116         testpath="$(busybox realpath ${testpath})"
117
118         while true
119         do
120                 if echo "${mounts}" | grep -qs "^${testpath}"
121                 then
122                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
123                         echo ${1}
124                         break
125                 else
126                         testpath=$(dirname $testpath)
127                 fi
128         done
129 }
130
131 fs_size ()
132 {
133         # Returns used/free fs kbytes + 5% more
134         # You could pass a block device as ${1} or the mount point as ${2}
135
136         dev="${1}"
137         mountp="${2}"
138         used="${3}"
139
140         if [ -z "${mountp}" ]
141         then
142                 mountp="$(where_is_mounted ${dev})"
143
144                 if [ -z "${mountp}" ]
145                 then
146                         mountp="/mnt/tmp_fs_size"
147
148                         mkdir -p "${mountp}"
149                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
150
151                         doumount=1
152                 fi
153         fi
154
155         if [ "${used}" = "used" ]
156         then
157                 size=$(du -ks ${mountp} | cut -f1)
158                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
159         else
160                 # free space
161                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
162         fi
163
164         if [ -n "${doumount}" ]
165         then
166                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
167                 rmdir "${mountp}"
168         fi
169
170         echo "${size}"
171 }
172
173 load_keymap ()
174 {
175         # Load custom keymap
176         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
177         then
178                 loadkeys /etc/boottime.kmap.gz
179         fi
180 }
181
182 setup_loop ()
183 {
184         local fspath=${1}
185         local module=${2}
186         local pattern=${3}
187         local offset=${4}
188         local encryption=${5}
189         local readonly=${6}
190
191         modprobe -q -b "${module}"
192
193         if [ -x /sbin/udevadm ]
194         then
195                 # lenny
196                 udevadm settle
197         else
198                 # etch
199                 udevsettle
200         fi
201
202         for loopdev in ${pattern}
203         do
204                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
205                 then
206                         dev=$(sys2dev "${loopdev}")
207                         options=''
208
209                         if [ -n ${readonly} ]
210                         then
211                                 if losetup --help 2>&1 | grep -q -- "-r\b"
212                                 then
213                                         options="${options} -r"
214                                 fi
215                         fi
216
217                         if [ 0 -lt "${offset}" ]
218                         then
219                                 options="${options} -o ${offset}"
220                         fi
221
222                         if [ -z "${encryption}" ]
223                         then
224                                 losetup ${options} "${dev}" "${fspath}"
225                         else
226                                 # Loop AES encryption
227                                 while true
228                                 do
229                                         load_keymap
230
231                                         echo -n "Enter passphrase for root filesystem: " >&6
232                                         read -s passphrase
233                                         echo "${passphrase}" > /tmp/passphrase
234                                         unset passphrase
235                                         exec 9</tmp/passphrase
236                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
237                                         error=${?}
238                                         exec 9<&-
239                                         rm -f /tmp/passphrase
240
241                                         if [ 0 -eq ${error} ]
242                                         then
243                                                 unset error
244                                                 break
245                                         fi
246
247                                         echo
248                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
249                                         read answer
250
251                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
252                                         then
253                                                 unset answer
254                                                 break
255                                         fi
256                                 done
257                         fi
258
259                         echo "${dev}"
260                         return 0
261                 fi
262         done
263
264         panic "No loop devices available"
265 }
266
267 try_mount ()
268 {
269         dev="${1}"
270         mountp="${2}"
271         opts="${3}"
272         fstype="${4}"
273
274         old_mountp="$(where_is_mounted ${dev})"
275
276         if [ -n "${old_mountp}" ]
277         then
278                 if [ "${opts}" != "ro" ]
279                 then
280                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
281                 fi
282
283                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
284         else
285                 if [ -z "${fstype}" ]
286                 then
287                         fstype=$(get_fstype "${dev}")
288                 fi
289                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}"
290         fi
291 }
292
293 find_cow_device ()
294 {
295         # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
296         #  in the latter case the partition containing the file is left mounted
297         #  if is not in black_listed_devices
298         pers_label="${1}"
299         cow_backing="/${pers_label}-backing"
300         black_listed_devices="${2}"
301
302         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
303         do
304                 for dev in $(subdevices "${sysblock}")
305                 do
306                         devname=$(sys2dev "${dev}")
307
308                         if echo "${black_listed_devices}" | grep -q "${devname}"
309                         then
310                                 # skip this device enterely
311                                 break
312                         fi
313
314                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
315                         then
316                                 echo "${devname}"
317                                 return
318                         fi
319
320                         if [ "${PERSISTENT}" = "nofiles" ]
321                         then
322                                 # do not mount the device to find for image files
323                                 # just skip this
324                                 break
325                         fi
326
327                         case "$(get_fstype ${devname})" in
328                                 vfat|ext2|ext3|jffs2)
329                                         mkdir -p "${cow_backing}"
330                                         if ! try_mount "${devname}" "${cow_backing}" "rw"
331                                         then
332                                                 break
333                                         fi
334
335                                         if [ -f "${cow_backing}/${pers_label}" ]
336                                         then
337                                                 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
338                                                 return 0
339                                         else
340                                                 umount ${cow_backing}
341                                         fi
342                                         ;;
343                                 *)
344                                         ;;
345                         esac
346                 done
347         done
348 }
349
350 find_files ()
351 {
352         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
353         # FIXME: merge with above function
354
355         filenames="${1}"
356         snap_backing="/snap-backing"
357         black_listed_devices="${2}"
358
359         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
360         do
361                 for dev in $(subdevices "${sysblock}")
362                 do
363                         devname=$(sys2dev "${dev}")
364                         devfstype="$(get_fstype ${devname})"
365
366                         if echo "${black_listed_devices}" | grep -q "${devname}"
367                         then
368                                 # skip this device enterely
369                                 break
370                         fi
371
372                         if is_supported_fs ${devfstype}
373                         then
374                                 mkdir -p "${snap_backing}"
375
376                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
377                                 then
378                                         for filename in ${filenames}
379                                         do
380                                                 if [ -f "${snap_backing}/${filename}" ]
381                                                 then
382                                                         echo "${devname} ${snap_backing} ${filename}"
383                                                         umount ${snap_backing}
384                                                         return 0
385                                                 fi
386                                         done
387                                 fi
388
389                                 umount ${snap_backing}
390                         fi
391                 done
392         done
393 }
394
395 get_mac ()
396 {
397         mac=""
398
399         for adaptor in /sys/class/net/*
400         do
401                 status="$(cat ${adaptor}/iflink)"
402
403                 if [ "${status}" -eq 2 ]
404                 then
405                         mac="$(cat ${adaptor}/address)"
406                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
407                 fi
408         done
409
410         echo ${mac}
411 }
412
413 is_luks()
414 {
415     devname="${1}"
416     if [ -x /sbin/cryptsetup ]
417     then
418         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
419         return ${ret}
420     else
421         return 1
422     fi
423
424 }