linux 遇到硬盘设备名称会改变时,可以使用udev规则绑定硬盘

udev规则绑定硬盘

# lsblk -o NAME,MODEL,SERIAL
NAME        MODEL            SERIAL
sda         Samsung SSD 860  S3YLNM0NC12424A
├─sda1
├─sda2
└─sda3
  ├─cl-root
  └─cl-swap
sdb         Samsung SSD 860  S3ZBND0NC04099A
└─sdb1
sdc         Samsung SSD 860  S3ZBND0NC00975A
└─sdc1

# cat /etc/udev/rules.d/99-mydisks.rules

规则用于绑定第一块数据盘

SUBSYSTEM=="block", ENV{ID_SERIAL_SHORT}=="S3ZBND0NC04486A", SYMLINK+="data1"

规则用于绑定第二块数据盘

SUBSYSTEM=="block", ENV{ID_SERIAL_SHORT}=="S3ZBND0NC00073A", SYMLINK+="data2"

运行以下命令以重新加载 udev 规则:
udevadm control --reload-rules

您还可以运行以下命令触发规则立即生效:

udevadm trigger

完成后,您应该能够在 /dev/data1 和 /dev/data2 路径下找到您绑定的两块数据盘。

sed -i "s/abc/123/g" /var/www/test/index.html

# cat udev_disk_rules.sh
#!/bin/bash

AA=$1
BB=$2

RULES_FILE="/etc/udev/rules.d/99-mydisks.rules"

# 创建规则文件,并添加规则
sudo bash -c "cat << EOF > $RULES_FILE
# 规则用于绑定第一块数据盘
SUBSYSTEM==\"block\", ENV{ID_SERIAL_SHORT}==\"$AA\", SYMLINK+=\"data1\"

# 规则用于绑定第二块数据盘
SUBSYSTEM==\"block\", ENV{ID_SERIAL_SHORT}==\"$BB\", SYMLINK+=\"data2\"
EOF
"

# 重新加载udev规则
sudo udevadm control --reload-rules

# 触发规则立即生效
sudo udevadm trigger

echo "完成创建并添加udev规则。"
$ cat getserial.sh
#!/bin/bash
sn1=`lsblk -o NAME,SIZE,SERIAL|egrep -w "sda|sdb|sdc|sdd"|awk '$2>900G {print $3}'|sed -n 1p`
sn2=`lsblk -o NAME,SIZE,SERIAL|egrep -w "sda|sdb|sdc|sdd"|awk '$2>900G {print $3}'|sed -n 2p`
echo $sn1
echo $sn2
echo "SUBSYSTEM==\"block\", ENV{ID_SERIAL_SHORT}==\"$sn1\", SYMLINK+=\"data1\"" >/etc/udev/rules.d/99-mydisks.rules
echo "SUBSYSTEM==\"block\", ENV{ID_SERIAL_SHORT}==\"$sn2\", SYMLINK+=\"data2\"" >>/etc/udev/rules.d/99-mydisks.rules
udevadm control --reload-rules
udevadm trigger
vm1=`virsh list --all|grep G|awk '{print$2}'|sed -n 1p`
vm2=`virsh list --all|grep G|awk '{print$2}'|sed -n 2p`
perl -pi -e 's/\/dev\/sd([a-z])/\/dev\/data1/g' /opt/vmxml/${vm1}.xml
perl -pi -e 's/\/dev\/sd([a-z])/\/dev\/data2/g' /opt/vmxml/${vm2}.xml
virsh define /opt/vmxml/${vm1}.xml
virsh define /opt/vmxml/${vm2}.xml
posted @ 2024-06-06 10:09  Suixin随心  阅读(138)  评论(0编辑  收藏  举报