Dernière activité 1725451905

Révision 0848a3da38b605a96fc2d558fed29fb289ffeaaa

burn.sh Brut
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 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
143burn "$@"