디스크가 마운트된 위치를 확인하는 명령이 있나요?

디스크가 마운트된 위치를 확인하는 명령이 있나요?

디스크의 장치 노드를 입력으로 사용하고 해당 디스크가 마운트된 위치(및 마운트 여부)를 알려주는 간단한 명령이 있습니까? 마운트 지점을 자체적으로 가져와서 다른 명령에 전달할 수 있습니까?

나는 최소한의 설치로 Debian Squeeze 라이브 시스템을 작업하고 있습니다(필요하다면 추가 패키지를 설치할 수 있습니다).

답변1

Linux에서는 이제 버전 2.18 findmnt부터 다음 명령을 사용할 수 있습니다 .util-linux

$ findmnt -S /dev/VG_SC/home
TARGET SOURCE                 FSTYPE OPTIONS
/home  /dev/mapper/VG_SC-home ext4   rw,relatime,errors=remount-ro,data=ordered

또는 lsblk(또한 util-linux2.19부터):

$ lsblk /dev/VG_SC/home
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
VG_SC-home 254:2    0  200G  0 lvm  /home

이는 특정 장치(디스크 또는 파티션...)에 마운트된 모든 파일 시스템을 찾는 데에도 유용합니다.

$ lsblk  /dev/sda2
NAME                    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda2                      8:2    0  59.5G  0 part
├─linux-debian64 (dm-1) 252:1    0    15G  0 lvm
└─linux-mint (dm-2)     252:2    0    15G  0 lvm  /

마운트 지점만 얻으려면:

$ findmnt -nr -o target -S /dev/storage/home
/home
$ lsblk -o MOUNTPOINT -nr /dev/storage/home
/home

위의 경우 findmnt장치가 마운트되지 않은 경우 오류 종료 상태가 반환됩니다 lsblk.

그래서:

if mountpoint=$(findmnt -nr -o target -S "$device"); then
  printf '"%s" is mounted on "%s"\n' "$device" "$mountpoint"
else
  printf '"%s" does not appear to be directly mounted\n' "$device"
fi

답변2

Linux에서는 의 커널에서 직접 마운트 지점 정보를 얻을 수 있습니다 /proc/mounts. 프로그램 mount은 유사한 정보를 에 기록합니다 /etc/mtab. 경로와 옵션은 커널에 전달된 /etc/mtab내용을 나타내는 반면, 데이터는 커널 내부에 표시되는 것과 같이 다를 수 있습니다. 항상 최신 상태이지만 부팅 스크립트에서 예상하지 못한 특정 시점에 읽기 전용 인 경우에는 그렇지 않을 수도 있습니다 . 형식은 다음과 유사합니다.mount/proc/mounts/proc/mounts/etc/mtab/etc/etc/fstab.

두 파일 모두 공백으로 구분된 첫 번째 필드에는 장치 경로가 포함되고 두 번째 필드에는 마운트 지점이 포함됩니다.

awk -v needle="$device_path" '$1==needle {print $2}' /proc/mounts

또는 awk가 없는 경우:

grep "^$device_path " /proc/mounts | cut -d ' ' -f 2

기대한 결과를 얻지 못할 수 있는 극단적인 경우가 많이 있습니다. 동일한 장치를 지정 하는 다른 경로를 통해 장치가 마운트된 경우에는 /dev이 방법을 알 수 없습니다. 에서는 /proc/mounts바인드 마운트가 원본과 구별되지 않습니다. 마운트 지점이 다른 마운트 지점을 섀도잉하는 경우 일치하는 항목이 두 개 이상 있을 수 있습니다(이는 일반적이지 않습니다).

/proc/self또는 에는 전역 파일을 모방하는 /proc/$pid프로세스별 파일이 있습니다 . mounts마운트 정보는 다음과 같은 이유로 프로세스마다 다를 수 있습니다.chroot. mountinfo다른 형식을 가지며 추가 정보, 특히 장치 메이저 및 마이너 번호를 포함하는 추가 파일이 있습니다 . 로부터선적 서류 비치:

36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
(1)(2)(3)   (4)   (5)      (6)      (7)   (8) (9)   (10)         (11)

(1) mount ID:  unique identifier of the mount (may be reused after umount)
(2) parent ID:  ID of parent (or of self for the top of the mount tree)
(3) major:minor:  value of st_dev for files on filesystem
(4) root:  root of the mount within the filesystem
(5) mount point:  mount point relative to the process's root
(6) mount options:  per mount options
(7) optional fields:  zero or more fields of the form "tag[:value]"
(8) separator:  marks the end of the optional fields
(9) filesystem type:  name of filesystem of the form "type[.subtype]"
(10) mount source:  filesystem specific information or "none"
(11) super options:  per super block options

따라서 번호로 장치를 찾고 있다면 다음과 같이 할 수 있습니다.

awk -v dev="$major:minor" '$3==dev {print $5}'
awk -v dev="$(stat -L -c %t:%T /dev/block/something)" '$3==dev {print $5}'

답변3

mount인수가 없는 명령은 현재 마운트된 모든 파일 시스템을 나열합니다 . grep원하는 디스크(또는 정보를 읽는 grep /etc/mtab파일 )에 대해 그렇게 할 수 있습니다 .mount

$ grep /dev/sda /etc/mtab
/dev/sda3 /boot ext2 rw,noatime 0 0

답변4

장치 노드가 마운트되었는지 여부를 감지하는 깨끗하고 간단한 방법을 모르겠습니다 /dev. 하지만 이것이 제가 제안할 수 있는 것입니다. 직접 마운트된 장치( /dev/sda1)와 UUID에 의해 마운트된 장치( )를 처리합니다 /dev/disk/by-uuid/aa4e7b08-6547-4b5a-85ad-094e9e1af74f. 장치 이름에 공백이 포함되어 있으면 중단됩니다.

deviceIsMounted()
{
    local DEVICE="$1"
    local MOUNT=$(
        (
            echo "$DEVICE"
            find /dev -type l -lname "*${DEVICE/*\/}" -exec readlink -f {} \; -print |
                xargs -n2 |
                awk -v device="$DEVICE" '$1 == device {print $2}'
        ) |
            xargs -n1 -i{} grep -F {} /proc/mounts
    )
    test -n "$MOUNT" && echo "$MOUNT"
}

deviceIsMounted /dev/sda1 && echo yes              # Mountpoint and status
deviceIsMounted /dev/md2 >/dev/null && echo yes    # Just status

이 함수는 항목을 에서 까지 씁니다 /proc/mounts.표준 출력발견되면 종료 상태도 0으로 설정합니다. 그렇지 않으면 종료 상태는 1입니다.

관련 정보