cb12c4b4535eb608a6b2d746fe0063006242cba1
[live-boot-grml.git] / scripts / boot / 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         case ${UNIONTYPE} in
12                 aufs|unionfs|overlayfs)
13                         modprobe -q -b ${UNIONTYPE}
14
15                         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
16                         then
17                                 echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
18                                 echo "This might be really slow."
19
20                                 UNIONTYPE="unionfs-fuse"
21                         fi
22                         ;;
23         esac
24
25         case "${UNIONTYPE}" in
26                 unionfs-fuse)
27                         modprobe fuse
28                         ;;
29         esac
30
31         # run-init can't deal with images in a subdir, but we're going to
32         # move all of these away before it runs anyway.  No, we're not,
33         # put them in / since move-mounting them into / breaks mono and
34         # some other apps.
35
36         croot="/"
37
38         # Let's just mount the read-only file systems first
39         rofslist=""
40
41         if [ -z "${PLAIN_ROOT}" ]
42         then
43                 # Read image names from ${MODULE}.module if it exists
44                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
45                 then
46                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
47                         do
48                                 image_string="${image_string} ${image_directory}/${IMAGE}"
49                         done
50                 elif [ -e "${image_directory}/${MODULE}.module" ]
51                 then
52                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
53                         do
54                                 image_string="${image_string} ${image_directory}/${IMAGE}"
55                         done
56                 else
57                         # ${MODULE}.module does not exist, create a list of images
58                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
59                         do
60                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
61                                 do
62                                         if [ -e "${IMAGE}" ]
63                                         then
64                                                 image_string="${image_string} ${IMAGE}"
65                                         fi
66                                 done
67                         done
68
69                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
70                         then
71                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
72                                 do
73                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
74                                         do
75                                                 if [ -e "${IMAGE}" ]
76                                                 then
77                                                         image_string="${image_string} ${IMAGE}"
78                                                 fi
79                                         done
80                                 done
81                         fi
82
83                         # Now sort the list
84                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
85                 fi
86
87                 [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
88
89                 mkdir -p "${croot}"
90
91                 for image in ${image_string}
92                 do
93                         imagename=$(basename "${image}")
94
95                         export image devname
96                         maybe_break live-realpremount
97                         log_begin_msg "Running /scripts/live-realpremount"
98                         run_scripts /scripts/live-realpremount
99                         log_end_msg
100
101                         if [ -d "${image}" ]
102                         then
103                                 # it is a plain directory: do nothing
104                                 rofslist="${image} ${rofslist}"
105                         elif [ -f "${image}" ]
106                         then
107                                 if losetup --help 2>&1 | grep -q -- "-r\b"
108                                 then
109                                         backdev=$(get_backing_device "${image}" "-r")
110                                 else
111                                         backdev=$(get_backing_device "${image}")
112                                 fi
113                                 fstype=$(get_fstype "${backdev}")
114
115                                 case "${fstype}" in
116                                         unknown)
117                                                 panic "Unknown file system type on ${backdev} (${image})"
118                                                 ;;
119
120                                         "")
121                                                 fstype="${imagename##*.}"
122                                                 log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
123                                                 ;;
124                                 esac
125
126                                 case "${UNIONTYPE}" in
127                                         unionmount)
128                                                 mpoint="${rootmnt}"
129                                                 rofslist="${rootmnt} ${rofslist}"
130                                                 ;;
131
132                                         *)
133                                                 mpoint="${croot}/${imagename}"
134                                                 rofslist="${mpoint} ${rofslist}"
135                                                 ;;
136                                 esac
137
138                                 mkdir -p "${mpoint}"
139                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
140                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
141                                 log_end_msg
142                         fi
143                 done
144         else
145                 # we have a plain root system
146                 mkdir -p "${croot}/filesystem"
147                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
148                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
149                         panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
150                         rofslist="${croot}/filesystem ${rofslist}"
151                 # probably broken:
152                 mount -o bind ${croot}/filesystem $mountpoint
153                 log_end_msg
154         fi
155
156         # tmpfs file systems
157         touch /etc/fstab
158         mkdir -p /live
159         mount -t tmpfs tmpfs /live
160         mkdir -p /live/overlay
161
162         # Looking for persistence devices or files
163         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
164         then
165
166                 if [ -z "${QUICKUSBMODULES}" ]
167                 then
168                         # Load USB modules
169                         num_block=$(ls -l /sys/block | wc -l)
170                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
171                         do
172                                 modprobe -q -b ${module}
173                         done
174
175                         udevadm trigger
176                         udevadm settle
177
178                         # For some reason, udevsettle does not block in this scenario,
179                         # so we sleep for a little while.
180                         #
181                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
182                         for timeout in 5 4 3 2 1
183                         do
184                                 sleep 1
185
186                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
187                                 then
188                                         break
189                                 fi
190                         done
191                 fi
192
193                 case "${PERSISTENCE_MEDIA}" in
194                         removable)
195                                 whitelistdev="$(removable_dev)"
196                                 ;;
197
198                         removable-usb)
199                                 whitelistdev="$(removable_usb_dev)"
200                                 ;;
201
202                         *)
203                                 whitelistdev=""
204                                 ;;
205                 esac
206
207                 if is_in_comma_sep_list overlay ${PERSISTENCE_METHOD}
208                 then
209                         overlays="${old_root_overlay_label} ${old_home_overlay_label} ${custom_overlay_label}"
210                 fi
211
212                 local overlay_devices=""
213                 for media in $(find_persistence_media "${overlays}" "${whitelistdev}")
214                 do
215                         media="$(echo ${media} | tr ":" " ")"
216
217                         case ${media} in
218                                 ${old_root_overlay_label}=*)
219                                         device="${media#*=}"
220                                         fix_backwards_compatibility ${device} / union
221                                         overlay_devices="${overlay_devices} ${device}"
222                                         ;;
223
224                                 ${old_home_overlay_label}=*)
225                                         device="${media#*=}"
226                                         fix_backwards_compatibility ${device} /home bind
227                                         overlay_devices="${overlay_devices} ${device}"
228                                         ;;
229
230                                 ${custom_overlay_label}=*)
231                                         device="${media#*=}"
232                                         overlay_devices="${overlay_devices} ${device}"
233                                         ;;
234                          esac
235                 done
236         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENCE}" ]
237         then
238                 # check if there are any nfs options
239                 if echo ${NFS_COW} | grep -q ','
240                 then
241                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
242                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
243                 else
244                         nfs_cow_opts="-o nolock"
245                         nfs_cow=${NFS_COW}
246                 fi
247
248                 if [ -n "${PERSISTENCE_READONLY}" ]
249                 then
250                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
251                 fi
252
253                 mac="$(get_mac)"
254                 if [ -n "${mac}" ]
255                 then
256                         cowdevice=$(echo ${nfs_cow} | sed "s/client_mac_address/${mac}/")
257                         cow_fstype="nfs"
258                 else
259                         panic "unable to determine mac address"
260                 fi
261         fi
262
263         if [ -z "${cowdevice}" ]
264         then
265                 cowdevice="tmpfs"
266                 cow_fstype="tmpfs"
267                 cow_mountopt="rw,noatime,mode=755"
268         fi
269
270         if [ "${UNIONTYPE}" != "unionmount" ]
271         then
272                 if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
273                 then
274                         mount -t tmpfs -o rw,noatime,mode=755 tmpfs "/live/overlay"
275                         root_backing="/live/persistence/$(basename ${cowdevice})-root"
276                         mkdir -p ${root_backing}
277                 else
278                         root_backing="/live/overlay"
279                 fi
280
281                 if [ "${cow_fstype}" = "nfs" ]
282                 then
283                         log_begin_msg \
284                                 "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing}"
285                         nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing} || \
286                                 panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${root_backing}"
287                 else
288                         mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${root_backing} || \
289                                 panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${root_backing}"
290                 fi
291         fi
292
293         rofscount=$(echo ${rofslist} |wc -w)
294
295         rofs=${rofslist%% }
296
297         if [ -n "${EXPOSED_ROOT}" ]
298         then
299                 if [ ${rofscount} -ne 1 ]
300                 then
301                         panic "only one RO file system supported with exposedroot: ${rofslist}"
302                 fi
303
304                 mount --bind ${rofs} ${rootmnt} || \
305                         panic "bind mount of ${rofs} failed"
306
307                 if [ -z "${SKIP_UNION_MOUNTS}" ]
308                 then
309                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
310                 else
311                         cow_dirs=''
312                 fi
313         else
314                 cow_dirs="/"
315         fi
316
317         if [ "${cow_fstype}" != "tmpfs" ] && [ "${cow_dirs}" != "/" ] && [ "${UNIONTYPE}" = "unionmount" ]
318         then
319                 true # FIXME: Maybe it does, I don't really know.
320                 #panic "unionmount does not support subunions (${cow_dirs})."
321         fi
322
323         for dir in ${cow_dirs}; do
324                 unionmountpoint="${rootmnt}${dir}"
325                 mkdir -p ${unionmountpoint}
326                 if [ "${UNIONTYPE}" = "unionmount" ]
327                 then
328                         # FIXME: handle PERSISTENCE_READONLY
329                         unionmountopts="-t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice}"
330                         mount_full $unionmountopts "${unionmountpoint}"
331                 else
332                         cow_dir="/live/overlay${dir}"
333                         rofs_dir="${rofs}${dir}"
334                         mkdir -p ${cow_dir}
335                         if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
336                         then
337                                 do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rofs_dir}
338                         else
339                                 do_union ${unionmountpoint} ${cow_dir} ${rofs_dir}
340                         fi
341                 fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
342         done
343
344         # Correct the permissions of /:
345         chmod 0755 "${rootmnt}"
346
347         live_rofs_list=""
348         # SHOWMOUNTS is necessary for custom mounts with the union option
349         # Since we may want to do custom mounts in user-space it's best to always enable SHOWMOUNTS
350         if true #[ -n "${SHOWMOUNTS}" ] || ( [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ] 1)
351         then
352                 # XXX: is the for loop really necessary? rofslist can only contain one item (see above XXX about EXPOSEDROOT) and this is also assumed elsewhere above (see use of $rofs above).
353                 for d in ${rofslist}
354                 do
355                         live_rofs="/live/rofs/${d##*/}"
356                         live_rofs_list="${live_rofs_list} ${live_rofs}"
357                         mkdir -p "${live_rofs}"
358                         case d in
359                                 *.dir)
360                                         # do nothing # mount -o bind "${d}" "${live_rofs}"
361                                         ;;
362                                 *)
363                                         case "${UNIONTYPE}" in
364                                                 unionfs-fuse)
365                                                         mount -o bind "${d}" "${live_rofs}"
366                                                         ;;
367
368                                                 *)
369                                                         mount -o move "${d}" "${live_rofs}"
370                                                         ;;
371                                         esac
372                                         ;;
373                         esac
374                 done
375         fi
376
377         # Adding custom persistence
378         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
379         then
380                 local custom_mounts="/tmp/custom_mounts.list"
381                 rm -rf ${custom_mounts} 2> /dev/null
382
383                 # Gather information about custom mounts from devies detected as overlays
384                 get_custom_mounts ${custom_mounts} ${overlay_devices}
385
386                 [ -n "${DEBUG}" ] && cp ${custom_mounts} "/live/persistence"
387
388                 # Now we do the actual mounting (and symlinking)
389                 local used_overlays=""
390                 used_overlays=$(activate_custom_mounts ${custom_mounts})
391                 rm ${custom_mounts}
392
393                 # Close unused overlays (e.g. due to missing $persistence_list)
394                 for overlay in ${overlay_devices}
395                 do
396                         if echo ${used_overlays} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
397                         then
398                                 close_persistence_media ${overlay}
399                         fi
400                 done
401         fi
402
403         mkdir -p "${rootmnt}/live"
404         mount -o move /live "${rootmnt}/live" >/dev/null 2>&1 || mount -o bind /live "${rootmnt}/live" || log_warning_msg "Unable to move or bind /live to ${rootmnt}/live"
405
406         # shows cow fs on /overlay (FIXME: do we still need/want this? probably yes)
407         mkdir -p "${rootmnt}/live/overlay"
408         mount -o move /live/overlay "${rootmnt}/live/overlay" >/dev/null 2>&1 || mount -o bind /overlay "${rootmnt}/live/overlay" || log_warning_msg "Unable to move or bind /overlay to ${rootmnt}/live/overlay"
409
410 }