File create_data_part of Package data-partition-service

#!/bin/bash

# Read configuration file
source /etc/data_part.cfg || exit 0

# Functions space
function get_root_device {
    local root_device=
    for boot_option in $(cat /proc/cmdline);do
        if [[ "${boot_option}" =~ ^root=UUID ]];then
            root_device="/dev/disk/by-uuid/${boot_option#root=UUID=}"
            break
        fi
    done
    if [ -z "${root_device}" ]; then
        root_device=$(findmnt / -no SOURCE)
    fi
    echo "${root_device}"
}

function lookup_disk_device_from_root {
    root_device=$(get_root_device)
    if [ -L "${root_device}" ];then
        root_device=/dev/$(basename "$(readlink "${root_device}")")
    fi
    lsblk -p -n -r -s -o NAME,TYPE "${root_device}" |\
        grep disk | cut -f1 -d ' '
}

function _partition_count {
    if [ -z "$1" ]; then
        echo 0
    else
        lsblk "$1" -p -r -n -o TYPE | grep -c part
    fi
}

[[ -n "${data_label}" ]] || exit 1

# check data partition by label, or create one
if ! blkid -L "${data_label}"; then
    root_disk=$(lookup_disk_device_from_root)
    partitions_origin=$(_partition_count "${root_disk}")

    # Check if the disk has been expanded
    if sgdisk -v "${root_disk}" | grep -q "secondary header"; then
        sgdisk --move-second-header "${root_disk}"
    fi
    sgdisk -n=0:0:0 "${root_disk}"

    partprobe "${root_disk}" || exit 1

    if [ "$(_partition_count "${root_disk}")" -gt "${partitions_origin}" ];then
        # create filesystem on data partition
        data_partition=$(lsblk "${root_disk}" -p -r -n -o NAME | tail -n1)
        mkfs."${data_fs}" -L "${data_label}" "${data_partition}" || exit 1
    fi
fi

openSUSE Build Service is sponsored by