summaryrefslogtreecommitdiffstats
path: root/src/spdk/scripts/vagrant/create_vbox.sh
blob: 71fd46d12915f3c8b9d2e73a0d82fd7e97f5a783 (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
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
#!/usr/bin/env bash

# create_vbox.sh
#
# Creates a virtual box with vagrant in the $PWD.
#
# This script creates a subdirectory called $PWD/<distro> and copies the Vagrantfile
# into that directory before running 'vagrant up'

set -e

VAGRANT_TARGET="$PWD"

DIR="$(cd "$(dirname $0)" && pwd)"
SPDK_DIR="$(cd "${DIR}/../../" && pwd)"

# The command line help
display_help() {
	echo
	echo " Usage: ${0##*/} [-b nvme-backing-file] [-n <num-cpus>] [-s <ram-size>] [-x <http-proxy>] [-hvrldcu] <distro>"
	echo
	echo "  distro = <centos7 | centos8| ubuntu1604 | ubuntu1804 | fedora30 |"
	echo "            fedora31 | fedora32 | freebsd11 | freebsd12 | arch | clearlinux>"
	echo
	echo "  -s <ram-size> in kb             Default: ${SPDK_VAGRANT_VMRAM}"
	echo "  -n <num-cpus> 1 to 4            Default: ${SPDK_VAGRANT_VMCPU}"
	echo "  -x <http-proxy>                 Default: \"${SPDK_VAGRANT_HTTP_PROXY}\""
	echo "  -p <provider>                   \"libvirt\" or \"virtualbox\". Default: ${SPDK_VAGRANT_PROVIDER}"
	echo "  -b <nvme-backing-file>          Emulated NVMe options."
	echo "                                  If no -b option is specified then this option defaults to emulating single"
	echo "                                  NVMe with 1 namespace and assumes path: /var/lib/libvirt/images/nvme_disk.img"
	echo "                                  -b option can be used multiple times for attaching multiple files to the VM"
	echo "                                  Parameters for -b option: <path>,<type>,<namespaces>,<cmb>"
	echo "                                  Available types: nvme, ocssd."
	echo "  -c                              Create all above disk, default 0"
	echo "  -H                              Use hugepages for allocating VM memory. Only for libvirt provider. Default: false."
	echo "  -u                              Use password authentication to the VM instead of SSH keys."
	echo "  -l                              Use a local copy of spdk, don't try to rsync from the host."
	echo "  -a                              Copy spdk/autorun.sh artifacts from VM to host system."
	echo "  -d                              Deploy a test vm by provisioning all prerequisites for spdk autotest"
	echo "  --qemu-emulator=<path>          Path to custom QEMU binary. Only works with libvirt provider"
	echo "  --vagrantfiles-dir=<path>       Destination directory to put Vagrantfile into."
	echo "  --package-box                   Install all dependencies for SPDK and create a local vagrant box version."
	echo "  -r dry-run"
	echo "  -h help"
	echo "  -v verbose"
	echo
	echo " Examples:"
	echo
	echo "  $0 -x http://user:password@host:port fedora30"
	echo "  $0 -s 2048 -n 2 ubuntu16"
	echo "  $0 -rv freebsd"
	echo "  $0 fedora30"
	echo "  $0 -b /var/lib/libvirt/images/nvme1.img,nvme,1 fedora30"
	echo "  $0 -b /var/lib/libvirt/images/ocssd.img,ocssd fedora30"
	echo "  $0 -b /var/lib/libvirt/images/nvme5.img,nvme,5 -b /var/lib/libvirt/images/ocssd.img,ocssd fedora30"
	echo
}

# Set up vagrant proxy. Assumes git-bash on Windows
# https://stackoverflow.com/questions/19872591/how-to-use-vagrant-in-a-proxy-environment
SPDK_VAGRANT_HTTP_PROXY=""

VERBOSE=0
HELP=0
COPY_SPDK_DIR=1
COPY_SPDK_ARTIFACTS=0
DRY_RUN=0
DEPLOY_TEST_VM=0
SPDK_VAGRANT_DISTRO="distro"
SPDK_VAGRANT_VMCPU=4
SPDK_VAGRANT_VMRAM=4096
SPDK_VAGRANT_PROVIDER="virtualbox"
SPDK_QEMU_EMULATOR=""
OPTIND=1
NVME_DISKS_TYPE=""
NVME_DISKS_NAMESPACES=""
NVME_FILE=""
NVME_AUTO_CREATE=0
VAGRANTFILE_DIR=""
VAGRANT_PASSWORD_AUTH=0
VAGRANT_PACKAGE_BOX=0
VAGRANT_HUGE_MEM=0

while getopts ":b:n:s:x:p:u:vcraldHh-:" opt; do
	case "${opt}" in
		-)
			case "${OPTARG}" in
				package-box) VAGRANT_PACKAGE_BOX=1 ;;
				qemu-emulator=*) SPDK_QEMU_EMULATOR="${OPTARG#*=}" ;;
				vagrantfiles-dir=*) VAGRANTFILE_DIR="${OPTARG#*=}" ;;
				*) echo "Invalid argument '$OPTARG'" ;;
			esac
			;;
		x)
			http_proxy=$OPTARG
			https_proxy=$http_proxy
			SPDK_VAGRANT_HTTP_PROXY="${http_proxy}"
			;;
		n)
			SPDK_VAGRANT_VMCPU=$OPTARG
			;;
		s)
			SPDK_VAGRANT_VMRAM=$OPTARG
			;;
		p)
			SPDK_VAGRANT_PROVIDER=$OPTARG
			;;
		v)
			VERBOSE=1
			;;
		c)
			NVME_AUTO_CREATE=1
			;;
		r)
			DRY_RUN=1
			;;
		h)
			display_help >&2
			exit 0
			;;
		a)
			COPY_SPDK_ARTIFACTS=1
			;;
		l)
			COPY_SPDK_DIR=0
			;;
		d)
			DEPLOY_TEST_VM=1
			;;
		b)
			NVME_FILE+="${OPTARG#*=} "
			;;
		u)
			VAGRANT_PASSWORD_AUTH=1
			;;
		H)
			VAGRANT_HUGE_MEM=1
			;;
		*)
			echo "  Invalid argument: -$OPTARG" >&2
			echo "  Try: \"$0 -h\"" >&2
			exit 1
			;;
	esac
