From 0fba7fe53985c7eb5c55f8c201ad2b8a44623360 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 19:31:54 +0000 Subject: [PATCH 01/16] Initial commit --- LICENSE | 9 +++++++++ README.md | 3 +++ 2 files changed, 12 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cc3230a --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 timoxa0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..478cb9c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# mklni + +mklni - Image builder for nabu, running in alpine chroot \ No newline at end of file From 5cbc22b5c8e5336f4883fbaedb4106a2e359ac4f Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Mon, 28 Oct 2024 00:28:19 +0500 Subject: [PATCH 02/16] Initial commit --- .gitignore | 2 + common.d/functions.sh | 266 ++++++++++++++++++++++++++++++++++++++++++ setup.sh | 11 ++ 3 files changed, 279 insertions(+) create mode 100644 .gitignore create mode 100644 common.d/functions.sh create mode 100755 setup.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59bbb47 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +alpine_chroot/ +alpine.tar.gz diff --git a/common.d/functions.sh b/common.d/functions.sh new file mode 100644 index 0000000..7f59848 --- /dev/null +++ b/common.d/functions.sh @@ -0,0 +1,266 @@ +#!/usr/bin/env bash + +ALPINE="https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-minirootfs-3.20.3-x86_64.tar.gz" +CHROOTDIR="./alpine_chroot" + +log() +{ + case "$2" in + error) + printf "\e[1m\e[31mERROR:\e[0m \e[1m%s\e[0m\n" "$1">&2;; + internal) + printf "\e[1m\e[96m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + ierror) + printf "\e[1m\e[31m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + *) + printf "\e[1m\e[92m>>>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + esac +} + +mkdir_if_not_exists() +{ + [ -d "$1" ] && return 0 + [ -f "$1" ] && rm -f "$1" + mkdir "$1" +} + +umount_if_mouted() +{ + [ ! -d "$1" ] && return 0 + if grep -qs "$(realpath "$1")" /proc/mounts; then + umount "$1" + return $? + else + return 0 + fi +} + +require () +{ + which "$1" > /dev/null 2>&1 || + { + log "$1 not found" error + exit 1 + } +} + +# shellcheck disable=SC2161 +# shellcheck disable=SC2086 +arguments() { + while [[ $# -gt 0 ]]; do + opt="$1" + + shift + + case "$(echo ${opt} | tr '[:upper:]' '[:lower:]')" in + install) + install_chroot + break 2 ;; + + remove) + remove_chroot + break 2 ;; + + chroot) + chroot_into + break 2 ;; + + -h | -help | --help) + usage ;; + + *) + log "Unknown option: $opt" error + break 2 ;; + esac + done +} + +usage() { + log "Usage commands:" + + cat <>\e[0m \e[1m%s\e[0m\n" "Shutdown signal received." + _shutdown $sig +} + +trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP + + +# Chroot functions +require "lsof" +require "tar" +require "gzip" +require "wget" + +setup_inet() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Rootdir [$CHROOTDIR] does not exists" ierror + return 2 + } + echo "nameserver 1.1.1.1" > "$CHROOTDIR/etc/resolv.conf" + echo "makelnichroot" > "$CHROOTDIR/etc/hostname" + echo "127.0.0.1 localhost" > "$CHROOTDIR/etc/hosts" +} + +prepare_chroot() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Rootdir [$CHROOTDIR] does not exists" ierror + return 2 + } + + export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH + + mount --bind "$CHROOTDIR" "$CHROOTDIR" + mount -t proc proc "$CHROOTDIR/proc" + mount -t sysfs sysfs "$CHROOTDIR/sys" + mount -t devtmpfs devtmpfs "$CHROOTDIR/dev" + mount -t devpts devpts "$CHROOTDIR/dev/pts" + mount -t tmpfs devshm "$CHROOTDIR/dev/shm" + + if uname -m | grep -q aarch64 || [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then + log "Cancel qemu-aarch64-static register" + else + + # shellcheck disable=SC2028 + echo ':aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true + + # shellcheck disable=SC2028 + echo ':aarch64ld:M::\x7fELF\x02\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true + fi +} + +detach_chroot() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Rootdir [$CHROOTDIR] does not exists" ierror + return 2 + } + + umount_if_mouted "$CHROOTDIR/sys" + umount_if_mouted "$CHROOTDIR/dev/pts" + umount_if_mouted "$CHROOTDIR/dev/shm" + umount_if_mouted "$CHROOTDIR/dev" + umount_if_mouted "$CHROOTDIR/proc" + umount_if_mouted "$CHROOTDIR" + + if [ -f "/proc/sys/fs/binfmt_misc/aarch64" ]; then + echo -1 > /proc/sys/fs/binfmt_misc/aarch64 2> /dev/null + fi + if [ -f "/proc/sys/fs/binfmt_misc/aarch64ld" ]; then + echo -1 > /proc/sys/fs/binfmt_misc/aarch64ld 2> /dev/null + fi +} + +install_chroot() +{ + [ -d "$CHROOTDIR" ] && { + log "Chroot already installed" error + exit 1 + } + mkdir "$CHROOTDIR" + [ ! -f "./alpine.tar.gz" ] && { + log "Downloading alpine rootfs tarball" + wget -q --show-progress "$ALPINE" -O "./alpine.tar.gz" || { + ec=$? + log "Failed to download rootfs tarball" error + exit "$ec" + } + } + + log "Extracting alpine rootfs tarball" + tar -xzf "./alpine.tar.gz" -C "$CHROOTDIR/" || { + log "Failed to extract rootfs" error + } + setup_inet + prepare_chroot || { + log "Failed to setup chroot" error + exit 1 + } + log "Updating apk repos" + chroot "$CHROOTDIR" apk update || { + log "Failed to update apk repos" error + exit 1 + } + log "Installing pakages" + chroot "$CHROOTDIR" apk add shadow fish bash git debootstrap pixz losetup rsync pv wget e2fsprogs libarchive-tools coreutils || { + log "Failed to install packages" error + exit 1 + } + chroot "$CHROOTDIR" chsh -s /usr/bin/fish root + log "Cloning makelni repo" + chroot "$CHROOTDIR" git clone "https://git.timoxa0.su/timoxa0/makelni.git" /makelni || { + log "Failed to clone makelni repo" error + exit 1 + } + log "Installing qemu-aarch64-static" + { + wget -q --show-progress -N https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static -O "$CHROOTDIR/qemu-aarch64-static" && chmod 755 "$CHROOTDIR/qemu-aarch64-static" + } || { + log "Failed to install qemu-aarch64-static" error + exit 1 + } + detach_chroot || { + log "Failed to detach chroot" error + exit 1 + } + log "Done!" + exit 0 +} + +remove_chroot() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Chroot not installed" error + exit 1 + } + detach_chroot || { + log "Failed to umount chroot" error + exit 1 + } + log "Done!" + rm -rf "${CHROOTDIR:?}/" +} + +chroot_into() +{ + prepare_chroot || { + log "Failed to setup chroot" error + exit 1 + } + chroot "$CHROOTDIR" /usr/bin/fish + detach_chroot || { + log "Failed to detach chroot" error + exit 1 + } +} diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..3288356 --- /dev/null +++ b/setup.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +source ./common.d/functions.sh + +if [ "$(id -u)" != "0" ]; then + log "$0 must be run as root" + exit 3 +fi + +arguments "$*" + From 1e84bdd2d291078f81cbd4e8bcb68bfdc3575d17 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 19:43:15 +0500 Subject: [PATCH 03/16] merge common.d/functions.sh into setup.sh and rename to mklni --- common.d/functions.sh | 264 --------------------------------------- mklni | 284 ++++++++++++++++++++++++++++++++++++++++++ setup.sh | 11 -- 3 files changed, 284 insertions(+), 275 deletions(-) create mode 100755 mklni delete mode 100755 setup.sh diff --git a/common.d/functions.sh b/common.d/functions.sh index 7f59848..20d602b 100644 --- a/common.d/functions.sh +++ b/common.d/functions.sh @@ -1,266 +1,2 @@ #!/usr/bin/env bash -ALPINE="https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-minirootfs-3.20.3-x86_64.tar.gz" -CHROOTDIR="./alpine_chroot" - -log() -{ - case "$2" in - error) - printf "\e[1m\e[31mERROR:\e[0m \e[1m%s\e[0m\n" "$1">&2;; - internal) - printf "\e[1m\e[96m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; - ierror) - printf "\e[1m\e[31m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; - *) - printf "\e[1m\e[92m>>>\e[0m \e[1m%s\e[0m\n" "$1">&2;; - esac -} - -mkdir_if_not_exists() -{ - [ -d "$1" ] && return 0 - [ -f "$1" ] && rm -f "$1" - mkdir "$1" -} - -umount_if_mouted() -{ - [ ! -d "$1" ] && return 0 - if grep -qs "$(realpath "$1")" /proc/mounts; then - umount "$1" - return $? - else - return 0 - fi -} - -require () -{ - which "$1" > /dev/null 2>&1 || - { - log "$1 not found" error - exit 1 - } -} - -# shellcheck disable=SC2161 -# shellcheck disable=SC2086 -arguments() { - while [[ $# -gt 0 ]]; do - opt="$1" - - shift - - case "$(echo ${opt} | tr '[:upper:]' '[:lower:]')" in - install) - install_chroot - break 2 ;; - - remove) - remove_chroot - break 2 ;; - - chroot) - chroot_into - break 2 ;; - - -h | -help | --help) - usage ;; - - *) - log "Unknown option: $opt" error - break 2 ;; - esac - done -} - -usage() { - log "Usage commands:" - - cat <>\e[0m \e[1m%s\e[0m\n" "Shutdown signal received." - _shutdown $sig -} - -trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP - - -# Chroot functions -require "lsof" -require "tar" -require "gzip" -require "wget" - -setup_inet() -{ - [ ! -d "$CHROOTDIR" ] && { - log "Rootdir [$CHROOTDIR] does not exists" ierror - return 2 - } - echo "nameserver 1.1.1.1" > "$CHROOTDIR/etc/resolv.conf" - echo "makelnichroot" > "$CHROOTDIR/etc/hostname" - echo "127.0.0.1 localhost" > "$CHROOTDIR/etc/hosts" -} - -prepare_chroot() -{ - [ ! -d "$CHROOTDIR" ] && { - log "Rootdir [$CHROOTDIR] does not exists" ierror - return 2 - } - - export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH - - mount --bind "$CHROOTDIR" "$CHROOTDIR" - mount -t proc proc "$CHROOTDIR/proc" - mount -t sysfs sysfs "$CHROOTDIR/sys" - mount -t devtmpfs devtmpfs "$CHROOTDIR/dev" - mount -t devpts devpts "$CHROOTDIR/dev/pts" - mount -t tmpfs devshm "$CHROOTDIR/dev/shm" - - if uname -m | grep -q aarch64 || [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then - log "Cancel qemu-aarch64-static register" - else - - # shellcheck disable=SC2028 - echo ':aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true - - # shellcheck disable=SC2028 - echo ':aarch64ld:M::\x7fELF\x02\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true - fi -} - -detach_chroot() -{ - [ ! -d "$CHROOTDIR" ] && { - log "Rootdir [$CHROOTDIR] does not exists" ierror - return 2 - } - - umount_if_mouted "$CHROOTDIR/sys" - umount_if_mouted "$CHROOTDIR/dev/pts" - umount_if_mouted "$CHROOTDIR/dev/shm" - umount_if_mouted "$CHROOTDIR/dev" - umount_if_mouted "$CHROOTDIR/proc" - umount_if_mouted "$CHROOTDIR" - - if [ -f "/proc/sys/fs/binfmt_misc/aarch64" ]; then - echo -1 > /proc/sys/fs/binfmt_misc/aarch64 2> /dev/null - fi - if [ -f "/proc/sys/fs/binfmt_misc/aarch64ld" ]; then - echo -1 > /proc/sys/fs/binfmt_misc/aarch64ld 2> /dev/null - fi -} - -install_chroot() -{ - [ -d "$CHROOTDIR" ] && { - log "Chroot already installed" error - exit 1 - } - mkdir "$CHROOTDIR" - [ ! -f "./alpine.tar.gz" ] && { - log "Downloading alpine rootfs tarball" - wget -q --show-progress "$ALPINE" -O "./alpine.tar.gz" || { - ec=$? - log "Failed to download rootfs tarball" error - exit "$ec" - } - } - - log "Extracting alpine rootfs tarball" - tar -xzf "./alpine.tar.gz" -C "$CHROOTDIR/" || { - log "Failed to extract rootfs" error - } - setup_inet - prepare_chroot || { - log "Failed to setup chroot" error - exit 1 - } - log "Updating apk repos" - chroot "$CHROOTDIR" apk update || { - log "Failed to update apk repos" error - exit 1 - } - log "Installing pakages" - chroot "$CHROOTDIR" apk add shadow fish bash git debootstrap pixz losetup rsync pv wget e2fsprogs libarchive-tools coreutils || { - log "Failed to install packages" error - exit 1 - } - chroot "$CHROOTDIR" chsh -s /usr/bin/fish root - log "Cloning makelni repo" - chroot "$CHROOTDIR" git clone "https://git.timoxa0.su/timoxa0/makelni.git" /makelni || { - log "Failed to clone makelni repo" error - exit 1 - } - log "Installing qemu-aarch64-static" - { - wget -q --show-progress -N https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static -O "$CHROOTDIR/qemu-aarch64-static" && chmod 755 "$CHROOTDIR/qemu-aarch64-static" - } || { - log "Failed to install qemu-aarch64-static" error - exit 1 - } - detach_chroot || { - log "Failed to detach chroot" error - exit 1 - } - log "Done!" - exit 0 -} - -remove_chroot() -{ - [ ! -d "$CHROOTDIR" ] && { - log "Chroot not installed" error - exit 1 - } - detach_chroot || { - log "Failed to umount chroot" error - exit 1 - } - log "Done!" - rm -rf "${CHROOTDIR:?}/" -} - -chroot_into() -{ - prepare_chroot || { - log "Failed to setup chroot" error - exit 1 - } - chroot "$CHROOTDIR" /usr/bin/fish - detach_chroot || { - log "Failed to detach chroot" error - exit 1 - } -} diff --git a/mklni b/mklni new file mode 100755 index 0000000..fdc554b --- /dev/null +++ b/mklni @@ -0,0 +1,284 @@ +#!/usr/bin/env bash + +ALPINE="https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-minirootfs-3.20.3-x86_64.tar.gz" +CHROOTDIR="./alpine_chroot" +LON_TOOL_URL="https://git.timoxa0.su/timoxa0/lon-tool/releases/download/latest/lon-tool_lin_amd64" + + +log() +{ + case "$2" in + error) + printf "\e[1m\e[31mERROR:\e[0m \e[1m%s\e[0m\n" "$1">&2;; + internal) + printf "\e[1m\e[96m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + ierror) + printf "\e[1m\e[31m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + *) + printf "\e[1m\e[92m>>>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + esac +} + +mkdir_if_not_exists() +{ + [ -d "$1" ] && return 0 + [ -f "$1" ] && rm -f "$1" + mkdir "$1" +} + +umount_if_mouted() +{ + [ ! -d "$1" ] && return 0 + if grep -qs "$(realpath "$1")" /proc/mounts; then + umount "$1" + return $? + else + return 0 + fi +} + +require () +{ + which "$1" > /dev/null 2>&1 || + { + log "$1 not found" error + exit 1 + } +} + +# shellcheck disable=SC2161 +# shellcheck disable=SC2086 +arguments() { + while [[ $# -gt 0 ]]; do + opt="$1" + + shift + + case "$(echo ${opt} | tr '[:upper:]' '[:lower:]')" in + install) + install_chroot + break 2 ;; + + remove) + remove_chroot + break 2 ;; + + chroot) + chroot_into + break 2 ;; + + -h | -help | --help) + usage ;; + + *) + log "Unknown option: $opt" error + break 2 ;; + esac + done +} + +usage() { + log "Usage commands:" + + cat <>\e[0m \e[1m%s\e[0m\n" "Shutdown signal received." + _shutdown $sig +} + +trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP + + +# Chroot functions +require "lsof" +require "tar" +require "gzip" +require "wget" + +setup_inet() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Rootdir [$CHROOTDIR] does not exists" ierror + return 2 + } + echo "nameserver 1.1.1.1" > "$CHROOTDIR/etc/resolv.conf" + echo "makelnichroot" > "$CHROOTDIR/etc/hostname" + echo "127.0.0.1 localhost" > "$CHROOTDIR/etc/hosts" +} + +prepare_chroot() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Rootdir [$CHROOTDIR] does not exists" ierror + return 2 + } + + export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH + + mount --bind "$CHROOTDIR" "$CHROOTDIR" + mount -t proc proc "$CHROOTDIR/proc" + mount -t sysfs sysfs "$CHROOTDIR/sys" + mount -t devtmpfs devtmpfs "$CHROOTDIR/dev" + mount -t devpts devpts "$CHROOTDIR/dev/pts" + mount -t tmpfs devshm "$CHROOTDIR/dev/shm" + + if uname -m | grep -q aarch64 || [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then + log "Cancel qemu-aarch64-static register" + else + + # shellcheck disable=SC2028 + echo ':aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true + + # shellcheck disable=SC2028 + echo ':aarch64ld:M::\x7fELF\x02\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true + fi +} + +detach_chroot() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Rootdir [$CHROOTDIR] does not exists" ierror + return 2 + } + + umount_if_mouted "$CHROOTDIR/sys" + umount_if_mouted "$CHROOTDIR/dev/pts" + umount_if_mouted "$CHROOTDIR/dev/shm" + umount_if_mouted "$CHROOTDIR/dev" + umount_if_mouted "$CHROOTDIR/proc" + + if [ -f "/proc/sys/fs/binfmt_misc/aarch64" ]; then + echo -1 > /proc/sys/fs/binfmt_misc/aarch64 2> /dev/null + fi + if [ -f "/proc/sys/fs/binfmt_misc/aarch64ld" ]; then + echo -1 > /proc/sys/fs/binfmt_misc/aarch64ld 2> /dev/null + fi + + umount_if_mouted "$CHROOTDIR" +} + +install_chroot() +{ + [ -d "$CHROOTDIR" ] && { + log "Chroot already installed" error + exit 1 + } + mkdir "$CHROOTDIR" + [ ! -f "./alpine.tar.gz" ] && { + log "Downloading alpine rootfs tarball" + wget -q --show-progress "$ALPINE" -O "./alpine.tar.gz" || { + ec=$? + log "Failed to download rootfs tarball" error + exit "$ec" + } + } + + log "Extracting alpine rootfs tarball" + tar -xzf "./alpine.tar.gz" -C "$CHROOTDIR/" || { + log "Failed to extract rootfs" error + } + setup_inet + prepare_chroot || { + log "Failed to setup chroot" error + exit 1 + } + log "Updating apk repos" + chroot "$CHROOTDIR" apk update || { + log "Failed to update apk repos" error + exit 1 + } + log "Installing pakages" + chroot "$CHROOTDIR" apk add shadow fish bash git debootstrap pixz losetup rsync pv wget e2fsprogs e2fsprogs-extra libarchive-tools coreutils || { + log "Failed to install packages" error + exit 1 + } + chroot "$CHROOTDIR" chsh -s /usr/bin/fish root + log "Cloning makelni repo" + chroot "$CHROOTDIR" git clone "https://git.timoxa0.su/timoxa0/makelni.git" /makelni || { + log "Failed to clone makelni repo" error + exit 1 + } + + log "Installing qemu-aarch64-static" + { + wget -q --show-progress -N https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static -O "$CHROOTDIR/qemu-aarch64-static" && chmod 755 "$CHROOTDIR/qemu-aarch64-static" + } || { + log "Failed to install qemu-aarch64-static" error + exit 1 + } + + detach_chroot || { + log "Failed to detach chroot" error + exit 1 + } + log "Done!" + exit 0 +} + +remove_chroot() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Chroot not installed" error + exit 1 + } + detach_chroot || { + log "Failed to umount chroot" error + exit 1 + } + log "Done!" + rm -rf "${CHROOTDIR:?}/" +} + +chroot_into() +{ + prepare_chroot || { + log "Failed to setup chroot" error + exit 1 + } + chroot "$CHROOTDIR" /usr/bin/fish + detach_chroot || { + log "Failed to detach chroot" error + exit 1 + } +} + +if [ "$(id -u)" != "0" ]; then + log "$0 must be run as root" + exit 3 +fi + +[[ "$(uname -m)" != "x86_64*" ]] || { + printf "Unsupported CPU arch\n" + exit 1 +} + +arguments "$*" + diff --git a/setup.sh b/setup.sh deleted file mode 100755 index 3288356..0000000 --- a/setup.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -source ./common.d/functions.sh - -if [ "$(id -u)" != "0" ]; then - log "$0 must be run as root" - exit 3 -fi - -arguments "$*" - From f36859b014f70521d430d6e235eafd569dcf0866 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:06:59 +0500 Subject: [PATCH 04/16] mklni: Mark logs as host --- mklni | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mklni b/mklni index fdc554b..20fb392 100755 --- a/mklni +++ b/mklni @@ -2,20 +2,19 @@ ALPINE="https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-minirootfs-3.20.3-x86_64.tar.gz" CHROOTDIR="./alpine_chroot" -LON_TOOL_URL="https://git.timoxa0.su/timoxa0/lon-tool/releases/download/latest/lon-tool_lin_amd64" - +QEMU_STATIC_URL="https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static" log() { case "$2" in error) - printf "\e[1m\e[31mERROR:\e[0m \e[1m%s\e[0m\n" "$1">&2;; + printf "\e[1m\e[31mERROR:\e[0m \e[1m%s\e[0m (host)\n" "$1">&2;; internal) - printf "\e[1m\e[96m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + printf "\e[1m\e[96m h>\e[0m \e[1m%s\e[0m\n" "$1">&2;; ierror) - printf "\e[1m\e[31m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + printf "\e[1m\e[31m h>\e[0m \e[1m%s\e[0m\n" "$1">&2;; *) - printf "\e[1m\e[92m>>>\e[0m \e[1m%s\e[0m\n" "$1">&2;; + printf "\e[1m\e[92mH>>\e[0m \e[1m%s\e[0m\n" "$1">&2;; esac } From 07fb3de773fe48f58226385601471b800c77d37a Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:09:58 +0500 Subject: [PATCH 05/16] mklni: Change qemu static install path --- mklni | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mklni b/mklni index 20fb392..7a3590c 100755 --- a/mklni +++ b/mklni @@ -154,10 +154,10 @@ prepare_chroot() else # shellcheck disable=SC2028 - echo ':aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true + echo ':aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/bin/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true # shellcheck disable=SC2028 - echo ':aarch64ld:M::\x7fELF\x02\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true + echo ':aarch64ld:M::\x7fELF\x02\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:'"$(realpath $CHROOTDIR/bin/qemu-aarch64-static):PF" > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true fi } @@ -228,7 +228,8 @@ install_chroot() log "Installing qemu-aarch64-static" { - wget -q --show-progress -N https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static -O "$CHROOTDIR/qemu-aarch64-static" && chmod 755 "$CHROOTDIR/qemu-aarch64-static" + wget -q --show-progress -N "$QEMU_STATIC_URL" -O "$CHROOTDIR/bin/qemu-aarch64-static" && + chmod 755 "$CHROOTDIR/bin/qemu-aarch64-static" } || { log "Failed to install qemu-aarch64-static" error exit 1 From c241509cf83032f25b5b9625255a781f4d9e549d Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:10:44 +0500 Subject: [PATCH 06/16] mklni: Make wget, apk and git quiet --- mklni | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mklni b/mklni index 7a3590c..4c97e65 100755 --- a/mklni +++ b/mklni @@ -210,18 +210,19 @@ install_chroot() exit 1 } log "Updating apk repos" - chroot "$CHROOTDIR" apk update || { + chroot "$CHROOTDIR" apk update --quiet || { log "Failed to update apk repos" error exit 1 } log "Installing pakages" - chroot "$CHROOTDIR" apk add shadow fish bash git debootstrap pixz losetup rsync pv wget e2fsprogs e2fsprogs-extra libarchive-tools coreutils || { + chroot "$CHROOTDIR" apk add --quiet shadow fish bash git debootstrap pixz losetup rsync pv wget e2fsprogs e2fsprogs-extra libarchive-tools coreutils go libusb libusb-dev pkgconfig || { log "Failed to install packages" error exit 1 } chroot "$CHROOTDIR" chsh -s /usr/bin/fish root + log "Cloning makelni repo" - chroot "$CHROOTDIR" git clone "https://git.timoxa0.su/timoxa0/makelni.git" /makelni || { + chroot "$CHROOTDIR" git clone --quiet "https://git.timoxa0.su/timoxa0/makelni.git" /makelni || { log "Failed to clone makelni repo" error exit 1 } From aa3a7d0dd53c5012074b7a3d050e58146f945c8e Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:11:07 +0500 Subject: [PATCH 07/16] mklni: Add lno-tool installation --- mklni | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mklni b/mklni index 4c97e65..6b85222 100755 --- a/mklni +++ b/mklni @@ -236,6 +236,16 @@ install_chroot() exit 1 } + log "Installing lon-tool" + { + chroot "$CHROOTDIR" git clone --quiet https://git.timoxa0.su/timoxa0/lon-tool.git /lon-tool-src && + chroot "$CHROOTDIR" /bin/bash -c 'cd /lon-tool-src && rev=$(git describe --abbrev=4 --dirty --always --tags) && go get git.timoxa0.su/timoxa0/lon-tool/cmd && go build -ldflags "-X git.timoxa0.su/timoxa0/lon-tool/cmd.version=$rev" -o /bin/lon-tool main.go' + + } || { + log "Failed to install lon-tool" error + exit 1 + } + detach_chroot || { log "Failed to detach chroot" error exit 1 From 4a5c85485596cee16f8e98e59e68341fabf1c0c7 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:12:05 +0500 Subject: [PATCH 08/16] mklni: Add build function --- mklni | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/mklni b/mklni index 6b85222..2ce550e 100755 --- a/mklni +++ b/mklni @@ -62,6 +62,10 @@ arguments() { remove_chroot break 2 ;; +build) + build "$1" + break 2 ;; + chroot) chroot_into break 2 ;; @@ -89,6 +93,9 @@ usage() { # Remove build chroot $0 uninstall + # Make image + $0 build path/to/lnibuild + # Chroot into $0 chroot EOF @@ -281,6 +288,46 @@ chroot_into() } } +build() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Chroot not installed" error + exit 1 + } + + local lni="$1" + [ ! -f "$lni" ] && { + log "$lni no such file" error + exit 2 + } + + cp "$lni" "$CHROOTDIR/tmp/lnibuild" + + prepare_chroot || { + log "Failed to setup chroot" error + exit 1 + } + + chroot "$CHROOTDIR" /bin/bash -c "cd /makelni && ./makelni /tmp/lnibuild" + local exitcode=$? + rm "$CHROOTDIR/tmp/lnibuild" + + detach_chroot || { + log "Failed to detach chroot" error + exit 1 + } + + [ "$exitcode" -eq "0" ] || { + log "Failed to build image" error + exit 1 + } + + source <( grep name "$lni" ) + mv "$CHROOTDIR/makelni/out/${name}.lni" "./${name}.lni" + chown "$(stat -c '%U:%G' mklni)" "./${name}.lni" + exit 0 +} + if [ "$(id -u)" != "0" ]; then log "$0 must be run as root" exit 3 From a564fd7787cd8abfe1bf9bb953c44b07b067b4f4 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:12:32 +0500 Subject: [PATCH 09/16] mklni: Add cleanup args --- mklni | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/mklni b/mklni index 2ce550e..a0553c9 100755 --- a/mklni +++ b/mklni @@ -70,6 +70,10 @@ build) chroot_into break 2 ;; + -r | -c | --cleanraw | --cleanup) + run_makelni "$opt" + break ;; + -h | -help | --help) usage ;; @@ -96,6 +100,12 @@ usage() { # Make image $0 build path/to/lnibuild + # Clean raw images + $0 -r or --cleanraw + + # Clean build chroots + $0 -c or --cleanup + # Chroot into $0 chroot EOF @@ -328,6 +338,35 @@ build() exit 0 } +run_makelni() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Chroot not installed" error + exit 1 + } + + log "Running makelni $1" + + prepare_chroot || { + log "Failed to setup chroot" error + exit 1 + } + + chroot "$CHROOTDIR" /bin/bash -c "cd /makelni && ./makelni $1" + local exitcode=$? + + detach_chroot || { + log "Failed to detach chroot" error + exit 1 + } + + [ "$exitcode" -eq "0" ] || { + log "Failed to build image" error + } + + return "$exitcode" +} + if [ "$(id -u)" != "0" ]; then log "$0 must be run as root" exit 3 From cd2f6cc93ee1da5634b08ac1029ae82e41f4e77b Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:13:01 +0500 Subject: [PATCH 10/16] mklni: Fix unsafe remove_chroot --- mklni | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mklni b/mklni index a0553c9..22ab630 100755 --- a/mklni +++ b/mklni @@ -281,8 +281,13 @@ remove_chroot() log "Failed to umount chroot" error exit 1 } + { + rm -rf "${CHROOTDIR:?}/" && log "Done!" - rm -rf "${CHROOTDIR:?}/" + } || { + log "Failed to remove chroot" error + exit 1 + } } chroot_into() From 2ba81cb16c9f81e338e2d2254817a7edf0a5cc1d Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:13:12 +0500 Subject: [PATCH 11/16] mklni: Fix args parser --- mklni | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mklni b/mklni index 22ab630..0cea460 100755 --- a/mklni +++ b/mklni @@ -382,5 +382,5 @@ fi exit 1 } -arguments "$*" +arguments $* From 6dd8be5eb38a687aea56c078b5f88021dcf53c05 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:14:43 +0500 Subject: [PATCH 12/16] mklni: Fix indents --- mklni | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mklni b/mklni index 0cea460..11e5693 100755 --- a/mklni +++ b/mklni @@ -62,7 +62,7 @@ arguments() { remove_chroot break 2 ;; -build) + build) build "$1" break 2 ;; @@ -262,7 +262,7 @@ install_chroot() log "Failed to install lon-tool" error exit 1 } - + detach_chroot || { log "Failed to detach chroot" error exit 1 @@ -283,7 +283,7 @@ remove_chroot() } { rm -rf "${CHROOTDIR:?}/" && - log "Done!" + log "Done!" } || { log "Failed to remove chroot" error exit 1 From a5c5c397983f8ff4603fdc23c6bed75d377760d9 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:15:49 +0500 Subject: [PATCH 13/16] mklni: Use $0 as user:group source --- mklni | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mklni b/mklni index 11e5693..cf3a0b4 100755 --- a/mklni +++ b/mklni @@ -339,7 +339,7 @@ build() source <( grep name "$lni" ) mv "$CHROOTDIR/makelni/out/${name}.lni" "./${name}.lni" - chown "$(stat -c '%U:%G' mklni)" "./${name}.lni" + chown "$(stat -c '%U:%G' "$0")" "./${name}.lni" exit 0 } From d8b2eb7f197da453feb89c42d3b7e5211fad86b8 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:16:15 +0500 Subject: [PATCH 14/16] functions: Remove common.d --- common.d/functions.sh | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 common.d/functions.sh diff --git a/common.d/functions.sh b/common.d/functions.sh deleted file mode 100644 index 20d602b..0000000 --- a/common.d/functions.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env bash - From e209d0d746eadbcccd45258200acf59fd9b2b5de Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sat, 2 Nov 2024 23:41:18 +0500 Subject: [PATCH 15/16] mklni: Add update --- mklni | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/mklni b/mklni index cf3a0b4..8a8d879 100755 --- a/mklni +++ b/mklni @@ -70,12 +70,17 @@ arguments() { chroot_into break 2 ;; + update) + update + break 2 ;; + -r | -c | --cleanraw | --cleanup) run_makelni "$opt" - break ;; + break 2 ;; -h | -help | --help) - usage ;; + usage + break 2 ;; *) log "Unknown option: $opt" error @@ -318,6 +323,7 @@ build() cp "$lni" "$CHROOTDIR/tmp/lnibuild" + log "Entering build chroot" prepare_chroot || { log "Failed to setup chroot" error exit 1 @@ -327,6 +333,7 @@ build() local exitcode=$? rm "$CHROOTDIR/tmp/lnibuild" + log "Exiting build chroot" detach_chroot || { log "Failed to detach chroot" error exit 1 @@ -372,6 +379,34 @@ run_makelni() return "$exitcode" } +update() +{ + [ ! -d "$CHROOTDIR" ] && { + log "Chroot not installed" error + exit 1 + } + + prepare_chroot || { + log "Failed to setup chroot" error + exit 1 + } + + log "Updating makelni" + chroot "$CHROOTDIR" /bin/bash -c "cd /makelni && git pull" + local exitcode=$? + + detach_chroot || { + log "Failed to detach chroot" error + exit 1 + } + + [ "$exitcode" -eq "0" ] || { + log "Failed to update makelni" error + } + + return "$exitcode" +} + if [ "$(id -u)" != "0" ]; then log "$0 must be run as root" exit 3 From 8ab20ebd349207c7ea5c0678b967869adcecd836 Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Sun, 3 Nov 2024 00:26:43 +0500 Subject: [PATCH 16/16] readme: Add readme --- README.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 478cb9c..671143f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,26 @@ +<<<<<<< HEAD # mklni -mklni - Image builder for nabu, running in alpine chroot \ No newline at end of file +mklni - Image builder for nabu, running in alpine chroot +======= +# mklni - Chroot helper for makelni + +## Installation +1. Create a new directory +2. Download [mklni](https://git.timoxa0.su/timoxa0/mklni/src/branch/main/mklni.sh) in that directory +3. Allow executing mklin ```chmod +x mklni``` +4. Install chroot ```sudo ./mklni install``` + +## Usage +- Setup build chroot ```sudo ./mklni install``` + +- Remove build chroot ```sudo ./mklni uninstall``` + +- Make image ```sudo ./mklni build path/to/lnibuild```. Image will be created in same directory + +- Clean raw images ```sudo ./mklni -r or --cleanraw``` + +- Clean build chroots ```sudo ./mklni -c or --cleanup``` + +- Chroot into ```sudo ./mklni chroot``` +>>>>>>> 45494b4 (readme: Add readme)