Ubuntu Overlay File System Usage Guide
OverlayFS is a union file system that combines multiple directories (layers) into a single virtual directory, enabling lightweight file system modifications without altering the underlying data. It is commonly used for containerized environments (e.g., Docker) and sandboxing. Below are the essential steps to set up and use OverlayFS on Ubuntu.
Before configuring OverlayFS, ensure your system meets the following requirements:
uname -r
sudo apt-get update
sudo apt-get install overlayfs aufs-tools
OverlayFS requires three core directories and one optional directory:
Create these directories using:
mkdir -p upper lower work merged
The lowerdir
acts as the base layer. You can populate it with files to simulate a read-only file system:
echo "Hello from lower" > lower/hello.txt
Use the mount
command to combine the layers into a single virtual directory. The syntax is:
sudo mount -t overlay overlay -o lowerdir=lower,upperdir=upper,workdir=work merged
lowerdir=lower
: Path to the base layer.upperdir=upper
: Path to the read-write layer.workdir=work
: Path to the work directory.merged
: Mount point for the virtual file system.After mounting, the merged
directory will contain files from lowerdir
. Any changes made to merged
will be stored in upperdir
.
You can interact with the merged
directory like a normal file system:
cat merged/hello.txt # Output: Hello from lower
upperdir
:echo "Hello from upper" > merged/hello.txt
cat merged/hello.txt # Output: Hello from upper
upperdir
:touch merged/newfile.txt
ls merged/ # Output: hello.txt newfile.txt
When you’re done using the overlay, unmount it to detach the virtual file system:
sudo umount merged
To automatically mount OverlayFS on system boot, add an entry to /etc/fstab
:
sudo nano /etc/fstab
Insert the following line (replace paths as needed):
overlay /merged overlay defaults,lowerdir=/lower,upperdir=/upper,workdir=/work 0 0
Save the file and remount all file systems to apply changes:
sudo mount -a
sudo
) for mounting/unmounting and modifying directories.lowerdir
(the base layer). Use upperdir
for modifications to prevent corruption.upper
, work
, and merged
directories. The lowerdir
remains unchanged.By following these steps, you can effectively use OverlayFS on Ubuntu for lightweight file system virtualization, ideal for containers or testing environments.