Skip to content

Commit

Permalink
update pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
UriHerrera committed Jun 13, 2023
1 parent 8eb22e1 commit 0216104
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 137 deletions.
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
nuts (0.1.2) nitrux; urgency=medium

* Move update and restore functionality to seprate scripts.

-- Uri Herrera <uri_herrera@nxos.org> Tue, 13 Jun 2023 05:05:00 -0500

nuts (0.1.1) nitrux; urgency=medium

* Move working directories variables to conf file.
Expand Down
144 changes: 7 additions & 137 deletions usr/bin/nuts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fi
# -- Functions.

dl_file () { axel -o /tmp -n 10 "$@"; }
overlay_ch () { $ROOT overlayroot-chroot; }
overlay_ch () { $ROOT overlayroot-chroot "$@"; }
bak_cc () { $ROOT find "$NUTS_DIR_BAK" -maxdepth 1 -type f | head -n "$BACKUPS_TO_KEEP" | xargs --no-run-if-empty rm; }


Expand All @@ -113,6 +113,8 @@ else
fi


# ====== START ======

# -- Flag parsing.

(
Expand Down Expand Up @@ -164,7 +166,6 @@ for cmd; do
esac
done

# ====== START ======

for cmd; do
case "$cmd" in
Expand Down Expand Up @@ -344,82 +345,9 @@ for cmd; do
# -- After using overlay_ch, it's essential to remember that **EVERY COMMAND IS RUNNING AS ROOT AND ON THE LOWER DIRECTORY OF THE OVERLAY**.

# -- Enter the Matri... I mean, the overlay.
# -- Run nuts-cru to perform the update actions inside the chroot.

tail "$0" | SYNC_NEW_ROOT=1 overlay_ch


# -- Inside the curly brackets, we enter a new shell because of the chroot.
# -- Whatever is on the script that is executed using a user shell will not be executed here.
# -- So, we must add what we want to run in the chroot inside the curly brackets, including variables.

[ "$SYNC_NEW_ROOT" ] && {

# -- Variables.

NUTS_DIR_DLS="/home/.nuts/downloads"
NUTS_DIR_ISO="/home/.nuts/iso"
NUTS_DIR_SQS="/home/.nuts/squashfs"


# -- Mount the devices filesystem, then mount the ISO and the SquashFS.

mount -t devtmpfs dev /dev
mount "$NUTS_DIR_DLS"/nitrux.iso "$NUTS_DIR_ISO"
mount "$NUTS_DIR_ISO"/casper/filesystem.squashfs "$NUTS_DIR_SQS"


# -- Now comes the good part; proceed with running rsync and update the current installation.

echo "Updating ${CURRENT_DIST^n}, continuing..."

rsync -avzh \
--exclude /dev/ \
--exclude /etc/shadow \
--exclude /home/ \
--exclude /proc/ \
--exclude /run/ \
--exclude /sys/ \
--exclude /var/{cache,lib,local,lock,log,mail,opt,run,spool,tmp} \
--progress \
"$NUTS_DIR_SQS" /
sync

echo "Success! Update has been applied."


# -- Unmount the ISO and the SquashFS.

umount "$NUTS_DIR_SQS"
umount "$NUTS_DIR_ISO"


# -- Update GRUB.

update-grub


# -- Unmount the devices filesystem.

umount /dev


# -- Do a cleanup of the synced root.

echo "Calling the $TOOL_NAME Cleanup Crew, continuing..."
dl_file https://raw.githubusercontent.com/Nitrux/nuts/main/tmp/nuts-cc
chmod +x /tmp//nuts-cc
bash /tmp//nuts-cc


# -- Finalizing.
# -- Leave the Matri... I mean, the overlay.

rm -r \
/tmp//nuts-cc \
"$NUTS_DIR_DLS"/nitrux.iso

exit
}
overlay_ch nuts-cru


# -- After running rsync reboot the device.
Expand All @@ -446,68 +374,10 @@ for cmd; do


# -- Enter the Matri... I mean, the overlay.
# -- Run nuts-cru to perform the restore actions inside the chroot.

tail "$0" | SYNC_BACKUP=1 overlay_ch


# -- Inside the curly brackets, we enter a new shell because of the chroot.
# -- Whatever is on the script that is executed using a user shell will not be executed here.
# -- So, we must add what we want to run in the chroot inside the curly brackets, including variables.

[ "$SYNC_BACKUP" ] && {

# -- Variables.

NUTS_DIR_SQS="/home/.nuts/squashfs"
NUTS_DIR_BAK="/home/.nuts/backup"


# -- Mount the devices filesystem, then mount the SquashFS.
# -- **ALWAYS** use the newest SquashFS in the directory. The user can't select which backup to restore.

mount -t devtmpfs dev /dev
mount "$(echo "$NUTS_DIR_BAK"/nuts-backup-*.squashfs | tr ' ' '\n' | tail -n 1)" "$NUTS_DIR_SQS"


# -- Now comes the good part; proceed with running rsync and restore the backup.

echo "Restoring backup of ${CURRENT_DIST^n}, continuing..."

rsync -avzh \
--exclude /dev/ \
--exclude /etc/shadow \
--exclude /home/ \
--exclude /proc/ \
--exclude /run/ \
--exclude /sys/ \
--exclude /var/{cache,lib,local,lock,log,mail,opt,run,spool,tmp} \
--progress \
"$NUTS_DIR_SQS" /
sync

echo "Success! Backup has been restored."


# -- Unmount the SquashFS.

umount "$NUTS_DIR_SQS"


# -- Update GRUB.

update-grub


# -- Unmount the devices filesystem.

umount /dev


# -- Finalizing.
# -- Leave the Matri... I mean, the overlay.
overlay_ch nuts-crb

exit
}

# -- After running rsync reboot the device.

Expand Down
115 changes: 115 additions & 0 deletions usr/bin/nuts-crb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#! /bin/bash

#############################################################################################################################################################################
# The license used for this file and its contents is: BSD-3-Clause #
# #
# Copyright <2023> <Uri Herrera <uri_herrera@nxos.org>> #
# Copyright <2023> <Luis Lavaire <luis_lavaire@nxos.org>> #
# #
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: #
# #
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. #
# #
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer #
# in the documentation and/or other materials provided with the distribution. #
# #
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software #
# without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, #
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS #
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, #
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
#############################################################################################################################################################################


# -- Exit on errors.

set -e


# -- Set program name and version.

TOOL_NAME="Nitrux Update System Tool"


# -- Define the path of the nuts configuration file.

CONFIG_FILE="/etc/nuts.conf"


# -- Load values from configuration file.
# -- Include fix for SC1090 – ShellCheck.

if [[ ! -f $CONFIG_FILE ]]; then
puts-error "$TOOL_NAME configuration file not found!, quitting." 2>&1
exit 1
else
puts-success "Success! $TOOL_NAME configuration file found, continuing..."
# shellcheck source=/dev/null
. "$CONFIG_FILE"
fi


# ====== START ======

# -- Variables.

CURRENT_DIST=$(tail /etc/os-release | grep "VERSION_CODENAME=*" | rev | cut -d '=' -f 1 | rev)


# -- Mount the devices filesystem, then mount the ISO and the SquashFS.

mount -t devtmpfs dev /dev
mount "$NUTS_DIR_DLS"/nitrux.iso "$NUTS_DIR_ISO"
mount "$NUTS_DIR_ISO"/casper/filesystem.squashfs "$NUTS_DIR_SQS"


# -- Mount the devices filesystem, then mount the SquashFS.
# -- **ALWAYS** use the newest SquashFS in the directory. The user can't select which backup to restore.

mount -t devtmpfs dev /dev
mount "$(echo "$NUTS_DIR_BAK"/nuts-backup-*.squashfs | tr ' ' '\n' | tail -n 1)" "$NUTS_DIR_SQS"


# -- Now comes the good part; proceed with running rsync and restore the backup.

echo "Restoring backup of ${CURRENT_DIST^n}, continuing..."

rsync -avzh \
--exclude /dev/ \
--exclude /etc/shadow \
--exclude /home/ \
--exclude /proc/ \
--exclude /run/ \
--exclude /sys/ \
--exclude /var/{cache,lib,local,lock,log,mail,opt,run,spool,tmp} \
--progress \
"$NUTS_DIR_SQS" /
sync

echo "Success! Backup has been restored."


# -- Unmount the SquashFS.

umount "$NUTS_DIR_SQS"


# -- Update GRUB.

update-grub


# -- Unmount the devices filesystem.

umount /dev


# -- Finalizing.
# -- Leave the Matri... I mean, the overlay.

exit

# ====== END ======
Loading

0 comments on commit 0216104

Please sign in to comment.