-
Notifications
You must be signed in to change notification settings - Fork 2
/
kali-burn-iso
executable file
·131 lines (109 loc) · 3.77 KB
/
kali-burn-iso
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
#!/bin/bash
# (c) 2020 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/kali-burn-iso
#
PROG="${0##*/}"
perror() {
msg="$*"
echo "${msg:-Failed}"
exit 1
}
DDorDCF() {
DD=$(which dcfldd)
if [ -n "${DD}" ]
then
OFLAG=$(${DD} --help | egrep -wo '(^\s*)direct' | sed 's/[^a-z]//ig')
if [ -n "${OFLAG}" ]
then
DCFD=1
else
DCFD=0
fi
fi
DD=$(which dd)
if [ -n "${DD}" ]
then
OFLAG=$(${DD} --help | egrep -wo '(^\s*)direct' | sed 's/[^a-z]//ig')
if [ -n "${OFLAG}" ]
then
DDD=1
else
DDD=0
fi
fi
PROGRESS=$(${DD} --help | egrep -wo '(^\s*)status' | sed 's/[^a-z]//ig')
if [ -n "${PROGRESS}" ];
then
if [ ${DDD:-0} -eq 1 -o ${DCFD:-0} -eq 0 ]
then
# Always prefer native DD
DD=dd
PROGRESS="status=progress"
elif [ ${DDD:-0} -eq 0 -a ${DCFD:-0} -eq 1 ]
then
# only when DDD -eq 0 and DCFD -eq 1
PROGRESS="status=on"
else
PROGRESS=""
fi
else
PROGRESS=""
fi
# Do we support direct io? we've tested this twice, but this is the final test
OFLAG=$(${DD} --help | egrep -wo '(^\s*)direct' | sed 's/[^a-z]//ig')
}
prereqs() {
which lsscsi >/dev/null 2>&1 || perror "Can't find lsscsi"
which eject >/dev/null 2>&1 || perror "Can't find eject"
which parted >/dev/null 2>&1 || perror "Can't find parted"
which mkfs.ext4 >/dev/null 2>&1 || perror "Can't find mkfs.ext4"
}
usage() {
echo "usage:"
# echo ""
echo -e " \$ ${PROG} [iso] {bs}"
# echo ""
echo -e " ${PROG//?/ } iso - KALI iso filename"
echo -e " ${PROG//?/ } bs - optional blocksize to use for dd"
echo ""
exit 1
}
prereqs
DDorDCF
[[ -z "$1" ]] && usage
[[ ! -r "$1" ]] && perror "Can't read ${1} file for burning"
ISO=${1}
BS=${2//[^0-9kKmMgGbB]/}
BS=${BS:-512k}
if [ ! \( \( "${USER}" = "root" \) -o \( -n "${EUID}" -a ${EUID} = 0 \) \) ]
then
perror "root privs required. re-run with sudo."
fi
flash=$( lsscsi -s 2>&1 | grep -i 'Flash.*GB' | grep -o '/dev/sd.' )
[[ -z "${flash}" ]] && perror "Can't determine flash drive"
ionice -c2 -n7 ${DD} if="${ISO}" bs=${BS} conv=noerror,sync,notrunc ${OFLAG:+oflag=$OFLAG} ${PROGRESS} of="${flash}"
[[ $? ]] || perror "Couldn't write iso ${ISO} to flash drive ${flash}"
# length of ISO partition
read start _ < <(du -bm ${ISO})
[[ -z "${start}" ]] && perror "Can't determine size of iso ${ISO}"
# end of the flash drive
end=$( parted ${flash} print | grep ${flash} | grep -oE '[0-9]+[GM]B' )
[[ -z "${end}" ]] && perror "Can't determine size of flash drive ${flash}"
# non-alignment errors-out with an actual value, capture it in case the first attempt fails
start=$( parted -s -ao ${flash} mkpart primary $start $end 2>&1 | grep -oE 'we can manage is [0-9]+MB' | grep -oE '[0-9]+MB' )
# ensure we actually create the partition if the alignment is necessary
test -n "${start}" && parted -s -ao ${flash} mkpart primary $start $end
#The new partition should be created, but we'll double-check
part=$( parted ${flash} print | grep primary | tail -1 | awk '{print $1}' )
[[ ${part:-0} -le 2 ]] && perror "Partition creation failed!"
mkfs.ext4 -L persistence "${flash}${part}"
MNT=$( mktemp -p /mnt -d )
[[ -z "${MNT}" ]] && perror "Couldn't create temporary mount directory ${MNT}"
mount "${flash}${part}" ${MNT} || perror "Couldn't mount ${flash}${part} into ${MNT}"
echo "/ union" > ${MNT}/persistence.conf
umount ${MNT} || perror "Couldn't unmount ${MNT}"
rmdir ${MNT} || perror "Couldn't remove temporary mount directory ${MNT}"
eject ${flash} || perror "Couldn't eject flash drive ${flash}"
echo "done"