diff --git a/tools/json/config.system.json b/tools/json/config.system.json index a35b99ca0..0c096215e 100644 --- a/tools/json/config.system.json +++ b/tools/json/config.system.json @@ -9,22 +9,22 @@ "description": "Enable Armbian firmware upgrades", "about": "This will enable Armbian kernel upgrades that are currently put on hold.", "command": [ - "armbian_fw_manipulate unhold" + "module_armbian_firmware unhold" ], "status": "Stable", "author": "@igorpecovnik", - "condition": "[[ -n \"$(apt-mark showhold)\" ]]" + "condition": "module_armbian_firmware hold status" }, { "id": "SY002", "description": "Disable Armbian kernel upgrades", "about": "Disable Armbian kernel/firmware upgrades", "command": [ - "armbian_fw_manipulate hold" + "module_armbian_firmware hold" ], "status": "Stable", "author": "@igorpecovnik", - "condition": "[[ -z \"$(apt-mark showhold)\" ]]" + "condition": "! module_armbian_firmware hold status" }, { "id": "SY003", @@ -230,25 +230,25 @@ }, { "id": "SY010", - "description": "Switch to rolling release", + "description": "Switch to rolling packages repository", "about": "This will switch OS to rolling releases.", "command": [ - "set_rolling" + "module_armbian_firmware repository rolling" ], "status": "Stable", "author": "@igorpecovnik", - "condition": "grep -q 'apt.armbian.com' /etc/apt/sources.list.d/armbian.list && [[ -z \"$(apt-mark showhold)\" ]]" + "condition": "module_armbian_firmware repository stable status" }, { "id": "SY011", - "description": "Switch to stable release", + "description": "Switch to stable packages repository", "about": "This will switch OS to stable releases", "command": [ - "set_stable" + "module_armbian_firmware repository stable" ], "status": "Stable", "author": "@igorpecovnik", - "condition": "grep -q 'beta.armbian.com' /etc/apt/sources.list.d/armbian.list && [[ -z \"$(apt-mark showhold)\" ]]" + "condition": "module_armbian_firmware repository rolling status" }, { "id": "SY012", @@ -287,7 +287,7 @@ "description": "Install alternative kernels", "about": "Switching between kernels might change functionality of your device. \n\nIt might fail to boot!", "command": [ - "switch_kernels" + "module_armbian_firmware select" ], "status": "Stable", "author": "@igorpecovnik", diff --git a/tools/modules/system/armbian_fw_manipulate.sh b/tools/modules/system/armbian_fw_manipulate.sh deleted file mode 100644 index 162a0aba1..000000000 --- a/tools/modules/system/armbian_fw_manipulate.sh +++ /dev/null @@ -1,84 +0,0 @@ -module_options+=( - ["armbian_fw_manipulate,author"]="@igorpecovnik" - ["armbian_fw_manipulate,ref_link"]="" - ["armbian_fw_manipulate,feature"]="armbian_fw_manipulate" - ["armbian_fw_manipulate,desc"]="freeze, unhold, reinstall armbian related packages." - ["armbian_fw_manipulate,example"]="armbian_fw_manipulate unhold/freeze/reinstall" - ["armbian_fw_manipulate,status"]="Active" -) -# -# freeze/unhold/reinstall armbian firmware packages -# -armbian_fw_manipulate() { - - local function=$1 - local version=$2 - local branch=$3 - - [[ -n $version ]] && local version="=${version}" - - # fallback to $BRANCH - [[ -z "${branch}" ]] && branch="${BRANCH}" - [[ -z "${branch}" ]] && branch="current" # fallback in case we switch to very old BSP that have no such info - - # packages to install - local armbian_packages=( - "linux-u-boot-${BOARD}-${branch}" - "linux-image-${branch}-${LINUXFAMILY}" - "linux-dtb-${branch}-${LINUXFAMILY}" - "armbian-zsh" - "armbian-bsp-cli-${BOARD}-${branch}" - ) - - # reinstall headers only if they were previously installed - if dpkg -l | grep -E "linux-headers" >/dev/null; then - local armbian_packages+=("linux-headers-${branch}-${LINUXFAMILY}") - fi - - local packages="" - for pkg in "${armbian_packages[@]}"; do - if [[ "${function}" == reinstall ]]; then - local pkg_search=$(apt search "$pkg" 2> /dev/null | grep "^$pkg") - if [[ $? -eq 0 && -n "${pkg_search}" ]]; then - if [[ "${pkg_search}" == *$version* ]] ; then - packages+="$pkg${version} "; - else - packages+="$pkg "; - fi - fi - else - if check_if_installed $pkg; then - packages+="$pkg${version} " - fi - fi - done - for pkg in "${packages[@]}"; do - case $function in - unhold) apt-mark unhold ${pkg} | show_infobox ;; - hold) apt-mark hold ${pkg} | show_infobox ;; - reinstall) - apt_install_wrapper apt-get -y --simulate --download-only --allow-change-held-packages --allow-downgrades install ${pkg} - if [[ $? == 0 ]]; then - apt_install_wrapper apt-get -y purge "linux-u-boot-*" "linux-image-*" "linux-dtb-*" "linux-headers-*" "armbian-zsh-*" "armbian-bsp-cli-*" # remove all branches - apt_install_wrapper apt-get -y --allow-change-held-packages install ${pkg} - apt_install_wrapper apt-get -y autoremove - apt_install_wrapper apt-get -y clean - else - exit 1 - fi - - - ;; - *) return ;; - esac - done - - # update branch information - BRANCH=$(dpkg -l | grep -E "linux-image" | grep -E "current|legacy|edge" | awk '{print $2}' | cut -d"-" -f3 | head -1) - if grep -q BRANCH /etc/armbian-release; then - [[ -n ${BRANCH} ]] && sed -i "s/BRANCH=.*/BRANCH=$BRANCH/g" /etc/armbian-release - else - [[ -n ${BRANCH} ]] && echo "BRANCH=$BRANCH" >> /etc/armbian-release - fi -} - diff --git a/tools/modules/system/module_armbian_firmware.sh b/tools/modules/system/module_armbian_firmware.sh new file mode 100644 index 000000000..a4e58fa8e --- /dev/null +++ b/tools/modules/system/module_armbian_firmware.sh @@ -0,0 +1,254 @@ +module_options+=( + ["module_armbian_firmware,author"]="@igorpecovnik" + ["module_armbian_firmware,feature"]="module_armbian_firmware" + ["module_armbian_firmware,example"]="select install show hold unhold repository help" + ["module_armbian_firmware,desc"]="Module for Armbian firmware manipulating." + ["module_armbian_firmware,status"]="review" +) + +function module_armbian_firmware() { + local title="Armbian FW" + local condition=$(which "$title" 2>/dev/null) + + # Convert the example string to an array + local commands + IFS=' ' read -r -a commands <<< "${module_options["module_armbian_firmware,example"]}" + + case "$1" in + "${commands[0]}") # choose kernel from the list + + # make sure to proceed if this variable is not defined. This can surface on some old builds + [[ -z "${KERNEL_TEST_TARGET}" ]] && KERNEL_TEST_TARGET="legacy,vendor,current,edge" + + # show warning when packages are put on hold and ask to release it + if ${module_options["module_armbian_firmware,feature"]} ${commands[3]} "status"; then + if $DIALOG --title "Warning!" --yesno "Firmware upgrade is disabled. Release hold and proceed?" 7 60; then + ${module_options["module_armbian_firmware,feature"]} ${commands[4]} + else + exit 0 + fi + fi + + # by default we define which kernels are suitable + if ! $DIALOG --title "Advanced options" --yesno "Show only mainstream kernels on the list?" 7 60; then + KERNEL_TEST_TARGET="legacy,vendor,current,edge" + fi + + # read what is possible to install + local kernel_test_target=$(\ + for kernel_test_target in ${KERNEL_TEST_TARGET//,/ } + do + echo "linux-image-${kernel_test_target}-${LINUXFAMILY}" + done + ) + local installed_kernel_version=$(dpkg -l | grep '^ii' | grep linux-image | awk '{print $2"="$3}') + + # workaroun in case current is not installed + [[ -n ${installed_kernel_version} ]] && local grep_current_kernel=" | grep -v ${installed_kernel_version}" + + # main search command + local search_exec="apt-cache show ${kernel_test_target} \ + | grep -E \"Package:|Version:|version:|family\" \ + | grep -v \"Config-Version\" \ + | sed -n -e 's/^.*: //p' \ + | sed 's/\.$//g' \ + | xargs -n3 -d'\n' \ + | sed \"s/ /=/\" $grep_current_kernel" + + # construct a list of kernels with their Armbian release versions and kernel version + IFS=$'\n' + local LIST=() + for line in $(eval ${search_exec}); do + LIST+=($(echo $line | awk -F ' ' '{print $1 " "}') $(echo $line | awk -F ' ' '{print "v"$2}')) + done + unset IFS + + # generate selection menu + local list_length=$((${#LIST[@]} / 2)) + if [ "$list_length" -eq 0 ]; then + $DIALOG --backtitle "$BACKTITLE" --title " Warning " --msgbox "No other kernels available!" 7 31 + else + if target_version=$(\ + $DIALOG \ + --separate-output \ + --title "Select kernel" \ + --menu "" \ + $((${list_length} + 7)) 80 $((${list_length})) "${LIST[@]}" \ + 3>&1 1>&2 2>&3) + then + # extract branch + local branch=${target_version##*image-} + # call install function + ${module_options["module_armbian_firmware,feature"]} ${commands[1]} "${branch%%-*}" "${target_version/*=/}" + fi + fi + + ;; + + "${commands[1]}") # purge old and install selected kernel from desired branch and version + + # input parameters + local branch=$2 + local version=$3 + + # generate list + ${module_options["module_armbian_firmware,feature"]} ${commands[2]} "${branch}" "${version}" "hide" + + # purge and install + for pkg in "${packages[@]}"; do + purge_pkg=$(echo $pkg | sed -e 's/current/*/;s/legacy/*/;s/edge/*/;s/vendor/*/') + apt_install_wrapper apt-get -y --simulate --download-only --allow-downgrades install ${pkg} + # if test install is succesfull, proceed + if [[ $? == 0 ]]; then + apt_install_wrapper apt-get -y purge ${purge_pkg} + apt_install_wrapper apt-get -y --autoremove --allow-downgrades install ${pkg} + fi + done + + ;; + "${commands[2]}") # generate a list of possible packages to install + + # input parameters + local branch="$2" + local version="$3" + local hide="$4" + local repository="$5" + + # if branch is not defined, we use the one that is currently installed + [[ -z $branch ]] && local branch=$BRANCH + + # if repository is not defined, we use stable one + [[ -z $repository ]] && local repository="apt.armbian.com" + + # select Armbian packages we want to searching for + armbian_packages=( + "linux-image-${branch}-${LINUXFAMILY}" + "linux-dtb-${branch}-${LINUXFAMILY}" + ) + + # install headers only if they were previously installed + if dpkg -l | grep -E "linux-headers" >/dev/null; then + armbian_packages+=("linux-headers-${branch}-${LINUXFAMILY}") + fi + + # install full firmware if it was installed previously + if dpkg -l | grep -E "armbian-firmware-full" >/dev/null; then + armbian_packages+=("armbian-firmware-full") + else + armbian_packages+=("armbian-firmware") + fi + + # when we select a specific version of Armbian, we need to make sure that version exists + # for each package we want to install. In case desired version does not exists, it installs + # package without specifying version. This prevent breaking install in case some + # package version was removed from repository. Just in case. + packages="" + for pkg in "${armbian_packages[@]}"; do + # use package + version if found else use package if found + if apt-cache show "$pkg" 2> /dev/null \ + | grep -E "Package:|^Version:|family" \ + | sed -n -e 's/^.*: //p' \ + | sed 's/\.$//g' \ + | xargs -n2 -d'\n' \ + | grep ${pkg} | grep -e ${version} >/dev/null 2>&1; then + packages+="${pkg}=${version} "; + elif + apt-cache show "$pkg" 2> /dev/null \ + | grep -E "Package:|^Version:|family" \ + | sed -n -e 's/^.*: //p' \ + | sed 's/\.$//g' \ + | xargs -n2 -d'\n' \ + | grep "${pkg}" >/dev/null 2>&1 ; then + packages+="${pkg} "; + fi + done + + # if this is called with a parameter hide, we only prepare this list but don't show its content + [[ "$4" != "hide" ]] && echo ${packages[@]} + + ;; + "${commands[3]}") # holds Armbian firmware packages or provides status + + # input parameter + local status=$2 + + # generate a list of packages + ${module_options["module_armbian_firmware,feature"]} ${commands[2]} "" "" hide + + # we are only interested in which Armbian packages are put on hold + if [[ "$status" == "status" ]]; then + local get_hold=($(apt-mark showhold)) + local test_hold=($(for all_packages in ${packages[@]}; do + for hold_packages in ${get_hold[@]}; do + echo $all_packages | grep $hold_packages + done + done)) + [[ -z ${test_hold[@]} ]] && return 1 || return 0 + else + # put Armbian packages on hold + apt-mark hold ${packages[@]} >/dev/null 2>&1 + fi + + ;; + "${commands[4]}") # unhold Armbian firmware packages + + # generate a list of packages + ${module_options["module_armbian_firmware,feature"]} ${commands[2]} "" "" hide + + # release Armbian packages from hold + apt-mark unhold ${packages[@]} >/dev/null 2>&1 + + ;; + "${commands[5]}") # switches repository to rolling / stable and performs update or provides status + + # input parameters + + local repository=$2 + local status=$3 + + if grep -q 'apt.armbian.com' /etc/apt/sources.list.d/armbian.list; then + if [[ "$repository" == "rolling" && "$status" == "status" ]]; then + return 1 + elif [[ "$status" == "status" ]]; then + return 0 + fi + # performs list change & update if this is needed + if [[ "$repository" == "rolling" ]]; then + sed -i "s/http:\/\/[^ ]*/http:\/\/beta.armbian.com/" /etc/apt/sources.list.d/armbian.list + apt_install_wrapper apt-get update + fi + else + if [[ "$repository" == "stable" && "$status" == "status" ]]; then + return 1 + elif [[ "$status" == "status" ]]; then + return 0 + fi + # performs list change & update if this is needed + if [[ "$repository" == "stable" ]]; then + sed -i "s/http:\/\/[^ ]*/http:\/\/apt.armbian.com/" /etc/apt/sources.list.d/armbian.list + apt_install_wrapper apt-get update + fi + fi + + # if we are not only checking status, it reinstall firmware automatically + [[ "$status" != "status" ]] && ${module_options["module_armbian_firmware,feature"]} ${commands[1]} + + ;; + + "${commands[6]}") + echo -e "\nUsage: ${module_options["module_armbian_firmware,feature"]} " + echo -e "Commands: ${module_options["module_armbian_firmware,example"]}" + echo "Available commands:" + echo -e "\tselect \t- TUI to select $title. \t switches: [ stable | rolling ]" + echo -e "\tinstall \t- Install $title. \t switches: [ \$branch | \$version ]" + echo -e "\tshow \t- Show $title packages. \t switches: [ \$branch | \$version | hide ]" + echo -e "\thold \t- Mark $title packages as held back. \t switches: [status] returns true or false" + echo -e "\tunhold \t- Unset $title packages set as held back." + echo -e "\trepository\t- Selects repository and performs update. \t switches: [ stable | rolling ]" + echo + ;; + *) + ${module_options["module_armbian_firmware,feature"]} ${commands[6]} + ;; + esac +} diff --git a/tools/modules/system/set_rolling.sh b/tools/modules/system/set_rolling.sh deleted file mode 100644 index fe2aad4d1..000000000 --- a/tools/modules/system/set_rolling.sh +++ /dev/null @@ -1,20 +0,0 @@ -module_options+=( - ["set_rolling,author"]="@Tearran" - ["set_rolling,ref_link"]="https://github.com/armbian/config/blob/master/debian-config-jobs#L1446" - ["set_rolling,feature"]="set_rolling" - ["set_rolling,desc"]="Set Armbian to rolling release" - ["set_rolling,example"]="set_rolling" - ["set_rolling,status"]="Active" -) -# -# @description Set Armbian to rolling release -# -function set_rolling() { - - if ! grep -q 'beta.armbian.com' /etc/apt/sources.list.d/armbian.list; then - sed -i "s/http:\/\/[^ ]*/http:\/\/beta.armbian.com/" /etc/apt/sources.list.d/armbian.list - apt_install_wrapper apt-get update - armbian_fw_manipulate "reinstall" - fi -} - diff --git a/tools/modules/system/set_stable.sh b/tools/modules/system/set_stable.sh deleted file mode 100644 index 7cb467fbb..000000000 --- a/tools/modules/system/set_stable.sh +++ /dev/null @@ -1,20 +0,0 @@ -module_options+=( - ["set_stable,author"]="@Tearran" - ["set_stable,ref_link"]="https://github.com/armbian/config/blob/master/debian-config-jobs#L1446" - ["set_stable,feature"]="set_stable" - ["set_stable,desc"]="Set Armbian to stable release" - ["set_stable,example"]="set_stable" - ["set_stable,status"]="Active" -) -# -# @description Set Armbian to stable release -# -function set_stable() { - - if ! grep -q 'apt.armbian.com' /etc/apt/sources.list.d/armbian.list; then - sed -i "s/http:\/\/[^ ]*/http:\/\/apt.armbian.com/" /etc/apt/sources.list.d/armbian.list - apt_install_wrapper apt-get update - armbian_fw_manipulate "reinstall" - fi -} - diff --git a/tools/modules/system/switch_kernels.sh b/tools/modules/system/switch_kernels.sh deleted file mode 100644 index d183dc848..000000000 --- a/tools/modules/system/switch_kernels.sh +++ /dev/null @@ -1,41 +0,0 @@ -module_options+=( - ["switch_kernels,author"]="@igorpecovnik" - ["switch_kernels,ref_link"]="" - ["switch_kernels,feature"]="switch_kernels" - ["switch_kernels,desc"]="Switching to alternative kernels" - ["switch_kernels,example"]="switch_kernels" - ["switch_kernels,status"]="Active" -) -# -# @description Switch between alternative kernels -# -function switch_kernels() { - - # we only allow switching kerneles that are in the test pool - [[ -z "${KERNEL_TEST_TARGET}" ]] && KERNEL_TEST_TARGET="legacy,current,edge" - dialog --title "Warning!" --yesno "\nInclude untested kernels on the list?" 7 60 - [[ $? -eq 0 ]] && KERNEL_TEST_TARGET="legacy,vendor,current,edge" - local kernel_test_target=$(for x in ${KERNEL_TEST_TARGET//,/ }; do echo "linux-image-$x-${LINUXFAMILY}"; done;) - local installed_kernel_version=$(dpkg -l | grep '^ii' | grep linux-image | awk '{print $2"="$3}') - # just in case current is not installed - [[ -n ${installed_kernel_version} ]] && local grep_current_kernel=" | grep -v ${installed_kernel_version}" - local search_exec="apt-cache show ${kernel_test_target} | grep -E \"Package:|Version:|version:|family\" | grep -v \"Config-Version\" | sed -n -e 's/^.*: //p' | sed 's/\.$//g' | xargs -n3 -d'\n' | sed \"s/ /=/\" $grep_current_kernel" - IFS=$' -' - local LIST=() - for line in $(eval ${search_exec}); do - LIST+=($(echo $line | awk -F ' ' '{print $1 " "}') $(echo $line | awk -F ' ' '{print "v"$2}')) - done - unset IFS - local list_length=$((${#LIST[@]} / 2)) - if [ "$list_length" -eq 0 ]; then - dialog --backtitle "$BACKTITLE" --title " Warning " --msgbox "No other kernels available!" 7 32 - else - local target_version=$(whiptail --separate-output --title "Select kernel" --menu "ed" $((${list_length} + 7)) 80 $((${list_length})) "${LIST[@]}" 3>&1 1>&2 2>&3) - if [ $? -eq 0 ] && [ -n "${target_version}" ]; then - local branch=${target_version##*image-} - armbian_fw_manipulate "reinstall" "${target_version/*=/}" "${branch%%-*}" - fi - fi -} -