Last active 1725451905

anduin's Avatar anduin revised this gist 1725451905. Go to revision

1 file changed, 1 insertion

burn.sh

@@ -38,6 +38,7 @@ function print_ok() {
38 38
39 39 function print_error() {
40 40 echo -e "${ERROR} ${Red} $1 ${Font}"
41 + exit 1
41 42 }
42 43
43 44 function print_warn() {

anduin's Avatar anduin revised this gist 1725293828. Go to revision

1 file changed, 30 insertions, 19 deletions

burn.sh

@@ -59,23 +59,23 @@ function judge() {
59 59
60 60 function burn()
61 61 {
62 - # If the user want to burn /dev/sda, device should be 'sda'
63 - device=$1
64 - iso=$2
62 + # If the user want to burn /dev/sda, device should be '/dev/sda'
63 + iso=$1
64 + device=$2
65 65
66 66 if [ -z "$device" ]; then
67 - print_error "Usage: $0 <device> <iso>"
67 + print_error "Usage: burn.sh /path/to/iso /dev/device"
68 68 exit 1
69 69 fi
70 70
71 - # Ensure /dev/$device exists
72 - if [ ! -e "/dev/$device" ]; then
73 - print_error "Device /dev/$device does not exist."
71 + # Ensure $device exists
72 + if [ ! -e "$device" ]; then
73 + print_error "Device $device does not exist."
74 74 exit 1
75 75 fi
76 76
77 - # Unmount all partitions on /dev/$device
78 - for mountpoint in $(lsblk -o MOUNTPOINT "/dev/$device" | tail -n +2); do
77 + # Unmount all partitions on /$device
78 + for mountpoint in $(lsblk -o MOUNTPOINT "$device" | tail -n +2); do
79 79 print_ok "Unmounting $mountpoint"
80 80 sudo umount "$mountpoint"
81 81 judge "Unmount $mountpoint"
@@ -101,33 +101,36 @@ function burn()
101 101
102 102 # Ensure the disk is larger than the iso
103 103 iso_size=$(stat -c %s "$iso")
104 - device_size=$(sudo blockdev --getsize64 "/dev/$device")
104 + device_size=$(sudo blockdev --getsize64 "$device")
105 105 if [ "$iso_size" -gt "$device_size" ]; then
106 - print_error "ISO $iso is larger than /dev/$device."
106 + print_error "ISO $iso is larger than $device."
107 107 exit 1
108 108 else
109 - print_ok "ISO $iso is $iso_size bytes, /dev/$device is $device_size bytes. Device is large enough."
109 + print_ok "ISO $iso is $iso_size bytes, $device is $device_size bytes. Device is large enough."
110 110 fi
111 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
112 + print_ok "Burning $iso to $device. This will take a while..."
113 + sudo dd if="$iso" of="$device" bs=4M status=progress oflag=sync
114 114 sync && sync && sync
115 115
116 + print_ok "Sleep 10 seconds to ensure the USB write buffer is flushed."
117 + sleep 10
118 +
116 119 # Verify the burn
117 120 print_ok "Verifying the burn. This will take a while."
118 121
119 122 # Foreach the mount point under $device, unmount it
120 - for mountpoint in $(lsblk -o MOUNTPOINT "/dev/$device" | tail -n +2); do
123 + for mountpoint in $(lsblk -o MOUNTPOINT "$device" | tail -n +2); do
121 124 print_ok "Unmounting $mountpoint"
122 125 sudo umount "$mountpoint"
123 126 judge "Unmount $mountpoint"
124 127 done
125 128
126 129 # Mount the device to /tmp/burn/$device
127 - print_ok "Mounting /dev/$device to /tmp/burn/$device"
130 + print_ok "Mounting $device to /tmp/burn/$device"
128 131 mkdir -p /tmp/burn/$device
129 - sudo mount "/dev/$device" /tmp/burn/$device
130 - judge "Mount /dev/$device to /tmp/burn/$device"
132 + sudo mount "$device" /tmp/burn/$device
133 + judge "Mount $device to /tmp/burn/$device"
131 134
132 135 # Calculate the md5sum of the iso
133 136 print_ok "Calculating the md5sum of the files in the iso"
@@ -146,6 +149,7 @@ function burn()
146 149
147 150 # Unmount the device
148 151 print_ok "Unmounting /tmp/burn/$device"
152 + sleep 1
149 153 sudo umount /tmp/burn/$device
150 154 judge "Unmount /tmp/burn/$device"
151 155
@@ -154,7 +158,14 @@ function burn()
154 158 rm -rf /tmp/burn/$device
155 159 judge "Clean up"
156 160
157 - print_ok "Done. You can now remove the USB stick."
161 + # Ensure the USB stick safe to eject
162 + print_ok "Ejecting $device"
163 + sleep 5
164 + udisksctl power-off -b "$device"
165 + judge "Eject $device"
166 +
167 + print_ok "Done. The usb drive is safe to remove."
158 168 }
159 169
170 + clear
160 171 burn "$@"

anduin's Avatar anduin revised this gist 1725289544. Go to revision

1 file changed, 12 insertions, 2 deletions

burn.sh

@@ -99,9 +99,19 @@ function burn()
99 99 exit 1
100 100 fi
101 101
102 - print_ok "Burning $iso to /dev/$device. This will take a while."
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..."
103 113 sudo dd if="$iso" of="/dev/$device" bs=4M status=progress oflag=sync
104 - sync
114 + sync && sync && sync
105 115
106 116 # Verify the burn
107 117 print_ok "Verifying the burn. This will take a while."

anduin's Avatar anduin revised this gist 1725288559. Go to revision

1 file changed, 9 insertions, 2 deletions

burn.sh

@@ -120,10 +120,17 @@ function burn()
120 120 judge "Mount /dev/$device to /tmp/burn/$device"
121 121
122 122 # Calculate the md5sum of the iso
123 + print_ok "Calculating the md5sum of the files in the iso"
123 124 (
124 125 cd /tmp/burn/$device && \
125 - sudo md5sum -c md5sum.txt --quiet && \
126 - print_ok "Burn successful." || \
126 + sudo md5sum -c md5sum.txt && \
127 + print_ok "First pass verification succeeded." || \
128 + print_error "Burn failed."
129 + )
130 + (
131 + cd /tmp/burn/$device && \
132 + sudo md5sum -c md5sum.txt && \
133 + print_ok "Second pass verification succeeded." || \
127 134 print_error "Burn failed."
128 135 )
129 136

anduin's Avatar anduin revised this gist 1725288310. Go to revision

1 file changed, 143 insertions

burn.sh(file created)

@@ -0,0 +1,143 @@
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 + }
42 +
43 + function print_warn() {
44 + echo -e "${WARNING} ${Yellow} $1 ${Font}"
45 + }
46 +
47 + #==========================
48 + # Judge function
49 + #==========================
50 + function 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 +
60 + function 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 + print_ok "Burning $iso to /dev/$device. This will take a while."
103 + sudo dd if="$iso" of="/dev/$device" bs=4M status=progress oflag=sync
104 + sync
105 +
106 + # Verify the burn
107 + print_ok "Verifying the burn. This will take a while."
108 +
109 + # Foreach the mount point under $device, unmount it
110 + for mountpoint in $(lsblk -o MOUNTPOINT "/dev/$device" | tail -n +2); do
111 + print_ok "Unmounting $mountpoint"
112 + sudo umount "$mountpoint"
113 + judge "Unmount $mountpoint"
114 + done
115 +
116 + # Mount the device to /tmp/burn/$device
117 + print_ok "Mounting /dev/$device to /tmp/burn/$device"
118 + mkdir -p /tmp/burn/$device
119 + sudo mount "/dev/$device" /tmp/burn/$device
120 + judge "Mount /dev/$device to /tmp/burn/$device"
121 +
122 + # Calculate the md5sum of the iso
123 + (
124 + cd /tmp/burn/$device && \
125 + sudo md5sum -c md5sum.txt --quiet && \
126 + print_ok "Burn successful." || \
127 + print_error "Burn failed."
128 + )
129 +
130 + # Unmount the device
131 + print_ok "Unmounting /tmp/burn/$device"
132 + sudo umount /tmp/burn/$device
133 + judge "Unmount /tmp/burn/$device"
134 +
135 + # Clean up
136 + print_ok "Cleaning up"
137 + rm -rf /tmp/burn/$device
138 + judge "Clean up"
139 +
140 + print_ok "Done. You can now remove the USB stick."
141 + }
142 +
143 + burn "$@"
Newer Older