我正在试图找到一个基于UUID的硬盘制造/模型,我知道这个UUID是命令中的硬盘驱动器。
我尝试使用lsblk -o FSTYPE,MOUNTPOINT,UUID,MODEL,SIZE
列出所有硬盘驱动器,然后将它们存储在一个数组中,以找到UUID及其模型。然而,挂载硬盘的UUID从来不包含硬盘驱动器的模型信息。
它只列出了/sda的根位置上的模型hdd。
我正在编写一个bash脚本来存储UUID、hdd的制造/模型以及文件系统类型,这样我就可以自动挂载在文件夹前缀/someLocation/${model}-${UUID-first-4-char}
上具有有效文件系统类型的hdd。
下面创建两个数组,一个用于uuid,另一个用于文件类型。
#create array
fsTypeArray=()
uuidArray=()
mapfile -t fsTypeArray < <(lsblk -o FSTYPE,MOUNTPOINT,UUID | awk 'NF==2 {print $1}')
mapfile -t uuidArray < <(lsblk -o FSTYPE,MOUNTPOINT,UUID | awk 'NF==2 {print $2}')
下面是安装ext4或ntfs的if语句
index=0
for i in "${fsTypeArray[@]}"
do
if [ $i == ntfs ]
then
echo "mountNtfsDrive"
currentDate="date +%Y%m%d%H%M%S"
mountNtfsDrive "${uuidArray[$index]}" `$currentDate`
elif [ $i == ext4 ]
then
echo "mount ext4"
currentDate="date +%Y%m%d%H%M%S"
mountExt4Drive "${uuidArray[$index]}" `$currentDate`
fi
echo index
let "index++"
sleep 1s
我想替换和删除帽子currentDate基本上${MODEL/MAKE(前4个字符)}。
currentDate基本上使用当前日期和时间作为文件夹名,但我希望它始终在该hdd的UUID和硬盘make/型号所特有的文件夹上创建/挂载。
eg;mount UUID=1234567 /media/ext/western-digital-1234
下面是安装硬盘的两个功能:
# Mount a ext4 drive
function mountExt4Drive {
uuid=$1
location=$2
mkdir -pv "/home/$location"
sleep 3s
echo "mounting ext4 hdd $uuid in $location"
mount UUID=$uuid $location
}
# Mount NTFS drive
function mountNtfsDrive {
echo "mounting ntfs $1 in $2"
mkdir -pv "/home/$2"
sleep 3s
mount -t ntfs-3g "UUID=$1" "/home/$2"
}
edit:建议的帖子不符合我的要求,因为它找不到基于UUID的硬盘驱动器,它与USB连接的存储设备有关
发布于 2022-07-11 13:04:15
实际上,您需要的是VENDOR,MODEL
输出格式设置选项,但是,有一点需要改进(请参阅下面的部分):
lsblk -o FSTYPE,UUID,VENDOR,MODEL
您可能希望通过添加选项print 和stop打印标题(以获得更好的输出解析)来添加选项-ln
和exclude循环设备.有关其他设备类型,请参见cat /proc/devices
下面的Block devices:
,您可能希望用逗号(如-e 7,11,...
)将它们排除和分隔。
请参阅有关lsblk -h
的更多选项:
$ lsblk -h
Usage:
lsblk [options] [ ...]
List information about block devices.
Options:
-D, --discard print discard capabilities
-E, --dedup de-duplicate output by
-I, --include 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[=] 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 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 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 specifies output width as number of characters
-x, --sort sort output by
-z, --zoned print zone model
--sysroot 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).解决方案注意 lsblk将列出磁盘(即块设备)的VENDOR和MODEL,而不是分区.但你可以通过实现这样的东西(只是一个模板.根据您的需要修改):#!/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或者直接向前(将partition PARTUUID__(不是磁盘UUID)传递给函数):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
}使用此函数如下:model_by_partuuid "partition-uuid-here"因此,您的预期脚本可能如下所示:#/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
doneAlternatives此外,alternative方法用于标识磁盘分区并自动将其挂载到某个挂载点,这些方法在这个答案中都有描述。
发布于 2022-07-11 18:54:44
我以前做过类似的事情,修改过它,并认为下面的least脚本可以做您想做的事情,或者至少类似于您想要的。应该可以修改脚本并将其包含在脚本中。
#!/bin/bash
inversvid="\0033[7m"
resetvid="\0033[0m"
function lister {
sdx=$(<<< "$1" sed 's/[0-9]*$//')
#echo "sdx=$sdx"
nvmex=$(<<< "$sdx" grep -o '.*[0-9]')
#echo "nvmex=$nvmex"
if [ "$nvmex" != "" ]
then
drive="$nvmex"
else
drive="$sdx"
fi
#echo "drive=$drive"
model=$(lsblk -ndo model "$drive"| sed 's/ *$//')
partition=$(lsblk -no name,uuid "$1")
len=$(( (${#model} + ${#partition} - 13)/2 ))
#echo len=$len
leng=""
for ((i=0;i
示例:
$ ./uuid2model 8ce17a86-5b31-45cc-a89d-338f618eebad
checking for specific UUID
name uuid model
nvme0n1p1 8ce17a86-5b31-45cc-a89d-338f618eebad KINGSTON SA2000M8250G
如果没有参数,它会打印所有UUID的驱动器模型。
https://askubuntu.com/questions/1418179
复制相似问题