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

48 lines
1.4 KiB
Bash
Raw Normal View History

2021-09-23 12:19:44 +02:00
#!/usr/bin/env bash
2023-08-11 17:19:18 +02:00
# SPDX-License-Identifier: GPL-3.0-or-later
2024-08-01 12:39:49 +02:00
# archboot-restore-usbstick.sh - restore usbstick to FAT32
# by Tobias Powalowski <tpowa@archlinux.org>
2024-07-30 21:33:26 +02:00
. /usr/lib/archboot/common.sh
_usage()
2021-09-23 12:19:44 +02:00
{
echo -e "\e[1m\e[36mARCHBOOT\e[m \e[1m- Restore USB Stick\e[m"
echo -e "\e[1m----------------------------\e[m"
2023-02-07 20:19:31 +01:00
echo -e "This script restores an USB device to a \e[1mFAT32\e[m device."
echo -e "\e[1m\e[91mWARNING: ALL DATA WILL BE LOST ON THE DEVICE! \e[m"
2022-04-05 21:01:07 +02:00
echo ""
echo -e "Usage: \e[1m${_BASENAME} <device>\e[m"
2023-04-24 07:02:39 +02:00
exit 0
2021-09-23 12:19:44 +02:00
}
##################################################
2024-08-13 23:14:54 +02:00
[[ -z "${1}" ]] && _usage
_root_check
2024-08-13 23:25:12 +02:00
if [[ -b "${1}" ]]; then
echo -e "\e[91mWARNING: 10 seconds for hitting CTRL+C to stop the process on ${1} now! \e[m"
sleep 10
# clean partitiontable
echo -e "\e[1mRestoring USB STICK...\e[m"
echo -e "\e[1mSTEP 1/3:\e[m Cleaning partition table..."
dd if=/dev/zero of="${1}" bs=512 count=2048
wipefs -a "${1}"
# create new MBR and partition on <DEVICE>
echo -e "\e[1mSTEP 2/3:\e[m Create new MBR and partitiontable..."
fdisk "${1}" <<EOF
2021-09-23 12:19:44 +02:00
n
p
1
t
b
w
EOF
2024-08-13 23:25:12 +02:00
# wait for partitiontable to be resynced
sleep 5
# create FAT32 filesystem on <device-partition>
echo -e "\e[1mSTEP 3/3:\e[m Create FAT32 filesystem..."
mkfs.vfat -F32 "${1}"1
echo -e "\e[1mFinished.\e[m"
else
echo -e "\e[1m\e[91mError: ${1} not a valid blockdevice! \e[m"
fi