Background
What you, indeed, want is the VENDOR,MODEL
output formatting options but, with a twist(see workaround section below):
lsblk -o FSTYPE,UUID,VENDOR,MODEL
You might as well want to print a list and stop printing headings(for better output parsing) by adding the options -ln
and exclude loop devices by adding the option -e 7
... See cat /proc/devices
under Block devices:
for other device types you might want to exclude and separate them by commas like -e 7,11,...
.
See more options with lsblk -h
:
$ lsblk -h
Usage:
lsblk [options] [<device> ...]
List information about block devices.
Options:
-D, --discard print discard capabilities
-E, --dedup <column> de-duplicate output by <column>
-I, --include <list> show only devices with specified major numbers
-J, --json use JSON output format
-O, --output-all output all columns
-P, --pairs use key="value" output format
-S, --scsi output info about SCSI devices
-T, --tree[=<column>] use tree format output
-a, --all print all devices
-b, --bytes print SIZE in bytes rather than in human readable format
-d, --nodeps don't print slaves or holders
-e, --exclude <list> exclude devices by major number (default: RAM disks)
-f, --fs output info about filesystems
-i, --ascii use ascii characters only
-l, --list use list format output
-M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)
-m, --perms output info about permissions
-n, --noheadings don't print headings
-o, --output <list> output columns
-p, --paths print complete device path
-r, --raw use raw output format
-s, --inverse inverse dependencies
-t, --topology output info about topology
-w, --width <num> specifies output width as number of characters
-x, --sort <column> sort output by <column>
-z, --zoned print zone model
--sysroot <dir> use specified directory as system root
-h, --help display this help
-V, --version display version
Available output columns:
NAME device name
KNAME internal kernel device name
PATH path to the device node
MAJ:MIN major:minor device number
FSAVAIL filesystem size available
FSSIZE filesystem size
FSTYPE filesystem type
FSUSED filesystem size used
FSUSE% filesystem use percentage
FSROOTS mounted filesystem roots
FSVER filesystem version
MOUNTPOINT where the device is mounted
MOUNTPOINTS all locations where device is mounted
LABEL filesystem LABEL
UUID filesystem UUID
PTUUID partition table identifier (usually UUID)
PTTYPE partition table type
PARTTYPE partition type code or UUID
PARTTYPENAME partition type name
PARTLABEL partition LABEL
PARTUUID partition UUID
PARTFLAGS partition flags
RA read-ahead of the device
RO read-only device
RM removable device
HOTPLUG removable or hotplug device (usb, pcmcia, ...)
MODEL device identifier
SERIAL disk serial number
SIZE size of the device
STATE state of the device
OWNER user name
GROUP group name
MODE device node permissions
ALIGNMENT alignment offset
MIN-IO minimum I/O size
OPT-IO optimal I/O size
PHY-SEC physical sector size
LOG-SEC logical sector size
ROTA rotational device
SCHED I/O scheduler name
RQ-SIZE request queue size
TYPE device type
DISC-ALN discard alignment offset
DISC-GRAN discard granularity
DISC-MAX discard max bytes
DISC-ZERO discard zeroes data
WSAME write same max bytes
WWN unique storage identifier
RAND adds randomness
PKNAME internal parent kernel device name
HCTL Host:Channel:Target:Lun for SCSI
TRAN device transport type
SUBSYSTEMS de-duplicated chain of subsystems
REV device revision
VENDOR device vendor
ZONED zone model
DAX dax-capable device
For more details see lsblk(8).
Workaround
Notice that lsblk
will list VENDOR
and MODEL
for disks(i.e. block devices) and not partitions ... but you can get around that by implementing something like this(just a template ... modify it to your needs):
#!/bin/bash
for f in $(/bin/lsblk -nlo TYPE,NAME | /bin/awk '/part/ {print $2}') # Get partitions only
do
f="/dev/$f" # Set full path to partition
uuid=$(/bin/lsblk -nlo PARTUUID "$f") # Get partition UUID
fstype=$(/bin/lsblk -nlo FSTYPE "$f") # Get partition file system type
d=$(/bin/lsblk -nldo PKNAME "$f") # Get partition block device
d="/dev/$d" # Set full path to block device
vendor=$(/bin/lsblk -nlo VENDOR "$d") # Get vendor of the block device
model=$(/bin/lsblk -nlo MODEL "$d") # Get model of the block device
echo "$f: PARTUUID=$uuid FSTYPE=$fstype VENDOR=$vendor MODEL=$model" # Print partition name, partition uuid, partition file system type, vendor, model
done
Or straight forward(by passing the partition PARTUUID(NOT DISK UUID) to a function):
model_by_partuuid () {
p=$(/bin/readlink -f /dev/disk/by-partuuid/"$1") # Get partition device /path/name by its PARTUUID
b=$(/bin/lsblk -nldo PKNAME "$p") # Get partition block device
vendor=$(/bin/lsblk -nlo VENDOR /dev/"$b") # Get vendor of the block device
model=$(/bin/lsblk -nlo MODEL /dev/"$b") # Get model of the block device
echo "VENDOR=$vendor MODEL=$model" # Print partition vendor, model
}
Use this function like so:
model_by_partuuid "partition-uuid-here"
So, your intended script might look something like this:
#/bin/bash
ext4_main_mountpoint="/mnt/ext4/" # Specify EXT4 filesystems main mount point
ntfs_main_mountpoint="/mnt/ntfs/" # Specify NTFS filesystems main mount point
"echo" in-front of "mkdir" and "mount" is for dry-run(simulation) ... You need to remove "echo" once you're done testing for the script to actually create mount points and mount partitions.
for f in $(/bin/lsblk -nlo TYPE,NAME | /bin/awk '/part/ {print $2}') # Get partitions only
do
f="/dev/$f" # Set full path to partition
uuid=$(/bin/lsblk -nlo PARTUUID "$f") # Get partition UUID
uuidf4="${uuid:0:4}" # Get the first four characters of the partition UUID(for reference ... not used below)
uuidl4="${uuid: -4}" # Get the last four characters of the partition UUID(for reference ... not used below)
fstype=$(/bin/lsblk -nlo FSTYPE "$f") # Get partition file system type
d=$(/bin/lsblk -nldo PKNAME "$f") # Get partition block device
d="/dev/$d" # Set full path to block device
vendor=$(/bin/lsblk -nlo VENDOR "$d") # Get vendor of the block device
vendor=$(/bin/echo "$vendor" | /bin/tr -d '[:space:]') # Trim whitespace
model=$(/bin/lsblk -nlo MODEL "$d") # Get model of the block device
model=$(/bin/echo "$model" | /bin/tr -d '[:space:]') # Trim whitespace
if [ "$fstype" == "ext4" ]; then # Check if the filesystem on the partition is EXT4
echo mkdir -p "$ext4_main_mountpoint$vendor-$model-$uuid" # Create the mount point if it doesn't exist
echo mount -U "$uuid" "$ext4_main_mountpoint$vendor-$model-$uuid" # Mount the partition
elif [ "$fstype" == "ntfs" ]; then # Check if the filesystem on the partition is NTFS
echo mkdir -p "$ntfs_main_mountpoint$vendor-$model-$uuid" # Create the mount point if it doesn't exist
echo mount -t "$fstype" -U "$uuid" "$ntfs_main_mountpoint$vendor-$model-$uuid" # Mount the partition
else
echo "No rule specified for $f: PARTUUID=$uuid FSTYPE=$fstype VENDOR=$vendor MODEL=$model" # For other filesystem types(not specified above), print partition name, partition uuid, partition file system type, vendor, model
fi
done
Alternatives
Furthermore, alternative methods of identifying your disks partitions and mounting them automatically to a certain mount point are described in this answer.
lsblk -o name,fstype,size,fsused,label,partlabel,mountpoint
I have my data on different drives. Main drive is just data, copies are in m2_data (my m2 sata drive), data_nvme (my nmve drive), data256a (one of my flash drives). – oldfred Jul 11 '22 at 15:08