init/functions: rewrite
Signed-off-by: AuxXxilium <info@auxxxilium.tech>
This commit is contained in:
@@ -186,74 +186,15 @@ function _set_conf_kv() {
|
||||
echo "${1}=\"${2}\"" >>"${3}"
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# get bus of disk
|
||||
# 1 - device path
|
||||
function getBus() {
|
||||
BUS=""
|
||||
# usb/ata(sata/ide)/scsi
|
||||
[ -z "${BUS}" ] && BUS=$(udevadm info --query property --name "${1}" 2>/dev/null | grep ID_BUS | cut -d= -f2 | sed 's/ata/sata/')
|
||||
# usb/sata(sata/ide)/nvme
|
||||
[ -z "${BUS}" ] && BUS=$(lsblk -dpno KNAME,TRAN 2>/dev/null | grep "${1} " | awk '{print $2}')
|
||||
# usb/scsi(sata/ide)/virtio(scsi/virtio)/mmc/nvme
|
||||
[ -z "${BUS}" ] && BUS=$(lsblk -dpno KNAME,SUBSYSTEMS 2>/dev/null | grep "${1} " | awk -F':' '{print $(NF-1)}' | sed 's/_host//')
|
||||
echo "${BUS}"
|
||||
return 0
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# get IP
|
||||
# 1 - ethN
|
||||
function getIP() {
|
||||
IP=""
|
||||
if [[ -n "${1}" && -d "/sys/class/net/${1}" ]]; then
|
||||
IP=$(ip route show dev ${1} 2>/dev/null | sed -n 's/.* via .* src \(.*\) metric .*/\1/p')
|
||||
[ -z "${IP}" ] && IP=$(ip addr show ${1} | grep -E "inet .* eth" | awk '{print $2}' | cut -f1 -d'/' | head -1)
|
||||
else
|
||||
IP=$(ip route show 2>/dev/null | sed -n 's/.* via .* src \(.*\) metric .*/\1/p' | head -1)
|
||||
[ -z "${IP}" ] && IP=$(ip addr show | grep -E "inet .* eth" | awk '{print $2}' | cut -f1 -d'/' | head -1)
|
||||
fi
|
||||
echo "${IP}"
|
||||
return 0
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# Find and mount the DSM root filesystem
|
||||
# (based on pocopico's TCRP code)
|
||||
function findAndMountDSMRoot() {
|
||||
[ $(mount | grep -i "${TMP_PATH}/mdX" | wc -l) -gt 0 ] && return 0
|
||||
dsmrootdisk="$(blkid | grep -i linux_raid_member | grep -E "/dev/.*1:" | head -1 | awk -F ":" '{print $1}')"
|
||||
[ -z "${dsmrootdisk}" ] && return 1
|
||||
[ ! -d "${TMP_PATH}/mdX" ] && mkdir -p "${TMP_PATH}/mdX"
|
||||
[ $(mount | grep -i "${TMP_PATH}/mdX" | wc -l) -eq 0 ] && mount -t ext4 "${dsmrootdisk}" "${TMP_PATH}/mdX"
|
||||
if [ $(mount | grep -i "${TMP_PATH}/mdX" | wc -l) -eq 0 ]; then
|
||||
echo "Failed to mount"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# Convert Netmask eq. 255.255.255.0 to /24
|
||||
# 1 - Netmask
|
||||
function convert_netmask() {
|
||||
bits=0
|
||||
for octet in $(echo $1| sed 's/\./ /g'); do
|
||||
binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g')
|
||||
bits=$((${bits} + ${#binbits}))
|
||||
done
|
||||
echo "${bits}"
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# sort netif name
|
||||
# @1 -mac1,mac2,mac3...
|
||||
function _sort_netif() {
|
||||
ETHLIST=""
|
||||
ETHX=$(ls /sys/class/net/ | grep eth) # real network cards list
|
||||
ETHX=$(ls /sys/class/net/ 2>/dev/null | grep eth) # real network cards list
|
||||
for ETH in ${ETHX}; do
|
||||
MAC="$(cat /sys/class/net/${ETH}/address | sed 's/://g' | tr '[:upper:]' '[:lower:]')"
|
||||
BUS=$(ethtool -i ${ETH} | grep bus-info | awk '{print $2}')
|
||||
MAC="$(cat /sys/class/net/${ETH}/address 2>/dev/null | sed 's/://g' | tr '[:upper:]' '[:lower:]')"
|
||||
BUS=$(ethtool -i ${ETH} 2>/dev/null | grep bus-info | awk '{print $2}')
|
||||
ETHLIST="${ETHLIST}${BUS} ${MAC} ${ETH}\n"
|
||||
done
|
||||
|
||||
@@ -289,7 +230,7 @@ EOF
|
||||
[ ${IDX} -ge $(wc -l <${TMP_PATH}/ethlist) ] && break
|
||||
ETH=$(cat ${TMP_PATH}/ethlist | sed -n "$((${IDX} + 1))p" | awk '{print $3}')
|
||||
# echo "ETH: ${ETH}"
|
||||
if [[ -n "${ETH}" && ! "${ETH}" = "eth${IDX}" ]]; then
|
||||
if [ -n "${ETH}" ] && [ ! "${ETH}" = "eth${IDX}" ]; then
|
||||
# echo "change ${ETH} <=> eth${IDX}"
|
||||
ip link set dev eth${IDX} down
|
||||
ip link set dev ${ETH} down
|
||||
@@ -313,6 +254,65 @@ EOF
|
||||
return 0
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# get bus of disk
|
||||
# 1 - device path
|
||||
function getBus() {
|
||||
BUS=""
|
||||
# usb/ata(sata/ide)/scsi
|
||||
[ -z "${BUS}" ] && BUS=$(udevadm info --query property --name "${1}" 2>/dev/null | grep ID_BUS | cut -d= -f2 | sed 's/ata/sata/')
|
||||
# usb/sata(sata/ide)/nvme
|
||||
[ -z "${BUS}" ] && BUS=$(lsblk -dpno KNAME,TRAN 2>/dev/null | grep "${1} " | awk '{print $2}') #Spaces are intentional
|
||||
# usb/scsi(sata/ide)/virtio(scsi/virtio)/mmc/nvme
|
||||
[ -z "${BUS}" ] && BUS=$(lsblk -dpno KNAME,SUBSYSTEMS 2>/dev/null | grep "${1} " | awk -F':' '{print $(NF-1)}' | sed 's/_host//') #Spaces are intentional
|
||||
echo "${BUS}"
|
||||
return 0
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# get IP
|
||||
# 1 - ethN
|
||||
function getIP() {
|
||||
IP=""
|
||||
if [ -n "${1}" -a -d "/sys/class/net/${1}" ]; then
|
||||
IP=$(ip route show dev ${1} 2>/dev/null | sed -n 's/.* via .* src \(.*\) metric .*/\1/p')
|
||||
[ -z "${IP}" ] && IP=$(ip addr show ${1} scope global 2>/dev/null | grep -E "inet .* eth" | awk '{print $2}' | cut -f1 -d'/' | head -1)
|
||||
else
|
||||
IP=$(ip route show 2>/dev/null | sed -n 's/.* via .* src \(.*\) metric .*/\1/p' | head -1)
|
||||
[ -z "${IP}" ] && IP=$(ip addr show scope global 2>/dev/null | grep -E "inet .* eth" | awk '{print $2}' | cut -f1 -d'/' | head -1)
|
||||
fi
|
||||
echo "${IP}"
|
||||
return 0
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# Find and mount the DSM root filesystem
|
||||
# (based on pocopico's TCRP code)
|
||||
function findAndMountDSMRoot() {
|
||||
[ $(mount | grep -i "${TMP_PATH}/mdX" | wc -l) -gt 0 ] && return 0
|
||||
dsmrootdisk="$(blkid | grep -i linux_raid_member | grep -E "/dev/.*1:" | head -1 | awk -F ":" '{print $1}')"
|
||||
[ -z "${dsmrootdisk}" ] && return 1
|
||||
[ ! -d "${TMP_PATH}/mdX" ] && mkdir -p "${TMP_PATH}/mdX"
|
||||
[ $(mount | grep -i "${TMP_PATH}/mdX" | wc -l) -eq 0 ] && mount -t ext4 "${dsmrootdisk}" "${TMP_PATH}/mdX"
|
||||
if [ $(mount | grep -i "${TMP_PATH}/mdX" | wc -l) -eq 0 ]; then
|
||||
echo "Failed to mount"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# Convert Netmask eq. 255.255.255.0 to /24
|
||||
# 1 - Netmask
|
||||
function convert_netmask() {
|
||||
bits=0
|
||||
for octet in $(echo $1| sed 's/\./ /g'); do
|
||||
binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g')
|
||||
bits=$((${bits} + ${#binbits}))
|
||||
done
|
||||
echo "${bits}"
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# Livepatch
|
||||
function livepatch() {
|
||||
@@ -403,4 +403,54 @@ function rebootTo() {
|
||||
[ ! -f "${ENVFILE}" ] && grub-editenv ${ENVFILE} create
|
||||
grub-editenv ${ENVFILE} set next_entry="${1}"
|
||||
reboot
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# Config Init
|
||||
# If user config file not exists, initialize it
|
||||
function configInit() {
|
||||
if [ ! -f "${USER_CONFIG_FILE}" ]; then
|
||||
touch "${USER_CONFIG_FILE}"
|
||||
fi
|
||||
initConfigKey "lkm" "prod" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "model" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "productver" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "layout" "qwertz" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "keymap" "de" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "zimage-hash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "ramdisk-hash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "cmdline" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "synoinfo" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "addons" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.confdone" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.builddone" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.paturl" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.pathash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.sn" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.ipv6" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.emmcboot" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.offline" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.directboot" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.patch" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.pathash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.paturl" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.bootipwait" "20" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.kernelload" "power" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.kernelpanic" "5" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.macsys" "hardware" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.odp" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.modulescopy" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.hddsort" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.usbmount" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.kernel" "official" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.custom" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.version" "${ARC_VERSION}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "device" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "device.externalcontroller" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "ip" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "netmask" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "mac" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "static" "{}" "${USER_CONFIG_FILE}"
|
||||
}
|
||||
@@ -28,51 +28,8 @@ TITLE="Boot:"
|
||||
TITLE+=" [${BUS}]"
|
||||
printf "\033[1;34m%*s\033[0m\n" $(((${#TITLE} + ${COLUMNS}) / 2)) "${TITLE}"
|
||||
|
||||
# If user config file not exists, initialize it
|
||||
if [ ! -f "${USER_CONFIG_FILE}" ]; then
|
||||
touch "${USER_CONFIG_FILE}"
|
||||
fi
|
||||
initConfigKey "lkm" "prod" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "model" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "productver" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "layout" "qwertz" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "keymap" "de" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "zimage-hash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "ramdisk-hash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "cmdline" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "synoinfo" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "addons" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "modules" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.confdone" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.builddone" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.paturl" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.pathash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.sn" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.ipv6" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.emmcboot" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.offline" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.directboot" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.patch" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.pathash" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.paturl" "" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.bootipwait" "20" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.kernelload" "power" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.kernelpanic" "5" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.macsys" "hardware" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.odp" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.modulescopy" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.hddsort" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.usbmount" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.kernel" "official" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.custom" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "arc.version" "${ARC_VERSION}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "device" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "device.externalcontroller" "false" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "ip" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "netmask" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "mac" "{}" "${USER_CONFIG_FILE}"
|
||||
initConfigKey "static" "{}" "${USER_CONFIG_FILE}"
|
||||
# Init User Config
|
||||
configInit
|
||||
|
||||
# Init Network
|
||||
ETHX=$(ls /sys/class/net/ 2>/dev/null | grep eth) || true
|
||||
|
||||
Reference in New Issue
Block a user