Last active
December 4, 2025 00:55
-
-
Save DJStompZone/42af2c011000b8545dc15b455c5284bb to your computer and use it in GitHub Desktop.
LUKS Partition Utility
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # LUKS Partition Utility | |
| # DJ Stomp 2025 | |
| # MIT License | |
| ############################################################################## | |
| # MIT LICENSE # | |
| # Copyright 2025 DJ Stomp # | |
| # Permission is hereby granted, free of charge, to any person obtaining a # | |
| # copy of this software and associated documentation files (the "Software"), # | |
| # to deal in the Software without restriction, including without limitation # | |
| # the rights to use, copy, modify, merge, publish, distribute, sublicense, # | |
| # and/or sell copies of the Software, and to permit persons to whom the # | |
| # Software is furnished to do so, subject to the following conditions: The # | |
| # above copyright notice and this permission notice shall be included in all # | |
| # copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED # | |
| # "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT # | |
| # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR # | |
| # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # | |
| # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # | |
| # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # | |
| # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # | |
| ############################################################################## | |
| set -euo pipefail | |
| # ASCII Art Banner | |
| clear | |
| python3 -c "from zlib import decompress as dc; from base64 import b64decode as d6; $(echo "cHJpbnQoZGMoZDYoYnIiZUp5VGpqYTJzRGExTnJRMHkzMDByV05ZSWk3cFVUOFNSbEFEakF3c2dBWk1waHVpMk4yNGZDNGRiWkNyQUFFWUtsRDlpcVJTQVZzb0lPUXhaUEZxeGFNUExrdFFCdG5SYU1aaENUUDhnVUdNSlBiNHg2dUVDUDNVTVF1YlxKXERSVWNBVStOc1hraEJHSkFZdkRPZVNITWNIa1JNT3d4V0ludnN4RldwZ1I3dzRTTWhGTXdOQUVLREFKSzBMVmlhYVFXcEs0blUrSkdIRmxKL0hsSXZiQ0RtOFlZcVpGVEdWNHd3bS9HYmkxVWhoaWVNTU5NOFNJTVFWZklodHNpTFJRUVFzUC9JbVE1R1xDQVw2VE1DR1Q2UnBvaFNKK0xOZGx6WWN4RitHeW4yUERpVkgxcnA0Z1ZtQkpmazV4YUFXVVlHUnFaZ0JoVnNJTTRMeU43R0RBdDhlWmpDbUFkN1U4a3ZIMHdIWmFabmxCUkRtS25GcVVWbHFTbEsxQWtIM0tHQjRWa3U5SklJbzFpalc1S25EOExwUTBnQVxjQTNcL0VNQVJEbHdBZlJmQ3ZBPT0iKSkuZGVjb2RlKCkp" | base64 -d)" | |
| sleep 2 | |
| # Colors & logging | |
| RESET="\033[0m"; BOLD="\033[1m" | |
| AQUA="\033[38;5;51m"; ORANGE="\033[38;5;208m"; GREEN="\033[38;5;46m" | |
| RED="\033[38;5;196m"; WHITE="\033[38;5;15m"; GRAY="\033[38;5;245m" | |
| BG_RED="\033[48;5;196m" | |
| log_info() { echo -e "${BOLD}${AQUA}[INFO]${RESET} ${WHITE}$*${RESET}"; } | |
| log_warn() { echo -e "${BOLD}${ORANGE}[WARN]${RESET} ${WHITE}$*${RESET}"; } | |
| log_error() { echo -e "${BOLD}${BG_RED}${WHITE}[ERROR]${RESET} ${RED}$*${RESET}" >&2; } | |
| log_success() { echo -e "${BOLD}${GREEN}[OK]${RESET} ${WHITE}$*${RESET}"; } | |
| log_debug() { echo -e "${BOLD}${GRAY}[DEBUG]${RESET} ${WHITE}$*${RESET}"; } | |
| MAPPER_NAME_DEFAULT="xmrwallet" | |
| MOUNT_POINT_DEFAULT="/mnt/xmrwallet" | |
| print_usage() { | |
| cat <<EOF | |
| Usage: | |
| $0 init /dev/sdXn [mapper_name] [mount_point] | |
| $0 open /dev/sdXn [mapper_name] [mount_point] | |
| $0 close [mapper_name] [mount_point] | |
| $0 status | |
| Commands: | |
| init Initialize a LUKS-encrypted filesystem on the given partition. | |
| WARNING: This will ERASE ALL DATA on that partition. | |
| open Open an existing LUKS volume and mount it. | |
| close Unmount and close the LUKS mapping. | |
| status Show block devices and active LUKS mappings. | |
| Examples: | |
| $0 init /dev/sdb1 | |
| $0 open /dev/sdb1 | |
| $0 close | |
| $0 status | |
| EOF | |
| } | |
| require_root() { | |
| if [[ "$EUID" -ne 0 ]]; then | |
| log_error "This script needs root (sudo). Try: sudo $0 $*" | |
| exit 1 | |
| fi | |
| } | |
| confirm_destruction() { | |
| local dev="$1" | |
| log_warn "You are about to initialize LUKS on ${dev}." | |
| log_warn "THIS WILL DESTROY ALL DATA on ${dev}." | |
| read -r -p "Type 'YES' (all caps) to continue: " answer | |
| if [[ "${answer}" != "YES" ]]; then | |
| log_info "Aborted by user." | |
| exit 1 | |
| fi | |
| } | |
| do_init() { | |
| require_root | |
| local dev="${1:-}" | |
| local mapper_name="${2:-$MAPPER_NAME_DEFAULT}" | |
| local mount_point="${3:-$MOUNT_POINT_DEFAULT}" | |
| if [[ -z "${dev}" ]]; then | |
| log_error "Device path required, e.g. /dev/sdb1" | |
| exit 1 | |
| fi | |
| if [[ ! -b "${dev}" ]]; then | |
| log_error "${dev} is not a block device." | |
| exit 1 | |
| fi | |
| confirm_destruction "${dev}" | |
| log_info "Setting up LUKS on ${dev}..." | |
| cryptsetup luksFormat "${dev}" | |
| log_success "LUKS header written to ${dev}." | |
| log_info "Opening LUKS volume as ${mapper_name}..." | |
| cryptsetup open "${dev}" "${mapper_name}" | |
| log_success "Opened LUKS mapper /dev/mapper/${mapper_name}." | |
| local mapper_path="/dev/mapper/${mapper_name}" | |
| log_info "Creating ext4 filesystem on ${mapper_path}..." | |
| mkfs.ext4 -L "${mapper_name}" "${mapper_path}" | |
| log_success "Filesystem created on ${mapper_path}." | |
| log_debug "Ensuring mount point ${mount_point} exists..." | |
| mkdir -p "${mount_point}" | |
| log_info "Mounting ${mapper_path} to ${mount_point}..." | |
| mount "${mapper_path}" "${mount_point}" | |
| log_success "Mounted at ${mount_point}." | |
| log_info "You can now store your Monero wallet files there." | |
| } | |
| do_open() { | |
| require_root | |
| local dev="${1:-}" | |
| local mapper_name="${2:-$MAPPER_NAME_DEFAULT}" | |
| local mount_point="${3:-$MOUNT_POINT_DEFAULT}" | |
| if [[ -z "${dev}" ]]; then | |
| log_error "Device path required, e.g. /dev/sdb1" | |
| exit 1 | |
| fi | |
| if [[ ! -b "${dev}" ]]; then | |
| log_error "${dev} is not a block device." | |
| exit 1 | |
| fi | |
| local mapper_path="/dev/mapper/${mapper_name}" | |
| if [[ -e "${mapper_path}" ]]; then | |
| log_warn "Mapper ${mapper_path} already exists, skipping cryptsetup open." | |
| else | |
| log_info "Opening LUKS volume ${dev} as ${mapper_name}..." | |
| cryptsetup open "${dev}" "${mapper_name}" | |
| log_success "Opened LUKS mapper ${mapper_path}." | |
| fi | |
| log_debug "Ensuring mount point ${mount_point} exists..." | |
| mkdir -p "${mount_point}" | |
| if mountpoint -q "${mount_point}"; then | |
| log_warn "${mount_point} is already mounted." | |
| else | |
| log_info "Mounting ${mapper_path} to ${mount_point}..." | |
| mount "${mapper_path}" "${mount_point}" | |
| log_success "LUKS volume mounted at ${mount_point}." | |
| fi | |
| } | |
| do_close() { | |
| require_root | |
| local mapper_name="${1:-$MAPPER_NAME_DEFAULT}" | |
| local mount_point="${2:-$MOUNT_POINT_DEFAULT}" | |
| local mapper_path="/dev/mapper/${mapper_name}" | |
| if mountpoint -q "${mount_point}"; then | |
| log_info "Unmounting ${mount_point}..." | |
| umount "${mount_point}" | |
| log_success "Unmounted ${mount_point}." | |
| else | |
| log_warn "${mount_point} is not mounted." | |
| fi | |
| if [[ -e "${mapper_path}" ]]; then | |
| log_info "Closing LUKS mapper ${mapper_name}..." | |
| cryptsetup close "${mapper_name}" | |
| log_success "Closed LUKS mapper ${mapper_name}." | |
| else | |
| log_warn "Mapper ${mapper_path} does not exist." | |
| fi | |
| log_info "All done." | |
| } | |
| do_status() { | |
| log_info "Block devices:" | |
| lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT | |
| echo | |
| log_info "Active LUKS mappings:" | |
| # cryptsetup status without args prints usage; we want real mappings: | |
| if command -v dmsetup >/dev/null 2>&1; then | |
| dmsetup ls || true | |
| else | |
| log_warn "dmsetup not found; showing cryptsetup --help instead." | |
| cryptsetup --help | head -n 5 || true | |
| fi | |
| } | |
| main() { | |
| local cmd="${1:-}" | |
| case "${cmd}" in | |
| init) | |
| shift | |
| do_init "$@" | |
| ;; | |
| open) | |
| shift | |
| do_open "$@" | |
| ;; | |
| close) | |
| shift | |
| do_close "$@" | |
| ;; | |
| status) | |
| do_status | |
| ;; | |
| ""|-h|--help|help) | |
| print_usage | |
| ;; | |
| *) | |
| log_error "Unknown command: ${cmd}" | |
| print_usage | |
| exit 1 | |
| ;; | |
| esac | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment