burn.sh
                        
                             · 4.0 KiB · Bash
                        
                    
                    
                      
                        Исходник
                      
                    
                      
                    
                        
                          
                        
                    
                    
                
                
                
            #!/bin/bash
#==========================
# Set up the environment
#==========================
set -e                  # exit on error
set -o pipefail         # exit on pipeline error
set -u                  # treat unset variable as error
#==========================
# Basic Information
#==========================
export LC_ALL=C
export LANG=en_US.UTF-8
export DEBIAN_FRONTEND=noninteractive
export SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
#==========================
# Color
#==========================
Green="\033[32m"
Red="\033[31m"
Yellow="\033[33m"
Blue="\033[36m"
Font="\033[0m"
GreenBG="\033[42;37m"
RedBG="\033[41;37m"
OK="${Green}[  OK  ]${Font}"
ERROR="${Red}[FAILED]${Font}"
WARNING="${Yellow}[ WARN ]${Font}"
#==========================
# Print Colorful Text
#==========================
function print_ok() {
  echo -e "${OK} ${Blue} $1 ${Font}"
}
function print_error() {
  echo -e "${ERROR} ${Red} $1 ${Font}"
}
function print_warn() {
  echo -e "${WARNING} ${Yellow} $1 ${Font}"
}
#==========================
# Judge function
#==========================
function judge() {
  if [[ 0 -eq $? ]]; then
    print_ok "$1 succeeded"
    sleep 0.2
  else
    print_error "$1 failed"
    exit 1
  fi
}
function burn()
{
    # If the user want to burn /dev/sda, device should be 'sda'
    device=$1
    iso=$2
    if [ -z "$device" ]; then
        print_error "Usage: $0 <device> <iso>"
        exit 1
    fi
    # Ensure /dev/$device exists
    if [ ! -e "/dev/$device" ]; then
        print_error "Device /dev/$device does not exist."
        exit 1
    fi
    # Unmount all partitions on /dev/$device
    for mountpoint in $(lsblk -o MOUNTPOINT "/dev/$device" | tail -n +2); do
        print_ok "Unmounting $mountpoint"
        sudo umount "$mountpoint"
        judge "Unmount $mountpoint"
    done
    # Ensure the iso exists
    if [ ! -e "$iso" ]; then
        print_error "ISO $iso does not exist."
        exit 1
    fi
    # Ensure the iso is a file
    if [ ! -f "$iso" ]; then
        print_error "ISO $iso is not a file."
        exit 1
    fi
    # Ensure the iso is readable
    if [ ! -r "$iso" ]; then
        print_error "ISO $iso is not readable."
        exit 1
    fi
    # Ensure the disk is larger than the iso
    iso_size=$(stat -c %s "$iso")
    device_size=$(sudo blockdev --getsize64 "/dev/$device")
    if [ "$iso_size" -gt "$device_size" ]; then
        print_error "ISO $iso is larger than /dev/$device."
        exit 1
    else
        print_ok "ISO $iso is $iso_size bytes, /dev/$device is $device_size bytes. Device is large enough."
    fi
    print_ok "Burning $iso to /dev/$device. This will take a while..."
    sudo dd if="$iso" of="/dev/$device" bs=4M status=progress oflag=sync
    sync && sync && sync
    # Verify the burn
    print_ok "Verifying the burn. This will take a while."
    # Foreach the mount point under $device, unmount it
    for mountpoint in $(lsblk -o MOUNTPOINT "/dev/$device" | tail -n +2); do
        print_ok "Unmounting $mountpoint"
        sudo umount "$mountpoint"
        judge "Unmount $mountpoint"
    done
    # Mount the device to /tmp/burn/$device
    print_ok "Mounting /dev/$device to /tmp/burn/$device"
    mkdir -p /tmp/burn/$device
    sudo mount "/dev/$device" /tmp/burn/$device
    judge "Mount /dev/$device to /tmp/burn/$device"
    # Calculate the md5sum of the iso
    print_ok "Calculating the md5sum of the files in the iso"
    (
        cd /tmp/burn/$device && \
        sudo md5sum -c md5sum.txt && \
        print_ok "First pass verification succeeded." || \
        print_error "Burn failed."
    )
    (
        cd /tmp/burn/$device && \
        sudo md5sum -c md5sum.txt && \
        print_ok "Second pass verification succeeded." || \
        print_error "Burn failed."
    )
    # Unmount the device
    print_ok "Unmounting /tmp/burn/$device"
    sudo umount /tmp/burn/$device
    judge "Unmount /tmp/burn/$device"
    # Clean up
    print_ok "Cleaning up"
    rm -rf /tmp/burn/$device
    judge "Clean up"
    print_ok "Done. You can now remove the USB stick."
}
burn "$@"
                | 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 | # 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 | |
| 160 | burn "$@" |