Last active
June 19, 2018 13:41
-
-
Save marinnedea/a5179c7693c061c6d50d12fa33a20aca 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 | |
################################################################################# | |
# Script to partition automatically new disks added to a linux machine. # | |
# The script will create a single partition on the whole free space of the disk # | |
################################################################################# | |
# Setting the logfile location | |
logfile=/var/log/disk_part.log | |
# Logging all actions | |
exec 3>&1 4>&2 | |
trap 'exec 2>&4 1>&3' 0 1 2 3 | |
exec 1>$logfile 2>&1 | |
# Logging the start date and time | |
date +"%F_%R" | |
if [ "${UID}" -ne 0 ]; | |
then | |
echo "Script executed without root permissions" | |
echo "You must be root to run this program." >&2 | |
exit 3 | |
fi | |
is_partitioned() { | |
OUTPUT=$(partx -s ${1} 2>&1) | |
egrep "partition table does not contains usable partitions|failed to read partition table" <<< "${OUTPUT}" >/dev/null 2>&1 | |
if [ ${?} -eq 0 ]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
has_filesystem() { | |
DEVICE=${1} | |
OUTPUT=$(file -L -s ${DEVICE}) | |
grep filesystem <<< "${OUTPUT}" > /dev/null 2>&1 | |
return ${?} | |
} | |
scan_for_new_disks() { | |
# Looks for unpartitioned disks | |
declare -a RET | |
DEVS=($(ls -1 /dev/sd*|egrep -v "[0-9]$")) | |
for DEV in "${DEVS[@]}"; | |
do | |
# The disk will be considered a candidate for partitioning | |
# and formatting if it does not have a sd?1 entry or | |
# if it does have an sd?1 entry and does not contain a filesystem | |
is_partitioned "${DEV}" | |
if [ ${?} -eq 0 ]; | |
then | |
has_filesystem "${DEV}1" | |
if [ ${?} -ne 0 ]; | |
then | |
RET+=" ${DEV}" | |
fi | |
else | |
RET+=" ${DEV}" | |
fi | |
done | |
echo "${RET}" | |
} | |
do_partition() { | |
# This function creates one (1) primary partition on the | |
# disk, using all available space | |
_disk=${1} | |
_type=${2} | |
if [ -z "${_type}" ]; then | |
# default to Linux partition type (ie, ext3/ext4/xfs) | |
_type=83 | |
fi | |
echo "n | |
p | |
1 | |
t | |
${_type} | |
w"| fdisk "${_disk}" | |
# | |
# Use the bash-specific $PIPESTATUS to ensure we get the correct exit code | |
# from fdisk and not from echo | |
if [ ${PIPESTATUS[1]} -ne 0 ]; | |
then | |
echo "An error occurred partitioning ${_disk}" >&2 | |
echo "I cannot continue" >&2 | |
exit 2 | |
fi | |
} | |
#end do_partition | |
scan_partition_format() | |
{ | |
log "Begin scanning and formatting data disks" | |
DISKS=($(scan_for_new_disks)) | |
if [ "${#DISKS}" -eq 0 ]; | |
then | |
log "No unpartitioned disks without filesystems detected" | |
return | |
fi | |
echo "Disks are ${DISKS[@]}" | |
for DISK in "${DISKS[@]}"; | |
do | |
echo "Working on ${DISK}" | |
is_partitioned ${DISK} | |
if [ ${?} -ne 0 ]; | |
then | |
echo "${DISK} is not partitioned, partitioning" | |
do_partition ${DISK} | |
fi | |
PARTITION=$(fdisk -l ${DISK}|grep -A 1 Device|tail -n 1|awk '{print $1}') | |
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 10 | |
mkfs -j -t ext4 ${PARTITION} | |
fi | |
done | |
} | |
# Create Partitions | |
DISKS=$(scan_for_new_disks) | |
scan_partition_format |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment