ee9521ea741a4f9c071cd5f7958ae941846047b6
[live-boot-grml.git] / scripts / boot / persistence-excludes.sh
1 #!/bin/sh
2
3 #set -e
4
5 persistence_exclude ()
6 {
7         # Persistence enhancer script
8         # This script saves precious time on slow persistence devices/image files
9         # and writes on flash based device.
10         # a tmpfs on $PERSTMP is mounted and directories listed in
11         # /etc/live-persistence.binds will be copied there and then bind mounted back.
12
13         if [ -z "${PERSISTENCE}" ] || [ -n "${NOPERSISTENCE}" ] || [ -z "${PERSISTENCE_IS_ON}" ] || [ ! -f /root/etc/live-persistence.binds ]
14         then
15                 return
16         fi
17
18         # FIXME: stop hardcoding overloading of initramfs-tools functions
19         . /scripts/functions
20         . /lib/live/boot/initramfs-tools.sh
21
22         dirs="$(sed -e '/^ *$/d' -e '/^#.*$/d' /root/etc/live-persistence.binds | tr '\n' '\0')"
23         if [ -z "${dirs}" ]
24         then
25                 return
26         fi
27
28         log_begin_msg "Moving persistence bind mounts"
29
30         PERSTMP="/root/live/persistence-binds"
31         CPIO="/bin/cpio"
32
33         if [ ! -d "${PERSTMP}" ]
34         then
35                 mkdir -p "${PERSTMP}"
36         fi
37
38         mount -t tmpfs tmpfs "${PERSTMP}"
39
40         for dir in $(echo "${dirs}" | tr '\0' '\n')
41         do
42                 if [ ! -e "/root/${dir}" ] && [ ! -L "/root/${dir}" ]
43                 then
44                         # directory do not exists, create it
45                         mkdir -p "/root/${dir}"
46                 elif [ ! -d "/root/${dir}" ]
47                 then
48                         # it is not a directory, skip it
49                         break
50                 fi
51
52                 # Copy previous content if any
53                 cd "/root/${dir}"
54                 find . -print0 | ${CPIO} -pumd0 "${PERSTMP}/${dir}"
55                 cd "${OLDPWD}"
56
57                 # Bind mount it to origin
58                 mount -o bind "${PERSTMP}/${dir}" "/root/${dir}"
59         done
60
61         log_end_msg
62 }