Skip to content

Commit 1fc32df

Browse files
committed
update scripts as per PR review
for both scripts - sector size wise generic computations, handles 4K native, 512 all disks taking inputs - take device/disk from user as cmd arg - Interactively ask for confirmation before running destructive data loss operations / zeroing out
1 parent 32223c2 commit 1fc32df

File tree

3 files changed

+353
-113
lines changed

3 files changed

+353
-113
lines changed

solaris/admin/create_gpt_parts.sh

Lines changed: 171 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,127 @@
44
# -----------------------------------------------------------------------------
55
# Purpose : Create GPT partition layout optimized for OpenIndiana/illumos systems.
66
# Sets up ESP, Solaris root ZFS pool, data partition, and reserved partition.
7-
# Usage : Run after disk_hammer_illumos.sh to create clean partition layout.
8-
# Set DISK_DEVICE variable to your target disk.
9-
# Notes : - Requires prior run of disk_hammer_illumos.sh for clean disk state
7+
# Usage : ./create_gpt_parts.sh -d /dev/sdX
8+
# ./create_gpt_parts.sh --target-disk /dev/nvme0n1
9+
# Notes : - Requires prior run of wipe_pt.sh for clean disk state
1010
# - Creates 4 partitions: ESP (512MB), Solaris root, data, reserved (8MB)
1111
# - Uses proper Solaris partition type codes (BF00, BF05, BF07)
12+
# - Works with both 512-byte and 4Kn disks
1213
# =============================================================================
1314

14-
# Configuration: Set your target disk device here
15-
# Examples: sda, sdb, nvme0n1, nvme1n1, etc.
16-
DISK_DEVICE="sdX" # CHANGE THIS TO YOUR ACTUAL DISK
15+
# Function to display usage information
16+
usage() {
17+
echo "Usage: $0 -d DEVICE | --target-disk DEVICE"
18+
echo ""
19+
echo "Options:"
20+
echo " -d, --target-disk DEVICE Target disk device"
21+
echo " (e.g., /dev/sda, /dev/nvme0n1)"
22+
echo " -h, --help Show this help message"
23+
echo ""
24+
echo "Examples:"
25+
echo " $0 -d /dev/sda"
26+
echo " $0 --target-disk /dev/nvme0n1"
27+
exit 1
28+
}
1729

18-
# Validate that user has updated the device variable
19-
if [ "$DISK_DEVICE" = "sdX" ]; then
20-
echo "ERROR: Please update DISK_DEVICE variable with your actual disk device name"
21-
echo "Examples: sda, sdb, nvme0n1, etc."
30+
# Function to handle fatal errors
31+
fail() {
32+
echo "ERROR: $1" >&2
2233
exit 1
34+
}
35+
36+
# Function to get user confirmation
37+
confirm() {
38+
local prompt="$1"
39+
local response
40+
while true; do
41+
echo -n "$prompt (y/N): "
42+
read -r response
43+
case "$response" in
44+
[Yy]|[Yy][Ee][Ss])
45+
return 0
46+
;;
47+
[Nn]|[Nn][Oo]|"")
48+
return 1
49+
;;
50+
*)
51+
echo "Please answer yes (y) or no (n)."
52+
;;
53+
esac
54+
done
55+
}
56+
57+
# Parse command line arguments (supports both short and long options)
58+
DISK_DEVICE=""
59+
60+
while [[ $# -gt 0 ]]; do
61+
case "$1" in
62+
-d|--target-disk)
63+
if [[ -n $2 && $2 != -* ]]; then
64+
DISK_DEVICE="$2"
65+
shift 2
66+
else
67+
fail "--target-disk requires a non-empty option argument"
68+
fi
69+
;;
70+
-h|--help)
71+
usage
72+
;;
73+
*)
74+
echo "Unknown option: $1"
75+
usage
76+
;;
77+
esac
78+
done
79+
80+
# Check if device was specified
81+
if [[ -z $DISK_DEVICE ]]; then
82+
echo "ERROR: No device specified"
83+
usage
84+
fi
85+
86+
# Validate that the device exists and is a block device
87+
[[ -b $DISK_DEVICE ]] || fail "no such block device $DISK_DEVICE"
88+
89+
# Check if running as root
90+
if [[ $EUID -ne 0 ]]; then
91+
fail "This script must be run as root"
2392
fi
2493

94+
# Get disk information for sector size calculations
95+
SECTOR_SIZE=$(blockdev --getss "$DISK_DEVICE") || \
96+
fail "Failed to get sector size"
97+
TOTAL_SECTORS=$(blockdev --getsz "$DISK_DEVICE") || \
98+
fail "Failed to get disk size"
99+
100+
echo "Disk information:"
101+
echo " Device: $DISK_DEVICE"
102+
echo " Logical sector size: $SECTOR_SIZE bytes"
103+
echo " Total sectors: $TOTAL_SECTORS"
104+
25105
# Configuration: Solaris root partition size (adjust as needed)
26106
SOLARIS_ROOT_SIZE_GB=128
27107
SOLARIS_ROOT_END_MIB=$((513 + SOLARIS_ROOT_SIZE_GB * 1024))
28108

