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