done

shift "$((OPTIND - 1))" # Discard the options and sentinel --

SPDK_VAGRANT_DISTRO="$*"

case "${SPDK_VAGRANT_DISTRO}" in
	centos7)
		export SPDK_VAGRANT_DISTRO
		;;
	centos8)
		export SPDK_VAGRANT_DISTRO
		;;
	ubuntu1604)
		export SPDK_VAGRANT_DISTRO
		;;
	ubuntu1804)
		export SPDK_VAGRANT_DISTRO
		;;
	fedora30)
		export SPDK_VAGRANT_DISTRO
		;;
	fedora31)
		export SPDK_VAGRANT_DISTRO
		;;
	fedora32)
		export SPDK_VAGRANT_DISTRO
		;;
	freebsd11)
		export SPDK_VAGRANT_DISTRO
		;;
	freebsd12)
		export SPDK_VAGRANT_DISTRO
		;;
	arch)
		export SPDK_VAGRANT_DISTRO
		;;
	clearlinux)
		export SPDK_VAGRANT_DISTRO
		;;
	*)
		echo "  Invalid argument \"${SPDK_VAGRANT_DISTRO}\""
		echo "  Try: \"$0 -h\"" >&2
		exit 1
		;;
esac

if ! echo "$SPDK_VAGRANT_DISTRO" | grep -q fedora && [ $DEPLOY_TEST_VM -eq 1 ]; then
	echo "Warning: Test machine deployment is only available on fedora distros. Disabling it for this build"
	DEPLOY_TEST_VM=0
fi
if [ -z "$NVME_FILE" ]; then
	TMP="/var/lib/libvirt/images/nvme_disk.img"
	NVME_DISKS_TYPE="nvme"
else
	TMP=""
	for args in $NVME_FILE; do
		while IFS=, read -r path type namespace cmb; do
			TMP+="$path,"
			if [ -z "$type" ]; then
				type="nvme"
			fi
			if [[ -n $cmb ]]; then
				NVME_CMB=${NVME_CMB:+$NVME_CMB,}true
			fi
			NVME_DISKS_TYPE+="$type,"
			if [ -z "$namespace" ] && [ -n "$SPDK_QEMU_EMULATOR" ]; then
				namespace="1"
			fi
			NVME_DISKS_NAMESPACES+="$namespace,"
			if [ ${NVME_AUTO_CREATE} = 1 ]; then
				$SPDK_DIR/scripts/vagrant/create_nvme_img.sh -t $type -n $path
			fi
		done <<< $args
	done
fi
NVME_FILE=$TMP

