這個是我幾年前寫的, 用來自動設定好新的機器
雖然現在回頭看寫得很鱉腳, 但當時也幫我順利搞定了上百台新 server
你可以修改一下 disks 這個變數, 來指定要初始化哪些硬碟
注意它會格式化硬碟, 最好當參考就好, 或一定要看懂以後再使用
我也不能保證這是當初順利運作上百台的版本..
<pre class="c" name="code">
# make_partition /dev/sd?
function make_partition {
echo "use fdisk create ${1}1"
/sbin/fdisk $1 &> /dev/null <<END_FDISK
n
p
1
t
83
w
END_FDISK
}
# no arguments
function make_disk {
# change this variable if disk not sdb, sdc
disks="sdb sdc"
for disk in $disks; do
disk_char=$(echo -n "$disk" | tail -c -1)
dstdir="/disk$disk_char"
diskp1="${disk}1"
if [ -e "/dev/$disk" -a "$(/sbin/fdisk -l /dev/$disk)" ]; then
#echo "disk = $disk"
partition_exists=$(cat /proc/partitions| grep $diskp1 | wc -l)
#echo "partition_exists = $partition_exists"
if [ "$partition_exists" -eq "0" ]; then
echo "$diskp1 unexists"
make_partition "/dev/$disk"
sleep 3
else
echo "$diskp1 is ready"
fi
echo "mkfs.ext3 -q /dev/$diskp1"
if /sbin/mkfs.ext3 -q /dev/$diskp1; then
fstab_exists=$(grep "/dev/$diskp1" /etc/fstab | wc -l)
if [ "$fstab_exists" -eq "0" ]; then
echo "add to fstab"
echo "/dev/$diskp1 $dstdir ext3 defaults 1 2"
echo "/dev/$diskp1 $dstdir ext3 defaults 1 2" >> /etc/fstab
else
echo "found /dev/$diskp1 related records in fstab, skip add it"
fi
else
echo "warning: unable to format $diskp1, skipped"
fi
else
echo "warning: disk $disk unexists, $dstdir will put under sda"
fi
if [ ! -d "$dstdir" ]; then
echo "making dir $dstdir"
mkdir -p "$dstdir"
fi
done
/bin/mount -a
sleep 1
echo "===================="
df -h
echo "===================="
echo "sdb1 and sdc1(if exists) should ready and mounted on /diskb /diskc"
}
把這段程式存成 xxx.bash
然後在 bash 底下這樣用:
<pre class="c" name="code">source xxx.bash
make_disk