29-
echo "Creating GPT partition layout on /dev/$DISK_DEVICE"
30-
echo "Solaris root partition will be ${SOLARIS_ROOT_SIZE_GB}GB"
31-
echo "Press Ctrl+C within 5 seconds to cancel..."
32-
sleep 5
109+
echo ""
110+
echo "Creating GPT partition layout optimized for OpenIndiana/illumos"
111+
echo ""
112+
echo "Planned partition layout:"
113+
echo " 1. ESP (EFI System Partition) - 512MB FAT32 for UEFI boot"
114+
echo " 2. Solaris root ZFS pool - ${SOLARIS_ROOT_SIZE_GB}GB for system"
115+
echo " 3. Data partition - remaining space for user data/additional pools"
116+
echo " 4. Solaris reserved - 8MB traditional marker at end of disk"
117+
echo ""
118+
119+
# Prerequisite check
120+
echo "NOTE: Ensure you have run wipe_pt.sh first for clean disk state"
121+
echo ""
33122

34-
# Prerequisite: Clean/hammer GPT partition table using disk_hammer_illumos.sh
35-
echo "NOTE: Ensure you have run disk_hammer_illumos.sh first for clean disk state"
123+
# Get confirmation from user
124+
if ! confirm "Proceed with creating partition layout?"; then
125+
echo "Operation cancelled."
126+
exit 0
127+
fi
36128

37129
# Layout: 4 partitions total
38130
# 1. ESP (EFI System Partition) - 512MB FAT32 for UEFI boot
@@ -41,48 +133,91 @@ echo "NOTE: Ensure you have run disk_hammer_illumos.sh first for clean disk stat
41133
# 4. Solaris reserved - traditional 8MB marker at end of disk
42134

43135
# ===== Partition 1: EFI System Partition (ESP) =====
136+
echo ""
44137
echo "Creating partition 1: EFI System Partition (512MB)..."
45138
# Create 512MB ESP partition starting at 1MiB for proper alignment
46-
sudo parted /dev/$DISK_DEVICE 'mkpart "EFI System Partition" fat32 1MiB 513MiB'
139+
# MiB units automatically handle sector size alignment
140+
parted "$DISK_DEVICE" 'mkpart "EFI System Partition" fat32 1MiB 513MiB' || \
141+
fail "Failed to create ESP partition"
142+
47143
# Set ESP and boot flags required for UEFI systems
48-
sudo parted /dev/$DISK_DEVICE set 1 esp on
49-
sudo parted /dev/$DISK_DEVICE set 1 boot on
144+
parted "$DISK_DEVICE" set 1 esp on || \
145+
fail "Failed to set ESP flag"
146+
parted "$DISK_DEVICE" set 1 boot on || \
147+
fail "Failed to set boot flag"
148+
50149
# Format as FAT32 with "EFI" label for bootloader compatibility
51-
sudo mkfs.fat -F 32 -n EFI /dev/${DISK_DEVICE}p1 || sudo mkfs.fat -F 32 -n EFI /dev/${DISK_DEVICE}1
150+
# Handle both NVMe (p1) and SATA (1) partition naming
151+
if [[ $DISK_DEVICE =~ nvme ]]; then
152+
ESP_PARTITION="${DISK_DEVICE}p1"
153+
else
154+
ESP_PARTITION="${DISK_DEVICE}1"
155+
fi
156+
157+
mkfs.fat -F 32 -n EFI "$ESP_PARTITION" || \
158+
fail "Failed to format ESP partition"
52159

53160
# ===== Partition 2: Solaris Root Pool =====
54161
echo "Creating partition 2: Solaris root pool (${SOLARIS_ROOT_SIZE_GB}GB)..."
55162
# Create partition for OpenIndiana/illumos root ZFS pool
56-
sudo parted /dev/$DISK_DEVICE mkpart "solaris" 513MiB ${SOLARIS_ROOT_END_MIB}MiB
163+
# MiB units automatically handle alignment for any sector size
164+
parted "$DISK_DEVICE" mkpart "solaris" 513MiB ${SOLARIS_ROOT_END_MIB}MiB || \
165+
fail "Failed to create Solaris root partition"
166+
57167
# Set Solaris root partition type (BF00) for proper recognition by illumos tools
58-
sudo sgdisk --typecode=2:BF00 /dev/$DISK_DEVICE
168+
sgdisk --typecode=2:BF00 "$DISK_DEVICE" || \
169+
fail "Failed to set Solaris root partition type"
59170

