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