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