Skip to content

Commit

Permalink
update pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
UriHerrera committed Aug 26, 2023
2 parents a750561 + 9144cbf commit 7a6f9d3
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 33 deletions.
4 changes: 1 addition & 3 deletions .github/ISSUE_TEMPLATE/bug_report.md
Expand Up @@ -7,8 +7,7 @@ about: Create a report to help us improve
**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
**Steps to reproduce bug**
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
Expand All @@ -22,5 +21,4 @@ If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -15,7 +15,7 @@ The Nitrux Update Tool System (`nuts`) utility is designed to update [Nitrux OS]

`nuts` is a simple and minimalistic system update and rollback utility. It performs three steps:

1. Creates a backup of the root directory using SquashFS and stores it locally.
1. Creates a backup of the root directory using SquashFS and the XFS partition and stores them locally.
2. Downloads the latest ISO image using the BitTorrent protocol and updates the system using `rsync`.
3. When restoring a backup, uses the locally generated SquashFS file (instead of downloading an ISO).

Expand Down Expand Up @@ -73,11 +73,16 @@ sudo cp $HOME/nuts/etc/nuts.conf /etc
### Commands:

**Update**: `sudo nuts update`
- Updates the currently installed root using the specified media in `nuts-query` and backs up the current root directory.
- Updates the currently installed root using the specified media in `nuts-query` and backs up the current root directory and partition.
- _♦ Information: This operation also switches the kernel on supported hardware after completing the update, i.e., not you, Nvidia._

**Restore**: `sudo nuts restore`
- Restores the backup of the root directory generated during the update.

**Rescue**: `sudo nuts rescue`
- Restores the backup of the XFS root partition in case of an interrupted update.
- _♦ Information: This operation is a special handling of an unforeseen event. If rsync were to be interrupted, the root would be inconsistent. That means the root is unusable, i.e., the user can't access the GUI or, worse, a TTY, so the user can't restore the SquashFS. This operation will allow the user to restore the root partition from a Live session. This operation does not replace `restore`; it exists if using `restore` is impossible._

### Configuration:

`nuts` uses the file `/etc/nuts.conf` to load some settings.
Expand Down
30 changes: 28 additions & 2 deletions debian/changelog
@@ -1,8 +1,34 @@
nuts (1.1.1) nitrux; urgency=medium
nuts (1.1.3) nitrux; urgency=medium

* Remove kexec code.

-- Uri Herrera <uri_herrera@nxos.org> Mon, 21 Aug 2023 16:05:00 -0500
-- Uri Herrera <uri_herrera@nxos.org> Sat, 26 Aug 2023 11:48:00 -0500

nuts (1.1.3) nitrux; urgency=medium

* Add a timeout before executing kexec.

-- Uri Herrera <uri_herrera@nxos.org> Sun, 20 Aug 2023 11:48:00 -0500

nuts (1.1.2) nitrux; urgency=medium

* Add new rescue operation to help.
* Fixed file paths when using rescue from Live session.
* Create directories used during the execution of the rescue operation.
* Remove the compressed xfs backup as we only intend to keep one.
* Specify that using a different branch of nuts in nuts.conf does not download a different ISO.
* Also update the README on the source repository with information about the new operation.

-- Uri Herrera <uri_herrera@nxos.org> Sun, 20 Aug 2023 09:40:00 -0500

nuts (1.1.1) nitrux; urgency=medium

* Add new rescue option to create and restore a backup of the root partition.
* Fix command to create xfs backup.
* Use as many threads as possible to compress xfs backup file.
* Update filename of compressed xfs backup for decompression and utilization.

-- Uri Herrera <uri_herrera@nxos.org> Sat, 19 Aug 2023 23:25:00 -0500

nuts (1.1.0) nitrux; urgency=medium

Expand Down
8 changes: 5 additions & 3 deletions etc/nuts.conf
Expand Up @@ -2,12 +2,13 @@
# nuts.conf 2023 (c) Nitrux Latinoamericana S.C.

