Release new version 1:20210208+grml.5
[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                                 for overlay_label in ${custom_overlay_label}
199                                 do
200                                         case ${media} in
201                                                 ${overlay_label}=*)
202                                                         device="${media#*=}"
203                                                         overlay_devices="${overlay_devices} ${device}"
204                                                         ;;
205                                         esac
206                                 done
207                         done
208                 fi
209         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENCE}" ]
210         then
211                 # check if there are any nfs options
212                 if echo ${NFS_COW} | grep -q ','
213                 then
214                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
215                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
216                 else
217                         nfs_cow_opts="-o nolock"
218                         nfs_cow=${NFS_COW}
219                 fi
220
221                 if [ -n "${PERSISTENCE_READONLY}" ]
222                 then
223                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
224                 fi
225
226                 mac="$(get_mac)"
227                 if [ -n "${mac}" ]
228                 then
229                         cowdevice=$(echo ${nfs_cow} | sed "s/client_mac_address/${mac}/")
230                         cow_fstype="nfs"
231                 else
232                         panic "unable to determine mac address"
233                 fi
234         fi
235
236         if [ -z "${cowdevice}" ]
237         then
238                 cowdevice="tmpfs"
239                 cow_fstype="tmpfs"
240                 cow_mountopt="rw,noatime,mode=755,size=${OVERLAY_SIZE:-50%}"
241         fi
242
243         if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
244         then
245                 mount -t tmpfs -o rw,noatime,mode=755,size=${OVERLAY_SIZE:-50%} tmpfs "/run/live/overlay"
246                 root_backing="/run/live/persistence/$(basename ${cowdevice})-root"
247                 mkdir -p ${root_backing}
248         else
249                 root_backing="/run/live/overlay"
250         fi
251
252         if [ "${cow_fstype}" = "nfs" ]
253         then
254                 log_begin_msg \
255                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing}"
256                 nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing} || \
257                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${root_backing}"
258         else
259                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${root_backing} || \
260                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${root_backing}"
261         fi
262
263         rootfscount=$(echo ${rootfslist} |wc -w)
264
265         rootfs=${rootfslist%% }
266
267         if [ -n "${EXPOSED_ROOT}" ]
268         then
269                 if [ ${rootfscount} -ne 1 ]
270                 then
271                         panic "only one RO file system supported with exposedroot: ${rootfslist}"
272                 fi
273
274                 mount -o bind ${rootfs} ${rootmnt} || \
275                         panic "bind mount of ${rootfs} failed"
276
277                 if [ -z "${SKIP_UNION_MOUNTS}" ]
278                 then
279                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
280                 else
281                         cow_dirs=''
282                 fi
283         else
284                 cow_dirs="/"
285         fi
286
287         for dir in ${cow_dirs}; do
288                 unionmountpoint=$(trim_path "${rootmnt}${dir}")
289                 mkdir -p ${unionmountpoint}
290                 cow_dir=$(trim_path "/run/live/overlay${dir}")
291                 rootfs_dir="${rootfs}${dir}"
292                 mkdir -p ${cow_dir}
293                 if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
294                 then
295                         do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rootfs_dir}
296                 else
297                         do_union ${unionmountpoint} ${cow_dir} ${rootfs_dir}
298                 fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
299         done
300
301         # Remove persistence depending on boot parameter
302         Remove_persistence
303
304         # Correct the permissions of /:
305         chmod 0755 "${rootmnt}"
306
307         # Correct the permission of /tmp:
308         if [ -d "${rootmnt}/tmp" ]
309         then
310                 chmod 1777 "${rootmnt}"/tmp
311         fi
312
313         # Correct the permission of /var/tmp:
314         if [ -d "${rootmnt}/var/tmp" ]
315         then
316                 chmod 1777 "${rootmnt}"/var/tmp
317         fi
318
319         # Adding custom persistence
320         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
321         then
322                 local custom_mounts
323                 custom_mounts="/tmp/custom_mounts.list"
324                 rm -f ${custom_mounts}
325
326                 # Gather information about custom mounts from devies detected as overlays
327                 get_custom_mounts ${custom_mounts} ${overlay_devices}
328
329                 [ -n "${LIVE_BOOT_DEBUG}" ] && cp ${custom_mounts} "/run/live/persistence"
330
331                 # Now we do the actual mounting (and symlinking)
332                 local used_overlays
333                 used_overlays=""
334                 used_overlays=$(activate_custom_mounts ${custom_mounts})
335                 rm -f ${custom_mounts}
336
337                 # Close unused overlays (e.g. due to missing $persistence_list)
338                 for overlay in ${overlay_devices}
339                 do
340                         if echo ${used_overlays} | grep -qve "^\(.* \)\?${overlay}\( .*\)\?$"
341                         then
342                                 close_persistence_media ${overlay}
343                         fi
344                 done
345         fi
346 }