Be sure that the discovered filesystem is not null.
[live-boot-grml.git] / scripts / live-helpers
1 #!/bin/sh
2 # live-initramfs helper functions, used by live-initramfs on boot and by live-snapshot
3
4 if [ ! -x "/bin/fstype" ]
5 then
6         # klibc not in path -> not in initramfs
7         export PATH="${PATH}:/usr/lib/klibc/bin"
8 fi
9
10 sys2dev ()
11 {
12         sysdev=${1#/sys}
13         echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
14 }
15
16 subdevices ()
17 {
18         sysblock=${1}
19         r=""
20
21         for dev in "${sysblock}" "${sysblock}"/*
22         do
23                 if [ -e "${dev}/dev" ]
24                 then
25                         r="${r} ${dev}"
26                 fi
27         done
28
29         echo ${r}
30 }
31
32 is_supported_fs ()
33 {
34         fstype="${1}"
35
36         # Validate input first
37         if [ -z "${fstype}" ]
38         then
39                 return 1
40         fi
41
42         # Try to look if it is already supported by the kernel
43         if grep -q ${fstype} /proc/filesystems
44         then
45                 return 0
46         else
47                 # Then try to add support for it the gentle way using the initramfs capabilities
48                 modprobe ${fstype}
49                 if grep -q ${fstype} /proc/filesystems
50                 then
51                         return 0
52                 # Then try the hard way if /root is already reachable
53                 else
54                         kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
55                         if [ -e "${kmodule}" ]
56                         then
57                                 insmod "${kmodule}"
58                                 if grep -q ${fstype} /proc/filesystems
59                                 then
60                                         return 0
61                                 fi
62                         fi
63                 fi
64         fi
65
66         return 1
67 }
68
69 get_fstype ()
70 {
71         local FSTYPE
72         local FSSIZE
73
74         # fstype misreports LUKS devices
75         if is_luks "${1}"
76         then
77             /lib/udev/vol_id -t ${1} 2>/dev/null
78             return
79         fi
80
81         eval $(fstype < ${1})
82
83         if [ "${FSTYPE}" != "unknown" ]
84         then
85                 echo ${FSTYPE}
86                 return 0
87         fi
88
89         /lib/udev/vol_id -t ${1} 2>/dev/null
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 "^${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}"
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}"
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         udevsettle
194
195         for loopdev in ${pattern}
196         do
197                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
198                 then
199                         dev=$(sys2dev "${loopdev}")
200                         options=''
201
202                         if [ -n ${readonly} ]
203                         then
204                                 if losetup --help 2>&1 | grep -q -- "-r\b"
205                                 then
206                                         options="${options} -r"
207                                 fi
208                         fi
209
210                         if [ 0 -lt "${offset}" ]
211                         then
212                                 options="${options} -o ${offset}"
213                         fi
214
215                         if [ -z "${encryption}" ]
216                         then
217                                 losetup ${options} "${dev}" "${fspath}"
218                         else
219                                 # Loop AES encryption
220                                 while true
221                                 do
222                                         load_keymap
223
224                                         echo -n "Enter passphrase for root filesystem: " >&6
225                                         read -s passphrase
226                                         echo "${passphrase}" > /tmp/passphrase
227                                         unset passphrase
228                                         exec 9</tmp/passphrase
229                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
230                                         error=${?}
231                                         exec 9<&-
232                                         rm -f /tmp/passphrase
233
234                                         if [ 0 -eq ${error} ]
235                                         then
236                                                 unset error
237                                                 break
238                                         fi
239
240                                         echo
241                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
242                                         read answer
243
244                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
245                                         then
246                                                 unset answer
247                                                 break
248                                         fi
249                                 done
250                         fi
251
252                         echo "${dev}"
253                         return 0
254                 fi
255         done
256
257         panic "No loop devices available"
258 }
259
260 try_mount ()
261 {
262         dev="${1}"
263         mountp="${2}"
264         opts="${3}"
265
266         old_mountp="$(where_is_mounted ${dev})"
267
268         if [ -n "${old_mountp}" ]
269         then
270                 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
271                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
272         else
273                 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
274         fi
275 }
276
277 find_cow_device ()
278 {
279         pers_label="${1}"
280         cow_backing="/${pers_label}-backing"
281
282         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
283         do
284                 for dev in $(subdevices "${sysblock}")
285                 do
286                         devname=$(sys2dev "${dev}")
287
288                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
289                         then
290                                 echo "${devname}"
291                                 return
292                         fi
293
294                         case "$(get_fstype ${devname})" in
295                                 vfat|ext2|ext3|jffs2)
296                                         mkdir -p "${cow_backing}"
297                                         try_mount "${devname}" "${cow_backing}" "rw"
298
299                                         if [ -f "${cow_backing}/${pers_label}" ]
300                                         then
301                                                 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
302                                                 return 0
303                                         else
304                                                 umount ${cow_backing}
305                                         fi
306                                         ;;
307                                 *)
308                                         ;;
309                         esac
310                 done
311         done
312 }
313
314 find_files ()
315 {
316         # return the first of ${filenames} found on vfat and ext2/ext3 devices
317         # FIXME: merge with above function
318
319         filenames="${1}"
320         snap_backing="/snap-backing"
321
322         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
323         do
324                 for dev in $(subdevices "${sysblock}")
325                 do
326                         devname=$(sys2dev "${dev}")
327                         devfstype="$(get_fstype ${devname})"
328
329                         if is_supported_fs ${devfstype}
330                         then
331                                 mkdir -p "${snap_backing}"
332                                 try_mount "${devname}" "${snap_backing}" "ro"
333
334                                 for filename in ${filenames}
335                                         do
336                                         if [ -f "${snap_backing}/${filename}" ]
337                                         then
338                                                 echo "${devname} ${snap_backing} ${filename}"
339                                                 return 0
340                                         fi
341                                 done
342
343                                 umount ${snap_backing}
344                         fi
345                 done
346         done
347 }
348
349 get_mac ()
350 {
351         mac=""
352
353         for adaptor in /sys/class/net/*
354         do
355                 status="$(cat ${adaptor}/iflink)"
356
357                 if [ "${status}" -eq 2 ]
358                 then
359                         mac="$(cat ${adaptor}/address)"
360                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
361                 fi
362         done
363
364         echo ${mac}
365 }
366
367 is_luks()
368 {
369     devname="${1}"
370     if [ -x /sbin/cryptsetup ]
371     then
372         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
373         return ${ret}
374     else
375         return 1
376     fi
377
378 }