mount bind解决chroot无法访问目录的问题

mount bind解决chroot无法访问目录的问题

在使用chroot时,在新的根目录下无法访问外面的目录,软链接无法解决这个问题,可以使用mount bind解决。

bind mounts

Since Linux 2.4.0 it is possible to remount part of the file hierarchy somewhere else. The call is
mount --bind olddir newdir
or shortoption
mount -B olddir newdir
or fstab entry is:
/olddir /newdir none bind

shared subtrees

Supported operations:
mount --make-shared mountpoint
mount --make-slave mountpoint
mount --make-private mountpoint
mount --make-unbindable mountpoint

The following commands allows one to recursively change the type of all the mounts under a given mountpoint.
mount --make-rshared mountpoint
mount --make-rslave mountpoint
mount --make-rprivate mountpoint
mount --make-runbindable mountpoint

示例

假设chroot到/var/chroot下面,并想访问外面的/proc目录,执行如下命令:

mount --make-rshared /proc
mount --rbind /proc /var/chroot/proc
mount --make-rslave /var/chroot/proc

为了重启后也生效,可以写到/etc/rc.d/rc.local里面,如下:

sed -i '/mount.*\/proc/d' /etc/rc.d/rc.local
echo "mount --make-rshared /proc" >> /etc/rc.d/rc.local
echo "mount --rbind /proc /var/chroot/proc" >> /etc/rc.d/rc.local
echo "mount --make-rslave /var/chroot/proc" >> /etc/rc.d/rc.local
posted @ 2023-10-25 14:40  IUNI_JM  阅读(204)  评论(0)    收藏  举报