기본 마운트 지점 경로의 콘텐츠에 액세스

기본 마운트 지점 경로의 콘텐츠에 액세스

/dev/sda1루트에 파티션이 마운트되어 있고 루트에 파티션이 마운트되어 있는 경우 /먼저 마운트를 해제하지 않고 해당 파티션의 원래 내용에 액세스할 수 있는 방법이 있습니까?/dev/sdb1/var/varsda1sdb1

답변1

Linux에서는 바인드 마운트를 사용하여 파일 계층 구조의 일부를 다른 위치에 다시 마운트할 수 있습니다. 예를 들어 다음과 같이 할 수 있습니다.

# mkdir /mnt/bindroot
# mount --bind / /mnt/bindroot

이 시점에서는 /mnt/bindroot루트 파일 시스템의 내용이 포함되지만 다양한 디렉터리에 다른 파일 시스템은 마운트되지 않습니다.

# ls /home
user1 lost+found

# ls /mnt/bindroot/home
<whatever was in /home before a filesystem was mounted over it>

FreeBSD의 경우 nullfs마운트를 사용하여 비슷한 작업을 수행 할 수 있습니다 mount_nullfs. 을 참조하세요.

답변2

Linux에서는 chdir 또는 기본 디렉토리를 열고(마운트에 의해 숨겨지기 전) 그냥 대기하는 프로세스만 있으면 바인드 마운트 및 루트 권한 없이 이 작업을 수행할 수 있습니다. 그런 다음 다른 프로세스는 /proc/<pid>/cwd또는 를 통해 디렉토리에 액세스 할 수 있습니다 /proc/<pid>/fd/<dir_fd>.

chdir 예:

# mkdir dir; touch dir/file                   # create a sample dir and file
# (cd dir; while sleep 3600; do :; done) &    # start a bg process with its pwd being dir
[1] 3734
# mount -t tmpfs tmpfs dir                    # mount a tmpfs over dir which will hide its previous content
# ls dir
# ls /proc/3734/cwd                           # you can still access the old dir via /proc/<pid>/cwd 
file

디렉토리 열기의 예:

# mkdir -p dir1; touch dir1/file
# exec 9<dir1
# mount -t tmpfs tmpfs dir1
# ls dir1
# ls /proc/self/fd/9
file

나중에 마운트된 디렉터리를 사용하여 이 작업을 수행하려면 전용 네임스페이스를 만들고 네임스페이스 내에서 디렉터리를 마운트 해제하면 됩니다.

이 경우 /proc/<pid>/root/<path_to_dir>파일을 열거나 경로를 지정하지 않고도 외부에서 액세스할 수도 있습니다.

mkdir -p dir; touch dir/file
mount -t tmpfs tmpfs dir

unshare -m sh -c 'umount dir; while sleep 1000; do :; done' &
sleep .1
ls "/proc/$!/root/$PWD/dir"  # will show 'file'

/proc/<pid>/root맨페이지에서 추가 마법에 대한 자세한 내용을 읽을 수 있습니다 proc(5). 프로세스가 종료된 후에도 다른 곳에 바인드 마운트하여 /proc/<pid>/ns/mnt프로세스의 네임스페이스를 활성 상태로 유지할 수 있습니다. 그런 다음 re-entry 를 사용할 수 있습니다 nsenter(1).

답변3

참고: 리뷰를 놓친 분들을 위해.

sudo mount --bind original_location backup_link작동합니다. oldlocation무언가를 설치하고 그 위에 그림자를 설치한 후 아래 위치에 액세스할 수 있습니다.

하지만

  1. sudo mount --bind달려야 한다는 걸 깨달았어뒤쪽에 mount something original_location, 미리는 아닙니다.
  2. 섀도우가 마운트된 이후이므로 한 섀도우보다 높은 디렉터리를 마운트해야 합니다.sudo mount --bind parent_of_original_location backup_link

관련 정보