Created
September 13, 2024 21:35
-
-
Save Promit/4b0e6743deb178f68e2d018b36131f10 to your computer and use it in GitHub Desktop.
Simplified Linux partitioning script for Seagate Mach.2 drives
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 characters
| #!/bin/bash | |
| # Check if a LUN is provided as an argument | |
| if [ -z "$1" ]; then | |
| echo "Usage: $0 <lun-from-/dev/disk/by-id>" | |
| exit 1 | |
| fi | |
| # Validate the LUN path and ensure it's under /dev/disk/by-id | |
| LUN="$1" | |
| if [[ ! "$LUN" =~ ^/dev/disk/by-id/ ]]; then | |
| echo "Error: $LUN is not a valid path under /dev/disk/by-id." | |
| exit 1 | |
| fi | |
| # Ensure the device exists | |
| if [[ ! -b "$LUN" ]]; then | |
| echo "Error: $LUN is not a valid block device." | |
| exit 1 | |
| fi | |
| # Variables | |
| ZPOOL_NAME="MACH2" # Default value for ZPOOL_NAME | |
| ALIGN=$((1024*4)) | |
| GPT_OVERHEAD=34 | |
| # Default LOG_FILE to /dev/null if not set | |
| LOG_FILE=${LOG_FILE:-/dev/null} | |
| #======================================================= | |
| # Run a command but first tell the user what it's going to do. | |
| run() { | |
| # Print the command line, but trim extra white space. | |
| printf "$@ \n" | sed 's/\t/ /g' | sed 's/ \+/ /g' | tee -a "${LOG_FILE}" | |
| [[ 1 == $DRY_RUN ]] && return 0 | |
| eval "$@"; ret=$? | |
| [[ $ret == 0 ]] && return 0 | |
| printf " $@ - ERROR_CODE: $ret\n" | |
| exit $ret | |
| } | |
| #======================================================= | |
| # Extract the serial number from the LUN name using awk | |
| SERIAL=$(basename "${LUN}" | awk -F '_' '{print $NF}') | |
| # Get the largest beginning and ending sectors using sgdisk | |
| LARGEST_END="$(sgdisk -a ${ALIGN} -E ${LUN} | grep -v Creating)" | |
| LARGEST_BEG="$(sgdisk -a ${ALIGN} -F ${LUN} | grep -v Creating)" | |
| # Calculate the total sectors and the midpoint | |
| TOTAL_SECTORS=$((GPT_OVERHEAD + LARGEST_END)) | |
| MID_LBA=$((TOTAL_SECTORS / 2)) | |
| # Run the sgdisk commands to create the two partitions | |
| run "sgdisk -a ${ALIGN} -I \ | |
| --new=1:$LARGEST_BEG:$((MID_LBA-$ALIGN-1)) --change-name=1:${ZPOOL_NAME}_A_${SERIAL} \ | |
| --new=2:$((MID_LBA+$ALIGN)):0 --change-name=2:${ZPOOL_NAME}_B_${SERIAL} \ | |
| ${LUN} >/dev/null" | |
| # Wait for the commands to complete | |
| sleep 1 | |
| # Run partprobe to update the partition table | |
| run "partprobe" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment