Skip to content

Commit

Permalink
RR init 2
Browse files Browse the repository at this point in the history
  • Loading branch information
wjz304 committed Oct 28, 2023
1 parent a0e446c commit ab83c4b
Show file tree
Hide file tree
Showing 30 changed files with 2,161 additions and 1,810 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ cache
files/initrd/opt/rr/lang/*.mo
tests
/extractor
/br
53 changes: 29 additions & 24 deletions files/initrd/opt/rr/boot.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env bash

set -e
[ -z "${WORK_PATH}" -o ! -d "${WORK_PATH}/include" ] && WORK_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

. /opt/rr/include/functions.sh
. ${WORK_PATH}/include/functions.sh

[ -z "${LOADER_DISK}" ] && die "$(TEXT "Loader is not init!")"
# Sanity check
loaderIsConfigured || die "$(TEXT "Loader is not configured!")"

# Check if machine has EFI
[ -d /sys/firmware/efi ] && EFI=1 || EFI=0

LOADER_DISK="$(blkid | grep 'LABEL="RR3"' | cut -d3 -f1)"
BUS=$(udevadm info --query property --name ${LOADER_DISK} | grep ID_BUS | cut -d= -f2)
[ "${BUS}" = "ata" ] && BUS="sata"
BUS=$(getBus "${LOADER_DISK}")

# Print text centralized
clear
Expand All @@ -23,20 +23,15 @@ printf "\033[1;44m%*s\033[A\n" ${COLUMNS} ""
printf "\033[1;32m%*s\033[0m\n" $(((${#TITLE} + ${COLUMNS}) / 2)) "${TITLE}"
printf "\033[1;44m%*s\033[0m\n" ${COLUMNS} ""
TITLE="BOOTING:"
[ -d "/sys/firmware/efi" ] && TITLE+=" [UEFI]" || TITLE+=" [BIOS]"
[ ${EFI} -eq 1 ] && TITLE+=" [UEFI]" || TITLE+=" [BIOS]"
[ "${BUS}" = "usb" ] && TITLE+=" [${BUS^^} flashdisk]" || TITLE+=" [${BUS^^} DoM]"
printf "\033[1;33m%*s\033[0m\n" $(((${#TITLE} + ${COLUMNS}) / 2)) "${TITLE}"

DSMLOGO="$(readConfigKey "dsmlogo" "${USER_CONFIG_FILE}")"
if [ "${DSMLOGO}" = "true" -a -c "/dev/fb0" -a -f "${CACHE_PATH}/logo.png" ]; then
echo | fbv -acuf "${CACHE_PATH}/logo.png" >/dev/null 2>/dev/null || true
fi

# Check if DSM zImage changed, patch it if necessary
ZIMAGE_HASH="$(readConfigKey "zimage-hash" "${USER_CONFIG_FILE}")"
if [ "$(sha256sum "${ORI_ZIMAGE_FILE}" | awk '{print$1}')" != "${ZIMAGE_HASH}" ]; then
if [ -f ${PART1_PATH}/.build -o "$(sha256sum "${ORI_ZIMAGE_FILE}" | awk '{print$1}')" != "${ZIMAGE_HASH}" ]; then
echo -e "\033[1;43m$(TEXT "DSM zImage changed")\033[0m"
/opt/rr/zimage-patch.sh
${WORK_PATH}/zimage-patch.sh
if [ $? -ne 0 ]; then
dialog --backtitle "$(backtitle)" --colors --title "$(TEXT "Error")" \
--msgbox "$(TEXT "zImage not patched,\nPlease upgrade the bootloader version and try again.\nPatch error:\n")$(<"${LOG_FILE}")" 12 70
Expand All @@ -47,9 +42,9 @@ fi
# Check if DSM ramdisk changed, patch it if necessary
RAMDISK_HASH="$(readConfigKey "ramdisk-hash" "${USER_CONFIG_FILE}")"
RAMDISK_HASH_CUR="$(sha256sum "${ORI_RDGZ_FILE}" | awk '{print $1}')"
if [ "${RAMDISK_HASH_CUR}" != "${RAMDISK_HASH}" ]; then
if [ -f ${PART1_PATH}/.build -o "${RAMDISK_HASH_CUR}" != "${RAMDISK_HASH}" ]; then
echo -e "\033[1;43m$(TEXT "DSM Ramdisk changed")\033[0m"
/opt/rr/ramdisk-patch.sh
${WORK_PATH}/ramdisk-patch.sh
if [ $? -ne 0 ]; then
dialog --backtitle "$(backtitle)" --colors --title "$(TEXT "Error")" \
--msgbox "$(TEXT "Ramdisk not patched,\nPlease upgrade the bootloader version and try again.\nPatch error:\n")$(<"${LOG_FILE}")" 12 70
Expand All @@ -58,6 +53,7 @@ if [ "${RAMDISK_HASH_CUR}" != "${RAMDISK_HASH}" ]; then
# Update SHA256 hash
writeConfigKey "ramdisk-hash" "${RAMDISK_HASH_CUR}" "${USER_CONFIG_FILE}"
fi
[ -f ${PART1_PATH}/.build ] && rm -f ${PART1_PATH}/.build

# Load necessary variables
MODEL="$(readConfigKey "model" "${USER_CONFIG_FILE}")"
Expand All @@ -77,27 +73,29 @@ echo -e "$(TEXT "DMI: ") \033[1;36m${DMI}\033[0m"
echo -e "$(TEXT "CPU: ") \033[1;36m${CPU}\033[0m"
echo -e "$(TEXT "MEM: ") \033[1;36m${MEM}\033[0m"

if [ ! -f "${MODEL_CONFIG_PATH}/${MODEL}.yml" ] || [ -z "$(readConfigKey "productvers.[${PRODUCTVER}]" "${MODEL_CONFIG_PATH}/${MODEL}.yml")" ]; then
if [ ! -f "${WORK_PATH}/model-configs/${MODEL}.yml" ] || [ -z "$(readModelKey ${MODEL} "productvers.[${PRODUCTVER}]")" ]; then
echo -e "\033[1;33m*** $(printf "$(TEXT "The current version of bootloader does not support booting %s-%s, please upgrade and rebuild.")" "${MODEL}" "${PRODUCTVER}") ***\033[0m"
exit 1
fi

HASATA=0
for D in $(lsblk -dnp -o name); do
for D in $(lsblk -dpno NAME); do
[ "${D}" = "${LOADER_DISK}" ] && continue
if [ "$(udevadm info --query property --name ${D} | grep ID_BUS | cut -d= -f2)" = "ata" ]; then
if [ "$(getBus "${D}")" = "sata" -o "$(getBus "${D}")" = "scsi" ]; then
HASATA=1
break
fi
done
[ ${HASATA} = "0" ] && echo -e "\033[1;33m*** $(TEXT "Please insert at least one sata disk for system installation, except for the bootloader disk.") ***\033[0m"
[ ${HASATA} = "0" ] && echo -e "\033[1;33m*** $(TEXT "Please insert at least one sata/scsi disk for system installation, except for the bootloader disk.") ***\033[0m"

VID="$(readConfigKey "vid" "${USER_CONFIG_FILE}")"
PID="$(readConfigKey "pid" "${USER_CONFIG_FILE}")"
SN="$(readConfigKey "sn" "${USER_CONFIG_FILE}")"
MAC1="$(readConfigKey "mac1" "${USER_CONFIG_FILE}")"
KERNELPANIC="$(readConfigKey "kernelpanic" "${USER_CONFIG_FILE}")"

NETIFNUM=$(ls /sys/class/net/ | grep eth | wc -l); [ ${NETIFNUM} -eq 0 ] && NETIFNUM=1

declare -A CMDLINE

# Automatic values
Expand All @@ -108,7 +106,7 @@ CMDLINE['vid']="${VID}"
CMDLINE['pid']="${PID}"
CMDLINE['sn']="${SN}"
CMDLINE['mac1']="${MAC1}"
CMDLINE['netif_num']="1"
CMDLINE['netif_num']="${NETIFNUM}"

# set fixed cmdline
if grep -q "force_junior" /proc/cmdline; then
Expand All @@ -120,8 +118,7 @@ else
CMDLINE['noefi']=""
fi
if [ ! "${BUS}" = "usb" ]; then
LOADER_DEVICE_NAME=$(echo ${LOADER_DISK} | sed 's|/dev/||')
SIZE=$(($(cat /sys/block/${LOADER_DEVICE_NAME}/size) / 2048 + 10))
SIZE=$(($(cat /sys/block/${LOADER_DISK/\/dev\//}/size) / 2048 + 10))
# Read SATADoM type
DOM="$(readModelKey "${MODEL}" "dom")"
CMDLINE['synoboot_satadom']="${DOM}"
Expand Down Expand Up @@ -204,7 +201,7 @@ else
break
fi
COUNT=$((${COUNT} + 1))
IP=$(ip route show dev ${ETHX[${N}]} 2>/dev/null | sed -n 's/.* via .* src \(.*\) metric .*/\1/p')
IP="$(getIP ${ETHX[${N}]})"
if [ -n "${IP}" ]; then
echo -en "\r${ETHX[${N}]}(${DRIVER}): $(printf "$(TEXT "Access \033[1;34mhttp://%s:5000\033[0m to connect the DSM via web.")" "${IP}")\n"
break
Expand Down Expand Up @@ -234,6 +231,14 @@ else

echo -e "\033[1;37m$(TEXT "Loading DSM kernel...")\033[0m"

DSMLOGO="$(readConfigKey "dsmlogo" "${USER_CONFIG_FILE}")"
if [ "${DSMLOGO}" = "true" -a -c "/dev/fb0" ]; then
IP="$(getIP)"
[ -n "${IP}" ] && URL="http://${IP}:5000" || URL="http://find.synology.com/"
python ${WORK_PATH}/include/functions.py makeqr -d "${URL}" -l "br" -o "${TMP_PATH}/qrcode.png"
[ -f "${TMP_PATH}/qrcode.png" ] && echo | fbv -acufi "${TMP_PATH}/qrcode.png" >/dev/null 2>/dev/null || true
fi

# Executes DSM kernel via KEXEC
KVER=$(readModelKey "${MODEL}" "productvers.[${PRODUCTVER}].kver")
if [ "${KVER:0:1}" = "3" -a ${EFI} -eq 1 ]; then
Expand All @@ -244,9 +249,9 @@ else
fi
echo -e "\033[1;37m$(TEXT "Booting...")\033[0m"
for T in $(w | grep -v "TTY" | awk -F' ' '{print $2}'); do
echo -e "\n\033[1;43m$(TEXT "[This interface will not be operational.\nPlease wait for a few minutes before using the http://find.synology.com/ or Synology Assistant find DSM and connect.]")\033[0m\n" >"/dev/${T}" 2>/dev/null || true
echo -e "\n\033[1;43m$(TEXT "[This interface will not be operational. Please wait a few minutes.\nFind DSM via http://find.synology.com/ or Synology Assistant and connect.]")\033[0m\n" >"/dev/${T}" 2>/dev/null || true
done
KERNELWAY="$(readConfigKey "kernelway" "${USER_CONFIG_FILE}")"
[ "${KERNELWAY}" = "kexec" ] && kexec -f -e || poweroff
exit 0
fi
fi
15 changes: 14 additions & 1 deletion files/initrd/opt/rr/include/addons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function installAddon() {
[ ${HAS_FILES} -ne 1 ] && return 1
cp -f "${TMP_PATH}/${ADDON}/install.sh" "${RAMDISK_PATH}/addons/${ADDON}.sh" 2>"${LOG_FILE}" || dieLog
chmod +x "${RAMDISK_PATH}/addons/${ADDON}.sh"
[ -d ${TMP_PATH}/${ADDON}/root ] && (cp -Rf "${TMP_PATH}/${ADDON}/root/"* "${RAMDISK_PATH}/" 2>"${LOG_FILE}" || dieLog)
[ -d ${TMP_PATH}/${ADDON}/root ] && (cp -rnf "${TMP_PATH}/${ADDON}/root/"* "${RAMDISK_PATH}/" 2>"${LOG_FILE}" || dieLog)
rm -rf "${TMP_PATH}/${ADDON}"
return 0
}
Expand All @@ -72,3 +72,16 @@ function untarAddon() {
mv -f "${TMP_PATH}/addon" "${ADDONS_PATH}/${ADDON}"
echo "${ADDON}"
}

###############################################################################
# Detect if has new local plugins to install/reinstall
function updateAddons() {
for F in $(ls ${PART3_PATH}/*.addon 2>/dev/null); do
ADDON=$(basename "${F}" | sed 's|.addon||')
rm -rf "${ADDONS_PATH}/${ADDON}"
mkdir -p "${ADDONS_PATH}/${ADDON}"
echo "Installing ${F} to ${ADDONS_PATH}/${ADDON}"
tar -xaf "${F}" -C "${ADDONS_PATH}/${ADDON}"
rm -f "${F}"
done
}
9 changes: 9 additions & 0 deletions files/initrd/opt/rr/include/configFile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ function readConfigKey() {
[ "${RESULT}" == "null" ] && echo "" || echo ${RESULT}
}

###############################################################################
# Write to yaml config file if key not exists
# 1 - Path of Key
# 2 - Value
# 3 - Path of yaml config file
function initConfigKey() {
[ -z "$(readConfigKey "${1}" "${3}")" ] && writeConfigKey "${1}" "${2}" "${3}" || true
}

###############################################################################
# Read Entries as map(key=value) from yaml config file
# 1 - Path of key
Expand Down
35 changes: 19 additions & 16 deletions files/initrd/opt/rr/include/consts.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
RR_VERSION="23.10.4"
RR_TITLE="rr v${RR_VERSION}"
RR_TITLE="RR v${RR_VERSION}"

# Define paths

PART1_PATH="/mnt/p1"
PART2_PATH="/mnt/p2"
PART3_PATH="/mnt/p3"
DSMROOT_PATH="/mnt/dsmroot"
TMP_PATH="/tmp"

UNTAR_PAT_PATH="${TMP_PATH}/pat"
RAMDISK_PATH="${TMP_PATH}/ramdisk"
LOG_FILE="${TMP_PATH}/log.txt"

USER_CONFIG_FILE="${BOOTLOADER_PATH}/user-config.yml"
GRUB_PATH="${BOOTLOADER_PATH}/boot/grub"
USER_CONFIG_FILE="${PART1_PATH}/user-config.yml"
GRUB_PATH="${PART1_PATH}/boot/grub"

ORI_ZIMAGE_FILE="${SLPART_PATH}/zImage"
ORI_RDGZ_FILE="${SLPART_PATH}/rd.gz"
ORI_ZIMAGE_FILE="${PART2_PATH}/zImage"
ORI_RDGZ_FILE="${PART2_PATH}/rd.gz"

RR_BZIMAGE_FILE="${CACHE_PATH}/bzImage-rr"
RR_RAMDISK_FILE="${CACHE_PATH}/initrd-rr"
MOD_ZIMAGE_FILE="${CACHE_PATH}/zImage-dsm"
MOD_RDGZ_FILE="${CACHE_PATH}/initrd-dsm"
ADDONS_PATH="${CACHE_PATH}/addons"
LKM_PATH="${CACHE_PATH}/lkms"
MODULES_PATH="${CACHE_PATH}/modules"
USER_UP_PATH="${CACHE_PATH}/users"
RR_BZIMAGE_FILE="${PART3_PATH}/bzImage-rr"
RR_RAMDISK_FILE="${PART3_PATH}/initrd-rr"
MOD_ZIMAGE_FILE="${PART3_PATH}/zImage-dsm"
MOD_RDGZ_FILE="${PART3_PATH}/initrd-dsm"

MODEL_CONFIG_PATH="/opt/rr/model-configs"
INCLUDE_PATH="/opt/rr/include"
PATCH_PATH="/opt/rr/patch"
LKM_PATH="${PART3_PATH}/lkms"
ADDONS_PATH="${PART3_PATH}/addons"
MODULES_PATH="${PART3_PATH}/modules"
USER_UP_PATH="${PART3_PATH}/users"
63 changes: 63 additions & 0 deletions files/initrd/opt/rr/include/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Ing <https://github.com/wjz304>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#

import os, click

WORK_PATH = os.path.abspath(os.path.dirname(__file__))

@click.group()
def cli():
"""
The CLI is a commands to arpl.
"""
pass

@cli.command()
@click.option('-d', "--data", type=str, required=True, help="The data of QRCode.")
@click.option('-l', "--location", type=str, required=True, help="The location of QRCode. (tl, tr, bl, br, mid)")
@click.option('-o', "--output", type=str, required=True, help="The output file of QRCode.")
def makeqr(data, location, output):
"""
Generate a QRCode.
"""
import qrcode
from PIL import Image
qr = qrcode.QRCode(version=1, box_size=10, error_correction=qrcode.constants.ERROR_CORRECT_H, border=4)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="purple", back_color="white")
img = img.convert("RGBA")
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if pixels[i, j] == (255, 255, 255, 255):
pixels[i, j] = (255, 255, 255, 0)

if os.path.exists(os.path.join(WORK_PATH, "logo.png")):
icon = Image.open(os.path.join(WORK_PATH, "logo.png"))
icon = icon.convert("RGBA")
img.paste(icon.resize((int(img.size[0] / 5), int(img.size[1] / 5))), (int((img.size[0] - int(img.size[0] / 5)) / 2), int((img.size[1] - int(img.size[1] / 5)) / 2)))

alpha = Image.new("RGBA", (img.size[0] * 4, img.size[1] * 3), (0, 0, 0, 0))
if location == "tl":
loc = (0, 0)
elif location == "tr":
loc = (alpha.size[0] - img.size[0], 0)
elif location == "bl":
loc = (0, alpha.size[1] - img.size[1])
elif location == "br":
loc = (alpha.size[0] - img.size[0], alpha.size[1] - img.size[1])
else: # elif location == "mid":
loc = (int((alpha.size[0] - img.size[0]) / 2), int((alpha.size[1] - img.size[1]) / 2))

alpha.paste(img, loc)
alpha.save(output)


if __name__ == "__main__":
cli()
Loading

0 comments on commit ab83c4b

Please sign in to comment.