summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClaudius Heine <ch@denx.de>2016-10-20 14:23:36 +0200
committerClaudius Heine <ch@denx.de>2016-10-20 14:23:36 +0200
commit1c55880c8cbba009e07645e3a7b26f2bf2a5756c (patch)
tree7050f632cfa00017e5af3f37ad1ea1f6ddbf26ca
parent57080e0f657450382fcfbd8d1d7a5c9ca0308e63 (diff)
downloadmeta-readonly-rootfs-overlay-1c55880c8cbba009e07645e3a7b26f2bf2a5756c.tar.gz
added more important features
added ability to use current rootfs as read-only rootfs added ability to delete the read-write filesystem prior to overlay fs creation use init script in strict shell mode moved root mounts to /media/rfs and use /mnt as new rootfs mount, where the overlay is created
-rw-r--r--README.md12
-rw-r--r--recipes-core/initrdscripts/files/init-readonly-rootfs-overlay-boot.sh90
2 files changed, 65 insertions, 37 deletions
diff --git a/README.md b/README.md
index 4daec18..8f037b3 100644
--- a/README.md
+++ b/README.md
@@ -60,16 +60,20 @@ If you use this layer you do *not* need to set `read-only-rootfs` in the
60Example: 60Example:
61 61
62``` 62```
63root=/dev/vda rootfstype=ext4 rootrw=/dev/vdb rootrwfstype=btrfs 63root=/dev/sda1 rootfstype=ext4 rootrw=/dev/sda2 rootrwfstype=btrfs
64``` 64```
65 65
66`root=` specifies the read-only root filesystem device. (required) 66`root=` specifies the read-only root filesystem device. If this is not
67specified, the current rootfs is used.
67 68
68`rootfstype=` if support for the-read only filesystem is not build into the 69`rootfstype=` if support for the-read only filesystem is not build into the
69kernel, you can specifiy the required module name here. 70kernel, you can specifiy the required module name here.
70 71
71`rootrw=` specifies the read-write root filesystem device. If this is not 72`rootrw=` specifies the read-write filesystem device. If this is not
72specified, `tmpfs` is used. 73specified, `tmpfs` is used.
73 74
74`rootrwfstype=` if support for the read-write filesystem is not build into the 75`rootrwfstype=` if support for the read-write filesystem is not build into the
75kernel, you can specifiy the required module name here. 76kernel, you can specifiy the required module name here.
77
78`rootrwreset=` set to `yes` if you want to delete all the files in the
79read-write filesystem prior to building the overlay root files system.
diff --git a/recipes-core/initrdscripts/files/init-readonly-rootfs-overlay-boot.sh b/recipes-core/initrdscripts/files/init-readonly-rootfs-overlay-boot.sh
index 315da9b..37a1635 100644
--- a/recipes-core/initrdscripts/files/init-readonly-rootfs-overlay-boot.sh
+++ b/recipes-core/initrdscripts/files/init-readonly-rootfs-overlay-boot.sh
@@ -1,13 +1,21 @@
1#!/bin/sh 1#!/bin/sh
2 2
3# Enable strict shell mode
4set -euo pipefail
5
3PATH=/sbin:/bin:/usr/sbin:/usr/bin 6PATH=/sbin:/bin:/usr/sbin:/usr/bin
4 7
5ROOT_MOUNT="/rootfs"
6MOUNT="/bin/mount" 8MOUNT="/bin/mount"
7UMOUNT="/bin/umount" 9UMOUNT="/bin/umount"
8ROOT_RWDEVICE="tmpfs" 10
9ROOT_ROMOUNT="/rfs/ro" 11INIT="/sbin/init"
10ROOT_RWMOUNT="/rfs/rw" 12
13ROOT_MOUNT="/mnt"
14ROOT_RODEVICE=""
15ROOT_RWDEVICE=""
16ROOT_ROMOUNT="/media/rfs/ro"
17ROOT_RWMOUNT="/media/rfs/rw"
18ROOT_RWRESET="no"
11 19
12# Copied from initramfs-framework. The core of this script probably should be 20# Copied from initramfs-framework. The core of this script probably should be
13# turned into initramfs-framework modules to reduce duplication. 21# turned into initramfs-framework modules to reduce duplication.
@@ -33,9 +41,6 @@ early_setup() {
33 $MOUNT -t sysfs sysfs /sys 41 $MOUNT -t sysfs sysfs /sys
34 $MOUNT -t devtmpfs none /dev 42 $MOUNT -t devtmpfs none /dev
35 43
36 # support modular kernel
37 modprobe isofs 2> /dev/null
38
39 mkdir -p /run 44 mkdir -p /run
40 mkdir -p /var/run 45 mkdir -p /var/run
41 46
@@ -44,24 +49,28 @@ early_setup() {
44} 49}
45 50
46read_args() { 51read_args() {
47 [ -z "$CMDLINE" ] && CMDLINE=`cat /proc/cmdline` 52 [ -z "${CMDLINE+x}" ] && CMDLINE=`cat /proc/cmdline`
48 for arg in $CMDLINE; do 53 for arg in $CMDLINE; do
49 optarg=`expr "x$arg" : 'x[^=]*=\(.*\)'` 54 optarg=`expr "x$arg" : 'x[^=]*=\(.*\)'`
50 case $arg in 55 case $arg in
51 root=*) 56 root=*)
52 ROOT_DEVICE=$optarg ;; 57 ROOT_RODEVICE=$optarg ;;
53 rootfstype=*) 58 rootfstype=*)
54 modprobe $optarg 2> /dev/null ;; 59 modprobe $optarg 2> /dev/null ;;
55 rootrw=*) 60 rootrw=*)
56 ROOT_RWDEVICE=$optarg ;; 61 ROOT_RWDEVICE=$optarg ;;
57 rootrwfstype=*) 62 rootrwfstype=*)
58 modprobe $optarg 2> /dev/null ;; 63 modprobe $optarg 2> /dev/null ;;
64 rootrwreset=*)
65 ROOT_RWRESET=$optarg ;;
59 video=*) 66 video=*)
60 video_mode=$arg ;; 67 video_mode=$arg ;;
61 vga=*) 68 vga=*)
62 vga_mode=$arg ;; 69 vga_mode=$arg ;;
70 init=*)
71 INIT=$optarg ;;
63 console=*) 72 console=*)
64 if [ -z "${console_params}" ]; then 73 if [ -z "${console_params+x}" ]; then
65 console_params=$arg 74 console_params=$arg
66 else 75 else
67 console_params="$console_params $arg" 76 console_params="$console_params $arg"
@@ -78,42 +87,55 @@ fatal() {
78 87
79early_setup 88early_setup
80 89
81[ -z "$CONSOLE" ] && CONSOLE="/dev/console" 90[ -z "${CONSOLE+x}" ] && CONSOLE="/dev/console"
82 91
83read_args 92read_args
84 93
85mount_and_boot() { 94mount_and_boot() {
86 mkdir -p $ROOT_MOUNT $ROOT_ROMOUNT $ROOT_RWMOUNT 95 mkdir -p $ROOT_MOUNT $ROOT_ROMOUNT $ROOT_RWMOUNT
87 mknod /dev/loop0 b 7 0 2>/dev/null
88 96
89 # Mount read-only root filesystem into initramfs rootfs 97 # Build mount options for read only root filesystem.
90 if ! $MOUNT -o ro,noatime,nodiratime $ROOT_DEVICE $ROOT_ROMOUNT ; then 98 # If no read-only device was specified via kernel commandline, use current
91 fatal "Could not mount read-only rootfs" 99 # rootfs.
100 if [ -z "${ROOT_RODEVICE}" ]; then
101 ROOT_ROMOUNTOPTIONS="--bind,ro /"
102 else
103 ROOT_ROMOUNTOPTIONS="-o ro,noatime,nodiratime $ROOT_RODEVICE"
92 fi 104 fi
93 105
94 # determine which unification filesystem to use 106 # Mount rootfs as read-only to mount-point
95 union_fs_type="" 107 if ! $MOUNT $ROOT_ROMOUNTOPTIONS $ROOT_ROMOUNT ; then
96 if grep -w "overlay" /proc/filesystems; then 108 fatal "Could not mount read-only rootfs"
97 union_fs_type="overlay"
98 elif grep -w "aufs" /proc/filesystems; then
99 union_fs_type="aufs"
100 else
101 union_fs_type=""
102 fi 109 fi
103 110
104 # Build mount options for read write root filesystem. 111 # Build mount options for read write root filesystem.
105 # If no read-write device was specified via kernel commandline, use tmpfs. 112 # If no read-write device was specified via kernel commandline, use tmpfs.
106 if [ "tmpfs" == $ROOT_RWDEVICE ]; then 113 if [ -z "${ROOT_RWDEVICE}" ]; then
107 ROOT_RWMOUNTOPTIONS="-t tmpfs -o rw,noatime,mode=755" 114 ROOT_RWMOUNTOPTIONS="-t tmpfs -o rw,noatime,mode=755 tmpfs"
108 else 115 else
109 ROOT_RWMOUNTOPTIONS="-o rw,noatime,mode=755" 116 ROOT_RWMOUNTOPTIONS="-o rw,noatime,mode=755 $ROOT_RWDEVICE"
110 fi 117 fi
111 118
112 # Mount read-write filesystem into initram rootfs 119 # Mount read-write filesystem into initram rootfs
113 if ! $MOUNT $ROOT_RWMOUNTOPTIONS $ROOT_RWDEVICE $ROOT_RWMOUNT ; then 120 if ! $MOUNT $ROOT_RWMOUNTOPTIONS $ROOT_RWMOUNT ; then
114 fatal "Could not mount read-write rootfs" 121 fatal "Could not mount read-write rootfs"
115 fi 122 fi
116 123
124 # Reset read-write filesystem if specified
125 if [ "yes" == "$ROOT_RWRESET" -a -n "${ROOT_RWMOUNT}" ]; then
126 rm -rf $ROOT_RWMOUNT/*
127 fi
128
129 # Determine which unification filesystem to use
130 union_fs_type=""
131 if grep -w "overlay" /proc/filesystems; then
132 union_fs_type="overlay"
133 elif grep -w "aufs" /proc/filesystems; then
134 union_fs_type="aufs"
135 else
136 union_fs_type=""
137 fi
138
117 # Create/Mount overlay root filesystem 139 # Create/Mount overlay root filesystem
118 case $union_fs_type in 140 case $union_fs_type in
119 "overlay") 141 "overlay")
@@ -130,15 +152,18 @@ mount_and_boot() {
130 152
131 # Move read-only and read-write root filesystem into the overlay filesystem 153 # Move read-only and read-write root filesystem into the overlay filesystem
132 mkdir -p $ROOT_MOUNT/$ROOT_ROMOUNT $ROOT_MOUNT/$ROOT_RWMOUNT 154 mkdir -p $ROOT_MOUNT/$ROOT_ROMOUNT $ROOT_MOUNT/$ROOT_RWMOUNT
133 $MOUNT --move $ROOT_ROMOUNT $ROOT_MOUNT/$ROOT_ROMOUNT 155 $MOUNT -n --move $ROOT_ROMOUNT ${ROOT_MOUNT}/$ROOT_ROMOUNT
134 $MOUNT --move $ROOT_RWMOUNT $ROOT_MOUNT/$ROOT_RWMOUNT 156 $MOUNT -n --move $ROOT_RWMOUNT ${ROOT_MOUNT}/$ROOT_RWMOUNT
135 157
136 # Watches the udev event queue, and exits if all current events are handled 158 # Watches the udev event queue, and exits if all current events are handled
137 udevadm settle --timeout=3 --quiet 159 udevadm settle --timeout=3
138 # Kills the current udev running processes, which survived after 160 # Kills the current udev running processes, which survived after
139 # device node creation events were handled, to avoid unexpected behavior 161 # device node creation events were handled, to avoid unexpected behavior
140 killall -9 "${_UDEV_DAEMON##*/}" 2>/dev/null 162 killall -9 "${_UDEV_DAEMON##*/}" 2>/dev/null
141 163
164 # Remove /run /var/run that are created in early_setup
165 rm -rf /run /var/run
166
142 # Move the mount points of some filesystems over to 167 # Move the mount points of some filesystems over to
143 # the corresponding directories under the real root filesystem. 168 # the corresponding directories under the real root filesystem.
144 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts`; do 169 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts`; do
@@ -152,9 +177,8 @@ mount_and_boot() {
152 cd $ROOT_MOUNT 177 cd $ROOT_MOUNT
153 178
154 # busybox switch_root supports -c option 179 # busybox switch_root supports -c option
155 exec switch_root -c /dev/console $ROOT_MOUNT /sbin/init $CMDLINE || 180 exec chroot $ROOT_MOUNT $INIT ||
156 fatal "Couldn't switch_root, dropping to shell" 181 fatal "Couldn't chroot, dropping to shell"
157} 182}
158 183
159mount_and_boot 184mount_and_boot
160