Add bind-mount for /etc/adjtime to make subhost use host clock without ado
[rrq/overlay-boot.git] / overlay-diskfile
1 #!/bin/sh
2 #
3 # Helper script for using a diskfile for the LOWER or UPPER+WORK
4 # filesystems.
5 #
6 # Example use a whole diskfile filesystem as LOWER:
7 # LOWER=!overlay-diskfile mnt/ disk.img
8 #
9 # Example use a diskfile partition 2 as LOWER:
10 # LOWER=!overlay-diskfile mnt/ disk.img 2
11 #
12 # Example use a subtree of a diskfile partition 2 as LOWER:
13 # LOWER=!overlay-diskfile mnt/some/path disk.img 2
14 #
15 # Example use a while diskfile as UPPER and WORK.
16 # The diskfile is mounted on "mnt".
17 # UPPER=!overlay-diskfile mnt/some/upper/path disk.img 2
18 # WORK= mnt/some/work/path
19
20 # Helper function to determine the mount offset for the partition, if any
21 partoffset() {
22     local X
23     if [ -z "$2" ] ; then
24         echo 0
25     else
26         fdisk -lo Start "$1" | grep -A$2 '^ Start' | \
27             sed '${s|^|512*|;b};d' | bc -l
28     fi
29 }
30
31 # Only do something when invoked by overlay-boot
32 if [ "$ACTION" = "overlay-boot" ] ; then
33     DISKFILE="$2"
34     # .. and a diskfile is given
35     if [ -n "$DISKFILE" ] ; then
36         MOUNTPOINT="$(realpath "${1%/*}")"
37         PARTITIONINDEX="$3"
38         # .. and it's not mounted yet (for this subhost)
39         if ! grep -qE "^[^ ]* $MOUNTPOINT" /proc/mounts ; then
40             OFFSET=$(partoffset "$DISKFILE" "$PARTITIONINDEX")
41             mount -ooffset=$OFFSET "$DISKFILE" "$MOUNTPOINT" || exit 1
42         fi
43     fi
44 fi
45 echo "${1#/}"