iso2usb/iso2usb.sh

88 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# title :iso2usb.sh
# description :Script for writing an ISO image to a USB device
# author :Alexander Zhirov
# date :20240122
# version :0.1.0
# usage :bash iso2usb.sh
#===============================================================================
set -e
# set -x
PARTED=$(which parted)
if [ ! -x ${PARTED} ]
then
echo "The parted utility is missing" && exit 1
fi
TMP_USB_PATH="/tmp/usb_$(date +"%Y%m%d-%H%M%S")"
TMP_ISO_PATH="/tmp/iso_$(date +"%Y%m%d-%H%M%S")"
format_usb_device() {
local USB_DEVICE="${1}"
parted -s ${USB_DEVICE} mklabel msdos
parted -s ${USB_DEVICE} mkpart primary fat32 0% 100%
parted -s ${USB_DEVICE} set 1 boot on
mkfs -Vt fat ${USB_DEVICE}1
}
mount_usb_device() {
local USB_DEVICE="${1}1"
mkdir -v ${TMP_USB_PATH}
mount -v ${USB_DEVICE} ${TMP_USB_PATH}
}
umount_usb_device() {
local USB_DEVICE="${1}1"
umount -v ${USB_DEVICE}
rm -rv ${TMP_USB_PATH}
}
mount_iso_image() {
local ISO_IMAGE="${1}"
mkdir -v ${TMP_ISO_PATH}
mount -o loop,ro ${ISO_IMAGE} ${TMP_ISO_PATH}
}
umount_iso_image() {
umount -v ${TMP_ISO_PATH}
rm -rv ${TMP_ISO_PATH}
}
copy_iso_to_usb() {
cp -rpv ${TMP_ISO_PATH}/* ${TMP_USB_PATH}
}
main () {
local USB_DEVICE="${1}"
local ISO_IMAGE="${2}"
[ -z "${USB_DEVICE}" ] && echo "USB device is not specified" && exit 1
[ -z "${ISO_IMAGE}" ] && echo "ISO image is not specified" && exit 1
local PARTEDS=$(ls ${USB_DEVICE}* | grep -o "${USB_DEVICE}[0-9]")
for PARTED in ${PARTEDS[@]}
do
if [ ! -z "$(mount | grep "${PARTED}" )" ]
then
umount "${PARTED}"
fi
done
format_usb_device ${USB_DEVICE}
mount_usb_device ${USB_DEVICE}
mount_iso_image ${ISO_IMAGE}
copy_iso_to_usb
umount_iso_image
umount_usb_device ${USB_DEVICE}
}
main "${@}"