Kernel documentation for the overlay file system:
At mount time, the two directories given as mount options "lowerdir" and
"upperdir" are combined into a merged directory:
mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
workdir=/work /merged
The "workdir" needs to be an empty directory on the same filesystem
as upperdir.
From archlinux:
To add an overlayfs entry to /etc/fstab use the following format:
overlay /merged overlay noauto,x-systemd.automount,lowerdir=/lower,upperdir=/upper,workdir=/work 0 0
The noauto and x-systemd.automount mount options are necessary to prevent systemd from hanging on boot because it failed to mount the overlay. The overlay is now mounted whenever it is first accessed and requests are buffered until it is ready.
From flockport:
lxc plays well with overlayfs. LXC supports overlayfs in terms of snapshots. Say you create a container Debian Jessie container called p1. You can create an overlayfs snapshot of this container with the lxc-clone tool.
lxc-clone -s p1 p1-overlay
If you make changes to p1-overlay and would like to freeze that state, you can do that easily by taking a snapshot of p1-overlay.
lxc-clone -s p1-overlay p2-overlay
You can also choose to clone the container instead of a snapshot. Notice the absence of the -s flag. When you do this the entire data from the overlay layers are copied into the new P2 containers rootfs.
lxc-clone p2-overlay p2
Fusing the information together, my own example using an .img file to overlay the /var directory.
dd if=/dev/zero of=/srv/overlay/.var.img bs=4096 count=204800
mkfs -t ext4 -L ofsvar /srv/overlay/.var.img
mkdir /mnt/.var.ofs
mount /srv/overlay/.var.img /mnt/.var.ofs/
mkdir /mnt/.var.ofs/.workdir
mkdir /mnt/.var.ofs/.upper
mount -t overlay overlay -o lowerdir=/var,upperdir=/mnt/.var.ofs/.upper,workdir=/mnt/.var.ofs/.workdir /var
#sed -i -e "\$aoverlay /var overlay \
noauto,x-systemd.automount,lowerdir=/var,upperdir=/mnt/.var.ofs/.upper,workdir=/mnt/.var.ofs/.workdir \
0 0" /etc/fstab
There are two outstanding issues: a) the sed statement places an entry in /etc/fstab but systemd currently dislikes creating an overlay definition for an already existing mount (I have a /var partition), and b) the mount statement can't be placed in /etc/rc.local as debian/stretch has deprecated that mode of operation and i have to think of a different way of using that statement, and c)I am missing the sed statement in the above to create the /etc/fstab entry for /mnt/.var.ofs, which does work properly.
- copyninja: background on how systemd makes use of /etc/fstab and creates it's own mount files
- windsock.io: Union filesystems are often used for Live CD creation, where a read-only image is augmented with a writeable layer in tmpfs, thereby enabling a dynamic, but ephemeral session.
- zyisrad: complicated example with dm-crypt, btrfs, and overlayfs
- Systemd refrence files for auto-mounting:
fstab options,
systemd-fstab-generator
- Programster: using .img files for the upper file layout
- From docker: much detail on docker and overlayfs and various strategies
-
A useful tool for taking a look at mounts:
$ findmnt
TARGET SOURCE FSTYPE OPTIONS
/ /dev/sda2 ext4 rw,relatime,errors=remount-ro,data=ordered
├─/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/kernel/security securityfs securityfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/fs/cgroup tmpfs tmpfs ro,nosuid,nodev,noexec,mode=755
│ │ ├─/sys/fs/cgroup/systemd cgroup cgroup rw,nosuid,nodev,noexec,relatime,xattr, .....
│ │ ├─/sys/fs/cgroup/cpuset cgroup cgroup rw,nosuid,nodev,noexec,relatime,cpuset
│ │ ├─/sys/fs/cgroup/pids cgroup cgroup rw,nosuid,nodev,noexec,relatime,pids
│ │ ├─/sys/fs/cgroup/cpu,cpuacct cgroup cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct
│ │ ├─/sys/fs/cgroup/devices cgroup cgroup rw,nosuid,nodev,noexec,relatime,devices
│ │ ├─/sys/fs/cgroup/perf_event cgroup cgroup rw,nosuid,nodev,noexec,relatime,perf_event
│ │ ├─/sys/fs/cgroup/blkio cgroup cgroup rw,nosuid,nodev,noexec,relatime,blkio
│ │ ├─/sys/fs/cgroup/net_cls,net_prio cgroup cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio
│ │ ├─/sys/fs/cgroup/memory cgroup cgroup rw,nosuid,nodev,noexec,relatime,memory
│ │ └─/sys/fs/cgroup/freezer cgroup cgroup rw,nosuid,nodev,noexec,relatime,freezer
│ ├─/sys/fs/pstore pstore pstore rw,nosuid,nodev,noexec,relatime
│ └─/sys/kernel/debug debugfs debugfs rw,relatime
├─/proc proc proc rw,nosuid,nodev,noexec,relatime
│ └─/proc/sys/fs/binfmt_misc systemd-1 autofs rw,relatime,fd=27,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=8193
├─/dev udev devtmpfs rw,nosuid,relatime,size=967536k,nr_inodes=241884,mode=755
│ ├─/dev/pts devpts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
│ ├─/dev/shm tmpfs tmpfs rw,nosuid,nodev
│ ├─/dev/mqueue mqueue mqueue rw,relatime
│ └─/dev/hugepages hugetlbfs hugetlbfs rw,relatime
├─/run tmpfs tmpfs rw,nosuid,noexec,relatime,size=194288k,mode=755
│ ├─/run/lock tmpfs tmpfs rw,nosuid,nodev,noex:
├─/tmp /dev/sda6 ext4 rw,relatime,data=ordered
├─/boot /dev/sda1 ext4 ro,relatime,data=ordered
├─/srv/overlay /dev/sda3 ext4 rw,relatime,data=ordered
├─/var /dev/sda7 btrfs rw,relatime,space_cache,subvolid=5,subvol=/
│ └─/var overlay overlay rw,relatime,lowerdir=/var,upperdir=/mnt/.var.ofs/.upper,workdir=/mnt/.var.ofs/.workdir
├─/home /dev/sda5 btrfs rw,relatime,space_cache,subvolid=5,subvol=/
└─/mnt/.var.ofs /dev/loop0 ext4 rw,relatime,data=ordered
A tool for looking at partition locaions (sfdisk is a script-oriented tool for partitioning any block device):
$ sfdisk -d /dev/sda
label: dos
label-id: 0xb4e93f8f
device: /dev/sda
unit: sectors
/dev/sda1 : start= 2048, size= 231424, type=83, bootable
/dev/sda2 : start= 233472, size= 3905536, type=83
/dev/sda3 : start= 4139008, size= 3999744, type=83
/dev/sda4 : start= 8140798, size= 23150594, type=5
/dev/sda5 : start= 8140800, size= 1951744, type=83
/dev/sda6 : start= 10094592, size= 1951744, type=83
/dev/sda7 : start= 12048384, size= 19243008, type=83
$ sfdisk -g
/dev/sda: 1947 cylinders, 255 heads, 63 sectors/track
/dev/loop0: 101 cylinders, 255 heads, 63 sectors/track
As a random note, some storage systems perform better when block storage is on 4096 byte boundaries. More info for partitioning linux file systems:
Thomas Krenn:Partition Alignment