-
-
Save trentmswanson/9c22bb71182e982bd36f to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# An set of disks to ignore from partitioning and formatting | |
BLACKLIST="/dev/sda|/dev/sdb" | |
# Base directory to hold the data* files | |
DATA_BASE="/media" | |
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]$")) | |
for DEV in "${DEVS[@]}"; | |
do | |
# Check each device if there is a "1" partition. If not, | |
# "assume" it is not partitioned. | |
if [ ! -b ${DEV}1 ]; | |
then | |
RET+="${DEV} " | |
fi | |
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}\"\t${MOUNTPOINT}\text4\tnoatime,nodiratime,nodev,noexec,nosuid\t1 2" | |
echo -e "${LINE}" >> /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 "${?}" | |
} | |
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 | |
DISK=${1} | |
echo "n | |
p | |
1 | |
w"| fdisk "${DISK}" > /dev/null 2>&1 | |
# | |
# 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 | |
} | |
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 ${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 5 | |
mkfs -j -t ext4 ${PARTITION} | |
fi | |
MOUNTPOINT=$(get_next_mountpoint) | |
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 | |
Ignore please, I figured out. Thanks for the script though, works on CentOS.
the resource drive can be any device, its random. ie its not always sdb, im seeing it as sdd as well about 10% of the time.
Hi,
Getting error when i ran the file.
This is the error:
~$ ./autopart.sh
Disks are /dev/sdc /dev/sdd
Working on /dev/sdc
/dev/sdc is not partitioned, partitioning
An error occurred partitioning /dev/sdc
I cannot continue
I have two disks - not partition Please see:
~$ sudo fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x1a7d4c6a
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 62914526 62912479 30G 83 Linux
Disk /dev/sdb: 56 GiB, 60129542144 bytes, 117440512 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x47fbe03b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 128 117438463 117438336 56G 7 HPFS/NTFS/exFAT
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdd: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Any help?
Thanks!
Thanks for sharing Trent!
I see now, I can't send a PR on a gist. Alas, the patch is trivial anyways. If you were to add '-p' to the mkdir on line 118 — this will work for SuSE as well. Tested on "SUSE Linux Enterprise Server 12 SP2 x86_64 (64-bit)"
I also encountered the above-mentioned error
read UUID FS_TYPE < <(blkid...
and solved it by using the" bash scriptname.sh" command instead of the "sh scriptname.sh" command
when calling the script in the extension arm template.
I successfully initialized the added disk on a centOs 7.3 VM.
Thanks a ton, it worked on Centos.
Hi I am getting same error
Disks are /dev/sdd
Working on /dev/sdd
/dev/sdd is not partitioned, partitioning
An error occurred partitioning /dev/sdd
I cannot continue
I tried to do the work around and to add the -p. but did not work
Hi I am getting same error
Disks are /dev/sdd
Working on /dev/sdd
/dev/sdd is not partitioned, partitioning
An error occurred partitioning /dev/sdd
I cannot continue
I tried to do the work around and to add the -p. but did not work
You are getting this error because of lack of privileges. You gotta run it as superuser: "sudo bash ./autopart.sh"
Hi I am getting same error
Disks are /dev/sdd
Working on /dev/sdd
/dev/sdd is not partitioned, partitioning
An error occurred partitioning /dev/sdd
I cannot continue
I tried to do the work around and to add the -p. but did not workYou are getting this error because of lack of privileges. You gotta run it as superuser: "sudo bash ./autopart.sh"
Hello, I am getting the same error and i'm running the script with root privileges... I also tried the -p flag. Do you know what could be the problem?
So I was getting errors when I hit the do_partition()
function, fdisk wasn't recognising the inputs.
I changed this to:
DISK=${1}
(
echo n
echo p
echo 1
echo
echo
echo w
) | fdisk "${DISK}" > /dev/null 2>&1
And it seemed to work - tested on Ubuntu 18.04.4 LTS (GNU/Linux 5.3.0-1020-azure x86_64)
By the way, great script - saved me a lot of time!!
On CentOS I am getting an error:
Disks are
syntax error near unexpected token `('
` read UUID FS_TYPE < <(blkid -u filesystem ${PARTITION}|awk -F "[= ]" '{print $3" "$5}'|tr -d "\"")'
In the script syntax, it shows an error on the line below, can you help, as I am trying to use your script, but not an expert on scripting
read UUID FS_TYPE < <(blkid
Thanks