Merge lp:~smoser/cloud-initramfs-tools/recursive into lp:cloud-initramfs-tools

Proposed by Scott Moser
Status: Merged
Merged at revision: 30
Proposed branch: lp:~smoser/cloud-initramfs-tools/recursive
Merge into: lp:cloud-initramfs-tools
Diff against target: 276 lines (+122/-35)
3 files modified
overlayroot/etc/overlayroot.conf (+29/-3)
overlayroot/scripts/init-bottom/overlayroot (+76/-23)
overlayroot/usr/sbin/overlayroot-chroot (+17/-9)
To merge this branch: bzr merge lp:~smoser/cloud-initramfs-tools/recursive
Reviewer Review Type Date Requested Status
cloud-initramfs-tools Pending
Review via email: mp+116351@code.launchpad.net

Description of the change

support for overlaying all mounts, rather than just '/'

This adds the ability to do "recursive" overlay, and enables that
by default.

It also adds 'swap=' parameter, which allows you to enable swap
partitions. The behavior previously was to disable all swap.

The overlayroot-chroot program is updated to incorporate the recurse
setting.

To post a comment you must log in.
30. By Scott Moser

overlay-chroot: make sure you exit with number, not ''

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'overlayroot/etc/overlayroot.conf'
2--- overlayroot/etc/overlayroot.conf 2012-07-20 22:53:28 +0000
3+++ overlayroot/etc/overlayroot.conf 2012-07-23 19:58:19 +0000
4@@ -4,14 +4,18 @@
5 # 1) edit the 'overlayroot' definition below
6 # 2) reboot
7 #
8-# Supported parameters:
9-# * overlayroot="tmpfs"
10+# Supported values:
11+# * overlayroot=tmpfs or overlayroot=tmpfs:PARAMETERS
12 # write all changes to a temporary (ram only) backing device
13 # A tmpfs mount will be created, and usable filesystem can
14 # grow to 1/2 available memory.
15 #
16-# example:
17+# available parameters:
18+# * see COMMON PARAMETERS
19+#
20+# examples:
21 # overlayroot=tmpfs
22+# overlayroot=tmpfs:swap=1
23 #
24 # * overlayroot=DEVICE or overlayroot=device:PARAMETERS
25 # mount DEVICE as overlayfs and write changes there
26@@ -29,6 +33,7 @@
27 # the directory under the filesystem to use for writes
28 # default is to use top level directory. For example, use
29 # 'dir=my-tests/run1' and later 'dir=my-tests/run2'
30+# * see COMMON PARAMETERS
31 #
32 # examples:
33 # overlayroot=/dev/xvdb
34@@ -60,6 +65,7 @@
35 # * dir: default: "/"
36 # the directory under the filesystem to use for writes.
37 # only really useful if you will re-use the mount later.
38+# * see COMMON PARAMETERS
39 #
40 # examples:
41 # crypt:mapname=mapper,pass=foo,fstype=ext4,mkfs=1,dev=vdb
42@@ -70,6 +76,26 @@
43 # if set explicitly to 'disabled', or an empty string, then
44 # overlayroot will do nothing.
45 #
46+#
47+# COMMON PARAMETERS:
48+# The following parameters are supported for each of overlayroot=
49+# values above.
50+# * swap: default: 0
51+# allowed values: 0, 1
52+# indicate if swap partitions should be allowed. By default swap entries
53+# are removed from /etc/fstab to disable swap.
54+# Swap *files* are always disabled, independent of this setting.
55+#
56+# * recurse: default: 1
57+# allowed values: 0, 1
58+# indicate if all mounts should be made read-only, or just /.
59+# if set to 1, then all filesystems will be mounted read-only.
60+# if set to 0, only root will be set to read-only, and changes
61+# to other filesystems will be permenant. For example, if
62+# /home is on a separate partition from / and recurse set to 0
63+# then changes to /home will go through to the original device.
64+#
65+#
66 # Notes:
67 # * you can pass the same 'overlayroot=' parameters on the kernel
68 # command line, and they will override any values set here.
69
70=== modified file 'overlayroot/scripts/init-bottom/overlayroot'
71--- overlayroot/scripts/init-bottom/overlayroot 2012-07-20 22:53:28 +0000
72+++ overlayroot/scripts/init-bottom/overlayroot 2012-07-23 19:58:19 +0000
73@@ -229,6 +229,56 @@
74 _RET_DIR="$dir"
75 }
76
77+overlayrootify_fstab() {
78+ local input="$1" root_ro="${2:-/media/root-ro}"
79+ local root_rw="${3:-/media/root-rw}" dir_prefix="${4:-/}"
80+ local recurse=${5:-1} swap=${6:-0}
81+ local hash="#"
82+ local vfstypes=" proc sys tmpfs dev udev "
83+ local spec file vfstype opts pass freq line ro_line
84+ [ -f "$input" ] || return 1
85+ while read spec file vfstype opts pass freq; do
86+ line="$spec $file $vfstype $opts $pass $freq"
87+ case ",$opts," in
88+ *,ro,*) ro_opts="$opts";;
89+ *) ro_opts="ro,${opts}";;
90+ esac
91+ ro_line="$spec ${root_ro}$file $vfstype ${ro_opts} $pass $freq"
92+ if [ "${spec#${hash}}" != "$spec" ] ||
93+ [ -z "$freq" ]; then
94+ # line has a comment in first field, or not 6 fields
95+ echo "$line"
96+ elif [ "${vfstypes# *${vfstype} }" != "${vfstypes}" ]; then
97+ # this is a virtual filesystem, just let it through
98+ echo "${line}"
99+ elif [ "$vfstype" = "swap" ]; then
100+ if [ "$swap" = "0" ]; then
101+ # comment out swap lines
102+ echo "#overlayroot:swap=${swap}#${line}"
103+ elif [ "${spec#/}" != "${spec}" ] &&
104+ [ "${spec#/dev/}" = "${spec}" ]; then
105+ # comment out swap files (spec starts with / and not in /dev)
106+ echo "#overlayroot:swapfile#${line}"
107+ else
108+ echo "${line}"
109+ fi
110+ else
111+ if [ "$recurse" != "0" ]; then
112+ echo "$ro_line"
113+ else
114+ echo "$line"
115+ fi
116+ if [ "$file" = "/" ]; then
117+ # write an entry for /
118+ oline="overlayroot ${file} overlayfs "
119+ oline="${oline}lowerdir=${root_ro}${file},"
120+ oline="${oline}upperdir=${root_ro}${file} $pass $freq"
121+ echo "$oline"
122+ fi
123+ fi
124+ done < "$input"
125+}
126+
127
128 # collect kernel parameter value into a config file
129 kern_cfg="/tmp/${0##*/}.kernel-cmdline.cfg"
130@@ -261,8 +311,13 @@
131 done
132 rm -f "$kern_cfg"
133
134+opts=""
135 case "${overlayroot:-disabled}" in
136- tmpfs) mode="tmpfs";;
137+ tmpfs|tmpfs:*)
138+ mode="tmpfs"
139+ opts="${overlayroot#tmpfs}";
140+ opts=${opts#:}
141+ ;;
142 /dev/*|device:*)
143 case "$overlayroot" in
144 /dev/*) opts="dev=${overlayroot}";;
145@@ -274,9 +329,9 @@
146 device="$_RET_DEVICE"
147 dir_prefix="$_RET_DIR"
148 ;;
149- crypt|crypt:*)
150+ crypt:*)
151 mode="crypt"
152- t=${overlayroot#crypt}
153+ opts=${overlayroot#crypt:}
154 crypto_setup "${t#:}" ||
155 fail "failed setup crypt for ${overlayroot}"
156 device="$_RET_DEVICE"
157@@ -290,7 +345,16 @@
158 exit 0;;
159 esac
160
161-log_warn "configuring 'overlayroot=$overlayroot' per $used_desc"
162+parse_string "$opts" "," _RET_common_
163+swap=${_RET_common_swap:-0}
164+recurse=${_RET_common_recurse:-1}
165+
166+[ "$swap" = "0" -o "$swap" = "1" ] ||
167+ fail "invalid setting for swap: $swap. must be '0' or '1'"
168+[ "$recurse" = "0" -o "$recurse" = "1" ] ||
169+ fail "invalid setting for recurse: $recurse. must be '0' or '1'"
170+
171+log_warn "configuring overlayroot with mode=$mode opts='$opts' per $used_desc"
172
173 # overlayroot_driver *could* be defined in one of the configs above
174 # but we're not documenting that.
175@@ -398,33 +462,22 @@
176 # Remember we are still on the initramfs root fs here, so we have to work
177 # on ${ROOTMNT}/etc/fstab. The original fstab is
178 # ${ROOTMNT}${root_ro}/etc/fstab.
179-root_type=$(awk '$1 == root { print $3 }' "root=$ROOT" /proc/mounts)
180-root_options=$(awk '$1 == root { print $4 }' "root=$ROOT" /proc/mounts)
181 cat <<EOF >${ROOTMNT}/etc/fstab
182 #
183 # This fstab is in an overlayfs. The real one can be found at
184 # ${root_ro}/etc/fstab
185-# The original entry for '/' and all swap files have been removed. The new
186-# entry for the read-only the real root fs follows. To permanently
187-# modify this (or any other file), you should change-root into a writable
188-# view of the underlying filesystem using:
189+# The original entry for '/' and other mounts have been updated to be placed
190+# under $root_ro.
191+# To permanently modify this (or any other file), you should change-root into
192+# a writable view of the underlying filesystem using:
193 # sudo overlayroot-chroot
194 #
195-
196-${ROOT} ${root_ro} ${root_type} ${root_options} 0 0
197-
198-#
199-# remaining entries from the original ${root_ro}/etc/fstab follow.
200-#
201 EOF
202+
203 [ $? -eq 0 ] || log_fail "failed to modify /etc/fstab (step 1)"
204-
205-#comment out root and swap entries passing others through unmodified
206-awk '{
207- if ($0 ~ /^#/ || ($2 != "/" && $3 != "swap") ) { print $0 }
208- else { printf("%s%s\n","#overlayroot#",$0); }
209- }' "${ROOTMNT}${root_ro}/etc/fstab" >> "${ROOTMNT}/etc/fstab" ||
210- log_fail "failed to modify etc/fstab (step 2)"
211+overlayrootify_fstab ""${ROOTMNT}${root_ro}/etc/fstab"" "$root_ro" \
212+ "$root_rw" "$dir_prefix" "$recurse" "$swap" >> "${ROOTMNT}/etc/fstab" ||
213+ log_fail "failed to modify /etc/fstab (step 2)"
214
215 msg="configured root with '$overlayroot' using ${overlayroot_driver} per"
216 msg="$msg ${used_desc}"
217
218=== modified file 'overlayroot/usr/sbin/overlayroot-chroot'
219--- overlayroot/usr/sbin/overlayroot-chroot 2012-07-20 19:53:11 +0000
220+++ overlayroot/usr/sbin/overlayroot-chroot 2012-07-23 19:58:19 +0000
221@@ -18,6 +18,8 @@
222 # along with this program. If not, see <http://www.gnu.org/licenses/>.
223
224 set -e
225+set -f # disable path expansion
226+REMOUNTS=""
227
228 error() {
229 printf "ERROR: $@\n" 1>&2
230@@ -44,23 +46,26 @@
231 }
232
233 clean_exit() {
234- local mounts="$1" rc="" d="" lowerdir=""
235+ local mounts="$1" rc=0 d="" lowerdir="" mp=""
236 for d in ${mounts}; do
237 if mountpoint ${d} >/dev/null; then
238 umount ${d} || rc=1
239 fi
240 done
241- get_lowerdir
242- lowerdir=${_RET}
243- if ! mount -o remount,ro "${lowerdir}"; then
244- error "Note that [${lowerdir}] is still mounted read/write"
245- fi
246- [ "$2" = "return" ] && return ${rc} || exit "${rc}"
247+ for mp in $REMOUNTS; do
248+ mount -o remount,ro "${mp}" ||
249+ error "Note that [${mp}] is still mounted read/write"
250+ done
251+ [ "$2" = "return" ] && return ${rc} || exit ${rc}
252 }
253
254 # Try to find the overlayroot filesystem
255 get_lowerdir
256 lowerdir=${_RET}
257+
258+recurse_mps=$(awk '$1 ~ /^\/dev\// && $2 ~ starts { print $2 }' \
259+ starts="^$lowerdir/" /proc/mounts)
260+
261 mounts=
262 for d in proc run sys; do
263 if ! mountpoint "${lowerdir}/${d}" >/dev/null; then
264@@ -71,8 +76,11 @@
265 done
266
267 # Remount with read/write
268-mount -o remount,rw "${lowerdir}" ||
269- fail "Unable to remount [${lowerdir}] writable"
270+for mp in "$lowerdir" $recurse_mps; do
271+ mount -o remount,rw "${mp}" &&
272+ REMOUNTS="$mp $REMOUNTS" ||
273+ fail "Unable to remount [$mp] writable"
274+done
275 info "Chrooting into [${lowerdir}]"
276 chroot ${lowerdir} "$@"
277

Subscribers

People subscribed via source and target branches