最後更新: 2021-08-25
目錄
Container 的 mount 設定
方法
- 方式1: 透過另一個設定檔去設定 mount 什麼(lxc.mount=/path/to/fstab)
- 方式2: 在同一個 config file 內設定(lxc.mount.entry=...)
方式1: 透過另一個設定檔去設定 mount 什麼
lxc.mount = /var/lib/lxc/mx5/fstab
/var/lib/lxc/mx5/fstab:
proc proc proc nodev,noexec,nosuid 0 0 sysfs sys sysfs defaults 0 0
方式2: 在同一個 config file 內設定
lxc.mount.entry = proc /var/lib/lxc/centos6/rootfs/proc proc nodev,noexec,nosuid 0 0 lxc.mount.entry = sysfs /var/lib/lxc/centos6/rootfs/sys sysfs defaults,ro 0 0
relative destination path
* relative path is taken to be relative to the mounted container root
i.e. Host 的 /home/data mount 到 Container 的 /home/data
lxc.mount.entry = /home/data home/data none bind 0 0
i.e. Container 的 /dev/null mount 到 Container 的 /proc/kcore
dev/null proc/kcore none bind,relative 0 0
File / Folder Owner
If the host's user does not exist in the container, the container will still be mounted,
but with nobody:nogroup as the owner
Additional mount options
- "optional" # don't fail if mount does not work
- "create=dir" # to create dir (or file) when the point will be mounted.
- "create=file"
Bind mount
* container 有自己的 mounts namespace => can’t just mount it
=> 在 vps start 前已 mount 入去才有效
# 建立 folder
mkdir /backup/shared
# turn the directory into a "bind mount"
mount --bind /backup/shared /backup/shared
Remark: shortoption
mount -B olddir newdir
行 cmd mount 時會見到
/backup/shared on /backup/shared type none (rw,bind)
# 令它不被任何 container mount
mount --make-unbindable /shared
# 令其可以被 mount
mount --make-slave /backup/shared
Remark
The shared subtrees operations:
* private - does not forward or receive propagation (Default)
* slave - receives propagation from its master
* shared - provides ability to create mirrors of that mount
(any mount within the original mount point is reflected in it, and vice versa.)
* unbindable - mount is a private mount which cannot be cloned through a bind operation
Checking
/backup/shared on /backup/shared type none (rw,bind,make-slave)
fstab
/backup/shared /backup/shared none bind 0 0
vps config
mkdir /lxc/sshgw/rootfs/mnt/shared/
方式 1
/var/lib/lxc/sshgw/fstab
/dev/sda1 on /mnt/shared type ext4 (rw,noatime,user_xattr,barrier=1,data=ordered)
方式 2
/lxc/sshgw/config
# In the lxc.mount.entry, it’s generally recommented now to use the relative pathname for the target.
lxc.mount.entry = /backup/shared mnt/shared none defaults,noatime,bind 0 0
--rbind (-R)
* The --bind mount call attaches only a single filesystem, not possible submounts.
--rbind => The entire file hierarchy including submounts is attached
mount --rbind olddir newdir
Mount Example / Test
目錄
- slave mount example
- make the mount at /cdrom shared
- unbindable usage
mkdir MasterFolder SlaveFolder
mkdir MasterFolder/a MasterFolder/b
mount --bind /root/MasterFolder /root/SlaveFolder
mount --make-slave /root/SlaveFolder
mount /dev/sde1 /root/SlaveFolder/b
ls /root/MasterFolder/b
ls /root/SlaveFolder/b
s1 s2 s3
make the mount at /cdrom shared
mount --bind /cdrom /cdrom
mount --make-shared /cdrom
So when a CD is inserted and mounted at /cdrom that mount gets
propagated to the other mount at /cdrom in all the other clone namespaces.
unbindable usage
mount --bind /root/test1 /root/test1
mount --make-unbindable /root/test1
mount --bind /root/test1/ /root/test2
mount: wrong fs type, bad option, bad superblock on /root/test1, missing codepage or helper program, or other error In some cases useful info is found in syslog - try dmesg | tail or so
Q1. Why is bind mount needed? How is it different from symbolic links?
symbolic links can get stale if the destination mount gets unmounted or moved.
Bind mounts continue to exist even if the other mount is unmounted or moved.
Q2. Why is unbindable mount needed?
root / \ tmp usr
mount --bind /root/tmp /root/tmp
mount --make-rshared /root
mount --make-unbindable /root/tmp
mkdir -p /tmp/m1
mount --rbind /root /tmp/m1
mkdir -p /tmp/m2
mount --rbind /root /tmp/m2
mkdir -p /tmp/m3
mount --rbind /root /tmp/m3
root / \ tmp usr / \ \ m1 m2 m3 / \ / \ / \ tmp usr tmp usr tmp usr
fuse
Doc
https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt