#!/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 .# # # @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