aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2018-01-27 13:37:02 -0900
committerDavid M. Syzdek <david@syzdek.net>2018-01-27 13:37:02 -0900
commitba425402f3f6f7c338c4754a77d2cbac1061d990 (patch)
tree2287c85563edf36677705e453580e08e344a055d
parent1abfa9cdfc489f81e7a49e961ff972abdd78b236 (diff)
downloadiperd-ba425402f3f6f7c338c4754a77d2cbac1061d990.tar.gz
iperd-ba425402f3f6f7c338c4754a77d2cbac1061d990.tar.bz2
iperd-ba425402f3f6f7c338c4754a77d2cbac1061d990.tar.xz
iperd-ba425402f3f6f7c338c4754a77d2cbac1061d990.zip
refactoring thumbdrive.sh to support multiple partition types.
-rw-r--r--src/scripts/thumbdrive.sh291
1 files changed, 200 insertions, 91 deletions
diff --git a/src/scripts/thumbdrive.sh b/src/scripts/thumbdrive.sh
index c299724..6b9e11d 100644
--- a/src/scripts/thumbdrive.sh
+++ b/src/scripts/thumbdrive.sh
@@ -33,14 +33,13 @@
33# @SYZDEK_BSD_LICENSE_END@ 33# @SYZDEK_BSD_LICENSE_END@
34# 34#
35 35
36# +-=-=-=-=-=-+
37# | |
38# | Headers |
39# | |
40# +-=-=-=-=-=-+
41
36PROG_NAME="$(basename "${0}")" 42PROG_NAME="$(basename "${0}")"
37if test "x${2}" == "x";then
38 echo "Usage: ${PROG_NAME} <source> <device>"
39 exit 1
40fi
41SOURCE="${1}"
42DEVICE="${2}"
43PARTSIZE="${3}"
44 43
45 44
46SCRIPTDIR=src/scripts 45SCRIPTDIR=src/scripts
@@ -48,31 +47,11 @@ DIRS="boot src syslinux"
48FILES="COPYING Makefile Makefile.config Makefile.local syslinux/pxelinux.cfg" 47FILES="COPYING Makefile Makefile.config Makefile.local syslinux/pxelinux.cfg"
49 48
50 49
51unset MKTEMP 50# +-=-=-=-=-=-=-+
52 51# | |
53 52# | Functions |
54# check for required files/directories/devices 53# | |
55if test -z "${DEVICE}";then 54# +-=-=-=-=-=-=-+
56 echo "${PROG_NAME}: invalid device name"
57 exit 1
58fi
59if test ! -b "${DEVICE}";then
60 echo "${PROG_NAME}: device not found"
61 exit 1
62fi
63for DIR in ${DIRS};do
64 if test ! -d "${SOURCE}/${DIR}";then
65 echo "${PROG_NAME}: SOURCE/${DIR}: source directory not found"
66 exit 1
67 fi
68done
69for FILE in ${FILES};do
70 if test ! -f "${SOURCE}/${FILE}";then
71 echo "${PROG_NAME}: SOURCE/${FILE}: source file not found"
72 exit 1
73 fi
74done
75
76 55
77cleanup() 56cleanup()
78{ 57{
@@ -83,95 +62,223 @@ cleanup()
83 rm -Rf "${MKTEMP}" 62 rm -Rf "${MKTEMP}"
84 fi 63 fi
85} 64}
86trap cleanup EXIT
87 65
88 66
89set -x 67part_gpt()
90 68{
91
92## wipe MBR partition table on USB device
93#parted -a optimal -s "${DEVICE}" mktable msdos || exit 1
94
95## create DOS partition on USB device
96#parted -a optimal -s "${DEVICE}" mkpart primary fat32 0% 100% || exit 1
97
98## restore valid master boot record
99#dd conv=notrunc bs=440 count=1 if="${SOURCE}/syslinux/mbr.bin" of="${DEVICE}" || exit 1
100
101## set DOS partition as boot partition
102#parted -a optimal -s "${DEVICE}" set 1 boot on || exit 1
103
104
105##sgdisk "${DEVICE}" --zap-all || exit 1
106##sgdisk "${DEVICE}" --new=1:0:+${PARTSIZE} || exit 1
107##sgdisk "${DEVICE}" --typecode=1:ef00 || exit 1
108##sgdisk "${DEVICE}" --change-name=1:'BIOS/EFI Boot' || exit 1
109##sgdisk "${DEVICE}" --gpttombr=1 || exit 1
110##sfdisk "${DEVICE}" --activate 1 || exit 1
111##dd \
112## if="${SOURCE}/syslinux/gptmbr.bin" \
113## of="${DEVICE}" \
114## bs=440 \
115## conv=notrunc \
116## count=1 \
117## || exit 1
118
119
120# partition disk
121 # clear existing MBR and/or GPT data
122 sgdisk "${DEVICE}" --zap-all || exit 1
123
124 # create 1st partition: EFI 69 # create 1st partition: EFI
125 sgdisk "${DEVICE}" --new=1:0:+1M || exit 1 70 sgdisk "${DEVICE}" --new=1:0:+1M || return 1
126 sgdisk "${DEVICE}" --typecode=1:EF02 || exit 1 71 sgdisk "${DEVICE}" --typecode=1:EF02 || return 1
127 72
128 # create 2nd parition 73 # create 2nd parition
129 sgdisk "${DEVICE}" --new=2:0:0 || exit 1 74 sgdisk "${DEVICE}" --new=2:0:${PARTSIZE} || return 1
130 sgdisk "${DEVICE}" --typecode=2:0700 || exit 1 75 sgdisk "${DEVICE}" --typecode=2:0700 || return 1
76}
77
131 78
132 # make hybrid 79part_hybrid()
133 sgdisk "${DEVICE}" --hybrid=1:2 || exit 1 80{
81 # create base partition table
82 part_gpt || return 1
83
84 # convert to hybrid partition table
85 sgdisk "${DEVICE}" --hybrid=1:2 || return 1
134 86
135 # convert to MBR and activate partition 2 87 # convert to MBR and activate partition 2
136 sgdisk "${DEVICE}" --zap || exit 1 88 sgdisk "${DEVICE}" --zap || return 1
137 sfdisk --activate "${DEVICE}" 2 || exit 1 89 sfdisk --activate "${DEVICE}" 2 || return 1
138 90
139 # add boot code to MBR 91 # add boot code to MBR
140 dd \ 92 dd \
141 bs=440 count=1 conv=notrunc \ 93 bs=440 count=1 conv=notrunc \
142 if="${SOURCE}/syslinux/gptmbr.bin" \ 94 if="${SOURCE}/syslinux/gptmbr.bin" \
143 of="${DEVICE}" \ 95 of="${DEVICE}" \
144 || exit 1 96 || return 1
145 97
146 # backup MBR table 98 # backup MBR table
147 rm -f "${SOURCE}/tmp/mbr.backup" 99 rm -f "${SOURCE}/tmp/mbr.backup"
148 dd bs=512 count=1 conv=notrunc \ 100 dd bs=512 count=1 conv=notrunc \
149 if="${DEVICE}" \ 101 if="${DEVICE}" \
150 of="${SOURCE}/tmp/mbr.backup" \ 102 of="${SOURCE}/tmp/mbr.backup" \
151 || exit 1 103 || return 1
152 104
153 # convert back to GPT and adjust partition numbers 105 # convert back to GPT and adjust partition numbers
154 sgdisk "${DEVICE}" --mbrtogpt || exit 1 106 sgdisk "${DEVICE}" --mbrtogpt || return 1
155 sgdisk "${DEVICE}" --transpose=1:2 || exit 1 107 sgdisk "${DEVICE}" --transpose=1:2 || return 1
156 sgdisk "${DEVICE}" --transpose=2:3 || exit 1 108 sgdisk "${DEVICE}" --transpose=2:3 || return 1
157 109
158 # re-adjust partition 2 information for GPT 110 # re-adjust partition 2 information for GPT
159 sgdisk "${DEVICE}" --typecode=2:EF00 || exit 1 111 sgdisk "${DEVICE}" --typecode=2:EF00 || return 1
160 sgdisk "${DEVICE}" --change-name=2:"BootDisk" || exit 1 112 sgdisk "${DEVICE}" --change-name=2:"BootDisk" || return 1
161 sgdisk "${DEVICE}" --attributes=2:set:2 || exit 1 113 sgdisk "${DEVICE}" --attributes=2:set:2 || return 1
162 114
163 # convert GPT to hybrid GPT 115 # convert GPT to hybrid GPT
164 sgdisk "${DEVICE}" --hybrid=1:2 || exit 1 116 sgdisk "${DEVICE}" --hybrid=1:2 || return 1
165 117
166 # restore MBR with bootable partition 2 118 # restore MBR with bootable partition 2
167 dd bs=512 count=1 conv=notrunc \ 119 dd bs=512 count=1 conv=notrunc \
168 if="${SOURCE}/tmp/mbr.backup" \ 120 if="${SOURCE}/tmp/mbr.backup" \
169 of="${DEVICE}" \ 121 of="${DEVICE}" \
170 || exit 1 122 || return 1
171 rm -f "${SOURCE}/tmp/mbr.backup" 123 rm -f \
124 "${SOURCE}/tmp/mbr.backup" \
125 || return 1
126
127 return 0;
128}
129
130
131part_mbr()
132{
133 # create base partition table
134 part_gpt || return 1
172 135
173 # refresh partition table in kernel memory 136 # convert to hybrid partition table
174 partprobe "${DEVICE}" || exit 1 137 sgdisk "${DEVICE}" gpttombr=1:2 || return 1
138
139 # convert to MBR and activate partition 2
140 sgdisk "${DEVICE}" --zap || return 1
141 sfdisk --activate "${DEVICE}" 2 || return 1
142
143 # add boot code to MBR
144 dd \
145 bs=440 count=1 conv=notrunc \
146 if="${SOURCE}/syslinux/mbr.bin" \
147 of="${DEVICE}" \
148 || return 1
149
150 return 0;
151}
152
153
154usage()
155{
156 printf "Usage: %s [OPTIONS] <source> <device>\n" "$PROG_NAME"
157 printf "OPTIONS:\n"
158 printf " -h display this message\n"
159 printf " -q quiet\n"
160 printf " -s size size of data partition (default: 100%)"
161 printf " -t type partition type (mbr, hybrid [default], or gpt)\n"
162 printf " -v verbose\n"
163 printf "\n"
164}
165
166
167# +-=-=-=-=-=-=-+
168# | |
169# | Main Body |
170# | |
171# +-=-=-=-=-=-=-+
172
173# set defaults
174unset MKTEMP
175unset VERBOSE
176PARTTYPE="hybrid"
177PARTSIZE=0
178
179
180# parse CLI arguments
181while getopts :hqs:t:v OPT; do
182 case ${OPT} in
183 h) usage; exit 0;;
184 s) PARTSIZE="${OPTARG}";;
185 t) PARTTYPE="${OPTARG}";;
186 v) VERBOSE="1";;
187 q) unset VERBOSE;;
188 ?)
189 echo "${PROG_NAME}: illegal option -- ${OPTARG}" 1>&2
190 echo "Try '${PROG_NAME} -h' for more information." 1>&2
191 exit 1;
192 ;;
193 *)
194 ;;
195 esac
196done
197shift "$((OPTIND-1))"
198SOURCE="${1}"
199DEVICE="${2}"
200
201
202# check configuration options
203if test -z "${SOURCE}";then
204 echo "${PROG_NAME}: missing source"
205 echo "Try '${PROG_NAME} -h' for more information." 1>&2
206 exit 1
207fi
208if test -z "${DEVICE}";then
209 echo "${PROG_NAME}: missing device"
210 echo "Try '${PROG_NAME} -h' for more information." 1>&2
211 exit 1
212fi
213if test "x${PARTTYPE}" != "xhybrid" &&
214 test "x${PARTTYPE}" != "xmbr" &&
215 "x${PARTTYPE}" != "xgpt";then
216 echo "${PROG_NAME}: invalid partition type"
217 echo "Try '${PROG_NAME} -h' for more information." 1>&2
218 exit 1;
219fi
220if test ! -z "$(echo ${PARTSIZE} |sed -e 's/^[0-9]\+M\{0,1\}$//g'";then
221 echo "${PROG_NAME}: invalid data partition size"
222 echo "Try '${PROG_NAME} -h' for more information." 1>&2
223 exit 1;
224fi
225
226
227# adjusts PARTSIZE
228if test "x${PARTSIZE//[0-9]/}" == "x" &&
229 test "x${PARTSIZE}" != "x0"then
230 PARTSIZE="${PARTSIZE}M"
231fi
232
233
234# check for required files/directories/devices
235if test ! -b "${DEVICE}";then
236 echo "${PROG_NAME}: device not found"
237 exit 1
238fi
239for DIR in ${DIRS};do
240 if test ! -d "${SOURCE}/${DIR}";then
241 echo "${PROG_NAME}: SOURCE/${DIR}: source directory not found"
242 exit 1
243 fi
244done
245for FILE in ${FILES};do
246 if test ! -f "${SOURCE}/${FILE}";then
247 echo "${PROG_NAME}: SOURCE/${FILE}: source file not found"
248 exit 1
249 fi
250done
251
252
253# set up exit traps
254trap cleanup SIGHUP SIGINT SIGTERM EXIT
255
256
257# enable verbose output
258if test "x${VERBOSE}" == "x1";then
259 set -x
260fi
261
262
263# clear existing MBR and/or GPT data
264sgdisk "${DEVICE}" --zap-all || exit 1
265
266
267# partition disk
268case "${PARTTYPE}" in
269 'mbr') part_mbr || exit 1;;
270 'gpt') part_gpt || exit 1;;
271 'hybrid') part_hybrid || exit 1;;
272 *)
273 echo "${PROG_NAME}: unknown partition table type" 1>&2
274 echo "Try '${PROG_NAME} -h' for more information." 1>&2
275 exit 1;
276 ;;
277esac
278
279
280# refresh partition table in kernel memory
281partprobe "${DEVICE}" || exit 1
175 282
176 283
177 284
@@ -186,12 +293,14 @@ fi
186 293
187# make vFAT file systems 294# make vFAT file systems
188mkfs.vfat -F 32 -I -n "IP_ENG_BOOT" /dev/${DEVPART} || exit 1 295mkfs.vfat -F 32 -I -n "IP_ENG_BOOT" /dev/${DEVPART} || exit 1
189#fatlabel /dev/${DEVPART} IP_ENG_BOOT || exit 1
190parted -a optimal -s "${DEVICE}" print 296parted -a optimal -s "${DEVICE}" print
191 297
192 298
193# install syslinux 299# install syslinux
194"${SOURCE}/syslinux/bin/syslinux" -i /dev/${DEVPART} || exit 1 300if test "${PARTTYPE}" == "mbr" ||
301 test "${PARTTYPE}" == "hybrid";then
302 "${SOURCE}/syslinux/bin/syslinux" -i /dev/${DEVPART} || exit 1
303fi
195 304
196 305
197# mount disk image 306# mount disk image