Created
October 9, 2019 06:46
-
-
Save lizhiyong2000/7d95d972f9f95847e03c59dba84f6949 to your computer and use it in GitHub Desktop.
磁盘挂载脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# An set of disks to ignore from partitioning and formatting | |
BLACKLIST="sdm" | |
# Base directory to hold the data* files | |
DATA_BASE="/mnt" | |
usage() { | |
echo "Usage: $(basename $0) <new disk>" | |
} | |
scan_for_new_disks() { | |
# Looks for unpartitioned disks | |
declare -a RET | |
# DEVS=($(ls -1 /dev/sd*|egrep -v "${BLACKLIST}"|egrep -v "[0-9]$")) | |
DEVS=($(lsblk|grep -v 'NAME'|awk '{if($6~/disk/) print $1}'|egrep -v "${BLACKLIST}")) | |
for DEV in "${DEVS[@]}"; | |
do | |
# Check each device if there is a "1" partition. If not, | |
# "assume" it is not partitioned. | |
RET+="${DEV} " | |
done | |
echo "${RET}" | |
} | |
get_next_mountpoint() { | |
DIRS=($(ls -1d ${DATA_BASE}/data* 2>&1| sort --version-sort)) | |
if [ -z "${DIRS[0]}" ]; | |
then | |
echo "${DATA_BASE}/data1" | |
return | |
else | |
IDX=$(echo "${DIRS[${#DIRS[@]}-1]}"|tr -d "[a-zA-Z/]" ) | |
IDX=$(( ${IDX} + 1 )) | |
echo "${DATA_BASE}/data${IDX}" | |
fi | |
} | |
add_to_fstab() { | |
UUID=${1} | |
MOUNTPOINT=${2} | |
grep "${UUID}" /etc/fstab >/dev/null 2>&1 | |
if [ ${?} -eq 0 ]; | |
then | |
echo "Not adding ${UUID} to fstab again (it's already there!)" | |
else | |
LINE="UUID=${UUID} ${MOUNTPOINT}\txfs\tdefaults\t0 0" | |
echo -e "${LINE}" >> /etc/fstab | |
fi | |
cat /etc/fstab | |
} | |
is_partitioned() { | |
# Checks if there is a valid partition table on the | |
# specified disk | |
OUTPUT=$(sfdisk -l ${1} 2>&1) | |
grep "No partitions found" "${OUTPUT}" >/dev/null 2>&1 | |
return "${?}" | |
} | |
has_filesystem() { | |
DEVICE=${1} | |
OUTPUT=$(file -L -s ${DEVICE}) | |
grep filesystem <<< "${OUTPUT}" > /dev/null 2>&1 | |
return ${?} | |
} | |
do_partition() { | |
# This function creates one (1) primary partition on the | |
# disk, using all available space | |
P_DISK=${1} | |
# echo "n | |
#p | |
#1 | |
#w"| fdisk "${DISK}" > /dev/null 2>&1 | |
parted -s "${P_DISK}" mklabel gpt | |
if [ $? -ne 0 ]; | |
then | |
echo "An error occurred partitioning ${P_DISK}" >&2 | |
echo "I cannot continue" >&2 | |
exit 2 | |
fi | |
parted -a optimal -s "${P_DISK}" mkpart primary 0% 100% | |
if [ $? -ne 0 ]; | |
then | |
echo "An error occurred partitioning ${P_DISK}" >&2 | |
echo "I cannot continue" >&2 | |
exit 2 | |
fi | |
partprobe | |
# | |
# Use the bash-specific $PIPESTATUS to ensure we get the correct exit code | |
# from fdisk and not from echo | |
if [ $? -ne 0 ]; | |
then | |
echo "An error occurred partitioning ${P_DISK}" >&2 | |
echo "I cannot continue" >&2 | |
exit 2 | |
fi | |
} | |
remove_from_fstab() { | |
UUID=${1} | |
MOUNT_POINT=${2} | |
mount_name=$(basename $MOUNT_POINT) | |
chrlen=${#UUID} | |
if [ ${chrlen} -gt 0 ]; | |
then | |
grep "${UUID}" /etc/fstab >/dev/null 2>&1 | |
if [ ${?} -eq 0 ]; | |
then | |
echo "removing ${UUID} from /etc/fstab" | |
sed -i "/${UUID}/d" /etc/fstab | |
else | |
echo "${UUID} not exist in /etc/fstab" | |
fi | |
fi | |
grep "${MOUNT_POINT}" /etc/fstab >/dev/null 2>&1 | |
if [ ${?} -eq 0 ]; | |
then | |
echo "removing ${mount_name} from /etc/fstab" | |
sed -i "#${mount_name}#d" /etc/fstab | |
else | |
echo "MOUNTPOINT ${MOUNT_POINT} not exist in /etc/fstab" | |
fi | |
} | |
is_partitioned() { | |
# Checks if there is a valid partition table on the | |
# specified disk | |
OUTPUT=$(sfdisk -l ${1} 2>&1) | |
grep "No partitions found" "${OUTPUT}" >/dev/null 2>&1 | |
return "${?}" | |
} | |
remove_partition() { | |
DEVICE=${1} | |
echo "remove_partition on ${DEVICE}" | |
parted -s ${DEVICE} rm 1 | |
} | |
if [ -z "${1}" ]; | |
then | |
DISKS=($(scan_for_new_disks)) | |
else | |
DISKS=("${@}") | |
fi | |
echo "Disks are ${DISKS[@]}" | |
for DISK in "${DISKS[@]}"; | |
do | |
echo "Working on ${DISK}" | |
is_partitioned /dev/${DISK} | |
if [ ${?} -ne 0 ]; | |
then | |
echo "${DISK} is not partitioned, partitioning" | |
do_partition /dev/${DISK} | |
fi | |
if [[ $DISK == nvme* ]] ; | |
then | |
PARTITION=/dev/${DISK}p1 | |
else | |
PARTITION=/dev/${DISK}1 | |
fi | |
# PARTITION=$(lsblk|grep ${DISK}|awk '{if($6~/part/) print substr($1,3)}') | |
has_filesystem ${PARTITION} | |
if [ ${?} -ne 0 ]; | |
then | |
echo "Creating filesystem on ${PARTITION}." | |
#echo "Press Ctrl-C if you don't want to destroy all data on ${PARTITION}" | |
#sleep 5 | |
mkfs -t xfs -f ${PARTITION} | |
fi | |
MOUNTPOINT="${DATA_BASE}/disk_${DISK}1" | |
echo "Next mount point appears to be ${MOUNTPOINT}" | |
[ -d "${MOUNTPOINT}" ] || mkdir "${MOUNTPOINT}" | |
read UUID FS_TYPE < <(blkid -u filesystem ${PARTITION}|awk -F "[= ]" '{print $3" "$5}'|tr -d "\"") | |
add_to_fstab "${UUID}" "${MOUNTPOINT}" | |
echo "Mounting disk ${PARTITION} on ${MOUNTPOINT}" | |
mount "${MOUNTPOINT}" | |
done | |
#### remove partition | |
# if [ -z "${1}" ]; | |
# then | |
# DISKS=($(scan_for_new_disks)) | |
# else | |
# DISKS=("${@}") | |
# fi | |
# echo "Disks are ${DISKS[@]}" | |
# for DISK in "${DISKS[@]}"; | |
# do | |
# echo "Working on ${DISK}" | |
# if [[ $DISK == nvme* ]] ; | |
# then | |
# PARTITION=/dev/${DISK}p1 | |
# else | |
# PARTITION=/dev/${DISK}1 | |
# fi | |
# MOUNTPOINT="${DATA_BASE}/disk_${DISK}1" | |
# echo "umount ${MOUNTPOINT}" | |
# umount $MOUNTPOINT | |
# read UUID FS_TYPE < <(blkid -u filesystem ${PARTITION}|awk -F "[= ]" '{print $3" "$5}'|tr -d "\"") | |
# remove_from_fstab "${UUID}" $MOUNTPOINT | |
# remove_partition /dev/${DISK} | |
# done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment