# NetBSD Root Disk Expansion Guide

This guide explains how to safely increase the root file system (`/`) size on NetBSD using GPT partitioning when a swap partition blocks direct expansion.

## Overview of the Process
You cannot grow a partition if another partition (like swap) sits directly after it. Because swap contains no persistent data, you can safely turn it off, delete it, grow your root data partition, and recreate swap at the very end of the disk.

*Note: NetBSD cannot dynamically resize a wedge (`dk0`) while it is actively mounted as the writable root directory in multi-user mode. The process will trigger a single-user recovery shell to safely finalize the filesystem expansion.*

---

## Step-by-Step Procedure

### 1. Update the Disk Table
Target the raw physical disk (e.g., `sd0` or `wd0`), not the virtual wedge partition (`dk0`). Update the backup GPT headers to recognize the new physical disk limits:
```bash
gpt resizedisk sd0
```

### 2. Remove the Blocking Swap Partition
Turn off active swap and delete its partition layout:
```bash
swapctl -d /dev/dk1
gpt remove -i 2 sd0
```

### 3. Resize the Data Partition
Grow your primary NetBSD data partition (Index 1) into the unallocated space. Leave enough sectors at the end of the disk if you plan to recreate your swap space:
```bash
gpt resize -i 1 -s <target_sector_size> sd0
```

### 4. Recreate the Swap Partition
Put the swap partition back into the remaining free space at the absolute end of the disk:
```bash
gpt add -i 2 -t swap sd0
```

### 5. Reboot into Single-User Mode
A reboot is required to force the kernel to reload the new wedge dimensions. Because the root filesystem sizing is mismatched, the server will safely drop into a single-user recovery prompt (`#`):
```bash
reboot
```

### 6. Finalize Filesystem Expansion (Recovery Prompt)
Once you reach the single-user root prompt (`#`), make the root filesystem writable and run the expansion utilities directly against the raw device path:

```bash
# 1. Remount root as writable
mount -u -w /

# 2. Force check the raw wedge device (ignore "BAD SUPER BLOCK" warnings)
fsck -y /dev/rdk0

# 3. Grow the FFS filesystem structure (Type "Yes" when prompted about fsck)
resize_ffs -v /dev/rdk0
```

### 7. Reboot to Normal Mode and Verify
Restart the server one final time to return to normal multi-user mode and verify the new space allocation:

```bash
# 1. Reboot the system
reboot

# 2. (After login) Verify the new capacity
df -h /
```
