Merging casper 1.132.
[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         # FIXME: do something better like the scan of supported filesystems
35         fstype="${1}"
36
37         case ${fstype} in
38                 vfat|iso9660|udf|ext2|ext3|ntfs|jffs2)
39                 return 0
40                 ;;
41         esac
42
43         return 1
44 }
45
46 get_fstype ()
47 {
48         local FSTYPE
49         local FSSIZE
50
51         # fstype misreports LUKS devices
52         if is_luks "${1}"
53         then
54             /lib/udev/vol_id -t ${1} 2>/dev/null
55             return
56         fi
57
58         eval $(fstype < ${1})
59
60         if [ "${FSTYPE}" != "unknown" ]
61         then
62                 echo ${FSTYPE}
63                 return 0
64         fi
65
66         /lib/udev/vol_id -t ${1} 2>/dev/null
67 }
68
69 where_is_mounted ()
70 {
71         device=${1}
72
73         if grep -q "^${device} " /proc/mounts
74         then
75                 grep "^${device} " /proc/mounts | read d mountpoint rest
76                 echo ${mountpoint}
77                 return 0
78         fi
79
80         return 1
81 }
82
83 lastline ()
84 {
85         while read lines
86         do
87                 line=${lines}
88         done
89
90         echo "${line}"
91 }
92
93 base_path ()
94 {
95         testpath="${1}"
96         mounts="$(awk '{print $2}' /proc/mounts)"
97         testpath="$(busybox realpath ${testpath})"
98
99         while true
100         do
101                 if echo "${mounts}" | grep -qs "^${testpath}"
102                 then
103                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
104                         echo ${1}
105                         break
106                 else
107                         testpath=$(dirname $testpath)
108                 fi
109         done
110 }
111
112 fs_size ()
113 {
114         # Returns used/free fs kbytes + 5% more
115         # You could pass a block device as ${1} or the mount point as ${2}
116
117         dev="${1}"
118         mountp="${2}"
119         used="${3}"
120
121         if [ -z "${mountp}" ]
122         then
123                 mountp=$(where_is_mounted "${dev}")
124
125                 if [ "${?}" -gt 0 ]
126                 then
127                         mountp="/mnt/tmp_fs_size"
128
129                         mkdir -p "${mountp}"
130                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
131
132                         doumount=1
133                 fi
134         fi
135
136         if [ "${used}" = "used" ]
137         then
138                 size=$(du -ks ${mountp} | cut -f1)
139                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
140         else
141                 # free space
142                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
143         fi
144
145         if [ -n "${doumount}" ]
146         then
147                 umount "${mountp}"
148                 rmdir "${mountp}"
149         fi
150
151         echo "${size}"
152 }
153
154 load_keymap ()
155 {
156         # Load custom keymap
157         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
158         then
159                 loadkeys /etc/boottime.kmap.gz
160         fi
161 }
162
163 setup_loop ()
164 {
165         local fspath=${1}
166         local module=${2}
167         local pattern=${3}
168         local offset=${4}
169         local encryption=${5}
170
171         modprobe -q -b "${module}"
172         udevsettle
173
174         for loopdev in ${pattern}
175         do
176                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
177                 then
178                         dev=$(sys2dev "${loopdev}")
179                         options=''
180
181                         if [ 0 -lt "${offset}" ]
182                         then
183                                 options="${options} -o ${offset}"
184                         fi
185
186                         if [ -z "${encryption}" ]
187                         then
188                                 losetup ${options} "${dev}" "${fspath}"
189                         else
190                                 # Loop AES encryption
191                                 while true
192                                 do
193                                         load_keymap
194
195                                         echo -n "Enter passphrase for root filesystem: " >&6
196                                         read -s passphrase
197                                         echo "${passphrase}" > /tmp/passphrase
198                                         unset passphrase
199                                         exec 9</tmp/passphrase
200                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
201                                         error=${?}
202                                         exec 9<&-
203                                         rm -f /tmp/passphrase
204
205                                         if [ 0 -eq ${error} ]
206                                         then
207                                                 unset error
208                                                 break
209                                         fi
210
211                                         echo
212                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
213                                         read answer
214
215                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
216                                         then
217                                                 unset answer
218                                                 break
219                                         fi
220                                 done
221                         fi
222
223                         echo "${dev}"
224                         return 0
225                 fi
226         done
227
228         panic "No loop devices available"
229 }
230
231 try_mount ()
232 {
233         dev="${1}"
234         mountp="${2}"
235         opts="${3}"
236
237         if where_is_mounted ${dev} > /dev/null
238         then
239                 mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed"
240                 mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount"
241         else
242                 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
243         fi
244 }
245
246 find_cow_device ()
247 {
248         pers_label="${1}"
249         cow_backing="/${pers_label}-backing"
250
251         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
252         do
253                 for dev in $(subdevices "${sysblock}")
254                 do
255                         devname=$(sys2dev "${dev}")
256
257                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
258                         then
259                                 echo "${devname}"
260                                 return
261                         fi
262
263                         case "$(get_fstype ${devname})" in
264                                 vfat|ext2|ext3|jffs2)
265                                         mkdir -p "${cow_backing}"
266                                         try_mount "${devname}" "${cow_backing}" "rw"
267
268                                         if [ -f "${cow_backing}/${pers_label}" ]
269                                         then
270                                                 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
271                                                 return 0
272                                         else
273                                                 umount ${cow_backing}
274                                         fi
275                                         ;;
276                                 *)
277                                         ;;
278                         esac
279                 done
280         done
281 }
282
283 find_files ()
284 {
285         # return the first of ${filenames} found on vfat and ext2/ext3 devices
286         # FIXME: merge with above function
287
288         filenames="${1}"
289         snap_backing="/snap-backing"
290
291         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
292         do
293                 for dev in $(subdevices "${sysblock}")
294                 do
295                         devname=$(sys2dev "${dev}")
296                         devfstype="$(get_fstype ${devname})"
297
298                         if is_supported_fs ${devfstype}
299                         then
300                                 mkdir -p "${snap_backing}"
301                                 try_mount "${devname}" "${snap_backing}" "ro"
302
303                                 for filename in ${filenames}
304                                         do
305                                         if [ -f "${snap_backing}/${filename}" ]
306                                         then
307                                                 echo "${devname} ${snap_backing} ${filename}"
308                                                 return 0
309                                         fi
310                                 done
311
312                                 umount ${snap_backing}
313                         fi
314                 done
315         done
316 }
317
318 get_mac ()
319 {
320         mac=""
321
322         for adaptor in /sys/class/net/*
323         do
324                 status="$(cat ${adaptor}/iflink)"
325
326                 if [ "${status}" -eq 2 ]
327                 then
328                         mac="$(cat ${adaptor}/address)"
329                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
330                 fi
331         done
332
333         echo ${mac}
334 }
335
336 is_luks()
337 {
338     devname="${1}"
339     if [ -x /sbin/cryptsetup ]
340     then
341         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
342         return ${ret}
343     else
344         return 1
345     fi
346
347 }