# Define working directories.
# Default path is /home/.nuts
# Default path is: /home/.nuts
#
NUTS_DIR_DLS=/home/.nuts/downloads
NUTS_DIR_ISO=/home/.nuts/iso
NUTS_DIR_SQS=/home/.nuts/squashfs
NUTS_DIR_BAK=/home/.nuts/backup
NUTS_DIR_XFS=/home/.nuts/xfs

# Define whether to use a date or not for backup file.
# Default value is: $(date +%Y.%m.%d-%H.%M)
Expand All @@ -25,7 +26,8 @@ BACKUPS_TO_KEEP=1
#
NUTS_LOG=/var/log/nuts.log

# Define which branch on Github is used to download the components and other files.
# Default is main
# **ONLY CHANGE THIS IF YOU KNOW WHAT YOU ARE DOING.**
# Defines which branch the Nitrux Update Tool System uses to download its components and other files from GitHub (excluding a different ISO file).
# Default branch is: main
#
NUTS_BRANCH=main
162 changes: 162 additions & 0 deletions usr/bin/nuts
Expand Up @@ -74,6 +74,15 @@ print_message() {
}


# -- Print warning messages to stderr.

puts_warning() {
if [ -n "$1" ]; then
printf "%s: \e[33mWarning:\e[0m %s\n" "$TOOL_NAME" "$*" >&2
fi
}


# -- Check if this is running as root.
# -- Exit if not running as root.

Expand Down Expand Up @@ -157,6 +166,17 @@ overlay_ch() {
"${ROOT[@]}"
}

unmount_directory() {
local DIRECTORY="$1"

if mountpoint -q "$DIRECTORY"; then
puts_info "Unmounting: $DIRECTORY"
umount "$DIRECTORY"
else
puts_info "Directory is not mounted: $DIRECTORY"
fi
}


# -- Use a log file.
# -- Everything below will go to the file nuts.log.
Expand Down Expand Up @@ -198,6 +218,7 @@ for cmd; do
"Operations:" \
" update Update the currently installed root using the media specified in nuts-query and creates a backup of the current root directory." \
" restore Restore the backup of the root directory generated during the update." \
" rescue Restore the backup of the root partition in case of an interrupted update.." \
"" \
"Usage:" \
" sudo nuts <flag> (operation)" \
Expand Down Expand Up @@ -311,13 +332,68 @@ done
fi


# -- Create the directories for nuts.
# -- Create working directories in /home since this partition will likely have more space than the root partition.

DIRECTORIES=("$NUTS_DIR_BAK" "$NUTS_DIR_XFS" "$NUTS_DIR_ISO" "$NUTS_DIR_DLS" "$NUTS_DIR_SQS")

for DIR in "${DIRECTORIES[@]}"; do
if [[ ! -d "$DIR" ]]; then
puts_error "Directory '$DIR' does not exist. Creating the directory..."
if ! mkdir -p "$DIR"; then
puts_error "Failed to create directory '$DIR'. Exiting..."
exit 1
fi
puts_info "Directory '$DIR' created successfully."
fi
done

puts_info "All required directories exist. Continuing."


# -- Step 1: Backup sub-process

# -- This process begins as a step before doing an update.
# -- Backup files are only generated when updating; the user can't ask the utility to create them on demand.
# -- This process is done inside the chroot; otherwise, when using overlayroot-chroot, the files, and paths will not exist.


# -- Create a backup of the partition for redundancy using xfsdump to later use it during a rescue operation from a Live session.
# -- By the way, only *one* backup file is created.
# -- Compress the backup file using zstd.
# -- This backup is created outside of the chroot because xfsdump relies on /etc/mtab, and that file is unavailable when using overlayroot-chroot.

ROOT_PARTITION=$(findfs LABEL=NX_ROOT)
ROOT_PARTITION_LABEL=$(blkid -o value -s LABEL "$ROOT_PARTITION")
XFS_BACKUP_FILE="$NUTS_DIR_XFS/xfs-backup.xfs"
COMPRESSED_XFS_BACKUP="$XFS_BACKUP_FILE.zst"
ZSTD_COMP_THREADS=$(nproc)

puts_info "Creating redundant backup of $ROOT_PARTITION_LABEL, continuing..."

if [ -e "$XFS_BACKUP_FILE" ]; then
puts_info "Deleting existing backup: $XFS_BACKUP_FILE, continuing..."
rm -f "$XFS_BACKUP_FILE"
else
puts_info "Backup file not found: $XFS_BACKUP_FILE, creating it..."
xfsdump -l0 -L "Nitrux_Update_Tool_System-XFS_Backup" -M "NX_BAK-Vol-1" -f "$XFS_BACKUP_FILE" "$ROOT_PARTITION"
fi

if [ -e "$COMPRESSED_XFS_BACKUP" ]; then
puts_info "Deleting existing compressed backup: $COMPRESSED_XFS_BACKUP, continuing..."
rm -f "$COMPRESSED_XFS_BACKUP"
else
puts_info "Compressed backup file not found: $COMPRESSED_XFS_BACKUP, creating it..."
zstd -T"$ZSTD_COMP_THREADS" --ultra -19 "$XFS_BACKUP_FILE" -o "$COMPRESSED_XFS_BACKUP"
fi

md5sum "$COMPRESSED_XFS_BACKUP" > "$NUTS_DIR_BAK/xfs-backup.md5sum"

sync

puts_success "Success! $ROOT_PARTITION_LABEL redundant backup has been created."


# -- Step 2: Actual Update sub-process

# -- This process is also done inside the chroot and it is the same as Calamares, except for user creation, mounts and bootloader configuration.
Expand Down Expand Up @@ -378,6 +454,92 @@ done
puts_info "Time to reboot. Bye."

shift;;


rescue )

# -- Step X: Rescue Process

# -- The Rescue Process is a special handling of an unforeseen event; as such, this <operation> does not require using overlayroot-chroot.

# -- We do this because if, during the Actual Update sub-process with rsync, rsync is interrupted, the root will be in an inconsistent state.
# -- That means the root is unusable, i.e., the user can't access the GUI or, worse, a TTY, so the user can't restore the SquashFS.
# -- With that being the case, this <operation> allows the user to restore the root partition from a Live session.

# -- Automatically check if this utility is running in a Live session. Calamares is an easy check as it's only available in a Live session.
# -- This check assumes that the root is now inconsistent; thus, using the SquashFS backup is impossible, so we must restore the XFS backup hence why the user tells this utility to restore a backup from a Live session.
# -- If the check returns that this utility is not running from a Live session, use the SquashFS backup instead.

ROOT_PARTITION=$(findfs LABEL=NX_ROOT)
ROOT_PARTITION_LABEL=$(blkid -o value -s LABEL "$ROOT_PARTITION")
HOME_PARTITION=$(findfs LABEL=NX_HOME)
HOME_PARTITION_LABEL=$(blkid -o value -s LABEL "$HOME_PARTITION")

if mountpoint -q "/media/$USER/NX_ROOT"; then
puts_info "/media/root is already mounted. Unmounting..."
umount "/media/$USER/NX_ROOT"
fi

if mountpoint -q "/media/$USER/NX_HOME"; then
puts_info "/media/home is already mounted. Unmounting..."
umount "/media/$USER/NX_HOME"
fi

mkdir -p "/media/$USER/NX_ROOT" "/media/$USER/NX_HOME"

puts_info "Mounting $ROOT_PARTITION_LABEL to /media/$USER/NX_ROOT..."
mount -t auto "$ROOT_PARTITION" "/media/$USER/NX_ROOT"

puts_info "Mounting $HOME_PARTITION_LABEL to /media/$USER/NX_HOME..."
mount -t auto "$HOME_PARTITION" "/media/$USER/NX_HOME"

