shell之lvm

#!/bin/bash

#this script for LVM

echo "Initial a disk..." 
echo -e "33[31mWarning: 33[0m"
fdisk -l | grep -o "^Disk /dev/[sv]d[a-z]"
read -p "What's disk Your choice:" DISK
if [ "$DISK" == 'quit' ] || [ "$DISK" == 'q' ]; then
echo "quit"
exit 7
fi
######################Check input ################################
until fdisk -l | grep -o "^Disk /dev/[sv]d[a-z]" | grep "^Disk $DISK$" &> /dev/null;do
read -p "Wrong option,your choice again:" DISK
if [ $DISK == quit ] ; then
echo "quit"
exit 7
fi
done
########################Check mount#############################
if mount | grep "$DISK" &> /dev/null;then
echo `mount | grep "^$DISK" | awk '{print $1}'` is busy
read -p "Will you continue?" CHOICE 
until [ $CHOICE == 'y' -o $CHOICE == 'n' ] ; do
read -p "wrong choice ,please try again:" CHOICE
done
if [ $CHOICE == 'n' ] ;then
echo "$DISK is busy,initial disk stop..."
exit 9
else
for I in `mount | grep "$DISK" | awk '{print $1}'`;do
fuser -km $I
umount $I
echo "$I umount ok"
done
######################Continue building disk?##########################
read -p "will destroy all data, continue?" CHOICE
until [ $CHOICE == 'y' -o $CHOICE == 'n' ] ; do
read -p "wrong choice ,please try again:" CHOICE
done
if [ $CHOICE == 'n' ] ;then
echo "$DISK have exist data,initial disk stop..."
exit 9
else
dd if=/dev/zero of=$DISK bs=512 count=1
sync
    sleep 3
#######################fdisk $DISK##############################
echo 'n
p
1




t
8e
w' | fdisk -cu $DISK &> /dev/null
sleep 2
fi
fi
else
echo 'n
p
1




t
8e
w' | fdisk -cu $DISK &> /dev/null
sleep 2
fi
#####################initial disk #################################### 
read -p "Do you want to initial? " INITIAL
{
[ $INITIAL == Y ] || [ $INITIAL == y ] || [ $INITIAL == 'YES' ] || [ $INITIAL == 'yes' ] 
} && {
read -p "Please input vg_disk: " VG_DISK
read -p "Please input lv_disk: " LV_DISK
read -p "Please input lvs_disk's size: " LVS_SIZE
read -p "Please input File system type: " LVS_TYPE
pvcreate ${DISK}1  >/dev/null 2>&1
vgcreate $VG_DISK ${DISK}1 > /dev/null 2>&1
lvcreate -L $LVS_SIZE -n $LV_DISK $VG_DISK  > /dev/null 2>&1
mkfs.$LVS_TYPE /dev/$VG_DISK/$LV_DISK  > /dev/null 2>&1
##########################mount disk###################################
read -p "Do you want to mount now: " LVS_MOUNT  
{
[ $LVS_MOUNT == Y ] || [ $LVS_MOUNT == y ] || [ $LVS_MOUNT == 'YES' ] || [ $LVS_MOUNT == 'yes' ]
} && {
read -p "Please input mount point: " LVS_POINT
[ -d ${LVS_POINT} ] ||  mkdir -p ${LVS_POINT}
echo "/dev/$VG_DISK/$LV_DISK     ${LVS_POINT}   $LVS_TYPE     defaults   0 0  " >> /etc/fstab 
mount -a 
} || exit 
} || exit
原文地址:https://www.cnblogs.com/guoew/p/10391052.html