#!/usr/bin/env bash # zram.sh — ZRAM submenu: view, create/reconfigure, remove zram_menu() { while true; do local choice choice=$(_menu "ZRAM Configuration" \ "Select an operation:" $TUI_ALTO $TUI_ANCHO 4 \ "1" "View ZRAM status" \ "2" "Create / Reconfigure ZRAM" \ "3" "Remove ZRAM" \ "4" "Back to main menu") [ -z "$choice" ] && break clear case "$choice" in 1) _zram_view ;; 2) _zram_create ;; 3) _zram_remove ;; 4) break ;; esac done } _zram_view() { if ! is_installed "zram-tools"; then _msg "ZRAM Status" "ZRAM is not installed.\n\nUse option 2 (Create / Reconfigure ZRAM)\nto set it up." 10 55 return fi local algo="" size="" priority="" if [ -f /etc/default/zramswap ]; then while IFS='=' read -r key val; do case "$key" in ALGO) algo=$val ;; SIZE) size=$val ;; PRIORITY) priority=$val ;; esac done /dev/null || true) if [ -n "$zram_out" ]; then info+="$zram_out" else info+=" (none — service may be stopped)" fi _msg "ZRAM Status" "$info" 16 70 } _zram_create() { if [ -z "$RAM_KB" ] || [ "$RAM_KB" -eq 0 ]; then echo -e "${RED}Could not determine RAM size. Aborting.${NC}" return 1 fi if is_installed "zram-tools"; then local cur_algo="" cur_size="" cur_prio="" if [ -f /etc/default/zramswap ]; then while IFS='=' read -r key val; do case "$key" in ALGO) cur_algo=$val ;; SIZE) cur_size=$val ;; PRIORITY) cur_prio=$val ;; esac done /dev/null || true sudo modprobe -r zram 2>/dev/null || true echo "Writing configuration..." # SECURITY: Validate algo and size before writing to system file. [[ "$algo" =~ ^(lz4|zstd)$ ]] || { echo "Invalid ZRAM algorithm" >&2 return 1 } [[ "$zram_size" =~ ^[0-9]+$ ]] || { echo "Invalid ZRAM size" >&2 return 1 } sudo tee /etc/default/zramswap >/dev/null </dev/null; then echo -e "${YELLOW}Warning: ZRAM algorithm not applied yet. Reboot to finalize.${NC}" fi echo "" echo -e "${GREEN}ZRAM configured successfully.${NC}" echo "" sudo zramctl echo "" echo -e "${GREEN}You can verify with: sudo zramctl${NC}" _pause } _zram_remove() { if ! is_installed "zram-tools"; then _msg "ZRAM Remove" "ZRAM is not installed.\n\nNothing to remove." 10 50 return fi if ! _confirm "Remove ZRAM" \ "This will:\n - Stop the zramswap service\n - Purge zram-tools\n - Remove /etc/default/zramswap\n\nProceed?"; then echo "ZRAM removal cancelled." return fi echo "Stopping zramswap service..." sudo systemctl stop zramswap || true echo "Releasing ZRAM device..." sudo swapoff /dev/zram0 2>/dev/null || true sudo modprobe -r zram 2>/dev/null || true _run_cmd "ZRAM" "sudo apt purge -y zram-tools" "Purging zram-tools..." echo "Removing configuration..." sudo rm -f /etc/default/zramswap _msg "ZRAM Removed" "ZRAM has been removed successfully.\n\nThe zramswap service is stopped and disabled." 10 55 }