<commit_before>#!/bin/bash

set -e

hostname=''
declare -a hdds
declare -a ssds
zlogsize='1024MiB'
test_only=1
pool_name=''
mount_path='/mnt/rl-zfs-inst'

print_help()
{
    echo "Usage: $0 -h hostname -d (hdd-id ...) -s (ssd-id ...) [-l zlogsize] [-r]" 1>&2
}

cmd()
{
    echo "$@"
    if [ $test_only -eq 0 ]; then
        "$@"
        return $?
    else
        return 0
    fi
}

while getopts "h:d:s:l:rp:m:" opt; do
    case $opt in
    h)
        hostname=$OPTARG
    ;;
    d)
        hdds+=("$OPTARG")
    ;;
    s)
        ssds+=("$OPTARG")
    ;;
    l)
        zlogsize="$OPTARG"
    ;;
    p)
        pool_name="$OPTARG"
    ;;
    m)
        mount_path="$OPTARG"
    ;;
    r)
        test_only=0
    ;;
    \?)
        echo "Invalid option: -$OPTARG" >&2
        exit 1
    ;;
    :)
        echo "Option -$OPTARG requires an argument." >&2
        exit 1
    ;;
    esac
done

if [ -z "$hostname" ] || [ -z "$zlogsize" ]; then
    print_help
    exit 1
fi

if [ -z "$pool_name" ]; then
    pool_name="$hostname"
fi

echo "Using hostname '$hostname'"
old_hostname=$(hostname)
trap 'hostname ${old_hostname}' EXIT 
hostname ${hostname}

hdd_count="${#hdds[@]}" 
ssd_count="${#ssds[@]}"

if (( $hdd_count < 2 )) || (( $hdd_count % 2 != 0 )); then
    echo "Invalid HDD count ${hdd_count}: must be multiple of 2, non-zero"
    exit 1
fi

if (( $ssd_count < 2 )) || (( $ssd_count % 2 != 0 )); then
    echo "Invalid SSD count ${ssd_count}: must be multiple of 2, non-zero"
    exit 1
fi

check_disks()
{
    dest_var=$1
    shift

    local -a disks
    disks=("$@")

    local -A disk_devs
    for disk in "${disks[@]}"; do
        echo -n "- $disk => "
        if ! [ -e "/dev/disk/by-id/${disk}" ]; then
            echo "NOT FOUND"
            exit 1
        fi

        dev=$(readlink -f "/dev/disk/by-id/${disk}")
        echo "$dev"

        eval "${dest_var}[$disk]=\"$dev\""
    done
}

echo "Using ${hdd_count} HDDs: "
declare -A hdd_devs
check_disks "hdd_devs" "${hdds[@]}"
echo

echo "Using ${ssd_count} SSDs: "
declare -A ssd_devs
check_disks "ssd_devs" "${ssds[@]}"
echo

boot_ssd="${ssds[0]}"
echo "Using disk ${boot_ssd} for boot"

swap_ssd="${ssds[1]}"
echo "Using disk ${swap_ssd} for swap"

slog_ssds=("${boot_ssd}" "${swap_ssd}")
echo "Using ZIL SLOG size of ${zlogsize} on disks:"
for ssd in "${slog_ssds[@]}"; do
    echo "- ${ssd}"
done

echo 
read -p "Is everything right? Type YES to proceeed: " -r
if ! [ "$REPLY" == "YES" ]; then
    exit 1
fi

read -p "Are you sure? Type IAMSURE to proceeed: " -r
if ! [ "$REPLY" == "IAMSURE" ]; then
    exit 1
fi

echo "* Formatting SSDs"

SGDISK="sgdisk -a 2048"

