편집하다

편집하다

btrfs 하위 볼륨에 파일을 기록하면 0. btrfs 하위 볼륨인지 미리 알지 못한 채 이 장치의 마운트 지점을 찾는 안정적인 방법이 있습니까?

예를 들어 저는 Python으로 이 작업을 수행하고 싶습니다.

>>> st = os.stat('a file')
>>> os.major(st.st_dev)
0
>>> os.minor(st.st_dev)
38
>>> os.path.exists('/sys/dev/block/0:38')
False
>>> magic_method_that_gets_mount_point(0, 38)
'/home'
>>> magic_method_that_gets_mount_point(8, 1)
'/boot'

(내 경우에는 sda1( /sys/dev/block/8:1)가 에 마운트되어 /boot있으며 /home의 btrfs 하위 볼륨입니다 sda2.)

편집하다

파일 경로를 몰라도 이 작업을 수행할 수 있어야 합니다. 위에서는 os.stat예제로 사용했지만 정보는 실제로 ioctl다음을 반환하는 루프 장치 호출에서 검색됩니다.

struct loop_info64 {
    uint64_t    lo_device;
    uint64_t    lo_inode;
    uint64_t    lo_rdevice;
    uint64_t    lo_offset;
    uint64_t    lo_sizelimit;
    uint32_t    lo_number;
    uint32_t    lo_encrypt_type;
    uint32_t    lo_encrypt_key_size;
    uint32_t    lo_flags;
    uint8_t     lo_file_name[64];
    uint8_t     lo_crypt_name[64];
    uint8_t     lo_encrypt_key[32];
    uint64_t    lo_init[2];
};

필드가 있지만 lo_file_name최대 길이는 63자이므로 열려 있다고 믿을 수 없습니다. 나는 또한 알고 있지만 /sys/block/loopX/loop/backing_file이것은 Linux >= 2.6.37에서만 사용할 수 있으며 내 코드는 CentOS 6(2.6.32)에서 실행되어야 합니다.

편집 #2

여기서 나의 궁극적인 목표는 루프 장치의 백업 파일을 안정적으로 찾는 것입니다. 심지어 util-linux도 2.6.37 미만의 커널에서는 이 작업을 수행하지 않습니다. 예:

> dd if=/dev/urandom bs=4096 count=256 of=/root/a_really_really_really_really_really_really_really_long_file_name.img
256+0 records in
256+0 records out
1048576 bytes (1.0 MB) copied, 0.137397 s, 7.6 MB/s
> LD=`losetup -f --show /root/a_really_really_really_really_really_really_really_long_file_name.img`
> losetup $LD
/dev/loop1: [fd00]:922372 (/root/a_really_really_really_really_really_really_really_long_*)

파일 이름이 잘렸습니다. 이는 util-linux가 필드 loop_info64에 63자 제한이 있는 구조체를 사용하기 때문입니다 lo_file_name.

내가 확실하게 얻을 수 있는 것은 장치 ID와 백업 파일의 inode 번호입니다. 백업 파일이 btrfs 하위 볼륨에 저장되어 있기 때문에 이것이 벽에 부딪힌 부분입니다.

답변1

나는 Gnu core-utils 소스 코드, 특히 명령을 살펴보았습니다 df.

장치 ID가 변경될 때까지 계층 구조를 반복적으로 내려갑니다. ID가 변경되는 지점이 마운트 지점입니다.

~/home/me/a-dir/another-dir방금 현재 있는 파일 시스템의 마운트 지점을 찾으려고 했습니다 . 저는 다음과 같이 했습니다.

stat . #noting device IDs
while id not changes and root not hit
do
  cd ..
  stat .
done
if root not hit
then
  cd -
fi

(이 코드는 가상 bash이며 모든 조건부 및 루프는 수동으로 수행됩니다. 개념을 증명하기 위한 것입니다. 독자를 위해 연습할 때 프로그래밍과 번역을 Python에 맡기겠습니다.)

관련 정보