Last active 1725451905

Revision 174fe76ff50622e51ab10e8c87272c92c729f5c4

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}
42
43function print_warn() {
44 echo -e "${WARNING} ${Yellow} $1 ${Font}"
45}
46
47#==========================
48# Judge function
49#==========================
50function judge() {
51 if [[ 0 -eq $? ]]; then
52 print_ok "$1 succeeded"
53 sleep 0.2
54 else
55 print_error "$1 failed"
56 exit 1
57 fi
58}
59
60function burn()
61{
62 # If the user want to burn /dev/sda, device should be 'sda'
63 device=$1
64 iso=$2
65
66 if [ -z "$device" ]; then
67 print_error "Usage: $0 <device> <iso>"
68 exit 1
69 fi
70
71 # Ensure /dev/$device exists
72 if [ ! -e "/dev/$device" ]; then
73 print_error "Device /dev/$device does not exist."
74 exit 1
75 fi
76
77 # Unmount all partitions on /dev/$device
78 for mountpoint in $(lsblk -o MOUNTPOINT "/dev/$device" | tail -n +2); do
79 print_ok "Unmounting $mountpoint"
80 sudo umount "$mountpoint"
81 judge "Unmount $mountpoint"
82 done
83
84 # Ensure the iso exists
85 if [ ! -e "$iso" ]; then
86 print_error "ISO $iso does not exist."
87 exit 1
88 fi
89
90 # Ensure the iso is a file
91 if [ ! -f "$iso" ]; then
92 print_error "ISO $iso is not a file."
93 exit 1
94 fi
95
96 # Ensure the iso is readable
97 if [ ! -r "$iso" ]; then
98 print_error "ISO $iso is not readable."
99 exit 1
100 fi
101
102 # Ensure the disk is larger than the iso
103 iso_size=$(stat -c %s "$iso")
104 device_size=$(sudo blockdev --getsize64 "/dev/$device")
105 if [ "$iso_size" -gt "$device_size" ]; then
106 print_error "ISO $iso is larger than /dev/$device."
107 exit 1
108 else
109 print_ok "ISO $iso is $iso_size bytes, /dev/$device is $device_size bytes. Device is large enough."
110 fi
111
112 print_ok "Burning $iso to /dev/$device. This will take a while..."
113 sudo dd if="$iso" of="/dev/$device" bs=4M status=progress oflag=sync
114 sync && sync && sync
115
116 # Verify the burn
117 print_ok "Verifying the burn. This will take a while."
118
119 # Foreach the mount point under $device, unmount it
120 for mountpoint in $(lsblk -o MOUNTPOINT "/dev/$device" | tail -n +2); do
121 print_ok "Unmounting $mountpoint"
122 sudo umount "$mountpoint"
123 judge "Unmount $mountpoint"
124 done
125
126 # Mount the device to /tmp/burn/$device
127 print_ok "Mounting /dev/$device to /tmp/burn/$device"
128 mkdir -p /tmp/burn/$device
129 sudo mount "/dev/$device" /tmp/burn/$device
130 judge "Mount /dev/$device to /tmp/burn/$device"
131
132 # Calculate the md5sum of the iso
133 print_ok "Calculating the md5sum of the files in the iso"
134 (
135 cd /tmp/burn/$device && \
136 sudo md5sum -c md5sum.txt && \
137 print_ok "First pass verification succeeded." || \
138 print_error "Burn failed."
139 )
140 (
141 cd /tmp/burn/$device && \
142 sudo md5sum -c md5sum.txt && \
143 print_ok "Second pass verification succeeded." || \
144 print_error "Burn failed."
145 )
146
147 # Unmount the device
148 print_ok "Unmounting /tmp/burn/$device"
149 sudo umount /tmp/burn/$device
150 judge "Unmount /tmp/burn/$device"
151
152 # Clean up
153 print_ok "Cleaning up"
154 rm -rf /tmp/burn/$device
155 judge "Clean up"
156
157 print_ok "Done. You can now remove the USB stick."
158}
159
160burn "$@"