-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added automatic temporary swap file usage
- Loading branch information
Showing
6 changed files
with
245 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!/bin/bash | ||
|
||
# Sets up sudo permissions for the shaketune module to allow | ||
# the user to create and delete a swap file without requiring a password | ||
|
||
|
||
set -e | ||
|
||
SUDOERS_DIR='/etc/sudoers.d' | ||
SUDOERS_FILE='020-sudo-for-shaketune' | ||
NEW_GROUP='shaketunesudo' | ||
|
||
|
||
verify_ready() { | ||
if [ "$EUID" -eq 0 ]; then | ||
echo "This script must not run as root" | ||
exit -1 | ||
fi | ||
} | ||
|
||
create_sudoers_file() { | ||
SCRIPT_TEMP_PATH=/tmp | ||
|
||
echo "Creating ${SUDOERS_FILE} ..." | ||
sudo rm -f $SCRIPT_TEMP_PATH/$SUDOERS_FILE | ||
sudo tee $SCRIPT_TEMP_PATH/$SUDOERS_FILE > /dev/null << EOF | ||
Cmnd_Alias SWAP_CREATE = /usr/bin/fallocate -l * /home/*/shaketune_swap, /bin/dd if=/dev/zero of=/home/*/shaketune_swap bs=* count=* | ||
Cmnd_Alias SWAP_SETUP = /sbin/mkswap /home/*/shaketune_swap, /sbin/swapon /home/*/shaketune_swap | ||
Cmnd_Alias SWAP_REMOVE = /sbin/swapoff /home/*/shaketune_swap, /bin/rm /home/*/shaketune_swap | ||
%${NEW_GROUP} ALL=(root) NOPASSWD: SWAP_CREATE, SWAP_SETUP, SWAP_REMOVE | ||
EOF | ||
} | ||
|
||
verify_syntax() { | ||
if command -v visudo &> /dev/null; then | ||
echo "Verifying syntax of ${SUDOERS_FILE}..." | ||
if sudo visudo -cf $SCRIPT_TEMP_PATH/$SUDOERS_FILE; then | ||
VERIFY_STATUS=0 | ||
echo "Syntax OK" | ||
else | ||
echo "Syntax Error: Check file at $SCRIPT_TEMP_PATH/$SUDOERS_FILE" | ||
exit 1 | ||
fi | ||
else | ||
VERIFY_STATUS=0 | ||
echo "Command 'visudo' not found. Skipping syntax verification." | ||
fi | ||
} | ||
|
||
install_sudoers_file() { | ||
verify_syntax | ||
if [ $VERIFY_STATUS -eq 0 ]; then | ||
echo "Installing sudoers file..." | ||
sudo chmod 0440 $SCRIPT_TEMP_PATH/$SUDOERS_FILE | ||
sudo cp $SCRIPT_TEMP_PATH/$SUDOERS_FILE $SUDOERS_DIR/$SUDOERS_FILE | ||
else | ||
exit 1 | ||
fi | ||
} | ||
|
||
add_new_group() { | ||
if ! getent group $NEW_GROUP &> /dev/null; then | ||
echo "Creating group ${NEW_GROUP}..." | ||
sudo groupadd --system $NEW_GROUP | ||
else | ||
echo "Group ${NEW_GROUP} already exists." | ||
fi | ||
} | ||
|
||
add_user_to_group() { | ||
if groups $USER | grep -qw $NEW_GROUP; then | ||
echo "User ${USER} is already in group ${NEW_GROUP}." | ||
else | ||
echo "Adding user ${USER} to group ${NEW_GROUP}..." | ||
sudo usermod -aG $NEW_GROUP $USER | ||
fi | ||
} | ||
|
||
clean_temp() { | ||
sudo rm -f $SCRIPT_TEMP_PATH/$SUDOERS_FILE | ||
} | ||
|
||
|
||
# Run steps | ||
verify_ready | ||
create_sudoers_file | ||
install_sudoers_file | ||
add_new_group | ||
add_user_to_group | ||
clean_temp | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Shake&Tune: 3D printer analysis tools | ||
# | ||
# Copyright (C) 2022 - 2024 Félix Boisselier <[email protected]> (Frix_x on Discord) | ||
# Licensed under the GNU General Public License v3.0 (GPL-3.0) | ||
# | ||
# File: swap_manager.py | ||
# Description: Implements the SwapManager class for managing the creation and | ||
# activation of a temporary swap file on the system to avoid running | ||
# out of memory when processing large files (useful for low end devices like CB1) | ||
|
||
import shutil | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from .helpers.console_output import ConsoleOutput | ||
|
||
SWAP_FILE_PATH = Path.home() / 'shaketune_swap' | ||
|
||
|
||
class SwapManager: | ||
def __init__(self, swap_size_mb: int = 0) -> None: | ||
self._swap_size_mb = swap_size_mb | ||
self._swap_file_path = SWAP_FILE_PATH | ||
self._swap_activated = False | ||
|
||
def is_swap_activated(self) -> bool: | ||
return self._swap_activated | ||
|
||
def add_swap(self) -> None: | ||
if self._swap_size_mb <= 0: | ||
return | ||
|
||
# Check if swap file already exists and delete it if it does | ||
if self._swap_file_path.exists(): | ||
ConsoleOutput.print(f'Warning: {self._swap_file_path} already exists. Replacing it...') | ||
self.remove_swap() | ||
|
||
# Check available disk space to be sure there is enough space for the swap file | ||
total, used, free = shutil.disk_usage(self._swap_file_path.parent) | ||
free_mb = free // (1024 * 1024) | ||
if free_mb < self._swap_size_mb: | ||
ConsoleOutput.print( | ||
f'Warning: not enough disk space available ({free_mb} MB) to create the temporary swap file ' | ||
f'that you asked for ({self._swap_size_mb} MB). It will not be created for this run...' | ||
) | ||
return | ||
|
||
# Create the swap file and activate it | ||
try: | ||
subprocess.run( | ||
[ | ||
'sudo', | ||
'dd', | ||
'if=/dev/zero', | ||
'of=' + str(self._swap_file_path), | ||
'bs=1M', | ||
'count=' + str(self._swap_size_mb), | ||
], | ||
check=True, | ||
) | ||
subprocess.run(['sudo', 'chmod', '600', str(self._swap_file_path)], check=True) | ||
subprocess.run(['sudo', 'mkswap', str(self._swap_file_path)], check=True) | ||
subprocess.run(['sudo', 'swapon', str(self._swap_file_path)], check=True) | ||
self._swap_activated = True | ||
ConsoleOutput.print(f'Temporary swap file of {self._swap_size_mb} MB activated') | ||
except subprocess.CalledProcessError as err: | ||
self.remove_swap() | ||
raise RuntimeError('Failed to create and activate the temporary swap file!') from err | ||
|
||
def remove_swap(self) -> None: | ||
if not self._swap_file_path.exists(): | ||
return | ||
|
||
try: | ||
if self._swap_activated: | ||
subprocess.run(['sudo', 'swapoff', str(self._swap_file_path)], check=True) | ||
subprocess.run(['sudo', 'rm', str(self._swap_file_path)], check=True) | ||
except subprocess.CalledProcessError as err: | ||
raise RuntimeError('Failed to deactivate and delete the temporary swap file!') from err |