60171
# ===== Partition 4: Solaris Reserved (create before data partition) =====
61172
echo "Creating partition 4: Solaris reserved (8MB at end of disk)..."
62-
# Calculate sectors for 8MB reserved partition at end of disk
63-
# Get total sectors, reserve last 1MB, then place 8MB reserved partition before it
64-
TOTAL_SECTORS=$(sudo blockdev --getsz /dev/$DISK_DEVICE)
65-
LAST_USABLE_SECTOR=$((TOTAL_SECTORS - 2048)) # Reserve last 1MB (2048 sectors)
66-
RESERVED_START_SECTOR=$((LAST_USABLE_SECTOR - 16384)) # 8MB = 16384 sectors
173+
174+
# Calculate sectors for 8MB reserved partition based on actual sector size
175+
# This works correctly for both 512-byte and 4Kn disks
176+
RESERVED_SIZE_BYTES=$((8 * 1024 * 1024)) # 8MB in bytes
177+
RESERVE_END_BYTES=$((1 * 1024 * 1024)) # Reserve last 1MB in bytes
178+
179+
# Convert bytes to sectors based on actual sector size
180+
RESERVED_SECTORS=$((RESERVED_SIZE_BYTES / SECTOR_SIZE))
181+
RESERVE_END_SECTORS=$((RESERVE_END_BYTES / SECTOR_SIZE))
182+
183+
# Calculate partition boundaries, -1 since sector numbers use 0 based indexing
184+
LAST_USABLE_SECTOR=$((TOTAL_SECTORS - RESERVE_END_SECTORS - 1))
185+
RESERVED_START_SECTOR=$((LAST_USABLE_SECTOR - RESERVED_SECTORS))
186+
187+
echo "Calculated reserved partition:"
188+
echo " Start sector: $RESERVED_START_SECTOR"
189+
echo " End sector: $LAST_USABLE_SECTOR"
190+
echo " Size: $RESERVED_SECTORS sectors (8MB)"
67191

68192
# Create traditional Solaris reserved partition for compatibility
69-
sudo parted /dev/$DISK_DEVICE unit s mkpart solaris_reserved \
70-
$RESERVED_START_SECTOR $LAST_USABLE_SECTOR
193+
parted "$DISK_DEVICE" unit s mkpart solaris_reserved \
194+
$RESERVED_START_SECTOR $LAST_USABLE_SECTOR || \
195+
fail "Failed to create Solaris reserved partition"
196+
71197
# Set Solaris reserved partition type (BF07)
72-
sudo sgdisk --typecode=4:BF07 /dev/$DISK_DEVICE
198+
sgdisk --typecode=4:BF07 "$DISK_DEVICE" || \
199+
fail "Failed to set Solaris reserved partition type"
73200

74201
# ===== Partition 3: Data Partition =====
75202
echo "Creating partition 3: Data partition (remaining space)..."
76203
# Create data partition using remaining space between root and reserved partitions
77-
# parted will automatically use available space up to the reserved partition
78-
sudo parted /dev/$DISK_DEVICE mkpart data ${SOLARIS_ROOT_END_MIB}MiB -8200MiB
204+
# Calculate the end position in MiB - parted will automatically adjust sector alignment
205+
RESERVED_START_MB=$((RESERVED_START_SECTOR * SECTOR_SIZE / 1024 / 1024))
206+
207+
echo "Data partition end: ${RESERVED_START_MB}MiB"
208+
209+
parted "$DISK_DEVICE" mkpart data ${SOLARIS_ROOT_END_MIB}MiB ${RESERVED_START_MB}MiB || \
210+
fail "Failed to create data partition"
211+
79212
# Set Solaris /home partition type (BF05) for user data
80-
sudo sgdisk --typecode=3:BF05 /dev/$DISK_DEVICE
213+
sgdisk --typecode=3:BF05 "$DISK_DEVICE" || \
214+
fail "Failed to set data partition type"
81215

82216
# ===== Display final partition layout =====
83217
echo ""
84-
echo "Partition layout created successfully:"
85-
sudo parted /dev/$DISK_DEVICE print
218+
echo "SUCCESS: Partition layout created successfully!"
219+
echo ""
220+
parted "$DISK_DEVICE" print || echo "Warning: Could not display partition table"
86221
echo ""
87222
echo "Partition type codes set:"
88223
echo " Partition 1: ESP (EFI System Partition)"
@@ -91,3 +226,4 @@ echo " Partition 3: BF05 (Solaris /home)"
91226
echo " Partition 4: BF07 (Solaris reserved)"
92227
echo ""
93228
echo "Ready for OpenIndiana/illumos installation!"
229+
echo "You can now proceed with ZFS pool creation and OS installation."

solaris/admin/disk_hammer_illumos.sh

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)