burn.sh
· 4.2 KiB · Bash
Surowy
#!/bin/bash
#==========================
# Set up the environment
#==========================
set -e # exit on error
set -o pipefail # exit on pipeline error
set -u # treat unset variable as error
#==========================
# Basic Information
#==========================
export LC_ALL=C
export LANG=en_US.UTF-8
export DEBIAN_FRONTEND=noninteractive
export SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
#==========================
# Color
#==========================
Green="\033[32m"
Red="\033[31m"
Yellow="\033[33m"
Blue="\033[36m"
Font="\033[0m"
GreenBG="\033[42;37m"
RedBG="\033[41;37m"
OK="${Green}[ OK ]${Font}"
ERROR="${Red}[FAILED]${Font}"
WARNING="${Yellow}[ WARN ]${Font}"
#==========================
# Print Colorful Text
#==========================
function print_ok() {
echo -e "${OK} ${Blue} $1 ${Font}"
}
function print_error() {
echo -e "${ERROR} ${Red} $1 ${Font}"
exit 1
}
function print_warn() {
echo -e "${WARNING} ${Yellow} $1 ${Font}"
}
#==========================
# Judge function
#==========================
function judge() {
if [[ 0 -eq $? ]]; then
print_ok "$1 succeeded"
sleep 0.2
else
print_error "$1 failed"
exit 1
fi
}
function burn()
{
# If the user want to burn /dev/sda, device should be '/dev/sda'
iso=$1
device=$2
if [ -z "$device" ]; then
print_error "Usage: burn.sh /path/to/iso /dev/device"
exit 1
fi
# Ensure $device exists
if [ ! -e "$device" ]; then
print_error "Device $device does not exist."
exit 1
fi
# Unmount all partitions on /$device
for mountpoint in $(lsblk -o MOUNTPOINT "$device" | tail -n +2); do
print_ok "Unmounting $mountpoint"
sudo umount "$mountpoint"
judge "Unmount $mountpoint"
done
# Ensure the iso exists
if [ ! -e "$iso" ]; then
print_error "ISO $iso does not exist."
exit 1
fi
# Ensure the iso is a file
if [ ! -f "$iso" ]; then
print_error "ISO $iso is not a file."
exit 1
fi
# Ensure the iso is readable
if [ ! -r "$iso" ]; then
print_error "ISO $iso is not readable."
exit 1
fi
# Ensure the disk is larger than the iso
iso_size=$(stat -c %s "$iso")
device_size=$(sudo blockdev --getsize64 "$device")
if [ "$iso_size" -gt "$device_size" ]; then
print_error "ISO $iso is larger than $device."
exit 1
else
print_ok "ISO $iso is $iso_size bytes, $device is $device_size bytes. Device is large enough."
fi
print_ok "Burning $iso to $device. This will take a while..."
sudo dd if="$iso" of="$device" bs=4M status=progress oflag=sync
sync && sync && sync
print_ok "Sleep 10 seconds to ensure the USB write buffer is flushed."
sleep 10
# Verify the burn
print_ok "Verifying the burn. This will take a while."
# Foreach the mount point under $device, unmount it
for mountpoint in $(lsblk -o MOUNTPOINT "$device" | tail -n +2); do
print_ok "Unmounting $mountpoint"
sudo umount "$mountpoint"
judge "Unmount $mountpoint"
done
# Mount the device to /tmp/burn/$device
print_ok "Mounting $device to /tmp/burn/$device"
mkdir -p /tmp/burn/$device
sudo mount "$device" /tmp/burn/$device
judge "Mount $device to /tmp/burn/$device"
# Calculate the md5sum of the iso
print_ok "Calculating the md5sum of the files in the iso"
(
cd /tmp/burn/$device && \
sudo md5sum -c md5sum.txt && \
print_ok "First pass verification succeeded." || \
print_error "Burn failed."
)
(
cd /tmp/burn/$device && \
sudo md5sum -c md5sum.txt && \
print_ok "Second pass verification succeeded." || \
print_error "Burn failed."
)
# Unmount the device
print_ok "Unmounting /tmp/burn/$device"
sleep 1
sudo umount /tmp/burn/$device
judge "Unmount /tmp/burn/$device"
# Clean up
print_ok "Cleaning up"
rm -rf /tmp/burn/$device
judge "Clean up"
# Ensure the USB stick safe to eject
print_ok "Ejecting $device"
sleep 5
udisksctl power-off -b "$device"
judge "Eject $device"
print_ok "Done. The usb drive is safe to remove."
}
clear
burn "$@"
| 1 | #!/bin/bash |
| 2 | |
| 3 | #========================== |
| 4 | # Set up the environment |
| 5 | #========================== |
| 6 | set -e # exit on error |
| 7 | set -o pipefail # exit on pipeline error |
| 8 | set -u # treat unset variable as error |
| 9 | |
| 10 | #========================== |
| 11 | # Basic Information |
| 12 | #========================== |
| 13 | export LC_ALL=C |
| 14 | export LANG=en_US.UTF-8 |
| 15 | export DEBIAN_FRONTEND=noninteractive |
| 16 | export SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" |
| 17 | |
| 18 | #========================== |
| 19 | # Color |
| 20 | #========================== |
| 21 | Green="\033[32m" |
| 22 | Red="\033[31m" |
| 23 | Yellow="\033[33m" |
| 24 | Blue="\033[36m" |
| 25 | Font="\033[0m" |
| 26 | GreenBG="\033[42;37m" |
| 27 | RedBG="\033[41;37m" |
| 28 | OK="${Green}[ OK ]${Font}" |
| 29 | ERROR="${Red}[FAILED]${Font}" |
| 30 | WARNING="${Yellow}[ WARN ]${Font}" |
| 31 | |
| 32 | #========================== |
| 33 | # Print Colorful Text |
| 34 | #========================== |
| 35 | function print_ok() { |
| 36 | echo -e "${OK} ${Blue} $1 ${Font}" |
| 37 | } |
| 38 | |
| 39 | function print_error() { |
| 40 | echo -e "${ERROR} ${Red} $1 ${Font}" |
| 41 | exit 1 |
| 42 | } |
| 43 | |
| 44 | function print_warn() { |
| 45 | echo -e "${WARNING} ${Yellow} $1 ${Font}" |
| 46 | } |
| 47 | |
| 48 | #========================== |
| 49 | # Judge function |
| 50 | #========================== |
| 51 | function judge() { |
| 52 | if [[ 0 -eq $? ]]; then |
| 53 | print_ok "$1 succeeded" |
| 54 | sleep 0.2 |
| 55 | else |
| 56 | print_error "$1 failed" |
| 57 | exit 1 |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | function burn() |
| 62 | { |
| 63 | # If the user want to burn /dev/sda, device should be '/dev/sda' |
| 64 | iso=$1 |
| 65 | device=$2 |
| 66 | |
| 67 | if [ -z "$device" ]; then |
| 68 | print_error "Usage: burn.sh /path/to/iso /dev/device" |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | # Ensure $device exists |
| 73 | if [ ! -e "$device" ]; then |
| 74 | print_error "Device $device does not exist." |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | # Unmount all partitions on /$device |
| 79 | for mountpoint in $(lsblk -o MOUNTPOINT "$device" | tail -n +2); do |
| 80 | print_ok "Unmounting $mountpoint" |
| 81 | sudo umount "$mountpoint" |
| 82 | judge "Unmount $mountpoint" |
| 83 | done |
| 84 | |
| 85 | # Ensure the iso exists |
| 86 | if [ ! -e "$iso" ]; then |
| 87 | print_error "ISO $iso does not exist." |
| 88 | exit 1 |
| 89 | fi |
| 90 | |
| 91 | # Ensure the iso is a file |
| 92 | if [ ! -f "$iso" ]; then |
| 93 | print_error "ISO $iso is not a file." |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | # Ensure the iso is readable |
| 98 | if [ ! -r "$iso" ]; then |
| 99 | print_error "ISO $iso is not readable." |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | # Ensure the disk is larger than the iso |
| 104 | iso_size=$(stat -c %s "$iso") |
| 105 | device_size=$(sudo blockdev --getsize64 "$device") |
| 106 | if [ "$iso_size" -gt "$device_size" ]; then |
| 107 | print_error "ISO $iso is larger than $device." |
| 108 | exit 1 |
| 109 | else |
| 110 | print_ok "ISO $iso is $iso_size bytes, $device is $device_size bytes. Device is large enough." |
| 111 | fi |
| 112 | |
| 113 | print_ok "Burning $iso to $device. This will take a while..." |
| 114 | sudo dd if="$iso" of="$device" bs=4M status=progress oflag=sync |
| 115 | sync && sync && sync |
| 116 | |
| 117 | print_ok "Sleep 10 seconds to ensure the USB write buffer is flushed." |
| 118 | sleep 10 |
| 119 | |
| 120 | # Verify the burn |
| 121 | print_ok "Verifying the burn. This will take a while." |
| 122 | |
| 123 | # Foreach the mount point under $device, unmount it |
| 124 | for mountpoint in $(lsblk -o MOUNTPOINT "$device" | tail -n +2); do |
| 125 | print_ok "Unmounting $mountpoint" |
| 126 | sudo umount "$mountpoint" |
| 127 | judge "Unmount $mountpoint" |
| 128 | done |
| 129 | |
| 130 | # Mount the device to /tmp/burn/$device |
| 131 | print_ok "Mounting $device to /tmp/burn/$device" |
| 132 | mkdir -p /tmp/burn/$device |
| 133 | sudo mount "$device" /tmp/burn/$device |
| 134 | judge "Mount $device to /tmp/burn/$device" |
| 135 | |
| 136 | # Calculate the md5sum of the iso |
| 137 | print_ok "Calculating the md5sum of the files in the iso" |
| 138 | ( |
| 139 | cd /tmp/burn/$device && \ |
| 140 | sudo md5sum -c md5sum.txt && \ |
| 141 | print_ok "First pass verification succeeded." || \ |
| 142 | print_error "Burn failed." |
| 143 | ) |
| 144 | ( |
| 145 | cd /tmp/burn/$device && \ |
| 146 | sudo md5sum -c md5sum.txt && \ |
| 147 | print_ok "Second pass verification succeeded." || \ |
| 148 | print_error "Burn failed." |
| 149 | ) |
| 150 | |
| 151 | # Unmount the device |
| 152 | print_ok "Unmounting /tmp/burn/$device" |
| 153 | sleep 1 |
| 154 | sudo umount /tmp/burn/$device |
| 155 | judge "Unmount /tmp/burn/$device" |
| 156 | |
| 157 | # Clean up |
| 158 | print_ok "Cleaning up" |
| 159 | rm -rf /tmp/burn/$device |
| 160 | judge "Clean up" |
| 161 | |
| 162 | # Ensure the USB stick safe to eject |
| 163 | print_ok "Ejecting $device" |
| 164 | sleep 5 |
| 165 | udisksctl power-off -b "$device" |
| 166 | judge "Eject $device" |
| 167 | |
| 168 | print_ok "Done. The usb drive is safe to remove." |
| 169 | } |
| 170 | |
| 171 | clear |
| 172 | burn "$@" |