Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for overlayfs backed on persistent device (e.g. USB key) #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added support for overlayfs backed on persistent device (e.g. USB key)
- Bumped new micro release
- Modified 'overlayroot into 'overlayroot=<device>' where <device>='tmpfs' or a real device (can also be specified by tag)
- Fixed a typo in 'overlayroot-motd.sh'
- Added code to resynchronize the lower on boot when a 'overlayroot.resync' file is present in upper

Signed-off-by: Renaud Métrich <renaud.metrich@laposte.net>
  • Loading branch information
Renaud Métrich committed Sep 2, 2019
commit f5235bd3a50f410ff69e46f9da3582358138be86
10 changes: 10 additions & 0 deletions 90-overlayroot.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Trigger]
Type = File
Operation = Install
Operation = Upgrade
Target = usr/lib/initcpio/*

[Action]
Description = Updating overlayroot initcpios...
When = PostTransaction
Exec = /usr/bin/mkinitcpio -p overlayroot
37 changes: 26 additions & 11 deletions PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pkgname=overlayroot
pkgver=0.2
pkgrel=1
pkgrel=2
pkgdesc="overlayFS root file system"
arch=('any')
url="https://github.com/nils-werner/arch-overlayroot"
@@ -9,24 +9,36 @@ depends=(
'mkinitcpio'
'arch-install-scripts'
)
install=overlayroot.install
source=(
'overlayroot.install'
'initcpio-install-overlayroot'
'initcpio-hooks-overlayroot'
'rwrootfs'
'fsck.overlay'
'journald-volatile-storage.conf'
'overlayroot-motd.sh'
'mkinitcpio-overlayroot.conf'
'mkinitcpio-overlayroot.preset'
'90-overlayroot.hook'
)
sha256sums=(
'SKIP'
'SKIP'
'SKIP'
'SKIP'
'SKIP'
'SKIP'
'SKIP'
# 'initcpio-install-overlayroot'
'3f197246d31439c84d7433609628126354c02bc496b72291cf884a21d1e1d492'
# 'initcpio-hooks-overlayroot'
'ad262726508954ba51d821449cd03d2cb58ff181ea437fa0a229e3aeef675ee4'
# 'rwrootfs'
'75b8c2ebe9e08e58f207a82aa93cfdd0b46a2255e49493cf6fedfe97cc61bd98'
# 'fsck.overlay'
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
# 'journald-volatile-storage.conf'
'c0c9a711f7554fdd02650409b098fc0636b8f3f574f03d927c6df84f5ec82a7f'
# 'overlayroot-motd.sh'
'e2e917ab85d44abfc8e4c504cd8925d6fec5d26c452198303e2238464552e9a1'
# 'mkinitcpio-overlayroot.conf'
'0954c40ee94824707ed0d4bf65bda0be1b3e57c52dd8cd2d12df23bf597ae6a2'
# 'mkinitcpio-overlayroot.preset'
'7ebdad6c610b8d35dab94343b7a98fb7dca4534aa43c49680b346919496fd1e8'
# '90-overlayroot.hook'
'7ef79bcdbb7a4a340893ab40755a8f90c745d00396bee2f218cc8f78374aec19'
)

build() {
@@ -36,8 +48,11 @@ build() {
package() {
install -Dm644 "$srcdir/initcpio-install-overlayroot" "$pkgdir/usr/lib/initcpio/install/overlayroot"
install -Dm644 "$srcdir/initcpio-hooks-overlayroot" "$pkgdir/usr/lib/initcpio/hooks/overlayroot"
install -Dm644 "$srcdir/journald-volatile-storage.conf" "$pkgdir/etc/systemd/journald.conf.d/volatile-storage.conf"
# install -Dm644 "$srcdir/journald-volatile-storage.conf" "$pkgdir/etc/systemd/journald.conf.d/volatile-storage.conf"
install -Dm755 "$srcdir/rwrootfs" "$pkgdir/usr/bin/rwrootfs"
install -Dm755 "$srcdir/fsck.overlay" "$pkgdir/usr/bin/fsck.overlay"
install -Dm644 "$srcdir/mkinitcpio-overlayroot.conf" "$pkgdir/etc/mkinitcpio-overlayroot.conf"
install -Dm644 "$srcdir/mkinitcpio-overlayroot.preset" "$pkgdir/etc/mkinitcpio.d/overlayroot.preset"
install -Dm644 "$srcdir/overlayroot-motd.sh" "$pkgdir/etc/profile.d/overlayroot-motd.sh"
install -Dm644 "$srcdir/90-overlayroot.hook" "$pkgdir/usr/share/libalpm/hooks/90-overlayroot.hook"
}
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -32,34 +32,46 @@ Install this package
makepkg -si
```

Then try rebooting, it should boot as normal.
After installation, a new `initramfs-overlayroot.img` initramfs will be automatically created but not used.
Try rebooting, it should boot as normal.

### Enable overlayroot hook

Then add `overlayroot` to your `HOOKS` array in `/etc/mkinitcpio.conf` and rebuild the initramfs by running
To enable overlayroot hook, you need to change the `initramfs` command in `/boot/config.txt`:

```
mkinitcpio -P
initramfs initramfs-overlayroot.img followkernel
```

and reboot. It should boot as normal.
Try rebooting, it should boot as normal because there is no `overlayroot` on the commandline yet.

### Enable overlayroot in commandline
### Enable overlayroot backed on RAM (tmpfs)

With the initramfs in place, you can now enable overlayroot by adding `overlayroot` to the end of `/boot/cmdline.txt`
To boot with overlayroot over tmpfs, just add `overlayroot=tmpfs` to the end of `/boot/cmdline.txt`.

```
root=/dev/mmcblk0p2 rw rootwait console=ttyAMA0,115200 console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 kgdboc=ttyAMA0,115200 elevator=noop overlayroot
root=/dev/mmcblk0p2 rw rootwait console=ttyAMA0,115200 console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 kgdboc=ttyAMA0,115200 elevator=noop overlayroot=tmpfs
```

and reboot. You should see a warning during login that any changes you make to your filesystem will be non-persistent after this point.

### Enable overlayroot backed on USB

To boot with overlayroot backed on USB, just add `overlayroot=<device>` to the end of `/boot/cmdline.txt`. You can specify the device to use by device name or tag, e.g. `/dev/sda2`, `LABEL=ROOT_USB`, `UUID=xxx`, refer to mount(8) manpage for details.

```
root=/dev/mmcblk0p2 rw rootwait console=ttyAMA0,115200 console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 kgdboc=ttyAMA0,115200 elevator=noop overlayroot=LABEL=ROOT_USB
```

and reboot. You will not see a warning during login because any changes you make to your filesystem will be stored on the USB device.


### Set filesystems readonly

You can now also set the entire root filesystem as readonly by changing `rw` to `ro` in `/boot/cmdline.txt`

```
root=/dev/mmcblk0p2 ro rootwait console=ttyAMA0,115200 console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 kgdboc=ttyAMA0,115200 elevator=noop overlayroot
root=/dev/mmcblk0p2 ro rootwait console=ttyAMA0,115200 console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 kgdboc=ttyAMA0,115200 elevator=noop overlayroot=tmpfs
```

and adding `ro` to `/etc/fstab`
@@ -74,18 +86,22 @@ and adding `ro` to `/etc/fstab`

## Editing the root filesystem

You can run `rwrootfs` to remount all file systems as read-write and change into an interactive shell in your SD card file system. After exiting that shell, the fileystems will remain read-write until next reboot.
You can run `rwrootfs` to remount all file systems as read-write and change into an interactive shell in your SD card file system. After exiting that shell, the filesystems will remain read-write until next reboot.

Alternatively you can undo all changes from [Enable overlayroot in commandline](#enable-overlayroot-in-commandline) and [Set filesystems readonly](#set-filesystems-readonly) and reboot. This is the recommended way of system upgrades.

## Resyncing the SDCARD

When using USB backend, you can resync automatically the SDCARD by creating a file named `/overlayroot.resync`. Upon reboot, files (virtually) removed from the SDCARD will be removed from the SDCARD for real and the content of the USB backend will be moved to the SDCARD.

## Debugging

Sometimes, overlayroot may cause trouble during boot time. To boot without it simply remove `overlayroot` from `/boot/cmdline.txt`.

If you still have problems, you can also try removing the initramfs by removing

```
initramfs initramfs-linux.img followkernel
initramfs initramfs-overlayroot.img followkernel
```

from `/boot/config.txt`.
47 changes: 45 additions & 2 deletions initcpio-hooks-overlayroot
Original file line number Diff line number Diff line change
@@ -2,9 +2,13 @@

run_hook() {
if [ "${overlayroot}" ]; then
# Create overlay tree in RAM
mkdir -p /overlay
mount -t tmpfs overlayroot /overlay
if [ "${overlayroot}" = "tmpfs" ]; then
mount -t tmpfs overlayroot /overlay
else
mount ${overlayroot} /overlay
[ $? -eq 0 ] || mount -t tmpfs overlayroot /overlay
fi
mkdir -p /overlay/upper
mkdir -p /overlay/work
mkdir -p /overlay/lower
@@ -22,6 +26,45 @@ run_latehook() {
rm /new_root
mkdir -p /new_root

if [ -e "/overlay/upper/overlayroot.resync" ]; then

echo "overlayroot: Resyncing lower and upper ..."

awk '$2 == "/overlay/lower" && $4 ~ /ro,/ { exit 1 }' /proc/mounts
is_ro=$?

if [ $is_ro -eq 1 ]; then
mount -o rw,remount /overlay/lower
fi

# Delete all files which have been whited out from upper
echo "overlayroot: Deleting all files from lower that have been wiped from upper ..."
(cd /overlay/upper && find . -type c -print) | while read path; do
[ "$(stat -c '%t %T' /overlay/upper/$path)" = "0 0" ] || continue
rm -fr /overlay/lower/$path /overlay/upper/$path
done

# Delete all files from lower that exist in upper
echo "overlayroot: Deleting all files from lower that exist in upper ..."
(cd /overlay/upper && find . ! -type d -print0) | (cd /overlay/lower && xargs -0 rm -fr)

# Copy upper to lower
echo "overlayroot: Copying all files from upper to lower ..."
(cd /overlay/upper && tar cf - .) | (cd /overlay/lower && tar xf -)

# Delete overlayroot.resync from lower
rm /overlay/lower/overlayroot.resync

# Delete all files from upper
echo "overlayroot: Deleting all files from upper ..."
rm -fr /overlay/upper/* /overlay/upper/.*

# Remount 'ro' after we are finished
if [ $is_ro -eq 1 ]; then
mount -o ro,remount /overlay/lower
fi
fi

# Build overlay FS and mount it to /new_root
mount -t overlay overlay -o lowerdir=/overlay/lower,upperdir=/overlay/upper,workdir=/overlay/work /new_root

4 changes: 4 additions & 0 deletions mkinitcpio-overlayroot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
. /etc/mkinitcpio.conf

BINARIES+=( find mount rm tar xargs )
HOOKS+=( overlayroot )
4 changes: 4 additions & 0 deletions mkinitcpio-overlayroot.preset
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
. /etc/mkinitcpio.d/linux-raspberrypi.preset

default_config="/etc/mkinitcpio-overlayroot.conf"
default_image="/boot/initramfs-overlayroot.img"
10 changes: 5 additions & 5 deletions overlayroot-motd.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
if grep -qs 'overlayroot' /proc/cmdline; then
if [ "$(awk '$2 == "/overlay" { print $3 }' /proc/mounts)" = "tmpfs" ]; then
cat << EOF

==> WARNING: Overlayroot in effect.
None of the changes you make will be preseved after reboot.
==> WARNING: overlayroot backed on tmpfs in effect. None of the changes you
make will be preserved after reboot.

To disable overlayroot remove the 'overlayroot' argument
from the kernel commandline.
To disable overlayroot remove the 'overlayroot' argument from the kernel
commandline.
EOF
fi
12 changes: 0 additions & 12 deletions overlayroot.install

This file was deleted.