DD 零错误:设备上没有剩余空间

DD 零错误:设备上没有剩余空间

您好,目前,当我在我们的一台 xenial 服务器上运行此脚本以通过一个命令批量擦除一批驱动器时,我遇到了问题。首先,所有驱动器都显示在 lsblk 中,因此这不是问题,如果我sudo dd if=/dev/zero of=/dev/<insert drive path> bs=64KB status=progress在多个选项卡中启动,它们将单独擦除,但是最近我遇到了此错误的问题:

dd: writing to '/dev/sdb'$'\n''sdc'$'\n''sdd': No space left on device
1+0 records in
0+0 records out
0 bytes copied, 0.000280477 s, 0.0kB/s

知道是什么导致了这个问题吗?我在下面留下了我的代码。谢谢

#!/bin/bash
erasure=
RAIDFILE="/tmp/raiddrives"
sudo rm "$RAIDFILE"
echo "Drive Wiper 1.3"
echo "Waiting for Disks to initilise"
sleep 30s # Waiting for the HDDs and SSDs to power up.

sudo /opt/MegaRAID/storcli/storcli64 /c0 set alarm=off
sudo /opt/MegaRAID/storcli/storcli64 /c0 show | grep -E ^[[:digit:]]+:[[:digit:]] >> $RAIDFILE
while read LINE; do
    declare -a slotinfo
    IFS=' ' read -r -a slotinfo <<< "$LINE"

    if [ "${slotinfo[2]}" == "UBad" ] && [ "${slotinfo[3]}" == "-" ]; then
    IFS=':' read -r -a driveid <<< "${slotinfo[0]}"
    sudo /opt/MegaRAID/storcli/storcli64 /c0/e"${driveid[0]}"/s"${driveid[1]}" set good
    echo "${slotinfo[0]} has been set to good "

    if [ "${slotinfo[2]}" == "UGood" ] && [ "${slotinfo[3]}" == "F" ]; then
      sudo /opt/MegaRAID/storcli/storcli64 /c0/fall delete
      echo "Deleted foreign RAID config on ${slotinfo[0]} "
    fi

    if [ "${slotinfo[2]}" == "UGood" ]; then
    sudo /opt/MegaRAID/storcli/storcli64 /c0 add vd r0 drives="${slotinfo[0]}"
    echo "${slotinfo[0]} available for erasure"
    fi
done < "$RAIDFILE"

echo 'Available drive(s) to be wiped' #print a list of the unmounted drives only
declare -a drivevar
drivevar=$(lsblk | awk {'print $1'} | grep '^sd' | grep -v 'sda\|sdb') #removed nvme in live version
echo $drivevar
read -p "Confirm erasure of drives, or to amend list (Y/N/A): " erasure #confirm disk erasure
erasure=${erasure^^} #capitalise erasure

yesnocheck=0 #checking if the user enters a yes or no command.
while [[ yesnocheck -eq 0 ]]; do
  case "$erasure" in
    Y|YES)
      yesnocheck=1
      : handle 'yes' cases #dont need any special actions to happen for yes or no cases
      ;;
    N|NO)
      yesnocheck=1
      : handle 'no' cases
      ;;
    A|AMEND)
      unset drivevar 1>&2 #will loop error, and constantly ask for a yes no answer until it receives one.
      declare -a drivevar
      read -p "Confirm which drives to erase e.g.sda (each seperated by a space)" drivevar
      read -p "Confirm erasure of drives, or to amend list (Y/N/A): " erasure
      erasure=${erasure^^}
      ;;
    *)
      printf '%s\n' "Not a valid selection" 1>&2 #will loop error, and constantly ask for a yes no answer until it receives one.
      read -p "Confirm erasure of drives, or to amend list (Y/N/A): " erasure
      erasure=${erasure^^}
      ;;
  esac
done

if [ "$erasure" = "Y" -o "$erasure" = "YES" ] ; #or inside a statement
then
{ for i in "${drivevar[@]}"; #getting the results from the drivevar array
do
  sudo dd if=/dev/zero of=/dev/"$i" bs=64KB status=progress & #Parrallell Wiping process, wait allows the process to be cancelled although the process is slower.
  done
  wait
      }
elif [ "$erasure" = "N" -o "$erasure" = "NO" ] ;
then
  printf '%s\n' 'Operation Aborted!'
else
  echo "Ooooops!" #something has gone wrong somewhere, this shouldnt show.
fi

答案1

您声明drivevar为数组,但将其用作单元素字符串。

例如,drivevar=$(lsblk...)将输出分配给数组的第一个(第零个)元素,并echo $drivevar输出同一元素。但你并没有将它用作数组;而是将其用作数组。您将其用作包含空格分隔项目列表的字符串。

这意味着当您尝试使用 迭代它时for i in "${drivevar[@]}",所有结果都位于第一个(也是唯一的)元素中。

您可能应该使用以下形式的数组赋值var=(element element…)

drivevar=( $(lsblk …) )               # Assignment
echo "drivevar=( ${drivevar[@]} )"    # Debug line, I assume

哦。您需要删除您创建的导致原始错误的文件。请注意,文件名包含换行符,因此当您使用ls或 时可能看起来很奇怪rm。用于rm -i在删除前要求确认:

rm -i /dev/sdb*sdc*sdd*

顺便说一句,您的赋值表达式可以从此简化

lsblk | awk {'print $1'} | grep '^sd' | grep -v 'sda\|sdb'

像这样的东西,避免了awk | grep反模式

lsblk -dn -o NAME | awk '/^sd[^ab]/'

相关内容