-
Notifications
You must be signed in to change notification settings - Fork 0
/
burniso
executable file
·187 lines (143 loc) · 3.3 KB
/
burniso
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
# ┏┓ ╻ ╻┏━┓┏┓╻╻┏━┓┏━┓
# ┣┻┓┃ ┃┣┳┛┃┗┫┃┗━┓┃ ┃
# ┗━┛┗━┛╹┗╸╹ ╹╹┗━┛┗━┛
#
# script for burning iso images to usb devices using `dd`
PROG="${0##*/}"
DEPS=(fdisk dd realpath)
declare -a ARGS=(bs=4M status=progress oflag=sync)
# colors
BLUE=$(tput setaf 4)
CYAN=$(tput setaf 6)
GRAY=$(tput setaf 8)
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
YELLOW=$(tput setaf 3)
# style
BOLD=$(tput bold)
NC="$(tput sgr0)"
function usage {
printf "usage: %s <iso> <device>\n" "$PROG"
}
function logerr {
local msg="$1"
printf "%s: %s\n" "$PROG" "$msg" >&2
exit 1
}
function confirm {
local answer
echo -n "Are you sure you want to continue? ${GRAY}[y/N]:${NC} "
read -r answer
case "$answer" in
y | Y) return 0 ;;
n | N) return 1 ;;
*) return 1 ;;
esac
}
function show_dev_info {
local device="$1"
[[ -z "$device" ]] && {
usage
logerr "err device not provided"
}
if [[ ! -b "$device" ]]; then
logerr "invalid device: '$device'"
fi
echo -e "${GREEN}${BOLD}$device${NC} information\n"
if ! sudo fdisk -l "$device"; then
return 1
fi
return 0
}
function show_warning {
echo -en "\n${YELLOW}${BOLD}Warning${NC}:"
echo -e "${RED}${BOLD} device will be completely overwritten!${NC}\n"
}
function getsize {
local file="$1"
if [[ ! -f "$file" ]]; then
logerr "file not found: '$file'"
fi
size=$(du -b "$file" | awk '{printf "%.1f", $1 / (1024 * 1024) / 1024}')
echo "${size}GB"
}
function show_status {
local iso="$1"
local device="$2"
echo "${GREEN}${BOLD}Status:${NC}"
echo " ${CYAN}${BOLD}Device:${NC} ${BOLD}$device${NC}"
echo " ${BLUE}${BOLD}Path:${NC} $(dirname "$iso")"
echo " ${YELLOW}${BOLD}Image:${NC} $(basename "$iso")"
echo -e " ${BLUE}${BOLD}Size:${NC} $(getsize "$iso")\n"
}
function burn {
local iso="$1"
local device="$2"
if ! show_dev_info "$device"; then
exit 1
fi
show_warning
show_status "$iso" "$device"
if ! confirm; then
exit 1
fi
ARGS+=(if="$iso")
ARGS+=(of="$device")
sudo dd "${ARGS[@]}"
return 0
}
function format {
local device="$1"
read -p "New volume label: " -r label
sudo mkfs.ext4 "$device" -L "${label:-}"
}
function erase_dev {
local device="$1"
declare -a args=(bs=1M status=progress)
if ! confirm; then
return 1
fi
args+=(if=/dev/zero)
args+=(of="$device")
sudo dd "${args[@]}"
return 0
}
function wipe {
local device="$1"
if ! show_dev_info "$device"; then
exit 1
fi
show_warning
if ! erase_dev "$device"; then
return 1
fi
format "$device"
return 0
}
function main {
local iso="$1"
local device="$2"
for dep in "${DEPS[@]}"; do
if ! command -v "$dep" >/dev/null; then
logerr "'$dep' not found"
fi
done
[[ -z "$iso" ]] && {
usage
exit 1
}
case "$iso" in
--format)
wipe "$device"
exit
;;
*) ;;
esac
iso=$(realpath "$iso")
if [[ ! -f "$iso" ]]; then
logerr "file not found: '$iso'"
fi
burn "$iso" "$device"
}
main "$@"