array_contains() {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

for ssd in "${ssds[@]}"; do
    echo "** Formatting ${ssd}"

    SGDISK_SSD="${SGDISK} /dev/disk/by-id/${ssd}"

    cmd $SGDISK_SSD --clear

    if [ "$ssd" = "$boot_ssd" ]; then
        echo "** Creating boot partitions"
        
        cmd $SGDISK_SSD --new=1:1M:+255M \
          -c 1:"EFI System Partition" \
          -t 1:"ef00"
        cmd $SGDISK_SSD --new=2:0:+256M \
          -c 2:"/boot" \
          -t 2:"8300"

        sleep 1

        cmd mkfs.vfat "/dev/disk/by-id/${ssd}-part1"<commit_msg>zfs: update<commit_after>#!/bin/bash

set -e

hostname=''
declare -a hdds
declare -a ssds
zlogsize='1024MiB'
test_only=1
pool_name=''
mount_path='/mnt/rl-zfs-inst'

print_help()
{
    echo "Usage: $0 -h hostname -d (hdd-id ...) -s (ssd-id ...) [-l zlogsize] [-r]" 1>&2
}

cmd()
{
    echo "$@"
    if [ $test_only -eq 0 ]; then
        "$@"
        return $?
    else
        return 0
    fi
}

array_contains()
{
    local e
    for e in "${@:2}"; do
        [[ "$e" == "$1" ]] && return 0
    done
    return 1
}

while getopts "h:d:s:l:rp:m:" opt; do
    case $opt in
    h)
        hostname=$OPTARG
    ;;
    d)
        hdds+=("$OPTARG")
    ;;
    s)
        ssds+=("$OPTARG")
    ;;
    l)
        zlogsize="$OPTARG"
    ;;
    p)
        pool_name="$OPTARG"
    ;;
    m)
        mount_path="$OPTARG"
    ;;
    r)
        test_only=0
    ;;
    \?)
        echo "Invalid option: -$OPTARG" >&2
        exit 1
    ;;
    :)
        echo "Option -$OPTARG requires an argument." >&2
        exit 1
    ;;
    esac
done

if [ -z "$hostname" ] || [ -z "$zlogsize" ]; then
    print_help
    exit 1
fi

if [ -z "$pool_name" ]; then
    pool_name="$hostname"
fi

echo "Using hostname '$hostname'"
old_hostname=$(hostname)
trap 'hostname ${old_hostname}' EXIT 
hostname ${hostname}

hdd_count="${#hdds[@]}" 
ssd_count="${#ssds[@]}"

if (( $hdd_count < 2 )) || (( $hdd_count % 2 != 0 )); then
    echo "Invalid HDD count ${hdd_count}: must be multiple of 2, non-zero"
    exit 1
fi

if (( $ssd_count < 2 )) || (( $ssd_count % 2 != 0 )); then
    echo "Invalid SSD count ${ssd_count}: must be multiple of 2, non-zero"
    exit 1
fi

check_disks()
{
    dest_var=$1
    shift

    local -a disks
    disks=("$@")

    local -A disk_devs
    for disk in "${disks[@]}"; do
        echo -n "- $disk => "
        if ! [ -e "/dev/disk/by-id/${disk}" ]; then
            echo "NOT FOUND"
            exit 1
        fi

        dev=$(readlink -f "/dev/disk/by-id/${disk}")
        echo "$dev"

        eval "${dest_var}[$disk]=\"$dev\""
    done
}

echo "Using ${hdd_count} HDDs: "
declare -A hdd_devs
check_disks "hdd_devs" "${hdds[@]}"
echo

echo "Using ${ssd_count} SSDs: "
declare -A ssd_devs
check_disks "ssd_devs" "${ssds[@]}"
echo

boot_ssd="${ssds[0]}"
echo "Using disk ${boot_ssd} for boot"

swap_ssd="${ssds[1]}"
echo "Using disk ${swap_ssd} for swap"

slog_ssds=("${boot_ssd}" "${swap_ssd}")
echo "Using ZIL SLOG size of ${zlogsize} on disks:"
for ssd in "${slog_ssds[@]}"; do
    echo "- ${ssd}"
done

echo 

if [ $test_only -eq 0 ]; then
    read -p "Is everything right? Type YES to proceeed: " -r
    if ! [ "$REPLY" == "YES" ]; then
        exit 1
    fi

    read -p "Are you sure? Type IAMSURE to proceeed: " -r
    if ! [ "$REPLY" == "IAMSURE" ]; then
        exit 1
    fi
fi

echo "* Formatting SSDs"

SGDISK="sgdisk -a 2048"

for ssd in "${ssds[@]}"; do
    echo "** Formatting ${ssd}"

    SGDISK_SSD="${SGDISK} /dev/disk/by-id/${ssd}"

    cmd $SGDISK_SSD --clear

    if [ "$ssd" = "$boot_ssd" ]; then
        echo "** Creating boot partitions"
        
        cmd $SGDISK_SSD --new=1:1M:+255M \
          -c 1:"EFI System Partition" \
          -t 1:"ef00"
        cmd $SGDISK_SSD --new=2:0:+256M \
          -c 2:"/boot" \
          -t 2:"8300"

        sleep 1

        cmd mkfs.vfat "/dev/disk/by-id/${ssd}-part1"
{'fraction_non_alphanumeric': 0.18572513287775247, 'fraction_numerical': 0.018982536066818528, 'mean_word_length': 2.181642512077295, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7441625', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2656, 'n_words': 686}