init iso2usb

This commit is contained in:
alexander 2024-04-17 12:07:09 +03:00
commit a2fb7aec1a
2 changed files with 100 additions and 0 deletions

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# iso2usb
Script for writing an ISO image to a USB device
```
sudo ./iso2usb.sh <device> <path-to-iso>
```
### Example
```
sudo ./iso2usb.sh /dev/sde ./image.iso
```

87
iso2usb.sh Executable file
View File

@ -0,0 +1,87 @@
#!/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 "${@}"