Linux系统的目录绑定配置

一.源目录与目标目录情况

1、源目录文件及inode

[root@docker data]# pwd
/data
[root@docker data]# ll
total 140648
-rw-r--r-- 1 root root       205 Dec 11 13:59 backdata.tar.gz
-rw-r--r-- 1 root root         0 Dec 11 13:54 file1
-rw-r--r-- 1 root root         0 Dec 11 13:54 file10
-rw-r--r-- 1 root root         0 Dec 11 13:54 file2
-rw-r--r-- 1 root root         0 Dec 11 13:54 file3
-rw-r--r-- 1 root root         0 Dec 11 13:54 file4
-rw-r--r-- 1 root root         0 Dec 11 13:54 file5
-rw-r--r-- 1 root root         0 Dec 11 13:54 file6
-rw-r--r-- 1 root root         0 Dec 11 13:54 file7
-rw-r--r-- 1 root root         0 Dec 11 13:54 file8
-rw-r--r-- 1 root root         0 Dec 11 13:54 file9
-rw------- 1 root root 144015872 Dec 11 14:11 web01.tar
[root@docker data]# ls -lid /data
2401630 drwxr-xr-x. 2 root root 177 Dec 11 14:11 /data


2、目标目录的文件及inode

[root@docker backup]# pwd
/backup
[root@docker backup]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 17 17:00 test1
-rw-r--r-- 1 root root 0 Dec 17 17:00 test2
[root@docker backup]# ls -lid /backup/
38821596 drwxr-xr-x 2 root root 32 Dec 17 17:00 /backup/
[root@docker backup]# 

二、将两个目录进行绑定

1.mount --bind进行绑定

将test1挂载到test2上,inode号都变为test1的inode
root@docker backup]# mount --bind /data/ /backup/

2.检查绑定后的两个目录情况

[
[root@docker backup]# ls /data/
backdata.tar.gz  file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  web01.tar
[root@docker backup]# ls /backup/
backdata.tar.gz  file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  web01.tar
[root@docker backup]# lls -lid /data/ /backup/
bash: lls: command not found...
Similar command is: 'ls'
[root@docker backup]# ls -lid /data/ /backup/
2401630 drwxr-xr-x. 2 root root 177 Dec 11 14:11 /backup/
2401630 drwxr-xr-x. 2 root root 177 Dec 11 14:11 /data/

3.mount --bind绑定注意事项

#注意,mount --bind重启后会丢失挂载,包括/dev/shm目录重启后也会清空,要解决该问题,可以添加开机自启脚本
#添加到/etc/rc.local文件中

三、解挂载

1.对两个目录进行解挂载

[root@docker /]# umount /backup

2.查看解挂载后的情况

[root@docker ~]# ls /data
backdata.tar.gz  file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  web01.tar
[root@docker ~]# ls /backup/
test1  test2
[root@docker ~]# ls -lid /data /backup/
38821596 drwxr-xr-x  2 root root  32 Dec 17 17:00 /backup/
 2401630 drwxr-xr-x. 2 root root 177 Dec 11 14:11 /data

四、应用场景

1.场景描述

在固件开发过程中常常遇到这样的情况:为测试某个新功能,必需修改某个系统文件。而这个文件在只读文件系统上(总不能为一个小小的测试就重刷固件吧),或者是虽然文件可写,但是自己对这个改动没有把握,不愿意直接修改。这时候mount --bind就是你的好帮手。 

2.测试系统文件

[root@docker ~]# mount -o ro /dev/sdb1 /soft
[root@docker ~]# cd /soft/
[root@docker soft]# touch file1
touch: cannot touch 'file1': Read-only file system
[root@docker soft]# 

[root@docker soft]# mount --bind /test/ /soft/
[root@docker soft]# cd /test
[root@docker test]# touch test{1..10}.txt
[root@docker test]# ls
test10.txt  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test6.txt  test7.txt  test8.txt  test9.txt
[root@docker test]# ls /soft/
test10.txt  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test6.txt  test7.txt  test8.txt  test9.txt
[root@docker test]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖有缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值