I'm trying to port some scripts from FreeBSD to Linux so that I can share
some management scripts with non-BSD platforms.
One script in particular uses
mdconfig
, and I haven't yet found any Linux tool that matches its capabilities.
The script is a generalized ZFS pool constructor that can work with either physical or virtual block devices, creating matching GPT partition tables on each device according to a pre-specified schema, naming the partitions by joining the partition type and the device serial number. So for serial numbers AAAA1111
WXYZJ97
and 7JN52XH19
and partition types EFI
, SWAP
and ZFS
, the three disks will be partitioned identically with partitions named:
EFI-AAAA1111
SWAP-AAAA1111
ZFS-AAAA1111
EFI-WXYZJ97
SWAP-WXYZJ97
ZFS-WXYZJ97
EFI-7JN52XH19
SWAP-7JN52XH19
ZFS-7JN52XH19
and the Zpool will be built from the ZFS-* partitions, depending on how the user lays out the config file that specifies such things.
mdconfig
creates a file-, memory- or swap-backed (or even
bit-bucket-backed) block device, which is transparently indistinguishable
from a real hard drive,\* or most any other block device (swap
partition, tape drive, etc.), and tools based on FreeBSD's GEOM framework interface seamlessly with either physical or virtual block devices, to query device properties like size, sector count, sector size, device serial number, etc.
# truncate -s10G disk.img
# mdconfig disk.img
md0
# gpart create -s gpt /dev/md0
md0 created
# gpart show -l /dev/md0
=> 40 20971440 md0 GPT (10G)
40 20971440 - free - (10G)
# diskinfo -v /dev/md0
/dev/md0
512 # sectorsize
10737418240 # mediasize in bytes (10G)
20971520 # mediasize in sectors
0 # stripesize
0 # stripeoffset
MD-DEV94492983848663588-INO6048 # Disk ident.
Yes # TRIM/UNMAP support
Unknown # Rotation rate in RPM
When testing and experimenting, it's frequently convenient to be able to utilize virtual block devices just as though they were physical devices, without requiring a lot of what's and if's and special cases in my code.
The serial number ("Disk ident.") of the drive -- whether physical or virtual -- is pivotal in the naming of the GPT partitions, and the GPT partition names in turn become part of the ZFS vdev structure. Then if a device fails, I can quickly identify it because the serial number is printed directly on the drive label (and sometimes hand-labeled on the drive tray as well, for hot-swappable backplanes). While device failure isn't a common scenario with virtual devices, the point is to have flexible tools with a unified interface that don't need to be rewritten when switching from using virtual devices to physical, or indeed, a mix.
So far I have been experimenting with losetup
on
Linux. sfdisk
can understand the virtual devices it creates, but hdparm
cannot, nor can sdparm
. hdparm
is the primary tool I'm aware of that can report a device's serial number on Linux.
# hdparm /dev/loop1
/dev/loop1:
readonly = 0 (off)
readahead = 256 (on)
HDIO_GETGEO failed: Inappropriate ioctl for device
# sdparm /dev/loop1
unable to access /dev/loop1, ATA disk?
Lacking access to the serial number, I've been relying on a rather ugly hack to use the GPT UUID:
# $1 is device, what's its serial number?
if freebsd
then # Easy:
SN="$(
diskinfo -v "$1" |
awk '/Disk ident\./ {print $1; exit}'
)"
else # Not so easy:
partprobe "$1" >/dev/null 2>&1
udevadm="$(udevadm info "$1")"
diskinfo="$(
egrep " ID_SERIAL_SHORT=" \*: By that I mean that, although strictly speaking, one *can* distinguish
between the two, I haven't found any FreeBSD tools that work on real drives but
break when used on virtual drives.
Asked by Jim L.
(8745 rep)
Aug 27, 2024, 06:30 AM
Last activity: Aug 27, 2024, 07:30 PM
Last activity: Aug 27, 2024, 07:30 PM