How to merge two directories in overlay filesystem?

How to merge two directories in overlay filesystem?

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.

How is readdir handled in an OverlayFS file?

Readdir on directories that are not merged is simply handled by the underlying directory (upper or lower). When renaming a directory that is on the lower layer or merged (i.e. the directory was not created on the upper layer to start with) overlayfs can handle it in two different ways:

How does an overlay filesystem support RM and rmdir?

In order to support rm and rmdir without changing the lower filesystem, an overlay filesystem needs to record in the upper filesystem that files have been removed. This is done using whiteouts and opaque directories (non-directories are always opaque). A whiteout is created as a character device with 0/0 device number.

How is a folder hidden in an overlay filesystem?

If a given name appears in both upper and lower filesystems and refers to a non-directory in either, then the lower object is hidden – the name refers only to the upper object. Where both upper and lower objects are directories, a merged directory is formed.

How does the overlay filesystem work in Linux?

In case the underlying inode number does overflow into the high xino bits, overlay filesystem will fall back to the non xino behavior for that inode. The “xino” feature can be enabled with the “-o xino=on” overlay mount option.

How is a file copied from the lower filesystem to the upper?

When a file in the lower filesystem is accessed in a way the requires write-access, such as opening for write access, changing some metadata etc., the file is first copied from the lower filesystem to the upper filesystem (copy_up). Note that creating a hard-link also requires copy_up, though of course creation of a symlink does not.