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