Skip to content

Commit 0b47e2f

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 create_gpt_parts.sh - use correct partition numbers for setting type code - added sorting of partition numbers since they are not sorted - added sgdisk print command to show the newly created part. table
1 parent 32223c2 commit 0b47e2f

File tree

3 files changed

+384
-122
lines changed

3 files changed

+384
-122
lines changed

solaris/admin/create_gpt_parts.sh

100644100755
Lines changed: 202 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,136 @@
22
# =============================================================================
33
# Solaris GPT Partition Creator - OpenIndiana/illumos System Layout
44
# -----------------------------------------------------------------------------
5-
# Purpose : Create GPT partition layout optimized for OpenIndiana/illumos systems.
6-
# 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
10-
# - Creates 4 partitions: ESP (512MB), Solaris root, data, reserved (8MB)
5+
# Purpose : Create GPT partition layout optimized for OpenIndiana/illumos
6+
# systems. Sets up ESP, Solaris root ZFS pool, data partition, and
7+
# reserved partition.
8+
# Usage : ./create_gpt_parts.sh -d /dev/sdX
9+
# ./create_gpt_parts.sh --target-disk /dev/nvme0n1
10+
# Notes : - Requires prior run of wipe_pt.sh for clean disk state
11+
# - Creates 4 partitions: ESP (512MB), Solaris root, data, reserved
1112
# - Uses proper Solaris partition type codes (BF00, BF05, BF07)
13+
# - Works with both 512-byte and 4Kn disks
1214
# =============================================================================
1315

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
16+
# Function to display usage information
17+
usage() {
18+
echo "Usage: $0 -d DEVICE | --target-disk DEVICE"
19+
echo ""
20+
echo "Options:"
21+
echo " -d, --target-disk DEVICE Target disk device"
22+
echo " (e.g., /dev/sda, /dev/nvme0n1)"
23+
echo " -h, --help Show this help message"
24+
echo ""
25+
echo "Examples:"
26+
echo " $0 -d /dev/sda"
27+
echo " $0 --target-disk /dev/nvme0n1"
28+
exit 1
29+
}
1730

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."
31+
# Function to handle fatal errors
32+
fail() {
33+
echo "ERROR: $1" >&2
2234
exit 1
35+
}
36+
37+
# Function to get user confirmation
38+
confirm() {
39+
local prompt="$1"
40+
local response
41+
while true; do
42+
echo -n "$prompt (y/N): "
43+
read -r response
44+
case "$response" in
45+
[Yy]|[Yy][Ee][Ss])
46+
return 0
47+
;;
48+
[Nn]|[Nn][Oo]|"")
49+
return 1
50+
;;
51+
*)
52+
echo "Please answer yes (y) or no (n)."
53+
;;
54+
esac
55+
done
56+
}
57+
58+
# Parse command line arguments (supports both short and long options)
59+
DISK_DEVICE=""
60+
61+
while [[ $# -gt 0 ]]; do
62+
case "$1" in
63+
-d|--target-disk)
64+
if [[ -n $2 && $2 != -* ]]; then
65+
DISK_DEVICE="$2"
66+
shift 2
67+
else
68+
fail "--target-disk requires a non-empty option argument"
69+
fi
70+
;;
71+
-h|--help)
72+
usage
73+
;;
74+
*)
75+
echo "Unknown option: $1"
76+
usage
77+
;;
78+
esac
79+
done
80+
81+
# Check if device was specified
82+
if [[ -z $DISK_DEVICE ]]; then
83+
echo "ERROR: No device specified"
84+
usage
2385
fi
2486

