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