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