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
=== modified file 'overlayroot/etc/overlayroot.conf'
--- overlayroot/etc/overlayroot.conf 2012-07-20 22:53:28 +0000
+++ overlayroot/etc/overlayroot.conf 2012-07-23 19:58:19 +0000
@@ -4,14 +4,18 @@
4# 1) edit the 'overlayroot' definition below4# 1) edit the 'overlayroot' definition below
5# 2) reboot5# 2) reboot
6#6#
7# Supported parameters:7# Supported values:
8# * overlayroot="tmpfs"8# * overlayroot=tmpfs or overlayroot=tmpfs:PARAMETERS
9# write all changes to a temporary (ram only) backing device9# write all changes to a temporary (ram only) backing device
10# A tmpfs mount will be created, and usable filesystem can10# A tmpfs mount will be created, and usable filesystem can
11# grow to 1/2 available memory.11# grow to 1/2 available memory.
12#12#
13# example:13# available parameters:
14# * see COMMON PARAMETERS
15#
16# examples:
14# overlayroot=tmpfs17# overlayroot=tmpfs
18# overlayroot=tmpfs:swap=1
15#19#
16# * overlayroot=DEVICE or overlayroot=device:PARAMETERS20# * overlayroot=DEVICE or overlayroot=device:PARAMETERS
17# mount DEVICE as overlayfs and write changes there21# mount DEVICE as overlayfs and write changes there
@@ -29,6 +33,7 @@
29# the directory under the filesystem to use for writes33# the directory under the filesystem to use for writes
30# default is to use top level directory. For example, use34# default is to use top level directory. For example, use
31# 'dir=my-tests/run1' and later 'dir=my-tests/run2'35# 'dir=my-tests/run1' and later 'dir=my-tests/run2'
36# * see COMMON PARAMETERS
32#37#
33# examples:38# examples:
34# overlayroot=/dev/xvdb39# overlayroot=/dev/xvdb
@@ -60,6 +65,7 @@
60# * dir: default: "/"65# * dir: default: "/"
61# the directory under the filesystem to use for writes.66# the directory under the filesystem to use for writes.
62# only really useful if you will re-use the mount later.67# only really useful if you will re-use the mount later.
68# * see COMMON PARAMETERS
63# 69#
64# examples:70# examples:
65# crypt:mapname=mapper,pass=foo,fstype=ext4,mkfs=1,dev=vdb71# crypt:mapname=mapper,pass=foo,fstype=ext4,mkfs=1,dev=vdb
@@ -70,6 +76,26 @@
70# if set explicitly to 'disabled', or an empty string, then76# if set explicitly to 'disabled', or an empty string, then
71# overlayroot will do nothing.77# overlayroot will do nothing.
72#78#
79#
80# COMMON PARAMETERS:
81# The following parameters are supported for each of overlayroot=
82# values above.
83# * swap: default: 0
84# allowed values: 0, 1
85# indicate if swap partitions should be allowed. By default swap entries
86# are removed from /etc/fstab to disable swap.
87# Swap *files* are always disabled, independent of this setting.
88#
89# * recurse: default: 1
90# allowed values: 0, 1
91# indicate if all mounts should be made read-only, or just /.
92# if set to 1, then all filesystems will be mounted read-only.
93# if set to 0, only root will be set to read-only, and changes
94# to other filesystems will be permenant. For example, if
95# /home is on a separate partition from / and recurse set to 0
96# then changes to /home will go through to the original device.
97#
98#
73# Notes:99# Notes:
74# * you can pass the same 'overlayroot=' parameters on the kernel100# * you can pass the same 'overlayroot=' parameters on the kernel
75# command line, and they will override any values set here.101# command line, and they will override any values set here.
76102
=== modified file 'overlayroot/scripts/init-bottom/overlayroot'
--- overlayroot/scripts/init-bottom/overlayroot 2012-07-20 22:53:28 +0000
+++ overlayroot/scripts/init-bottom/overlayroot 2012-07-23 19:58:19 +0000
@@ -229,6 +229,56 @@
229 _RET_DIR="$dir"229 _RET_DIR="$dir"
230}230}
231231
232overlayrootify_fstab() {
233 local input="$1" root_ro="${2:-/media/root-ro}"
234 local root_rw="${3:-/media/root-rw}" dir_prefix="${4:-/}"
235 local recurse=${5:-1} swap=${6:-0}
236 local hash="#"
237 local vfstypes=" proc sys tmpfs dev udev "
238 local spec file vfstype opts pass freq line ro_line
239 [ -f "$input" ] || return 1
240 while read spec file vfstype opts pass freq; do
241 line="$spec $file $vfstype $opts $pass $freq"
242 case ",$opts," in
243 *,ro,*) ro_opts="$opts";;
244 *) ro_opts="ro,${opts}";;
245 esac
246 ro_line="$spec ${root_ro}$file $vfstype ${ro_opts} $pass $freq"
247 if [ "${spec#${hash}}" != "$spec" ] ||
248 [ -z "$freq" ]; then
249 # line has a comment in first field, or not 6 fields
250 echo "$line"
251 elif [ "${vfstypes# *${vfstype} }" != "${vfstypes}" ]; then
252 # this is a virtual filesystem, just let it through
253 echo "${line}"
254 elif [ "$vfstype" = "swap" ]; then
255 if [ "$swap" = "0" ]; then
256 # comment out swap lines
257 echo "#overlayroot:swap=${swap}#${line}"
258 elif [ "${spec#/}" != "${spec}" ] &&
259 [ "${spec#/dev/}" = "${spec}" ]; then
260 # comment out swap files (spec starts with / and not in /dev)
261 echo "#overlayroot:swapfile#${line}"
262 else
263 echo "${line}"
264 fi
265 else
266 if [ "$recurse" != "0" ]; then
267 echo "$ro_line"
268 else
269 echo "$line"
270 fi
271 if [ "$file" = "/" ]; then
272 # write an entry for /
273 oline="overlayroot ${file} overlayfs "
274 oline="${oline}lowerdir=${root_ro}${file},"
275 oline="${oline}upperdir=${root_ro}${file} $pass $freq"
276 echo "$oline"
277 fi
278 fi
279 done < "$input"
280}
281
232282
233# collect kernel parameter value into a config file283# collect kernel parameter value into a config file
234kern_cfg="/tmp/${0##*/}.kernel-cmdline.cfg"284kern_cfg="/tmp/${0##*/}.kernel-cmdline.cfg"
@@ -261,8 +311,13 @@
261done311done
262rm -f "$kern_cfg"312rm -f "$kern_cfg"
263313
314opts=""
264case "${overlayroot:-disabled}" in315case "${overlayroot:-disabled}" in
265 tmpfs) mode="tmpfs";;316 tmpfs|tmpfs:*)
317 mode="tmpfs"
318 opts="${overlayroot#tmpfs}";
319 opts=${opts#:}
320 ;;
266 /dev/*|device:*)321 /dev/*|device:*)
267 case "$overlayroot" in322 case "$overlayroot" in
268 /dev/*) opts="dev=${overlayroot}";;323 /dev/*) opts="dev=${overlayroot}";;
@@ -274,9 +329,9 @@
274 device="$_RET_DEVICE"329 device="$_RET_DEVICE"
275 dir_prefix="$_RET_DIR"330 dir_prefix="$_RET_DIR"
276 ;;331 ;;
277 crypt|crypt:*)332 crypt:*)
278 mode="crypt"333 mode="crypt"
279 t=${overlayroot#crypt}334 opts=${overlayroot#crypt:}
280 crypto_setup "${t#:}" ||335 crypto_setup "${t#:}" ||
281 fail "failed setup crypt for ${overlayroot}"336 fail "failed setup crypt for ${overlayroot}"
282 device="$_RET_DEVICE"337 device="$_RET_DEVICE"
@@ -290,7 +345,16 @@
290 exit 0;;345 exit 0;;
291esac346esac
292347
293log_warn "configuring 'overlayroot=$overlayroot' per $used_desc"348parse_string "$opts" "," _RET_common_
349swap=${_RET_common_swap:-0}
350recurse=${_RET_common_recurse:-1}
351
352[ "$swap" = "0" -o "$swap" = "1" ] ||
353 fail "invalid setting for swap: $swap. must be '0' or '1'"
354[ "$recurse" = "0" -o "$recurse" = "1" ] ||
355 fail "invalid setting for recurse: $recurse. must be '0' or '1'"
356
357log_warn "configuring overlayroot with mode=$mode opts='$opts' per $used_desc"
294358
295# overlayroot_driver *could* be defined in one of the configs above359# overlayroot_driver *could* be defined in one of the configs above
296# but we're not documenting that.360# but we're not documenting that.
@@ -398,33 +462,22 @@
398# Remember we are still on the initramfs root fs here, so we have to work462# Remember we are still on the initramfs root fs here, so we have to work
399# on ${ROOTMNT}/etc/fstab. The original fstab is463# on ${ROOTMNT}/etc/fstab. The original fstab is
400# ${ROOTMNT}${root_ro}/etc/fstab.464# ${ROOTMNT}${root_ro}/etc/fstab.
401root_type=$(awk '$1 == root { print $3 }' "root=$ROOT" /proc/mounts)
402root_options=$(awk '$1 == root { print $4 }' "root=$ROOT" /proc/mounts)
403cat <<EOF >${ROOTMNT}/etc/fstab465cat <<EOF >${ROOTMNT}/etc/fstab
404#466#
405# This fstab is in an overlayfs. The real one can be found at467# This fstab is in an overlayfs. The real one can be found at
406# ${root_ro}/etc/fstab468# ${root_ro}/etc/fstab
407# The original entry for '/' and all swap files have been removed. The new469# The original entry for '/' and other mounts have been updated to be placed
408# entry for the read-only the real root fs follows. To permanently470# under $root_ro.
409# modify this (or any other file), you should change-root into a writable471# To permanently modify this (or any other file), you should change-root into
410# view of the underlying filesystem using:472# a writable view of the underlying filesystem using:
411# sudo overlayroot-chroot473# sudo overlayroot-chroot
412#474#
413
414${ROOT} ${root_ro} ${root_type} ${root_options} 0 0
415
416#
417# remaining entries from the original ${root_ro}/etc/fstab follow.
418#
419EOF475EOF
476
420[ $? -eq 0 ] || log_fail "failed to modify /etc/fstab (step 1)"477[ $? -eq 0 ] || log_fail "failed to modify /etc/fstab (step 1)"
421478overlayrootify_fstab ""${ROOTMNT}${root_ro}/etc/fstab"" "$root_ro" \
422#comment out root and swap entries passing others through unmodified479 "$root_rw" "$dir_prefix" "$recurse" "$swap" >> "${ROOTMNT}/etc/fstab" ||
423awk '{ 480 log_fail "failed to modify /etc/fstab (step 2)"
424 if ($0 ~ /^#/ || ($2 != "/" && $3 != "swap") ) { print $0 }
425 else { printf("%s%s\n","#overlayroot#",$0); }
426 }' "${ROOTMNT}${root_ro}/etc/fstab" >> "${ROOTMNT}/etc/fstab" ||
427 log_fail "failed to modify etc/fstab (step 2)"
428481
429msg="configured root with '$overlayroot' using ${overlayroot_driver} per"482msg="configured root with '$overlayroot' using ${overlayroot_driver} per"
430msg="$msg ${used_desc}"483msg="$msg ${used_desc}"
431484
=== modified file 'overlayroot/usr/sbin/overlayroot-chroot'
--- overlayroot/usr/sbin/overlayroot-chroot 2012-07-20 19:53:11 +0000
+++ overlayroot/usr/sbin/overlayroot-chroot 2012-07-23 19:58:19 +0000
@@ -18,6 +18,8 @@
18# along with this program. If not, see <http://www.gnu.org/licenses/>.18# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
20set -e20set -e
21set -f # disable path expansion
22REMOUNTS=""
2123
22error() {24error() {
23 printf "ERROR: $@\n" 1>&225 printf "ERROR: $@\n" 1>&2
@@ -44,23 +46,26 @@
44}46}
4547
46clean_exit() {48clean_exit() {
47 local mounts="$1" rc="" d="" lowerdir=""49 local mounts="$1" rc=0 d="" lowerdir="" mp=""
48 for d in ${mounts}; do50 for d in ${mounts}; do
49 if mountpoint ${d} >/dev/null; then51 if mountpoint ${d} >/dev/null; then
50 umount ${d} || rc=152 umount ${d} || rc=1
51 fi53 fi
52 done54 done
53 get_lowerdir55 for mp in $REMOUNTS; do
54 lowerdir=${_RET}56 mount -o remount,ro "${mp}" ||
55 if ! mount -o remount,ro "${lowerdir}"; then57 error "Note that [${mp}] is still mounted read/write"
56 error "Note that [${lowerdir}] is still mounted read/write"58 done
57 fi59 [ "$2" = "return" ] && return ${rc} || exit ${rc}
58 [ "$2" = "return" ] && return ${rc} || exit "${rc}"
59}60}
6061
61# Try to find the overlayroot filesystem62# Try to find the overlayroot filesystem
62get_lowerdir63get_lowerdir
63lowerdir=${_RET}64lowerdir=${_RET}
65
66recurse_mps=$(awk '$1 ~ /^\/dev\// && $2 ~ starts { print $2 }' \
67 starts="^$lowerdir/" /proc/mounts)
68
64mounts=69mounts=
65for d in proc run sys; do70for d in proc run sys; do
66 if ! mountpoint "${lowerdir}/${d}" >/dev/null; then71 if ! mountpoint "${lowerdir}/${d}" >/dev/null; then
@@ -71,8 +76,11 @@
71done76done
7277
73# Remount with read/write78# Remount with read/write
74mount -o remount,rw "${lowerdir}" ||79for mp in "$lowerdir" $recurse_mps; do
75 fail "Unable to remount [${lowerdir}] writable"80 mount -o remount,rw "${mp}" &&
81 REMOUNTS="$mp $REMOUNTS" ||
82 fail "Unable to remount [$mp] writable"
83done
76info "Chrooting into [${lowerdir}]"84info "Chrooting into [${lowerdir}]"
77chroot ${lowerdir} "$@"85chroot ${lowerdir} "$@"
7886

Subscribers

People subscribed via source and target branches