Ostatnio aktywny 1725451905

Rewizja 46c7ca00898304742f71e56f142a2f5251948be0

burn.sh Surowy
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 '/dev/sda'
63 iso=$1
64 device=$2
65
66 if [ -z "$device" ]; then
67 print_error "Usage: burn.sh /path/to/iso /dev/device"
68 exit 1
69 fi
70
71 # Ensure $device exists
72 if [ ! -e "$device" ]; then
73 print_error "Device $device does not exist."
74 exit 1
75 fi
76
77 # Unmount all partitions on /$device
78 for mountpoint in $(lsblk -o MOUNTPOINT "$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 "$device")
105 if [ "$iso_size" -gt "$device_size" ]; then
106 print_error "ISO $iso is larger than $device."
107 exit 1
108 else
109 print_ok "ISO $iso is $iso_size bytes, $device is $device_size bytes. Device is large enough."
110 fi
111
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 sync && sync && sync
115
116 print_ok "Sleep 10 seconds to ensure the USB write buffer is flushed."
117 sleep 10
118
119 # Verify the burn
120 print_ok "Verifying the burn. This will take a while."
121
122 # Foreach the mount point under $device, unmount it
123 for mountpoint in $(lsblk -o MOUNTPOINT "$device" | tail -n +2); do
124 print_ok "Unmounting $mountpoint"
125 sudo umount "$mountpoint"
126 judge "Unmount $mountpoint"
127 done
128
129 # Mount the device to /tmp/burn/$device
130 print_ok "Mounting $device to /tmp/burn/$device"
131 mkdir -p /tmp/burn/$device
132 sudo mount "$device" /tmp/burn/$device
133 judge "Mount $device to /tmp/burn/$device"
134
135 # Calculate the md5sum of the iso
136 print_ok "Calculating the md5sum of the files in the iso"
137 (
138 cd /tmp/burn/$device && \
139 sudo md5sum -c md5sum.txt && \
140 print_ok "First pass verification succeeded." || \
141 print_error "Burn failed."
142 )
143 (
144 cd /tmp/burn/$device && \
145 sudo md5sum -c md5sum.txt && \
146 print_ok "Second pass verification succeeded." || \
147 print_error "Burn failed."
148 )
149
150 # Unmount the device
151 print_ok "Unmounting /tmp/burn/$device"
152 sleep 1
153 sudo umount /tmp/burn/$device
154 judge "Unmount /tmp/burn/$device"
155
156 # Clean up
157 print_ok "Cleaning up"
158 rm -rf /tmp/burn/$device
159 judge "Clean up"
160
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."
168}
169
170clear
171burn "$@"