Adding ext4 support.
[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         if [ -z "${PERSISTENT_PATH}" ]
303         then
304                 pers_fpath=${cow_backing}/${pers_label}
305         else
306                 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
307         fi
308
309         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
310         do
311                 for dev in $(subdevices "${sysblock}")
312                 do
313                         devname=$(sys2dev "${dev}")
314
315                         if echo "${black_listed_devices}" | grep -q "${devname}"
316                         then
317                                 # skip this device enterely
318                                 break
319                         fi
320
321                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
322                         then
323                                 echo "${devname}"
324                                 return
325                         fi
326
327                         if [ "${PERSISTENT}" = "nofiles" ]
328                         then
329                                 # do not mount the device to find for image files
330                                 # just skip this
331                                 break
332                         fi
333
334                         case "$(get_fstype ${devname})" in
335                                 vfat|ext2|ext3|ext4|jffs2)
336                                         mkdir -p "${cow_backing}"
337                                         if ! try_mount "${devname}" "${cow_backing}" "rw"
338                                         then
339                                                 break
340                                         fi
341
342                                         if [ -f "${pers_fpath}" ]
343                                         then
344                                                 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
345                                                 return 0
346                                         else
347                                                 umount ${cow_backing}
348                                         fi
349                                         ;;
350                                 *)
351                                         ;;
352                         esac
353                 done
354         done
355 }
356
357 find_files ()
358 {
359         # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
360         # FIXME: merge with above function
361
362         filenames="${1}"
363         snap_backing="/snap-backing"
364         black_listed_devices="${2}"
365
366         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
367         do
368                 for dev in $(subdevices "${sysblock}")
369                 do
370                         devname=$(sys2dev "${dev}")
371                         devfstype="$(get_fstype ${devname})"
372
373                         if echo "${black_listed_devices}" | grep -q "${devname}"
374                         then
375                                 # skip this device enterely
376                                 break
377                         fi
378
379                         if is_supported_fs ${devfstype}
380                         then
381                                 mkdir -p "${snap_backing}"
382
383                                 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
384                                 then
385                                         for filename in ${filenames}
386                                         do
387                                                 if [ -f "${snap_backing}/${filename}" ]
388                                                 then
389                                                         echo "${devname} ${snap_backing} ${filename}"
390                                                         umount ${snap_backing}
391                                                         return 0
392                                                 fi
393                                         done
394                                 fi
395
396                                 umount ${snap_backing}
397                         fi
398                 done
399         done
400 }
401
402 get_mac ()
403 {
404         mac=""
405
406         for adaptor in /sys/class/net/*
407         do
408                 status="$(cat ${adaptor}/iflink)"
409
410                 if [ "${status}" -eq 2 ]
411                 then
412                         mac="$(cat ${adaptor}/address)"
413                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
414                 fi
415         done
416
417         echo ${mac}
418 }
419
420 is_luks()
421 {
422     devname="${1}"
423     if [ -x /sbin/cryptsetup ]
424     then
425         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
426         return ${ret}
427     else
428         return 1
429     fi
430
431 }