Revisions
-
trentmswanson revised this gist
Sep 6, 2014 . 1 changed file with 26 additions and 50 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,66 +9,33 @@ 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() { @@ -84,6 +51,21 @@ add_to_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 @@ -106,12 +88,6 @@ then fi } if [ -z "${1}" ]; then DISKS=($(scan_for_new_disks)) @@ -134,7 +110,7 @@ do 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) -
trentmswanson revised this gist
Sep 6, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -146,3 +146,4 @@ do mount "${MOUNTPOINT}" done -
trentmswanson revised this gist
Sep 6, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -133,8 +133,8 @@ do 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 MOUNTPOINT=$(get_next_mountpoint) -
trentmswanson created this gist
Sep 6, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,148 @@ #!/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>" } 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 ${?} } 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 # 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}" } get_next_mountpoint() { DIRS=$(ls -1d ${DATA_BASE}/data* 2>/dev/null| sort --version-sort) MAX=$(echo "${DIRS}"|tail -n 1 | tr -d "[a-zA-Z/]") if [ -z "${MAX}" ]; then echo "${DATA_BASE}/data1" return fi IDX=1 while [ "${IDX}" -lt "${MAX}" ]; do NEXT_DIR="${DATA_BASE}/data${IDX}" if [ ! -d "${NEXT_DIR}" ]; then echo "${NEXT_DIR}" return fi IDX=$(( ${IDX} + 1 )) done IDX=$(( ${MAX} + 1)) echo "${DATA_BASE}/data${IDX}" } 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 } 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 [ "${UID}" -ne 0 ]; then echo "You must be root to run this program." >&2 exit 3 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 10 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