Last active 1725451905

burn.sh Raw
1#!/bin/bash
2
3#==========================
4# Set up the environment
5#==========================
6set -e # exit on error
7set -o pipefail # exit on pipeline error
8set -u # treat unset variable as error
9
10#==========================
11# Basic Information
12#==========================
13export LC_ALL=C
14export LANG=en_US.UTF-8
15export DEBIAN_FRONTEND=noninteractive
16export SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
17
18#==========================
19# Color
20#==========================
21Green="\033[32m"
22Red="\033[31m"
23Yellow="\033[33m"
24Blue="\033[36m"
25Font="\033[0m"
26GreenBG="\033[42;37m"
27RedBG="\033[41;37m"
28OK="${Green}[ OK ]${Font}"
29ERROR="${Red}[FAILED]${Font}"
30WARNING="${Yellow}[ WARN ]${Font}"
31
32#==========================
33# Print Colorful Text
34#==========================
35function print_ok() {
36 echo -e "${OK} ${Blue} $1 ${Font}"
37}
38
39function print_error() {
40 echo -e "${ERROR} ${Red} $1 ${Font}"
41 exit 1
42}
43
44function print_warn() {
45 echo -e "${WARNING} ${Yellow} $1 ${Font}"
46}
47
48#==========================
49# Judge function
50#==========================
51function 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
61function 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
171clear
172burn "$@"