Created
June 22, 2025 09:48
-
-
Save adde88/0a70a71d8032a5d202d9eed5127f3868 to your computer and use it in GitHub Desktop.
Auto-setup USB drive on RPi3B+ with ext4 and 2GB swap
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
| #!/bin/bash | |
| # Exit immediately if a command exits with a non-zero status. | |
| set -e | |
| # --- Configuration --- | |
| SWAP_SIZE="2G" | |
| MOUNT_POINT="/mnt/usb" | |
| SWAP_PRIORITY="10" # Lower priority than ZRAM's 32767 | |
| # --- Safety Checks --- | |
| # 1. Check for root privileges | |
| if [ "$(id -u)" != "0" ]; then | |
| echo "This script must be run as root. Please use sudo." >&2 | |
| exit 1 | |
| fi | |
| # 2. Check if a device path was provided | |
| if [ -z "$1" ]; then | |
| echo "Usage: sudo $0 /dev/sdX" >&2 | |
| echo "Please specify the target device (e.g., /dev/sda)." >&2 | |
| exit 1 | |
| fi | |
| TARGET_DEVICE=$1 | |
| # 3. Check if the device exists | |
| if [ ! -b "$TARGET_DEVICE" ]; then | |
| echo "Error: Device $TARGET_DEVICE does not exist." >&2 | |
| exit 1 | |
| fi | |
| # 4. THE BIG WARNING | |
| echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" | |
| echo "This script will completely erase all data on the disk: $TARGET_DEVICE" | |
| echo "It will create two new partitions:" | |
| echo " 1. A ${SWAP_SIZE} swap partition" | |
| echo " 2. An ext4 data partition using the rest of the disk" | |
| echo "" | |
| echo "Please double-check that '$TARGET_DEVICE' is your USB disk and NOT your" | |
| echo "Kali Linux microSD card (e.g., /dev/mmcblk0)." | |
| echo "" | |
| read -p "Are you absolutely sure you want to continue? (yes/no): " CONFIRMATION | |
| if [ "$CONFIRMATION" != "yes" ]; then | |
| echo "Aborted." | |
| exit 0 | |
| fi | |
| # --- Partitioning and Formatting --- | |
| echo "--> Unmounting all partitions on $TARGET_DEVICE to prevent errors..." | |
| umount ${TARGET_DEVICE}* &>/dev/null || true | |
| echo "--> Wiping disk and creating new GPT partition table on $TARGET_DEVICE..." | |
| sgdisk --zap-all $TARGET_DEVICE | |
| echo "--> Creating ${SWAP_SIZE} swap partition..." | |
| # Partition 1: Swap | |
| sgdisk --new=1:0:+${SWAP_SIZE} --typecode=1:8200 --change-name=1:"SWAP" $TARGET_DEVICE | |
| echo "--> Creating ext4 data partition with remaining space..." | |
| # Partition 2: Data (ext4) | |
| sgdisk --new=2:0:0 --typecode=2:8300 --change-name=2:"DATA" $TARGET_DEVICE | |
| echo "--> Informing kernel of partition changes..." | |
| partprobe $TARGET_DEVICE | |
| sleep 2 # Give the system a moment to create the device nodes | |
| # Define partition names for clarity | |
| SWAP_PARTITION="${TARGET_DEVICE}1" | |
| DATA_PARTITION="${TARGET_DEVICE}2" | |
| echo "--> Formatting swap partition: $SWAP_PARTITION..." | |
| mkswap $SWAP_PARTITION | |
| echo "--> Formatting data partition as ext4: $DATA_PARTITION..." | |
| mkfs.ext4 -L USB_DATA $DATA_PARTITION | |
| # --- Persistent Mounting (fstab) --- | |
| echo "--> Getting new partition UUIDs..." | |
| SWAP_UUID=$(blkid -s UUID -o value $SWAP_PARTITION) | |
| DATA_UUID=$(blkid -s UUID -o value $DATA_PARTITION) | |
| echo "--> Creating mount point $MOUNT_POINT if it doesn't exist..." | |
| mkdir -p $MOUNT_POINT | |
| echo "--> Backing up /etc/fstab to /etc/fstab.bak..." | |
| cp /etc/fstab /etc/fstab.bak | |
| echo "--> Adding new entries to /etc/fstab..." | |
| # Add data partition mount if not already present | |
| if ! grep -q "UUID=$DATA_UUID" /etc/fstab; then | |
| echo "UUID=$DATA_UUID $MOUNT_POINT ext4 defaults,auto,nofail 0 2" >> /etc/fstab | |
| echo "Added ext4 mount entry." | |
| else | |
| echo "ext4 mount entry already exists in fstab." | |
| fi | |
| # Add swap partition mount if not already present | |
| if ! grep -q "UUID=$SWAP_UUID" /etc/fstab; then | |
| echo "UUID=$SWAP_UUID none swap sw,nofail,pri=$SWAP_PRIORITY 0 0" >> /etc/fstab | |
| echo "Added swap entry with priority $SWAP_PRIORITY." | |
| else | |
| echo "swap entry already exists in fstab." | |
| fi | |
| # --- Final Activation --- | |
| echo "--> Mounting new data partition..." | |
| mount -a | |
| echo "--> Activating new swap partition..." | |
| swapon -a | |
| echo "" | |
| echo "---=== SETUP COMPLETE ===---" | |
| echo "Your USB disk is now configured and mounted." | |
| echo "" | |
| echo "To verify, check the following:" | |
| echo "Disk partitions and mounts: lsblk" | |
| echo "Active swap devices and priorities: swapon --show" | |
| echo "" | |
| echo "You should see /dev/zram0 with a high priority and $SWAP_PARTITION with priority $SWAP_PRIORITY." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment