Adding debian version 4.0~a1-1.
[live-boot-grml.git] / scripts / boot / 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         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         rootfslist=""
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                                 rootfslist="${image} ${rootfslist}"
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                                                 rootfslist="${rootmnt} ${rootfslist}"
130                                                 ;;
131
132                                         *)
133                                                 mpoint="${croot}/${imagename}"
134                                                 rootfslist="${mpoint} ${rootfslist}"
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                         rootfslist="${croot}/filesystem ${rootfslist}"
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/overlay
159         mount -t tmpfs tmpfs /live/overlay
160
161         # Looking for persistence devices or files
162         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
163         then
164
165                 if [ -z "${QUICKUSBMODULES}" ]
166                 then
167                         # Load USB modules
168                         num_block=$(ls -l /sys/block | wc -l)
169                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
170                         do
171                                 modprobe -q -b ${module}
172                         done
173
174                         udevadm trigger
175                         udevadm settle
176
177                         # For some reason, udevsettle does not block in this scenario,
178                         # so we sleep for a little while.
179                         #
180                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
181                         for timeout in 5 4 3 2 1
182                         do
183                                 sleep 1
184
185                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
186                                 then
187                                         break
188                                 fi
189                         done
190                 fi
191
192                 case "${PERSISTENCE_MEDIA}" in
193                         removable)
194                                 whitelistdev="$(removable_dev)"
195                                 ;;
196
197                         removable-usb)
198                                 whitelistdev="$(removable_usb_dev)"
199                                 ;;
200
201                         *)
202                                 whitelistdev=""
203                                 ;;
204                 esac
205
206                 if is_in_comma_sep_list overlay ${PERSISTENCE_METHOD}
207                 then
208                         overlays="${custom_overlay_label}"
209                 fi
210
211                 local overlay_devices=""
212                 for media in $(find_persistence_media "${overlays}" "${whitelistdev}")
213                 do
214                         media="$(echo ${media} | tr ":" " ")"
215
216                         case ${media} in
217                                 ${custom_overlay_label}=*)
218                                         device="${media#*=}"
219                                         overlay_devices="${overlay_devices} ${device}"
220                                         ;;
221                          esac
222                 done
223         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENCE}" ]
224         then
225                 # check if there are any nfs options
226                 if echo ${NFS_COW} | grep -q ','
227                 then
228                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
229                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
230                 else
231                         nfs_cow_opts="-o nolock"
232                         nfs_cow=${NFS_COW}
233                 fi
234
235                 if [ -n "${PERSISTENCE_READONLY}" ]
236                 then
237                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
238                 fi
239
240                 mac="$(get_mac)"
241                 if [ -n "${mac}" ]
242                 then
243                         cowdevice=$(echo ${nfs_cow} | sed "s/client_mac_address/${mac}/")
244                         cow_fstype="nfs"
245                 else
246                         panic "unable to determine mac address"
247                 fi
248         fi
249
250         if [ -z "${cowdevice}" ]
251         then
252                 cowdevice="tmpfs"
253                 cow_fstype="tmpfs"
254                 cow_mountopt="rw,noatime,mode=755"
255         fi
256
257         if [ "${UNIONTYPE}" != "unionmount" ]
258         then
259                 if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
260                 then
261                         mount -t tmpfs -o rw,noatime,mode=755 tmpfs "/live/overlay"
262                         root_backing="/live/persistence/$(basename ${cowdevice})-root"
263                         mkdir -p ${root_backing}
264                 else
265                         root_backing="/live/overlay"
266                 fi
267
268                 if [ "${cow_fstype}" = "nfs" ]
269                 then
270                         log_begin_msg \
271                                 "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing}"
272                         nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing} || \
273                                 panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${root_backing}"
274                 else
275                         mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${root_backing} || \
276                                 panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${root_backing}"
277                 fi
278         fi
279
280         rootfscount=$(echo ${rootfslist} |wc -w)
281
282         rootfs=${rootfslist%% }
283
284         if [ -n "${EXPOSED_ROOT}" ]
285         then
286                 if [ ${rootfscount} -ne 1 ]
287                 then
288                         panic "only one RO file system supported with exposedroot: ${rootfslist}"
289                 fi
290
291                 mount --bind ${rootfs} ${rootmnt} || \
292                         panic "bind mount of ${rootfs} failed"
293
294                 if [ -z "${SKIP_UNION_MOUNTS}" ]
295                 then
296                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
297                 else
298                         cow_dirs=''
299                 fi
300         else
301                 cow_dirs="/"
302         fi
303
304         if [ "${cow_fstype}" != "tmpfs" ] && [ "${cow_dirs}" != "/" ] && [ "${UNIONTYPE}" = "unionmount" ]
305         then
306                 true # FIXME: Maybe it does, I don't really know.
307                 #panic "unionmount does not support subunions (${cow_dirs})."
308         fi
309
310         for dir in ${cow_dirs}; do
311                 unionmountpoint="${rootmnt}${dir}"
312                 mkdir -p ${unionmountpoint}
313                 if [ "${UNIONTYPE}" = "unionmount" ]
314                 then
315                         # FIXME: handle PERSISTENCE_READONLY
316                         unionmountopts="-t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice}"
317                         mount_full $unionmountopts "${unionmountpoint}"
318                 else
319                         cow_dir="/live/overlay${dir}"
320                         rootfs_dir="${rootfs}${dir}"
321                         mkdir -p ${cow_dir}
322                         if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
323                         then
324                                 do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rootfs_dir}
325                         else
326                                 do_union ${unionmountpoint} ${cow_dir} ${rootfs_dir}
327                         fi
328                 fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
329         done
330
331         # Correct the permissions of /:
332         chmod 0755 "${rootmnt}"
333
334         # Correct the permission of /tmp:
335         if [ -d "${rootmnt}/tmp" ]
336         then
337                 chmod 1777 "${rootmnt}"/tmp
338         fi
339
340         live_rootfs_list=""
341         for d in ${rootfslist}
342         do
343                 live_rootfs="/live/rootfs/${d##*/}"
344                 live_rootfs_list="${live_rootfs_list} ${live_rootfs}"
345                 mkdir -p "${live_rootfs}"
346                 case d in
347                         *.dir)
348                                 # do nothing # mount -o bind "${d}" "${live_rootfs}"
349                                 ;;
350                         *)
351                                 case "${UNIONTYPE}" in
352                                         unionfs-fuse)
353                                                 mount -o bind "${d}" "${live_rootfs}"
354                                                 ;;
355
356                                         *)
357                                                 mount -o move "${d}" "${live_rootfs}"
358                                                 ;;
359                                 esac
360                                 ;;
361                 esac
362         done
363
364         # Adding custom persistence
365         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
366         then
367                 local custom_mounts="/tmp/custom_mounts.list"
368                 rm -rf ${custom_mounts} 2> /dev/null
369
370                 # Gather information about custom mounts from devies detected as overlays
371                 get_custom_mounts ${custom_mounts} ${overlay_devices}
372
373                 [ -n "${DEBUG}" ] && cp ${custom_mounts} "/live/persistence"
374
375                 # Now we do the actual mounting (and symlinking)
376                 local used_overlays=""
377                 used_overlays=$(activate_custom_mounts ${custom_mounts})
378                 rm ${custom_mounts}
379
380                 # Close unused overlays (e.g. due to missing $persistence_list)
381                 for overlay in ${overlay_devices}
382                 do
383                         if echo ${used_overlays} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
384                         then
385                                 close_persistence_media ${overlay}
386                         fi
387                 done
388         fi
389
390         # move all mountpoints to root filesystem
391         for _DIRECTORY in rootfs persistence
392         do
393                 if [ -d "/live/${_DIRECTORY}" ]
394                 then
395                         mkdir -p "${rootmnt}/lib/live/mount/${_DIRECTORY}"
396
397                         for _MOUNT in $(ls /live/${_DIRECTORY})
398                         do
399                                 mkdir -p "${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}"
400                                 mount -o move "/live/${_DIRECTORY}/${_MOUNT}" "${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}" > /dev/null 2>&1 || \
401                                         mount -o bind "/live/${_DIRECTORY}/${_MOUNT}" "${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}" || \
402                                         log_warning_msg "W: failed to mount /live/${_DIRECTORY}/${_MOUNT} to ${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}"
403                         done
404                 fi
405         done
406
407         mkdir -p "${rootmnt}/lib/live/mount/overlay"
408         mount -o move /live/overlay "${rootmnt}/lib/live/mount/overlay" > /dev/null 2>&1 || \
409                 mount -o bind /live/overlay "${rootmnt}/lib/live/mount/overlay" || \
410                 log_warning_msg "W: failed to mount /live/overlay to ${rootmnt}/lib/live/mount/overlay"
411
412         # ensure that a potentially stray tmpfs gets removed
413         # otherways, initramfs-tools is unable to remove /live
414         # and fails to boot
415         umount /live/overlay || true
416 }