COMPRESSED_XFS_BACKUP="/media/$USER/NX_HOME/$NUTS_DIR_XFS/xfs-backup.xfs.zst"
XFS_BACKUP_CHECKSUM_FILE="/media/$USER/NX_HOME/$NUTS_DIR_XFS/xfs-backup.md5sum"
DECOMPRESSED_XFS_BACKUP="/media/$USER/NX_HOME/$NUTS_DIR_XFS/xfs-backup.xfs"

if [ ! -f "$COMPRESSED_XFS_BACKUP" ]; then
puts_error "XFS backup file $COMPRESSED_XFS_BACKUP not found. Quitting."
exit 1
fi

if [ ! -f "$XFS_BACKUP_CHECKSUM_FILE" ]; then
puts_error "XFS backup checksum file $XFS_BACKUP_CHECKSUM_FILE not found. Quitting."
exit 1
fi

BACKUP_SUM=$(md5sum "$COMPRESSED_XFS_BACKUP" | cut -c -32)
GEN_BAK_SUM=$(tail "$XFS_BACKUP_CHECKSUM_FILE" | cut -c -32)

if [[ "$BACKUP_SUM" == "$GEN_BAK_SUM" ]]; then
puts_info "Backup checksum matches, continuing..."
else
puts_error "Backup checksum does not match, quitting."
exit 1
fi

if [ -x "/usr/bin/calamares" ]; then
puts_info "Running $TOOL_NAME from a Live session, proceed with restoration of $ROOT_PARTITION_LABEL, continuing..."
zstd -dc "$COMPRESSED_XFS_BACKUP"
xfsrestore -f "$DECOMPRESSED_XFS_BACKUP" "$ROOT_PARTITION"

puts_success "Success! $ROOT_PARTITION_LABEL has been restored."

unmount_directory /media/root
unmount_directory /media/home
else
puts_warning "Only use this option from a Live session and in an emergency."
puts_info "For a non-emergency situation, use: "$TOOL_SHRT_NAME restore"."

unmount_directory /media/root
unmount_directory /media/home
exit 0
fi

# -- After restoring the XFS backup reboot the device.

puts_info "Time to reboot. Bye."

shift;;
esac
done ) 2>&1 | tee -a "$NUTS_LOG"

Expand Down
12 changes: 5 additions & 7 deletions usr/bin/nuts-crr
Expand Up @@ -125,10 +125,10 @@ unmount_directory() {
local DIRECTORY="$1"

if mountpoint -q "$DIRECTORY"; then
echo "Unmounting: $DIRECTORY"
puts_info "Unmounting: $DIRECTORY"
umount "$DIRECTORY"
else
echo "Directory is not mounted: $DIRECTORY"
puts_info "Directory is not mounted: $DIRECTORY"
fi
}

Expand All @@ -137,7 +137,7 @@ unmount_directory() {
# -- Check if /dev is already mounted.

if mountpoint -q /dev; then
echo "/dev is already mounted. Unmounting..."
puts_info "/dev is already mounted. Unmounting..."
umount /dev
fi

Expand All @@ -152,7 +152,7 @@ mount -t devtmpfs dev /dev
HOME_PARTITION=$(findfs LABEL=NX_HOME)

if mountpoint -q /home; then
echo "/home is already mounted. Unmounting..."
puts_info "/home is already mounted. Unmounting..."
umount /home
fi

Expand Down Expand Up @@ -187,14 +187,12 @@ for cmd; do

exit
;;

-d | --debug ) set -x; shift;;
esac
done


# -- Create the directories for nuts.
# -- Create working directories in /home since this partition will likely have more space than the root partition, as set in our custom partition layout and the instructions on the website.
# -- Create working directories in /home since this partition will likely have more space than the root partition.

DIRECTORIES=("$NUTS_DIR_BAK" "$NUTS_DIR_ISO" "$NUTS_DIR_DLS" "$NUTS_DIR_SQS")

Expand Down

0 comments on commit 7a6f9d3

Please sign in to comment.