Another fix about using udevsettle or "udevadm settle".
[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
194         if [ -x /sbin/udevadm ]
195         then
196                 # lenny
197                 udevadm settle
198         else
199                 # etch
200                 udevsettle
201         fi
202
203         for loopdev in ${pattern}
204         do
205                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
206                 then
207                         dev=$(sys2dev "${loopdev}")
208                         options=''
209
210                         if [ -n ${readonly} ]
211                         then
212                                 if losetup --help 2>&1 | grep -q -- "-r\b"
213                                 then
214                                         options="${options} -r"
215                                 fi
216                         fi
217
218                         if [ 0 -lt "${offset}" ]
219                         then
220                                 options="${options} -o ${offset}"
221                         fi
222
223                         if [ -z "${encryption}" ]
224                         then
225                                 losetup ${options} "${dev}" "${fspath}"
226                         else
227                                 # Loop AES encryption
228                                 while true
229                                 do
230                                         load_keymap
231
232                                         echo -n "Enter passphrase for root filesystem: " >&6
233                                         read -s passphrase
234                                         echo "${passphrase}" > /tmp/passphrase
235                                         unset passphrase
236                                         exec 9</tmp/passphrase
237                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
238                                         error=${?}
239                                         exec 9<&-
240                                         rm -f /tmp/passphrase
241
242                                         if [ 0 -eq ${error} ]
243                                         then
244                                                 unset error
245                                                 break
246                                         fi
247
248                                         echo
249                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
250                                         read answer
251
252                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
253                                         then
254                                                 unset answer
255                                                 break
256                                         fi
257                                 done
258                         fi
259
260                         echo "${dev}"
261                         return 0
262                 fi
263         done
264
265         panic "No loop devices available"
266 }
267
268 try_mount ()
269 {
270         dev="${1}"
271         mountp="${2}"
272         opts="${3}"
273
274         old_mountp="$(where_is_mounted ${dev})"
275
276         if [ -n "${old_mountp}" ]
277         then
278                 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
279                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
280         else
281                 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
282         fi
283 }
284
285 find_cow_device ()
286 {
287         pers_label="${1}"
288         cow_backing="/${pers_label}-backing"
289
290         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
291         do
292                 for dev in $(subdevices "${sysblock}")
293                 do
294                         devname=$(sys2dev "${dev}")
295
296                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
297                         then
298                                 echo "${devname}"
299                                 return
300                         fi
301
302                         case "$(get_fstype ${devname})" in
303                                 vfat|ext2|ext3|jffs2)
304                                         mkdir -p "${cow_backing}"
305                                         try_mount "${devname}" "${cow_backing}" "rw"
306
307                                         if [ -f "${cow_backing}/${pers_label}" ]
308                                         then
309                                                 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
310                                                 return 0
311                                         else
312                                                 umount ${cow_backing}
313                                         fi
314                                         ;;
315                                 *)
316                                         ;;
317                         esac
318                 done
319         done
320 }
321
322 find_files ()
323 {
324         # return the first of ${filenames} found on vfat and ext2/ext3 devices
325         # FIXME: merge with above function
326
327         filenames="${1}"
328         snap_backing="/snap-backing"
329
330         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
331         do
332                 for dev in $(subdevices "${sysblock}")
333                 do
334                         devname=$(sys2dev "${dev}")
335                         devfstype="$(get_fstype ${devname})"
336
337                         if is_supported_fs ${devfstype}
338                         then
339                                 mkdir -p "${snap_backing}"
340                                 try_mount "${devname}" "${snap_backing}" "ro"
341
342                                 for filename in ${filenames}
343                                         do
344                                         if [ -f "${snap_backing}/${filename}" ]
345                                         then
346                                                 echo "${devname} ${snap_backing} ${filename}"
347                                                 return 0
348                                         fi
349                                 done
350
351                                 umount ${snap_backing}
352                         fi
353                 done
354         done
355 }
356
357 get_mac ()
358 {
359         mac=""
360
361         for adaptor in /sys/class/net/*
362         do
363                 status="$(cat ${adaptor}/iflink)"
364
365                 if [ "${status}" -eq 2 ]
366                 then
367                         mac="$(cat ${adaptor}/address)"
368                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
369                 fi
370         done
371
372         echo ${mac}
373 }
374
375 is_luks()
376 {
377     devname="${1}"
378     if [ -x /sbin/cryptsetup ]
379     then
380         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
381         return ${ret}
382     else
383         return 1
384     fi
385
386 }