Welcome to mirror list, hosted at ThFree Co, Russian Federation.

image_zfs.sh « poudriere « share « src - github.com/freebsd/poudriere.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a4af764d90397a4f3decf42b615724df322e9952 (plain)
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
#!/bin/sh
#
# Copyright (c) 2018-2021 Allan Jude <allanjude@FreeBSD.org>
# Copyright (c) 2019 Marie Helene Kvello-Aune <freebsd@mhka.no>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

_zfs_writereplicationstream()
{

	# Arguments:
	# $1: snapshot to recursively replicate
	# $2: Image name to write replication stream to
	[ $# -eq 2 ] || eargs _zfs_writereplicationstream snapshot_from image_to
	msg "Creating replication stream"
	zfs send ${ZFS_SEND_FLAGS} "$1" > "${OUTPUTDIR}/$2" ||
	    err 1 "Failed to save ZFS replication stream"
}

zfs_check()
{

	[ -n "${IMAGESIZE}" ] || err 1 "Please specify the imagesize"
	[ -n "${ZFS_POOL_NAME}" ] || err 1 "Please specify a pool name"
	zpool list -Ho name ${ZFS_POOL_NAME} >/dev/null 2>&1 && \
		err 1 "Target pool name already exists"
	case "${IMAGENAME}" in
	''|*[!A-Za-z0-9]*)
		err 1 "Name can only contain alphanumeric characters"
		;;
	esac
	[ -f "${mnt}/boot/kernel/kernel" ] || \
	    err 1 "The ${MEDIATYPE} media type requires a jail with a kernel"
	if [ -n "${ORIGIN_IMAGE}" ]; then
		[ -z "${SNAPSHOT_NAME}" ] || err 1 \
			"You must specify the snapshot name (-S) when using -i"
	fi
}

zfs_prepare()
{

	truncate -s ${IMAGESIZE} ${WRKDIR}/raw.img
	md=$(/sbin/mdconfig ${WRKDIR}/raw.img)
	zroot=${ZFS_POOL_NAME}

	msg "Creating temporary ZFS pool"
	zpool create \
		-O mountpoint=/${ZFS_POOL_NAME} \
		-O canmount=noauto \
		-O checksum=sha512 \
		-O compression=on \
		-O atime=off \
		-R ${WRKDIR}/world ${zroot} /dev/${md} || exit

	if [ -n "${ORIGIN_IMAGE}" ]; then
		msg "Importing previous ZFS Datasets"
		zfs recv -F ${zroot} < "${ORIGIN_IMAGE}"
	else
		msg "Creating ZFS Datasets"
		zfs create -o mountpoint=none ${zroot}/${ZFS_BEROOT_NAME}
		zfs create -o mountpoint=/ ${zroot}/${ZFS_BEROOT_NAME}/${ZFS_BOOTFS_NAME}
		zfs create -o mountpoint=/tmp -o exec=on -o setuid=off ${zroot}/tmp
		zfs create -o mountpoint=/usr -o canmount=off ${zroot}/usr
		zfs create ${zroot}/usr/home
		zfs create -o setuid=off ${zroot}/usr/ports
		zfs create ${zroot}/usr/src
		zfs create ${zroot}/usr/obj
		zfs create -o mountpoint=/var -o canmount=off ${zroot}/var
		zfs create -o exec=off -o setuid=off ${zroot}/var/audit
		zfs create -o exec=off -o setuid=off ${zroot}/var/crash
		zfs create -o exec=off -o setuid=off ${zroot}/var/log
		zfs create -o atime=on ${zroot}/var/mail
		zfs create -o setuid=off ${zroot}/var/tmp
		chmod 1777 ${WRKDIR}/world/tmp ${WRKDIR}/world/var/tmp
	fi
}

zfs_build()
{
	if [ -z "${ORIGIN_IMAGE}" ]; then
		cat >> ${WRKDIR}/world/boot/loader.conf <<-EOF
		zfs_load="YES"
		EOF
		if [ -n "${SWAPSIZE}" -a "${SWAPSIZE}" != "0" ]; then
			cat >> ${WRKDIR}/world/etc/fstab <<-EOSWAP
			/dev/gpt/swapspace none swap sw 0 0
			EOSWAP
		fi
	fi
}

zfs_generate()
{

	: ${SNAPSHOT_NAME:=$IMAGENAME}
	FINALIMAGE=${IMAGENAME}.img
	zroot="${ZFS_POOL_NAME}"
	zpool set bootfs=${zroot}/${ZFS_BEROOT_NAME}/${ZFS_BOOTFS_NAME} ${zroot}
	zpool set autoexpand=on ${zroot}
	zfs set canmount=noauto ${zroot}/${ZFS_BEROOT_NAME}/${ZFS_BOOTFS_NAME}

	SNAPSPEC="${zroot}@${SNAPSHOT_NAME}"

	msg "Creating snapshot(s) for image generation"
	zfs snapshot -r "$SNAPSPEC"

	## If we are creating a send stream, we need to do it before we export
	## the pool. Call the function to export the replication stream(s) here.
	## We do the inner case twice so we create a +full and a +be in one run.
	case "$1" in
	send)
		FINALIMAGE=${IMAGENAME}.*.zfs
		case "${MEDIAREMAINDER}" in
		*full*|send|zfs)
			_zfs_writereplicationstream "${SNAPSPEC}" "${IMAGENAME}.full.zfs"
			;;
		esac
		case "${MEDIAREMAINDER}" in
		*be*)
			BESNAPSPEC="${zroot}/${ZFS_BEROOT_NAME}/${ZFS_BOOTFS_NAME}@${SNAPSHOT_NAME}"
			_zfs_writereplicationstream "${BESNAPSPEC}" "${IMAGENAME}.be.zfs"
			;;
		esac
		;;
	esac

	## When generating a disk image, we need to export the pool first.
	zpool export ${zroot}
	zroot=
	/sbin/mdconfig -d -u ${md#md}
	md=

	case "$1" in
	raw)
		mv "${WRKDIR}/raw.img" "${OUTPUTDIR}/${FINALIMAGE}"
		;;
	gpt|zfs)
		espfilename=$(mktemp /tmp/efiboot.XXXXXX)
		zfsimage=${WRKDIR}/raw.img
		make_esp_file ${espfilename} 10 ${mnt}/boot/loader.efi

		if [ ${SWAPSIZE} != "0" ]; then
			SWAPCMD="-p freebsd-swap/swapspace::${SWAPSIZE}"
			if [ $SWAPBEFORE -eq 1 ]; then
				SWAPFIRST="$SWAPCMD"
			else
				SWAPLAST="$SWAPCMD"
			fi
		fi
		if [ "${arch}" == "amd64" ] || [ "${arch}" == "i386" ]; then
			pmbr="-b ${mnt}/boot/pmbr"
			gptboot="-p freebsd-boot:=${mnt}/boot/gptzfsboot:512k"
		fi
		mkimg -s gpt ${pmbr} \
			  -p efi:=${espfilename} \
			  ${gptboot} \
			  ${SWAPFIRST} \
			  -p freebsd-zfs:=${zfsimage} \
			  ${SWAPLAST} \
			  -o "${OUTPUTDIR}/${FINALIMAGE}"
		rm -rf ${espfilename}
		;;
	esac
}