87+
# Validate that the device exists and is a block device
88+
[[ -b $DISK_DEVICE ]] || fail "no such block device $DISK_DEVICE"
89+
90+
# Check if running as root
91+
if [[ $EUID -ne 0 ]]; then
92+
fail "This script must be run as root"
93+
fi
94+
95+
# Get disk information for sector size calculations
96+
SECTOR_SIZE=$(blockdev --getss "$DISK_DEVICE") || \
97+
fail "Failed to get sector size"
98+
TOTAL_SECTORS=$(blockdev --getsz "$DISK_DEVICE") || \
99+
fail "Failed to get disk size"
100+
101+
echo "Disk information:"
102+
echo " Device: $DISK_DEVICE"
103+
echo " Logical sector size: $SECTOR_SIZE bytes"
104+
echo " Total sectors: $TOTAL_SECTORS"
105+
25106
# Configuration: Solaris root partition size (adjust as needed)
26107
SOLARIS_ROOT_SIZE_GB=128
27108
SOLARIS_ROOT_END_MIB=$((513 + SOLARIS_ROOT_SIZE_GB * 1024))
28109

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
110+
echo ""
111+
echo "Creating GPT partition layout optimized for OpenIndiana/illumos"
112+
echo ""
113+
echo "Planned partition layout:"
114+
echo " 1. ESP (EFI System Partition) - 512MB FAT32 for UEFI boot"
115+
echo " 2. Solaris root ZFS pool - ${SOLARIS_ROOT_SIZE_GB}GB for system"
116+
echo " 3. Data partition - remaining space for user data/additional pools"
117+
echo " 4. Solaris reserved - 8MB traditional marker at end of disk"
118+
echo ""
33119

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"
120+
# Prerequisite check
121+
echo "NOTE: Ensure you have run wipe_pt.sh first for clean disk state"
122+
echo ""
123+
124+
# Get confirmation from user
125+
if ! confirm "Proceed with creating partition layout?"; then
126+
echo "Operation cancelled."
127+
exit 0
128+
fi
129+
130+
# Initialize GPT label on the disk
131+
echo ""
132+
echo "Initializing GPT label on $DISK_DEVICE..."
133+
parted "$DISK_DEVICE" mklabel gpt || \
134+
fail "Failed to create GPT label on $DISK_DEVICE"
36135

37136
# Layout: 4 partitions total
38137
# 1. ESP (EFI System Partition) - 512MB FAT32 for UEFI boot
@@ -41,53 +140,112 @@ echo "NOTE: Ensure you have run disk_hammer_illumos.sh first for clean disk stat
41140
# 4. Solaris reserved - traditional 8MB marker at end of disk
42141

43142
# ===== Partition 1: EFI System Partition (ESP) =====
143+
echo ""
44144
echo "Creating partition 1: EFI System Partition (512MB)..."
45145
# Create 512MB ESP partition starting at 1MiB for proper alignment
46-
sudo parted /dev/$DISK_DEVICE 'mkpart "EFI System Partition" fat32 1MiB 513MiB'
146+
# MiB units automatically handle sector size alignment
147+
parted "$DISK_DEVICE" \
148+
'mkpart "EFI System Partition" fat32 1MiB 513MiB' || \
149+
fail "Failed to create ESP partition"
150+
47151
# 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
152+
parted "$DISK_DEVICE" set 1 esp on || \
153+
fail "Failed to set ESP flag"
154+
parted "$DISK_DEVICE" set 1 boot on || \
155+
fail "Failed to set boot flag"
156+
50157
# 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
158+
# Handle both NVMe (p1) and SATA (1) partition naming
159+
if [[ $DISK_DEVICE =~ nvme ]]; then
160+
ESP_PARTITION="${DISK_DEVICE}p1"
161+
else
162+
ESP_PARTITION="${DISK_DEVICE}1"
163+
fi
164+
165+
mkfs.fat -F 32 -n EFI "$ESP_PARTITION" || \
166+
fail "Failed to format ESP partition"
52167

53168
# ===== Partition 2: Solaris Root Pool =====
54169
echo "Creating partition 2: Solaris root pool (${SOLARIS_ROOT_SIZE_GB}GB)..."
55170
# Create partition for OpenIndiana/illumos root ZFS pool
56-
sudo parted /dev/$DISK_DEVICE mkpart "solaris" 513MiB ${SOLARIS_ROOT_END_MIB}MiB
57-
# Set Solaris root partition type (BF00) for proper recognition by illumos tools
58-
sudo sgdisk --typecode=2:BF00 /dev/$DISK_DEVICE
171+
# MiB units automatically handle alignment for any sector size
172+
parted "$DISK_DEVICE" mkpart solaris 513MiB ${SOLARIS_ROOT_END_MIB}MiB || \
173+
fail "Failed to create Solaris root partition"
174+
175+
# Set Solaris root partition type (BF00) for proper recognition by illumos
176+
sgdisk --typecode=2:BF00 "$DISK_DEVICE" || \
177+
fail "Failed to set Solaris root partition type"
59178

