2021-09-23 12:19:44 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# archboot-restore-usbstick.sh - restore usbstick to FAT32
|
|
|
|
# by Tobias Powalowski <tpowa@archlinux.org>
|
|
|
|
# usage(exitvalue)
|
|
|
|
# outputs a usage message and exits with value
|
|
|
|
APPNAME=$(basename "${0}")
|
|
|
|
usage()
|
|
|
|
{
|
2022-04-05 21:01:07 +02:00
|
|
|
echo -e "\033[1mWelcome to \033[34marchboot's\033[0m \033[1mRESTORE USB STICK:\033[0m"
|
|
|
|
echo -e "\033[1m----------------------------------------\033[0m"
|
|
|
|
echo -e "This script restores an USB device to a \033[1mFAT32\033[0m device."
|
2022-04-05 21:20:20 +02:00
|
|
|
echo -e "\033[91mWARNING: ALL DATA WILL BE LOST ON THE DEVICE! \033[0m"
|
2022-04-05 21:01:07 +02:00
|
|
|
echo ""
|
|
|
|
echo -e "usage: \033[1m${APPNAME} <device>\033[0m"
|
2022-04-05 21:23:06 +02:00
|
|
|
exit "1"
|
2021-09-23 12:19:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
|
2022-01-21 11:28:12 +01:00
|
|
|
[[ -z "${1}" ]] && usage "$@"
|
2021-09-23 12:19:44 +02:00
|
|
|
|
|
|
|
### check for root
|
|
|
|
if ! [[ ${UID} -eq 0 ]]; then
|
2022-04-05 21:01:07 +02:00
|
|
|
echo "ERROR: Please run as root user!"
|
|
|
|
exit 1
|
2021-09-23 12:19:44 +02:00
|
|
|
fi
|
2022-04-05 21:20:20 +02:00
|
|
|
echo -e "\033[91mWARNING: 10 seconds for hitting CTRL+C to stop the process on ${1} now! \033[0m"
|
2022-04-05 21:01:07 +02:00
|
|
|
sleep 10
|
2021-09-23 12:19:44 +02:00
|
|
|
# clean partitiontable
|
2022-04-05 21:01:07 +02:00
|
|
|
echo -e "\033[1mRestoring USB STICK...\033[0m"
|
|
|
|
echo -e "\033[1mSTEP 1/3:\033[0m Cleaning partition table..."
|
|
|
|
dd if=/dev/zero of="${1}" bs=512 count=2048
|
|
|
|
wipefs -a "${1}"
|
2021-09-23 12:19:44 +02:00
|
|
|
# create new MBR and partition on <DEVICE>
|
2022-04-05 21:01:07 +02:00
|
|
|
echo -e "\033[1mSTEP 2/3:\033[0m Create new MBR and partitiontable..."
|
|
|
|
fdisk "${1}" <<EOF
|
2021-09-23 12:19:44 +02:00
|
|
|
n
|
|
|
|
p
|
|
|
|
1
|
|
|
|
|
|
|
|
|
|
|
|
t
|
|
|
|
b
|
|
|
|
w
|
|
|
|
EOF
|
|
|
|
# wait for partitiontable to be resynced
|
|
|
|
sleep 5
|
|
|
|
# create FAT32 filesystem on <device-partition>
|
2022-04-05 21:01:07 +02:00
|
|
|
echo -e "\033[1mSTEP 3/3:\033[0m Create FAT32 filesystem..."
|
|
|
|
mkfs.vfat -F32 "${1}"1
|
|
|
|
echo -e "\033[1mFinished.\033[0m"
|