[root tip] [Utility script] Repartition and format your USB stick

Difficulty: ★★☆☆☆

Description

The scripts partitions and formats any USB device using exFAT filesystem. To avoid accidental format of non removable devices a couple of safeguards exist.

The script will check if the supplied device is valid then it checks if the device is in fact a removable device. If any of these checks fail the script aborts.

The script will ask for user confirmation twice and default to abort on Enter

Use case

Re-purpose the USB stick if you have flashed an ISO to it.

Why? Because writing an ISO using dd writes an ISO9660 filesystem to a small part of the USB - the rest is inaccessible.

Instructions

Create the folder ~/.local/bin

mkdir -p ~/.local/bin

Create a new file e.g. format-usb.sh

touch ~/.local/bin/format-usb.sh

Make the file executable

chmod +x ~/.local/bin/format-usb.sh

Open the file with your favorite editor e.g. kate

kate ~/.local/bin/format-usb.sh

Copy below content into the file and save it.

TIP: Use the copy to clipboard box in the upper right corner of below text box

#!/usr/bin/env bash
#
#    Utility ccript to repurpose a removable device using exfat filesystem
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.#
#
# @linux-aarhus - root.nix.dk
#

# format removable USB device to exfat with name 'USB STICK'
LABEL='USB STICK'

SCRIPTNAME=$(basename "$0")
VERSION="0.2"

# don't run as root
if [[ $(whoami) == "root" ]] ; then
    echo "Please run as user. Script will ask for root later."
    exit 1
fi

# ensure a device is given
if [[ -z "$1" ]]; then
    echo "No device specified ..."
    echo "Usage: ${SCRIPTNAME} /dev/sdy"
    exit
fi

# get the last part of device path
device="$(echo $1 | cut -d'/' -f3)"
echo "Checking device on => $device"

# create list of available devices
devices="$(lsblk -o NAME | egrep '^sd')"

# check if device is valid - abort if not
if ! [[ $devices =~ (^|[[:space:]])$device($|[[:space:]]) ]]; then
    echo "$1 not found"
    echo "Aborting"
    exit 1
fi

# check if device is removable - abort if not
[[ $(echo $(lsblk -no RM "$1" | head -n 1)) = '0' ]] && \
    echo "Non removable device detected!" && \
    echo "Aborting" && \
    exit 1

# Ask for confirmation
read -r -p "Confirm reformat of '$1' [y/N] " resp
if [[ "$resp" =~ ^([yY][eE][sS]|[yY])$ ]]; then

    # Repeat confirmation question
    read -r -p "Irrevocably format  '$1' [y/N] " resp2
    if [[ "$resp2" =~ ^([yY][eE][sS]|[yY])$ ]]; then

        # will do
        echo "Formatting $1 ..."

        # use gdisk to create new partition table
        # and a single Microsoft basic data partition type
        printf 'o\ny\nn\n\n\n\n0700\nw\ny\n' | sudo gdisk "$1" > /dev/null
        sudo mkexfatfs -n "${LABEL}"  "$1"1 > /dev/null

        # done
        echo "Done"
        exit
    fi
    echo "Formatting aborted"
else
    echo "Formatting aborted"
fi

Usage

Remove your USB if attached and list devices

lsblk

Insert your USB device and list the devices

lsblk

The new device in the list is your USB device e.g. /dev/sdy - format the device using the script

format-usb.sh /dev/sdy

Note

What this script does can probably be done in numerous ways with numerous tools.

Cross posted at root.nix.dk
The list contains validation is taken from this stackoverflow topic

Revisions

2021-01-11T05:44:00Z

  • Added label to format
  • Cosmetic fix → send output to /dev/null

2020-09-02T15:54:00Z

  • Fix typo in device check
12 Likes

Thanks for this work.

Is this not achievable via parted?

:man_shrugging:

I don’t know - I am using gpart gdisk because I can use printf to pipe the commands.

not in the script provided… :wink:

That’s what I meant to say :man_facepalming:

Thx ! Is there a way to have the created partition named differently by the script (NOT Microsoft basic data) ?

1 Like

Microsoft Basic data is not a label - but a partition type.

Just change the formatting command in the script to include a label - like

sudo mkexfatfs -n USBSTICK "$1"1

great, thx !

I added your suggestion to the original script.

If you want another label - change the LABEL variable at the top of the script.

cool ! Already using new your script, very handy !