Skip to content

Instantly share code, notes, and snippets.

@toc21c
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toc21c/9753873 to your computer and use it in GitHub Desktop.
Save toc21c/9753873 to your computer and use it in GitHub Desktop.
#!/bin/bash
# by atalntis@wisetodd.com
# Description :
# 1. sd로 시작되는 disk 정보에 대해서만 dd를 한다.
# 2. 만일 sda1, sda2식으로 이루어진 subpartition은 skip한다.
# 3. 아래의 disk 초기화는 sda ~ sdz까지로 제한한다.
#INPUTFILE=/proc/diskstats
INPUTFILE=/proc/partitions
TMPFILE=/tmp/diskinfo.tmp
BSSIZE=1M
COUNT=256
#:docstring strcmp:
# Usage: strcmp $s1 $s2
#
# Strcmp compares its arguments and returns an integer less than, equal to,
# or greater than zero, depending on whether string s1 is lexicographically
# less than, equal to, or greater than string s2.
#:end docstring:
###;;;autoload
function strcmp ()
{
[ "$1" = "$2" ] && return 0
[ "${1}" '<' "${2}" ] > /dev/null && return 2
return 1
}
#:docstring strncmp:
# Usage: strncmp $s1 $s2 $n
#
# Like strcmp, but makes the comparison by examining a maximum of n
# characters (n less than or equal to zero yields equality).
#:end docstring:
function strncmp ()
{
if [ -z "${3}" ] || [ "${3}" -le "0" ]
then
return 0
fi
if [ ${3} -ge ${#1} ] && [ ${3} -ge ${#2} ]
then
strcmp "$1" "$2"
return $?
else
s1=${1:0:$3}
s2=${2:0:$3}
strcmp $s1 $s2
return $?
fi
}
lownumber=048
highnumber=057
function isnumber()
{
result=$(printf "%03d\n" \'${1})
if [ "${result}" -le "$lownumber" ] ; then
return 1
fi
if [ "${result}" -ge "$highnumber" ] ; then
return 1
fi
return 0
}
function isexistnumber()
{
strextra=${1}
while [ ${strextra} ]
do
strextra_tmp=${strextra:1}
strextra=$strextra_tmp
isnumber $strextra_tmp
if [ $? -eq 0 ] ; then
return 0
fi
done
return 1
}
cat $INPUTFILE > $TMPFILE
while read -r f1 f2 f3 f4
do
#
# Skip main block-device for test
#
strncmp $f4 "sda" 3
if [ $? -eq 0 ] ; then
continue
fi
strncmp $f4 "sd" 2
if [ $? -eq 0 ] ; then
isexistnumber $f4
if [ $? -eq 0 ] ; then
continue;
fi
COUNT=$f3
dd if=/dev/zero of=/dev/$f4 bs=$BSSIZE count=$COUNT
echo "w" | fdisk /dev/$f4
echo "dd if=/dev/zero of=/dev/$f4 bs=$BSSIZE count=$COUNT"
fi
done < $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment