diff --git a/misc/core.func b/misc/core.func index e051b38..3db0d3b 100644 --- a/misc/core.func +++ b/misc/core.func @@ -503,3 +503,41 @@ run_container_safe() { $cmd " || __handle_general_error "lxc-attach to CT $ct" } + +check_or_create_swap() { + msg_info "Checking for active swap" + + if swapon --noheadings --show | grep -q 'swap'; then + msg_ok "Swap is active" + return 0 + fi + + msg_error "No active swap detected" + + read -p "Do you want to create a swap file? [y/N]: " create_swap + create_swap="${create_swap,,}" # to lowercase + + if [[ "$create_swap" != "y" && "$create_swap" != "yes" ]]; then + msg_info "Skipping swap file creation" + return 1 + fi + + read -p "Enter swap size in MB (e.g., 2048 for 2GB): " swap_size_mb + if ! [[ "$swap_size_mb" =~ ^[0-9]+$ ]]; then + msg_error "Invalid size input. Aborting." + return 1 + fi + + local swap_file="/swapfile" + + msg_info "Creating ${swap_size_mb}MB swap file at $swap_file" + if dd if=/dev/zero of="$swap_file" bs=1M count="$swap_size_mb" status=progress && + chmod 600 "$swap_file" && + mkswap "$swap_file" && + swapon "$swap_file"; then + msg_ok "Swap file created and activated successfully" + else + msg_error "Failed to create or activate swap" + return 1 + fi +} diff --git a/misc/vm-core.func b/misc/vm-core.func index 0a7b935..520caf0 100644 --- a/misc/vm-core.func +++ b/misc/vm-core.func @@ -27,7 +27,6 @@ load_functions() { pve_check arch_check ssh_check - check_hostname_conflict set_std_mode # add more } @@ -443,8 +442,14 @@ exit_script() { } check_hostname_conflict() { - if qm list | awk '{print $2}' | grep -qx "$HN"; then - msg_error "Hostname $HN already in use by another VM." + local hostname="${HN:-}" + if [[ -z "$hostname" ]]; then + msg_error "HN is not set – cannot check hostname conflict." + exit 1 + fi + + if qm list | awk '{print $2}' | grep -qx "$hostname"; then + msg_error "Hostname $hostname already in use by another VM." exit 1 fi } diff --git a/vm/umbrel-os-vm.sh b/vm/umbrel-os-vm.sh index da4833f..ac2b188 100644 --- a/vm/umbrel-os-vm.sh +++ b/vm/umbrel-os-vm.sh @@ -42,6 +42,8 @@ else header_info && echo -e "${CROSS}${RD}User exited script${CL}\n" && exit fi +check_hostname_conflict "$HN" + function default_settings() { VMID=$(get_valid_nextid) FORMAT=",efitype=4m"