aboutsummaryrefslogtreecommitdiff
path: root/custom/lib/disk.sh
blob: ea8c4022ca0ca25a6717c6262bd20a8e06c90d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
# disk.sh - Disk partitioning functions for archangel installer
# Source this file after common.sh

#############################
# Partition Disks
#############################

# Partition a single disk for ZFS/Btrfs installation
# Creates: EFI partition (512M) + root partition (rest)
partition_disk() {
    local disk="$1"
    local efi_size="${2:-512M}"

    info "Partitioning $disk..."

    # Wipe existing partition table
    sgdisk --zap-all "$disk" || error "Failed to wipe $disk"

    # Create EFI partition (512M, type EF00)
    sgdisk -n 1:0:+${efi_size} -t 1:EF00 -c 1:"EFI" "$disk" || error "Failed to create EFI partition on $disk"

    # Create root partition (rest of disk, type BF00 for ZFS or 8300 for Linux)
    sgdisk -n 2:0:0 -t 2:BF00 -c 2:"ROOT" "$disk" || error "Failed to create root partition on $disk"

    # Notify kernel of partition changes
    partprobe "$disk" 2>/dev/null || true
    sleep 1

    info "Partitioned $disk: EFI=${efi_size}, ROOT=remainder"
}

# Partition multiple disks (for RAID configurations)
partition_disks() {
    local efi_size="${1:-512M}"
    shift
    local disks=("$@")

    for disk in "${disks[@]}"; do
        partition_disk "$disk" "$efi_size"
    done
}

#############################
# Partition Helpers
#############################

# Get EFI partition path for a disk
get_efi_partition() {
    local disk="$1"
    if [[ "$disk" =~ nvme ]]; then
        echo "${disk}p1"
    else
        echo "${disk}1"
    fi
}

# Get root partition path for a disk
get_root_partition() {
    local disk="$1"
    if [[ "$disk" =~ nvme ]]; then
        echo "${disk}p2"
    else
        echo "${disk}2"
    fi
}

# Get all root partitions from disk array
get_root_partitions() {
    local disks=("$@")
    local parts=()
    for disk in "${disks[@]}"; do
        parts+=("$(get_root_partition "$disk")")
    done
    printf '%s\n' "${parts[@]}"
}

# Get all EFI partitions from disk array
get_efi_partitions() {
    local disks=("$@")
    local parts=()
    for disk in "${disks[@]}"; do
        parts+=("$(get_efi_partition "$disk")")
    done
    printf '%s\n' "${parts[@]}"
}

#############################
# EFI Partition Management
#############################

# Format EFI partition
format_efi() {
    local partition="$1"
    local label="${2:-EFI}"

    info "Formatting EFI partition: $partition"
    mkfs.fat -F32 -n "$label" "$partition" || error "Failed to format EFI: $partition"
}

# Format all EFI partitions
format_efi_partitions() {
    local disks=("$@")
    local first=true

    for disk in "${disks[@]}"; do
        local efi=$(get_efi_partition "$disk")
        if $first; then
            format_efi "$efi" "EFI"
            first=false
        else
            format_efi "$efi" "EFI2"
        fi
    done
}

# Mount EFI partition
mount_efi() {
    local partition="$1"
    local mount_point="${2:-/mnt/efi}"

    mkdir -p "$mount_point"
    mount "$partition" "$mount_point" || error "Failed to mount EFI at $mount_point"
    info "Mounted EFI: $partition -> $mount_point"
}

#############################
# Disk Selection (Interactive)
#############################

# Interactive disk selection using fzf
select_disks() {
    local available
    available=$(list_available_disks)

    if [[ -z "$available" ]]; then
        error "No available disks found"
    fi

    step "Select installation disk(s)"
    prompt "Use Tab to select multiple disks for RAID, Enter to confirm"

    local selected
    if has_fzf; then
        selected=$(echo "$available" | fzf --multi --prompt="Select disk(s): " --height=15 --reverse)
    else
        echo "$available"
        read -rp "Enter disk path(s) separated by space: " selected
    fi

    [[ -z "$selected" ]] && error "No disk selected"

    # Extract just the device paths (remove size/model info)
    SELECTED_DISKS=()
    while IFS= read -r line; do
        local disk=$(echo "$line" | cut -d' ' -f1)
        SELECTED_DISKS+=("$disk")
    done <<< "$selected"

    info "Selected disks: ${SELECTED_DISKS[*]}"
}

#############################
# RAID Level Selection
#############################

select_raid_level() {
    local num_disks=${#SELECTED_DISKS[@]}

    if [[ $num_disks -eq 1 ]]; then
        RAID_LEVEL=""
        info "Single disk - no RAID"
        return
    fi

    step "Select RAID level"

    local options=()
    options+=("mirror - Mirror data across disks (recommended)")

    if [[ $num_disks -ge 3 ]]; then
        options+=("raidz1 - Single parity, lose 1 disk capacity")
    fi
    if [[ $num_disks -ge 4 ]]; then
        options+=("raidz2 - Double parity, lose 2 disks capacity")
    fi

    local selected
    selected=$(fzf_select "RAID level:" "${options[@]}")
    RAID_LEVEL=$(echo "$selected" | cut -d' ' -f1)

    info "Selected RAID level: $RAID_LEVEL"
}