Remove sourcing /scripts/functions in components
[live-boot-grml.git] / components / 9990-overlay.sh
1 #!/bin/sh
2
3 #set -e
4
5 setup_unionfs ()
6 {
7         image_directory="${1}"
8         rootmnt="${2}"
9         addimage_directory="${3}"
10
11         modprobe -q -b ${UNIONTYPE}
12
13         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$"
14         then
15                 panic "${UNIONTYPE} not available."
16         fi
17
18         croot="/run/live/rootfs"
19
20         # Let's just mount the read-only file systems first
21         rootfslist=""
22
23         if [ -z "${PLAIN_ROOT}" ]
24         then
25                 # Read image names from ${MODULE}.module if it exists
26                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
27                 then
28                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
29                         do
30                                 image_string="${image_string} ${image_directory}/${IMAGE}"
31                         done
32                 elif [ -e "${image_directory}/${MODULE}.module" ]
33                 then
34                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
35                         do
36                                 image_string="${image_string} ${image_directory}/${IMAGE}"
37                         done
38                 else
39                         # ${MODULE}.module does not exist, create a list of images
40                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
41                         do
42                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
43                                 do
44                                         if [ -e "${IMAGE}" ]
45                                         then
46                                                 image_string="${image_string} ${IMAGE}"
47                                         fi
48                                 done
49                         done
50
51                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
52                         then
53                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
54                                 do
55                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
56                                         do
57                                                 if [ -e "${IMAGE}" ]
58                                                 then
59                                                         image_string="${image_string} ${IMAGE}"
60                                                 fi
61                                         done
62                                 done
63                         fi
64
65                         # Now sort the list
66                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
67                 fi
68
69                 [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
70
71                 mkdir -p "${croot}"
72
73                 for image in ${image_string}
74                 do
75                         imagename=$(basename "${image}")
76
77                         export image devname
78                         maybe_break live-realpremount
79                         log_begin_msg "Running /scripts/live-realpremount"
80                         run_scripts /scripts/live-realpremount
81                         log_end_msg
82
83                         if [ -d "${image}" ]
84                         then
85                                 # it is a plain directory: do nothing
86                                 rootfslist="${image} ${rootfslist}"
87                         elif [ -f "${image}" ]
88                         then
89                                 if losetup --help 2>&1 | grep -q -- "-r\b"
90                                 then
91                                         backdev=$(get_backing_device "${image}" "-r")
92                                 else
93                                         backdev=$(get_backing_device "${image}")
94                                 fi
95                                 fstype=$(get_fstype "${backdev}")
96
97                                 case "${fstype}" in
98                                         unknown)
99                                                 panic "Unknown file system type on ${backdev} (${image})"
100                                                 ;;
101
102                                         "")
103                                                 fstype="${imagename##*.}"
104                                                 log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
105                                                 ;;
106                                 esac
107
108                                 mpoint=$(trim_path "${croot}/${imagename}")
109                                 rootfslist="${mpoint} ${rootfslist}"
110
111                                 mkdir -p "${mpoint}"
112                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
113                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
114                                 log_end_msg
115                         else
116                                 log_warning_msg "Could not find image '${image}'. Most likely it is listed in a .module file, perhaps by mistake."
117                         fi
118                 done
119         else
120                 # we have a plain root system
121                 mkdir -p "${croot}/filesystem"
122                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
123                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
124                         panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
125                         rootfslist="${croot}/filesystem ${rootfslist}"
126                 # probably broken:
127                 mount -o bind ${croot}/filesystem $mountpoint
128                 log_end_msg
129         fi
130
131         # tmpfs file systems
132         touch /etc/fstab
133         mkdir -p /run/live/overlay
134
135         # Looking for persistence devices or files
136         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
137         then
138
139                 if [ -z "${QUICKUSBMODULES}" ]
140                 then
141                         # Load USB modules
142                         num_block=$(ls -l /sys/block | wc -l)
143                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
144                         do
145                                 modprobe -q -b ${module}
146                         done
147
148                         udevadm trigger
149                         udevadm settle
150
151                         # For some reason, udevsettle does not block in this scenario,
152                         # so we sleep for a little while.
153                         #
154                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
155                         for timeout in 5 4 3 2 1
156                         do
157                                 sleep 1
158
159                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
160                                 then
161                                         break
162                                 fi
163                         done
164                 fi
165
166                 local whitelistdev
167                 whitelistdev=""
168                 if [ -n "${PERSISTENCE_MEDIA}" ]
169                 then
170                         case "${PERSISTENCE_MEDIA}" in
171                                 removable)
172                                         whitelistdev="$(removable_dev)"
173                                         ;;
174
175                                 removable-usb)
176                                         whitelistdev="$(removable_usb_dev)"
177                                         ;;
178                         esac
179                         if [ -z "${whitelistdev}" ]
180                         then
181                                 whitelistdev="ignore_all_devices"
182                         fi
183                 fi
184
185                 if is_in_comma_sep_list overlay ${PERSISTENCE_METHOD}
186                 then
187                         overlays="${custom_overlay_label}"
188                 fi
189
190                 local overlay_devices
191                 overlay_devices=""
192                 if [ "${whitelistdev}" != "ignore_all_devices" ]
193                 then
194                         for media in $(find_persistence_media "${overlays}" "${whitelistdev}")
195                         do
196                                 media="$(echo ${media} | tr ":" " ")"
197
198                                 case ${media} in
199                                         ${custom_overlay_label}=*)
200                                                 device="${media#*=}"
201                                                 overlay_devices="${overlay_devices} ${device}"
202                                                 ;;
203                                  esac
204                         done
205                 fi
206         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENCE}" ]
207         then
208                 # check if there are any nfs options
209                 if echo ${NFS_COW} | grep -q ','
210                 then
211                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
212                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
213                 else
214                         nfs_cow_opts="-o nolock"
215                         nfs_cow=${NFS_COW}
216                 fi
217
218                 if [ -n "${PERSISTENCE_READONLY}" ]
219                 then
220                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
221                 fi
222
223                 mac="$(get_mac)"
224                 if [ -n "${mac}" ]
225                 then
226                         cowdevice=$(echo ${nfs_cow} | sed "s/client_mac_address/${mac}/")
227                         cow_fstype="nfs"
228                 else
229                         panic "unable to determine mac address"
230                 fi
231         fi
232
233         if [ -z "${cowdevice}" ]
234         then
235                 cowdevice="tmpfs"
236                 cow_fstype="tmpfs"
237                 cow_mountopt="rw,noatime,mode=755,size=${OVERLAY_SIZE:-50%}"
238         fi
239
240         if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
241         then
242                 mount -t tmpfs -o rw,noatime,mode=755,size=${OVERLAY_SIZE:-50%} tmpfs "/run/live/overlay"
243                 root_backing="/run/live/persistence/$(basename ${cowdevice})-root"
244                 mkdir -p ${root_backing}
245         else
246                 root_backing="/run/live/overlay"
247         fi
248
249         if [ "${cow_fstype}" = "nfs" ]
250         then
251                 log_begin_msg \
252                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing}"
253                 nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing} || \
254                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${root_backing}"
255         else
256                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${root_backing} || \
257                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${root_backing}"
258         fi
259
260         rootfscount=$(echo ${rootfslist} |wc -w)
261
262         rootfs=${rootfslist%% }
263
264         if [ -n "${EXPOSED_ROOT}" ]
265         then
266                 if [ ${rootfscount} -ne 1 ]
267                 then
268                         panic "only one RO file system supported with exposedroot: ${rootfslist}"
269                 fi
270
271                 mount -o bind ${rootfs} ${rootmnt} || \
272                         panic "bind mount of ${rootfs} failed"
273
274                 if [ -z "${SKIP_UNION_MOUNTS}" ]
275                 then
276                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
277                 else
278                         cow_dirs=''
279                 fi
280         else
281                 cow_dirs="/"
282         fi
283
284         for dir in ${cow_dirs}; do
285                 unionmountpoint=$(trim_path "${rootmnt}${dir}")
286                 mkdir -p ${unionmountpoint}
287                 cow_dir=$(trim_path "/run/live/overlay${dir}")
288                 rootfs_dir="${rootfs}${dir}"
289                 mkdir -p ${cow_dir}
290                 if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
291                 then
292                         do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rootfs_dir}
293                 else
294                         do_union ${unionmountpoint} ${cow_dir} ${rootfs_dir}
295                 fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
296         done
297
298         # Remove persistence depending on boot parameter
299         Remove_persistence
300
301         # Correct the permissions of /:
302         chmod 0755 "${rootmnt}"
303
304         # Correct the permission of /tmp:
305         if [ -d "${rootmnt}/tmp" ]
306         then
307                 chmod 1777 "${rootmnt}"/tmp
308         fi
309
310         # Adding custom persistence
311         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
312         then
313                 local custom_mounts
314                 custom_mounts="/tmp/custom_mounts.list"
315                 rm -f ${custom_mounts}
316
317                 # Gather information about custom mounts from devies detected as overlays
318                 get_custom_mounts ${custom_mounts} ${overlay_devices}
319
320                 [ -n "${LIVE_BOOT_DEBUG}" ] && cp ${custom_mounts} "/run/live/persistence"
321
322                 # Now we do the actual mounting (and symlinking)
323                 local used_overlays
324                 used_overlays=""
325                 used_overlays=$(activate_custom_mounts ${custom_mounts})
326                 rm -f ${custom_mounts}
327
328                 # Close unused overlays (e.g. due to missing $persistence_list)
329                 for overlay in ${overlay_devices}
330                 do
331                         if echo ${used_overlays} | grep -qve "^\(.* \)\?${overlay}\( .*\)\?$"
332                         then
333                                 close_persistence_media ${overlay}
334                         fi
335                 done
336         fi
337 }