60179
# ===== Partition 4: Solaris Reserved (create before data partition) =====
180+
echo ""
181+
echo "NOTE: The Solaris reserved partition is a traditional 8MB placeholder"
182+
echo "and intentionally does not require 1MiB alignment for performance."
183+
echo "Parted may display an alignment warning - this is expected and safe"
184+
echo "to ignore. If prompted, press 'i' to ignore the alignment warning."
185+
echo ""
61186
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
187+
188+
# Calculate sectors for 8MB reserved partition based on actual sector size
189+
# This works correctly for both 512-byte and 4Kn disks
190+
RESERVED_SIZE_BYTES=$((8 * 1024 * 1024)) # 8MB in bytes
191+
RESERVE_END_BYTES=$((1 * 1024 * 1024)) # Reserve last 1MB in bytes
192+
193+
# Convert bytes to sectors based on actual sector size
194+
RESERVED_SECTORS=$((RESERVED_SIZE_BYTES / SECTOR_SIZE))
195+
RESERVE_END_SECTORS=$((RESERVE_END_BYTES / SECTOR_SIZE))
196+
197+
# Calculate partition boundaries, -1 since sector numbers use 0-based indexing
198+
LAST_USABLE_SECTOR=$((TOTAL_SECTORS - RESERVE_END_SECTORS - 1))
199+
RESERVED_START_SECTOR=$((LAST_USABLE_SECTOR - RESERVED_SECTORS))
200+
201+
echo "Calculated reserved partition:"
202+
echo " Start sector: $RESERVED_START_SECTOR"
203+
echo " End sector: $LAST_USABLE_SECTOR"
204+
echo " Size: $RESERVED_SECTORS sectors (8MB)"
67205

68206
# 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
207+
parted "$DISK_DEVICE" unit s mkpart solaris_reserved \
208+
$RESERVED_START_SECTOR $LAST_USABLE_SECTOR || \
209+
fail "Failed to create Solaris reserved partition"
210+
71211
# Set Solaris reserved partition type (BF07)
72-
sudo sgdisk --typecode=4:BF07 /dev/$DISK_DEVICE
212+
# currently partition 3 even though it resides at the end of the disk
213+
sgdisk --typecode=3:BF07 "$DISK_DEVICE" || \
214+
fail "Failed to set Solaris reserved partition type"
73215

74216
# ===== Partition 3: Data Partition =====
75217
echo "Creating partition 3: Data partition (remaining space)..."
76-
# 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
79-
# Set Solaris /home partition type (BF05) for user data
80-
sudo sgdisk --typecode=3:BF05 /dev/$DISK_DEVICE
218+
# Create data partition using remaining space between root and reserved
219+
# Calculate the end position in MiB - parted will automatically adjust
220+
RESERVED_START_MB=$((RESERVED_START_SECTOR * SECTOR_SIZE / 1024 / 1024))
221+
222+
echo "Data partition end: ${RESERVED_START_MB}MiB"
223+
224+
parted "$DISK_DEVICE" mkpart data \
225+
${SOLARIS_ROOT_END_MIB}MiB ${RESERVED_START_MB}MiB || \
226+
fail "Failed to create data partition"
227+
228+
# Set Solaris /home partition type (BF05) - currently partition 4
229+
sgdisk --typecode=4:BF05 "$DISK_DEVICE" || \
230+
fail "Failed to set data partition type"
231+
232+
# ===== Sort partitions to correct logical order =====
233+
echo "Sorting partitions to proper numerical order..."
234+
sgdisk --sort "$DISK_DEVICE" || \
235+
fail "Failed to sort partitions"
81236

82237
# ===== Display final partition layout =====
83238
echo ""
84-
echo "Partition layout created successfully:"
85-
sudo parted /dev/$DISK_DEVICE print
239+
echo "SUCCESS: Partition layout created successfully!"
240+
echo ""
241+
sgdisk --print "$DISK_DEVICE" || \
242+
echo "Warning: Could not display partition table"
86243
echo ""
87-
echo "Partition type codes set:"
244+
echo "Final partition type codes:"
88245
echo " Partition 1: ESP (EFI System Partition)"
89-
echo " Partition 2: BF00 (Solaris root)"
90-
echo " Partition 3: BF05 (Solaris /home)"
246+
echo " Partition 2: BF00 (Solaris root)"
247+
echo " Partition 3: BF05 (Solaris /home - data)"
91248
echo " Partition 4: BF07 (Solaris reserved)"
92249
echo ""
93250
echo "Ready for OpenIndiana/illumos installation!"
251+
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)