iT邦幫忙

0

在Redhat下能夠自動偵測HDD數及自動mount起來的 script

目前在redhat下想要寫一隻能夠自動偵測目前機器上裝上去的HDD數目及能夠自動mount起來的script, 但是不知道要怎麼寫。

請問大家可以幫忙嗎? 感謝。

weiclin iT邦高手 4 級 ‧ 2016-01-10 11:29:51 檢舉
加到 fstab 不行嗎
自動偵測, 意思是數量會有變動嗎
mount 要掛在什麼地方
硬碟的分割與格式化都弄好了嗎, 都相同嗎

有好多疑問耶..
抱歉,第一次發問 有很多資料沒有提供到

1. HDD 是全新的,沒有format過。
2. 每顆HDD直接切成一個partition即可
3. Mount的位置是在/root底下
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
weiclin
iT邦高手 4 級 ‧ 2016-01-10 18:00:51
最佳解答

這個是我幾年前寫的, 用來自動設定好新的機器
雖然現在回頭看寫得很鱉腳, 但當時也幫我順利搞定了上百台新 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

我要發表回答

立即登入回答