-
Notifications
You must be signed in to change notification settings - Fork 133
/
zfsinstall
executable file
·501 lines (445 loc) · 13.4 KB
/
zfsinstall
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/bin/sh
# $Id$
#
# mfsBSD ZFS install script
# Copyright (c) 2018 Martin Matuska <mm at FreeBSD.org>
#
FREEBSD_MIRROR="${FREEBSD_MIRROR:-ftp://ftp.freebsd.org/pub/FreeBSD}"
FS_LIST="var tmp"
RAID_TYPES="mirror raidz raidz1 raidz2 raidz3"
usage() {
echo "Usage: $0 [-h] -d geom_provider [-d geom_provider ...] [ -u dist_url ] [-r mirror|raidz[1|2|3]] [-m mount_point] [-p zfs_pool_name] [-s swap_partition_size] [-z zfs_partition_size] [-c] [-C] [-l] [-4] [-A]"
}
osarch() {
/sbin/sysctl -n hw.machine_arch
}
osrelease() {
/sbin/sysctl -n kern.osrelease | /usr/bin/sed -E -e 's,-p[0-9]+$,,g'
}
check_raid() {
RAID_TYPE="$1"
test -n "$RAID_TYPE" || return 1
for raid_type in $RAID_TYPES; do
if [ "$raid_type" = $RAID_TYPE ]; then
return 0
fi
done
return 1
}
help() {
echo; echo "Install FreeBSD using ZFS from a compressed archive"
echo; echo "Required flags:"
echo "-d geom_provider : geom provider(s) to install to (e.g. da0)"
echo; echo "Optional flags:"
echo "-r raidz[1|2|3]|mirror : select raid mode if more than one -d provider given"
echo " (must begin after -d)"
echo "-u dist_url : URL or directory with base.txz and kernel.txz"
echo " (defaults to FreeBSD FTP mirror)"
echo "-s swap_part_size : create a swap partition with given size (default: no swap)"
echo "-z zfs_part_size : create zfs parition of this size (default: all space left)"
echo "-p pool_name : specify a name for the ZFS pool (default: tank)"
echo "-C : compatibility mode with limited feature flags"
echo " (enable only async_destroy, empty_bpobj and lz4_compress)"
echo "-m mount_point : use this mount point for operations (default: /mnt)"
echo "-c : enable compression for all datasets"
echo "-l : use legacy mounts (via fstab) instead of ZFS mounts"
echo "-4 : use fletcher4 as default checksum algorithm"
echo "-A : align partitions to 4K blocks"
echo; echo "Examples:"
echo "Install on a single drive with 2GB swap:"
echo "$0 -u /path/to/release -d da0 -s 2G"
echo "Install on four-disk stripe:"
echo "$0 -u /path/to/release -d da0 -d da1 -d da2 -d da3"
echo "Install on an stripped mirror:"
echo "$0 -u /path/to/release -d da0 -d da1 -r mirror -d da2 -d da3 -r mirror"
echo "Install on a raidz2:"
echo "$0 -u /path/to/release -d da0 -d da1 -d da2 -d da3 -r raidz2"
echo "Install on a mirror without swap, pool name rpool:"
echo "$0 -u /path/to/release -d da0 -d da1 -r mirror -p rpool"
echo; echo "Notes:"
echo "When using swap and raidz/mirror, the swap partition is created on all drives."
echo "The /etc/fstab entry will contatin only the first drive's swap partition."
echo "You can enable all swap partitions and/or make a gmirror-ed swap later."
}
while getopts d:u:t:r:p:s:z:m:V:Chcl4A o; do
case "$o" in
d) DEVS="$DEVS ${OPTARG##/dev/}"; ZDEVS="$ZDEVS ${OPTARG##/dev/}" ;;
u) URL="${OPTARG}" ;;
t) ARCHIVE="${OPTARG}" ;;
p) POOL="${OPTARG}" ;;
s) SWAP="${OPTARG}" ;;
m) MNT="${OPTARG}" ;;
r)
if [ -z "${ZDEVS}" ]; then
echo "Error: geom providers must be defined before using \
this option"
usage
exit 1
fi
if [ -n "${RAID}" -a "${RAID}" != "${OPTARG}" ]; then
echo "Error: array types do not match"
exit 1
else
RAID="${OPTARG}"
fi
if ! check_raid "$RAID"; then
echo "Error: please choose raid mode with the -r switch (mirror or raidz[1|2|3])"
exit 1
fi
COUNT=`echo ${ZDEVS} | /usr/bin/wc -w | /usr/bin/awk '{ print $1 }'`
if [ "$COUNT" -lt "3" ] && [ "$RAID" = "raidz" -o "$RAID" = "raidz1" ]; then
echo "Error: raidz needs at least three devices (-d switch)"
exit 1
elif [ "$COUNT" -lt "4" -a "$RAID" = "raidz2" ]; then
echo "Error: raidz2 needs at least four devices (-d switch)"
exit 1
elif [ "$COUNT" -lt "5" -a "$RAID" = "raidz3" ]; then
echo "Error: raidz3 needs at least five devices (-d switch)"
exit 1
elif [ "$COUNT" = "1" -a "$RAID" = "mirror" ]; then
echo "Error: mirror needs at least two devices (-d switch)"
exit 1
fi
ZARRAY="${ZARRAY} ${RAID} ${ZDEVS}"
unset ZDEVS
;;
z) ZPART="${OPTARG}" ;;
V) VERSION="${OPTARG}" ;;
C) COMPAT=1 ;;
c) COMPRESS=1 ;;
l) LEGACY=1 ;;
4) FLETCHER=1 ;;
A) ALIGN=1 ;;
h) help; exit 1;;
[?]) usage; exit 1;;
esac
done
if ! `/sbin/kldstat -m zfs >/dev/null 2>/dev/null`; then
/sbin/kldload zfs >/dev/null 2>/dev/null
fi
ZFS_VERSION=`/sbin/sysctl -n vfs.zfs.version.spa 2>/dev/null`
if [ -z "$ZFS_VERSION" ]; then
echo "Error: failed to load ZFS module"
exit 1
elif [ "$ZFS_VERSION" -lt "13" ]; then
echo "Error: ZFS module too old, version 13 or higher required"
exit 1
fi
if [ -z "$DEVS" ]; then
usage
exit 1
fi
if [ -z "$POOL" ]; then
POOL=tank
fi
if [ -z "$VERSION" ]; then
VERSION=${ZFS_VERSION}
elif [ "$VERSION" -gt "$ZFS_VERSION" ]; then
echo "Error: invalid ZFS pool version (maximum: $ZFS_VERSION)"
exit 1
fi
if [ "$VERSION" = "5000" ]; then
VERSION=
else
VERSION="-o version=${VERSION}"
fi
if [ "$COMPAT" = "1" ]; then
if [ "$ZFS_VERSION" != 5000 ]; then
echo "Error: compatibility mode requires ZFS version 5000"
exit 1
fi
COMPATFLAGS="-d -o feature@async_destroy=enabled -o feature@empty_bpobj=enabled -o feature@lz4_compress=enabled"
fi
if [ "$ALIGN" = "1" ]; then
ALIGNMENT="-a 4096"
fi
if /sbin/zpool list $POOL > /dev/null 2> /dev/null; then
echo Error: ZFS pool \"$POOL\" already exists
echo Please choose another pool name or rename/destroy the existing pool.
exit 1
fi
EXPOOLS=`/sbin/zpool import 2> /dev/null | /usr/bin/grep pool: | /usr/bin/awk '{ print $2 }'`
if [ -n "${EXPOOLS}" ]; then
for P in ${EXPOOLS}; do
if [ "$P" = "$POOL" ]; then
echo Error: An exported ZFS pool \"$POOL\" already exists
echo Please choose another pool name or rename/destroy the exported pool.
exit 1
fi
done
fi
if [ -z "${ZARRAY}" ]; then
echo Notice: no raid type has been chosen, it will be \`stripe\`
ZARRAY="${DEVS}"
fi
for DEV in ${DEVS}; do
if ! [ -c "/dev/${DEV}" ]; then
echo "Error: /dev/${DEV} is not a block device"
exit 1
fi
if /sbin/gpart show $DEV > /dev/null 2> /dev/null; then
echo "Error: /dev/${DEV} already contains a partition table."
echo ""
/sbin/gpart show $DEV
echo "You may erase the partition table manually with the destroygeom command"
exit 1
fi
done
EXTRACT_FILES="/tmp/base.txz /tmp/kernel.txz"
if [ -z "${URL}" ]; then
if [ -z "${ARCHIVE}" ]; then
URL="${FREEBSD_MIRROR}/releases/$(osarch)/$(osrelease)"
elif ! [ -f "${ARCHIVE}" ]; then
echo "Error: file $ARCHIVE does not exist"
exit 1
else
EXTRACT_FILES=${ARCHIVE}
fi
else
if [ -d "${URL}" ]; then
for file in base.txz kernel.txz; do
if ! [ -f "${URL}/${file}" ]; then
echo "File not found: ${URL}/${file}"
exit 1
fi
done
EXTRACT_FILES="${URL}/base.txz ${URL}/kernel.txz"
fi
fi
if [ -z "$MNT" ]; then
MNT=/mnt
fi
if ! [ -d "${MNT}" ]; then
echo "Error: $MNT is not a directory"
exit 1
fi
if [ -n "${ZPART}" ]; then
SZPART="-s ${ZPART}"
fi
if [ "${LEGACY}" = "1" ]; then
ALTROOT=
ROOTMNT=legacy
else
ALTROOT="-o altroot=${MNT} -o cachefile=/boot/zfs/zpool.cache"
ROOTMNT=/
fi
# Fetch base.txz and kernel.txz
if [ -n "${URL}" -a ! -d "${URL}" ]; then
echo "Fetching base files from: $URL"
if ! /usr/bin/fetch -o /tmp/base.txz "${URL}/base.txz"; then
echo "Error fetching ${URL}/base.txz"
exit 1
elif ! /usr/bin/fetch -o /tmp/kernel.txz "${URL}/kernel.txz"; then
echo "Error fetching ${URL}/kernel.txz"
exit 1
fi
fi
# Create GPT
for DEV in ${DEVS}; do
echo -n "Creating GUID partitions on ${DEV} ..."
if ! /sbin/gpart create -s GPT /dev/${DEV} > /dev/null; then
echo " error"
exit 1
fi
/bin/sleep 1
if ! echo "a 1" | /sbin/fdisk -f - ${DEV} > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
if ! /sbin/gpart add -t freebsd-boot -b 40 -s 472 ${DEV} > /dev/null; then
echo " error"
exit 1
fi
if [ -n "${SWAP}" ]; then
if ! /sbin/gpart add -t freebsd-swap ${ALIGNMENT} -s "${SWAP}" ${DEV} > /dev/null; then
echo " error"
exit 1
fi
SWAPPART=`/sbin/glabel status ${DEV}p2 | /usr/bin/grep gptid | /usr/bin/awk '{ print $1 }'`
if [ -z "$SWAPPART" ]; then
echo " error determining swap partition"
fi
if [ -z "$FSWAP" ]; then
FSWAP=${SWAPPART}
fi
fi
if ! /sbin/gpart add -t freebsd-zfs ${ALIGNMENT} ${SZPART} ${DEV} > /dev/null; then
echo " error"
exit 1
fi
/bin/dd if=/dev/zero of=/dev/${DEV}p2 bs=512 count=560 > /dev/null 2> /dev/null
if [ -n "${SWAP}" ]; then
/bin/dd if=/dev/zero of=/dev/${DEV}p3 bs=512 count=560 > /dev/null 2> /dev/null
fi
echo " done"
echo -n "Configuring ZFS bootcode on ${DEV} ..."
if ! /sbin/gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${DEV} > /dev/null; then
echo " error"
exit 1
fi
echo " done"
/sbin/gpart show ${DEV}
done
# Create zpool and zfs
for DEV in ${ZARRAY}; do
if check_raid "${DEV}"; then
if [ -n "${_PARTS}" ]; then
ZPARTS="${ZPARTS} ${RAID} ${_PARTS}"
unset _PARTS
fi
continue
fi
PART=`/sbin/gpart show ${DEV} | /usr/bin/grep freebsd-zfs | /usr/bin/awk '{ print $3 }'`
if [ -z "${PART}" ]; then
echo Error: freebsd-zfs partition not found on /dev/$DEV
exit 1
fi
GPART=`/sbin/glabel list ${DEV}p${PART} | /usr/bin/grep gptid | /usr/bin/awk -F"gptid/" '{ print "gptid/" $2 }'`
GPARTS="${GPARTS} ${GPART}"
PARTS="${PARTS} ${DEV}p${PART}"
_PARTS="${_PARTS} ${DEV}p${PART}"
done
ZPARTS="${ZPARTS} ${RAID} ${_PARTS}"
echo -n "Creating ZFS pool ${POOL} on ${PARTS} ..."
if ! /sbin/zpool create -f -m none ${ALTROOT} ${COMPATFLAGS} ${VERSION} ${POOL} ${ZPARTS} > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
echo " done"
if [ "${FLETCHER}" = "1" ]; then
echo -n "Setting default checksum to fletcher4 for ${POOL} ..."
if ! /sbin/zfs set checksum=fletcher4 ${POOL} > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
echo " done"
fi
if [ "${COMPRESS}" = "1" ]; then
echo -n "Enabling default compression on ${POOL} ..."
if ! /sbin/zfs set compression=on ${POOL} > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
echo " done"
fi
echo -n "Creating ${POOL} root partition:"
if ! /sbin/zfs create -o mountpoint=${ROOTMNT} ${POOL}/root > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
echo " ... done"
echo -n "Creating ${POOL} partitions:"
for FS in ${FS_LIST}; do
if [ "${LEGACY}" = 1 ]; then
MNTPT="-o mountpoint=legacy"
else
MNTPT=
fi
if ! /sbin/zfs create ${MNTPT} ${POOL}/root/${FS} > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
echo -n " ${FS}"
done
echo " ... done"
echo -n "Setting bootfs for ${POOL} to ${POOL}/root ..."
if ! /sbin/zpool set bootfs=${POOL}/root ${POOL} > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
echo " done"
/sbin/zfs list -r ${POOL}
# Mount and populate zfs (if legacy)
if [ "${LEGACY}" = "1" ]; then
echo -n "Mounting ${POOL} on ${MNT} ..."
/bin/mkdir -p ${MNT}
if ! /sbin/mount -t zfs ${POOL}/root ${MNT} > /dev/null 2> /dev/null; then
echo " error mounting pool/root"
exit 1
fi
for FS in ${FS_LIST}; do
/bin/mkdir -p ${MNT}/${FS}
if ! /sbin/mount -t zfs ${POOL}/root/${FS} ${MNT}/${FS} > /dev/null 2> /dev/null; then
echo " error mounting ${POOL}/root/${FS}"
exit 1
fi
done
echo " done"
fi
echo -n "Extracting FreeBSD distribution ..."
for file in ${EXTRACT_FILES}; do
if ! /usr/bin/tar -C ${MNT} -x -f ${file} > /dev/null 2> /dev/null; then
echo " error"
exit 1
fi
done
echo " done"
# Adjust configuration files
echo -n "Writing /boot/loader.conf..."
echo "zfs_load=\"YES\"" > ${MNT}/boot/loader.conf
echo "vfs.root.mountfrom=\"zfs:${POOL}/root\"" >> ${MNT}/boot/loader.conf
echo " done"
# Write fstab if swap or legacy
echo -n "Writing /etc/fstab..."
rm -f ${MNT}/etc/fstab
touch ${MNT}/etc/fstab
if [ -n "${FSWAP}" -o "${LEGACY}" = "1" ]; then
if [ -n "${FSWAP}" ]; then
echo "/dev/${FSWAP} none swap sw 0 0" > ${MNT}/etc/fstab
fi
if [ "${LEGACY}" = "1" ]; then
for FS in ${FS_LIST}; do
echo ${POOL}/root/${FS} /${FS} zfs rw 0 0 >> ${MNT}/etc/fstab
done
fi
fi
if [ "${LEGACY}" != "1" ]; then
echo -n "Writing /etc/rc.conf..."
echo 'zfs_enable="YES"' >> ${MNT}/etc/rc.conf
fi
echo " done"
echo -n "Copying /boot/zfs/zpool.cache ..."
if [ -n "${LEGACY}" ]; then
for FS in ${FS_LIST}; do
/sbin/umount ${MNT}/${FS} > /dev/null 2> /dev/null
done
/sbin/umount ${MNT} > /dev/null 2> /dev/null
fi
if ! /sbin/zpool export ${POOL} > /dev/null 2> /dev/null; then
echo " error exporting pool"
exit 1
fi
if ! /sbin/zpool import ${ALTROOT} ${POOL} > /dev/null 2> /dev/null; then
echo " error importing pool"
exit 1
fi
if [ -n "${LEGACY}" ]; then
if ! /sbin/mount -t zfs ${POOL}/root ${MNT} > /dev/null 2> /dev/null; then
echo " error mounting ${POOL}/root"
exit 1
fi
fi
if ! /bin/cp /boot/zfs/zpool.cache ${MNT}/boot/zfs/ > /dev/null 2> /dev/null; then
echo " error copying zpool.cache"
exit 1
fi
if [ -n "${LEGACY}" ]; then
for FS in ${FS_LIST}; do
if ! /sbin/mount -t zfs ${POOL}/${FS} ${MNT}/${FS} > /dev/null 2> /dev/null; then
echo " error mounting ${POOL}/${FS}"
exit 1
fi
done
fi
echo " done"
echo ""
echo "Installation complete."
echo "The system will boot from ZFS with clean install on next reboot"
echo ""
echo "You may make adjustments to the installed system using chroot:"
echo "chroot ${MNT}"
echo ""
echo "Some adjustments may require a mounted devfs:"
echo "mount -t devfs devfs ${MNT}/dev"
echo ""
echo "WARNING - Don't export ZFS pool \"${POOL}\"!"