archboot/usr/bin/archboot-restore-usbstick.sh

51 lines
1.4 KiB
Bash
Raw Normal View History

2021-09-23 12:19:44 +02:00
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0-only
2021-09-23 12:19:44 +02:00
#
# 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=${0##*/}
_usage()
2021-09-23 12:19:44 +02:00
{
2023-02-07 20:19:31 +01:00
echo -e "\e[1mWelcome to \e[36mARCHBOOT\e[m \e[1m- RESTORE USB STICK:\e[m"
echo -e "\e[1m----------------------------------------\e[m"
echo -e "This script restores an USB device to a \e[1mFAT32\e[m device."
echo -e "\e[91mWARNING: ALL DATA WILL BE LOST ON THE DEVICE! \e[m"
2022-04-05 21:01:07 +02:00
echo ""
2023-02-07 20:19:31 +01:00
echo -e "usage: \e[1m${_APPNAME} <device>\e[m"
2022-04-05 21:23:06 +02:00
exit "1"
2021-09-23 12:19:44 +02: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
2023-02-07 20:19:31 +01:00
echo -e "\e[91mWARNING: 10 seconds for hitting CTRL+C to stop the process on ${1} now! \e[m"
2022-04-05 21:01:07 +02:00
sleep 10
2021-09-23 12:19:44 +02:00
# clean partitiontable
2023-02-07 20:19:31 +01:00
echo -e "\e[1mRestoring USB STICK...\e[m"
echo -e "\e[1mSTEP 1/3:\e[m Cleaning partition table..."
2022-04-05 21:01:07 +02:00
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>
2023-02-07 20:19:31 +01:00
echo -e "\e[1mSTEP 2/3:\e[m Create new MBR and partitiontable..."
2022-04-05 21:01:07 +02:00
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>
2023-02-07 20:19:31 +01:00
echo -e "\e[1mSTEP 3/3:\e[m Create FAT32 filesystem..."
2022-04-05 21:01:07 +02:00
mkfs.vfat -F32 "${1}"1
2023-02-07 20:19:31 +01:00
echo -e "\e[1mFinished.\e[m"