有没有一种方便的方法来获取有关所有btrfs文件系统子卷的信息,而无需求助于 C,即在POSIX shell 中?
/sys/fs/btrfs只包含文件系统的信息,子卷上没有任何信息,所以目前我最终将所有文件系统安装在一个临时文件夹中,用 扫描它们btrfs subvol list,然后解析结果输出。不用说,这相当丑陋。
这是一个3.16.x内核和btrfs-progs v3.14.1(来自股票 Ubuntu 14.10)。
下面是我目前使用的(丑陋的)脚本。我知道我可以使用纯 C 获得我需要的信息,这可能是我最终要做的,但我想知道是否有更简单、更优雅的方法。
#!/bin/bash
for i in /sys/fs/btrfs/*[!features]; do
device="/dev/$(basename $i/devices/*)"
mountpoint=/var/lib/btrfs/tmp/mnt/$(basename "$i")
[ -d "$mountpoint" ] || mkdir "$mountpoint"
grep -qs $mountpoint /proc/mounts
[ $? -ne 0 ] && mount -v "$device" "$mountpoint"
while read -r subvol; do
# whatever you want
done < <(btrfs subvolume list "$mountpoint")
umount $mountpoint
rmdir $mountpoint
done
Run Code Online (Sandbox Code Playgroud)