• Welcome to LiuJason's Blog!

通过sg3-utils定位硬盘位置实现热插拔换硬盘

Linux笔记 Jason 4 months ago (08-21) 27 Views 0 Comments QR code of this page
文章目录[隐藏]

前言

首先还是做好标记最靠谱!我这次要处理的是一个6年前的设置的服务器存储集群,当时没有给硬盘盒加标签导致现在处理起来很麻烦。

解决方案

首先在系统中确定要更换硬盘的序列号:

ls -lh /dev/disk/by-id/
......
lrwxrwxrwx 1 root root  9 Aug  2 16:17 ata-WUH721816ALE6L4_2CKNWULJ -> ../../sdd
......

这里我要更换的是ata-WUH721816ALE6L4_2CKNWULJ,对应的设备为sdd
然后找到对应的sg设备位置为/dev/sg3

lsscsi -g | grep sdd
[0:0:3:0]    disk    ATA      WUH721816ALE6L4  W232  /dev/sdd   /dev/sg3

之后使用下面给出的脚本./blink_disk.sh /dev/sdX 5 1.5来让硬盘指示灯闪烁:

#!/bin/bash

# Function to display usage information
usage() {
  echo "Usage: $0 /dev/sdX BLINK_COUNT INTERVAL"
  echo "  /dev/sdX      : The disk device to blink (e.g., /dev/sda)"
  echo "  BLINK_COUNT   : Number of times to blink the disk"
  echo "  INTERVAL      : Time in seconds between each blink"
  exit 1
}

# Check if the correct number of arguments is provided
if [ "$#" -ne 3 ]; then
  usage
fi

DEVICE=$1
BLINK_COUNT=$2
INTERVAL=$3

# Validate BLINK_COUNT is a positive integer
if ! [[ "$BLINK_COUNT" =~ ^[0-9]+$ ]] || [ "$BLINK_COUNT" -le 0 ]; then
  echo "Error: BLINK_COUNT must be a positive integer."
  usage
fi

# Validate INTERVAL is a positive number
if ! [[ "$INTERVAL" =~ ^[0-9]+(\.[0-9]+)?$ ]] || (( $(echo "$INTERVAL <= 0" | bc -l) )); then
  echo "Error: INTERVAL must be a positive number."
  usage
fi

# Check if sg_start is installed
if ! command -v sg_start &> /dev/null; then
  echo "sg_start could not be found, please install sg3_utils."
  exit 1
fi

# Blink the drive
for ((i=1; i<=BLINK_COUNT; i++)); do
  echo "Blinking drive $DEVICE ($i/$BLINK_COUNT)..."
  sg_start --stop --imm $DEVICE
  sleep $INTERVAL
  sg_start --start --imm $DEVICE
  sleep $INTERVAL
done

echo "Completed blinking the drive $DEVICE."


This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/1280.html
Like (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址