if [ ${VERBOSE} = 1 ]; then
	echo
	echo DIR=${DIR}
	echo SPDK_DIR=${SPDK_DIR}
	echo VAGRANT_TARGET=${VAGRANT_TARGET}
	echo HELP=$HELP
	echo DRY_RUN=$DRY_RUN
	echo NVME_FILE=$NVME_FILE
	echo NVME_DISKS_TYPE=$NVME_DISKS_TYPE
	echo NVME_AUTO_CREATE=$NVME_AUTO_CREATE
	echo NVME_DISKS_NAMESPACES=$NVME_DISKS_NAMESPACES
	echo NVME_CMB=$NVME_CMB
	echo SPDK_VAGRANT_DISTRO=$SPDK_VAGRANT_DISTRO
	echo SPDK_VAGRANT_VMCPU=$SPDK_VAGRANT_VMCPU
	echo SPDK_VAGRANT_VMRAM=$SPDK_VAGRANT_VMRAM
	echo SPDK_VAGRANT_PROVIDER=$SPDK_VAGRANT_PROVIDER
	echo SPDK_VAGRANT_HTTP_PROXY=$SPDK_VAGRANT_HTTP_PROXY
	echo SPDK_QEMU_EMULATOR=$SPDK_QEMU_EMULATOR
	echo VAGRANT_PACKAGE_BOX=$VAGRANT_PACKAGE_BOX
	echo
fi

export SPDK_VAGRANT_HTTP_PROXY
export SPDK_VAGRANT_VMCPU
export SPDK_VAGRANT_VMRAM
export SPDK_DIR
export COPY_SPDK_DIR
export COPY_SPDK_ARTIFACTS
export DEPLOY_TEST_VM
export NVME_CMB
export NVME_DISKS_TYPE
export NVME_DISKS_NAMESPACES
export NVME_FILE
export VAGRANT_PASSWORD_AUTH
export VAGRANT_HUGE_MEM

if [ -n "$SPDK_VAGRANT_PROVIDER" ]; then
	provider="--provider=${SPDK_VAGRANT_PROVIDER}"
fi

if [ -n "$SPDK_VAGRANT_PROVIDER" ]; then
	export SPDK_VAGRANT_PROVIDER
fi

if [ -n "$SPDK_QEMU_EMULATOR" ] && [ "$SPDK_VAGRANT_PROVIDER" == "libvirt" ]; then
	export SPDK_QEMU_EMULATOR
fi

if [ ${DRY_RUN} = 1 ]; then
	echo "Environemnt Variables"
	printenv SPDK_VAGRANT_DISTRO
	printenv SPDK_VAGRANT_VMRAM
	printenv SPDK_VAGRANT_VMCPU
	printenv SPDK_VAGRANT_PROVIDER
	printenv SPDK_VAGRANT_HTTP_PROXY
	printenv SPDK_QEMU_EMULATOR
	printenv NVME_DISKS_TYPE
	printenv NVME_AUTO_CREATE
	printenv NVME_DISKS_NAMESPACES
	printenv NVME_FILE
	printenv SPDK_DIR
	printenv VAGRANT_HUGE_MEM
fi
if [ -z "$VAGRANTFILE_DIR" ]; then
	VAGRANTFILE_DIR="${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}-${SPDK_VAGRANT_PROVIDER}"
	export VAGRANTFILE_DIR
fi

if [ -d "${VAGRANTFILE_DIR}" ]; then
	echo "Error: ${VAGRANTFILE_DIR} already exists!"
	exit 1
fi

if [ ${DRY_RUN} != 1 ]; then
	mkdir -vp "${VAGRANTFILE_DIR}"
	cp ${DIR}/Vagrantfile ${VAGRANTFILE_DIR}
	pushd "${VAGRANTFILE_DIR}"
	if [ -n "${http_proxy}" ]; then
		export http_proxy
		export https_proxy
		if vagrant plugin list | grep -q vagrant-proxyconf; then
			echo "vagrant-proxyconf already installed... skipping"
		else
			vagrant plugin install vagrant-proxyconf
		fi
		if echo "$SPDK_VAGRANT_DISTRO" | grep -q freebsd; then
			cat > ~/vagrant_pkg.conf << EOF
pkg_env: {
http_proxy: ${http_proxy}
}
EOF
		fi
	fi
	mkdir -p "${VAGRANTFILE_DIR}/output"
	vagrant up $provider
	if [ ${VAGRANT_PACKAGE_BOX} == 1 ]; then
		vagrant ssh -c 'sudo spdk_repo/spdk/scripts/vagrant/update.sh'
		vagrant halt
		vagrant package --output spdk_${SPDK_VAGRANT_DISTRO}.box
		vagrant box add spdk/${SPDK_VAGRANT_DISTRO} spdk_${SPDK_VAGRANT_DISTRO}.box \
			&& rm spdk_${SPDK_VAGRANT_DISTRO}.box
		vagrant destroy
	fi
	echo ""
	echo "  SUCCESS!"
	echo ""
	echo "  cd to ${VAGRANTFILE_DIR} and type \"vagrant ssh\" to use."
	echo "  Use vagrant \"suspend\" and vagrant \"resume\" to stop and start."
	echo "  Use vagrant \"destroy\" followed by \"rm -rf ${VAGRANTFILE_DIR}\" to destroy all trace of vm."
	echo ""
fi