Skipped some runtime duplicated execution.
[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}"
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}"
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         pers_label="${1}"
292         cow_backing="/${pers_label}-backing"
293
294         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
295         do
296                 for dev in $(subdevices "${sysblock}")
297                 do
298                         devname=$(sys2dev "${dev}")
299
300                         if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
301                         then
302                                 echo "${devname}"
303                                 return
304                         fi
305
306                         case "$(get_fstype ${devname})" in
307                                 vfat|ext2|ext3|jffs2)
308                                         mkdir -p "${cow_backing}"
309                                         try_mount "${devname}" "${cow_backing}" "rw"
310
311                                         if [ -f "${cow_backing}/${pers_label}" ]
312                                         then
313                                                 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
314                                                 return 0
315                                         else
316                                                 umount ${cow_backing}
317                                         fi
318                                         ;;
319                                 *)
320                                         ;;
321                         esac
322                 done
323         done
324 }
325
326 find_files ()
327 {
328         # return the first of ${filenames} found on vfat and ext2/ext3 devices
329         # FIXME: merge with above function
330
331         filenames="${1}"
332         snap_backing="/snap-backing"
333
334         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
335         do
336                 for dev in $(subdevices "${sysblock}")
337                 do
338                         devname=$(sys2dev "${dev}")
339                         devfstype="$(get_fstype ${devname})"
340
341                         if is_supported_fs ${devfstype}
342                         then
343                                 mkdir -p "${snap_backing}"
344                                 try_mount "${devname}" "${snap_backing}" "ro" ${devfstype}
345
346                                 for filename in ${filenames}
347                                         do
348                                         if [ -f "${snap_backing}/${filename}" ]
349                                         then
350                                                 echo "${devname} ${snap_backing} ${filename}"
351                                                 return 0
352                                         fi
353                                 done
354
355                                 umount ${snap_backing}
356                         fi
357                 done
358         done
359 }
360
361 get_mac ()
362 {
363         mac=""
364
365         for adaptor in /sys/class/net/*
366         do
367                 status="$(cat ${adaptor}/iflink)"
368
369                 if [ "${status}" -eq 2 ]
370                 then
371                         mac="$(cat ${adaptor}/address)"
372                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
373                 fi
374         done
375
376         echo ${mac}
377 }
378
379 is_luks()
380 {
381     devname="${1}"
382     if [ -x /sbin/cryptsetup ]
383     then
384         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
385         return ${ret}
386     else
387         return 1
388     fi
389
390 }