1#!/bin/sh
2
3# Copyright (c) 2015, pr1ntf (Trent Thompson) All rights reserved.
4# Copyright (c) 2016, Justin D Holcomb All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# * Redistributions of source code must retain the above copyright notice, this
10#   list of conditions and the following disclaimer.
11#
12# * Redistributions in binary form must reproduce the above copyright notice,
13#   this list of conditions and the following disclaimer in the documentation
14#   and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27# Install chyves dataset to a pool
28__dataset_install() {
29	local _pool="$1"
30	local _creation_stamp="Dataset originally created on $( date ) by chyves $_VERSION_LONG using dataset version $_VERSION_DATASET"
31	echo "Setting up chyves on $_pool..."
32	zfs create -o mountpoint=/chyves/$_pool $_pool/chyves
33	zfs create $_pool/chyves/.config
34	__freenas_create_symbolic_link   # This will only execute an action if on FreeNAS system.
35	touch /chyves/$_pool/.config/pool.cfg
36	__write_property_value_to_config_file "manual" "creation"        "$_creation_stamp" "/chyves/$_pool/.config/pool.cfg"
37	__write_property_value_to_config_file "manual" "dataset_version" "$_VERSION_DATASET" "/chyves/$_pool/.config/pool.cfg"
38	zfs create $_pool/chyves/guests
39
40	# A chyves pool is already setup, so setup as secondary pool.
41	if [ -n "$_PRIMARY_POOL" ]; then
42		echo "Setting pool up in secondary dataset role..."
43		__write_property_value_to_config_file "manual" "dataset_role" "secondary" "/chyves/$_pool/.config/pool.cfg"
44
45	# chyves is not set up yet, setup primary pool.
46	else
47		_PRIMARY_POOL=$_pool
48		_RESTRICT_NEW_PROPERTY_NAMES="master-override"
49		_GLOBAL_CONFIG_FILE="/chyves/$_PRIMARY_POOL/.config/global.cfg"
50		_DEFAULT_CONFIG_FILE="/chyves/$_PRIMARY_POOL/guests/.defaults"
51		__write_property_value_to_config_file "manual" "dataset_role" "primary" "/chyves/$_PRIMARY_POOL/.config/pool.cfg"
52		touch "$_GLOBAL_CONFIG_FILE"
53		touch "$_DEFAULT_CONFIG_FILE"
54		echo "Creating compressed log dataset"
55		zfs create -o compression=on $_PRIMARY_POOL/chyves/logs
56		_LOG_TO_FILE="YES"
57		__log 2 "Creating ISO and Firmware resource datasets..."
58		zfs create $_PRIMARY_POOL/chyves/Firmware
59		zfs create $_PRIMARY_POOL/chyves/ISO
60
61		# Hosts on FreeBSD 10.3 or above need to use the "-S" bhyve wire memory flag otherwise fun stuff happens at memory starvation.
62		if [ "$_OS_VERSION_DATE" -lt "1003000" ]; then
63			local _bhyve_args="-A -H -P"
64		else
65			local _bhyve_args="-A -H -P -S"
66		fi
67
68		__log 2 "Creating guest defaults dataset and properties..."
69		__write_property_value_to_config_file "defaults" "bargs"                                "$_bhyve_args"
70		__write_property_value_to_config_file "defaults" "bhyveload_flags"                      ""
71		__write_property_value_to_config_file "defaults" "bhyve_disk_type"                      "ahci-hd"
72		__write_property_value_to_config_file "defaults" "bhyve_net_type"                       "virtio-net"
73		__write_property_value_to_config_file "defaults" "bridge"                               "bridge0"
74		__write_property_value_to_config_file "defaults" "cpu"                                  "1"
75		__write_property_value_to_config_file "defaults" "disk_volmode"                         "dev"
76		__write_property_value_to_config_file "defaults" "disk_volblocksize"                    "512"
77		__write_property_value_to_config_file "defaults" "disk_dedup"                           "inherit"
78		__write_property_value_to_config_file "defaults" "disk_compression"                     "inherit"
79		__write_property_value_to_config_file "defaults" "disk_primarycache"                    "inherit"
80		__write_property_value_to_config_file "defaults" "disk_secondarycache"                  "inherit"
81		__write_property_value_to_config_file "defaults" "loader"                               "bhyveload"
82		__write_property_value_to_config_file "defaults" "os"                                   "default"
83		__write_property_value_to_config_file "defaults" "ram"                                  "256M"
84		__write_property_value_to_config_file "defaults" "rcboot"                               "0"
85		__write_property_value_to_config_file "defaults" "revert_to_snapshot"                   ""
86		__write_property_value_to_config_file "defaults" "revert_to_snapshot_method"            "off"
87		__write_property_value_to_config_file "defaults" "size"                                 "8G"
88		__write_property_value_to_config_file "defaults" "template"                             "no"
89		__write_property_value_to_config_file "defaults" "uefi_console_output"                  "serial"
90		__write_property_value_to_config_file "defaults" "uefi_firmware"                        "-"
91		__write_property_value_to_config_file "defaults" "uefi_vnc_client"                      "print"
92		__write_property_value_to_config_file "defaults" "uefi_vnc_ip"                          "0.0.0.0"
93		__write_property_value_to_config_file "defaults" "uefi_vnc_mouse_type"                  "ps2"
94		__write_property_value_to_config_file "defaults" "uefi_vnc_pause_until_client_connect"  "no"
95		__write_property_value_to_config_file "defaults" "uefi_vnc_res"                         "800x600"
96
97		__log 2 "Setting up global properties on primary pool..."
98		__write_property_value_to_config_file "global" "auto_load_kernel_mods"                "yes"
99		__write_property_value_to_config_file "global" "check_for_updates"                    "weekly"
100		__write_property_value_to_config_file "global" "check_for_updates_timeout_seconds"    "2"
101		__write_property_value_to_config_file "global" "check_for_updates_last_check"         "$_DATE_YMD"
102		__write_property_value_to_config_file "global" "check_for_updates_last_check_status"  "0"
103		__write_property_value_to_config_file "global" "check_for_updates_unique_id"          "$_UUID_GENERAL_USE"
104		__write_property_value_to_config_file "global" "console_start_offset"                 "50"
105		__write_property_value_to_config_file "global" "consolidate_bhyve_pci_devices"        "no"
106		__write_property_value_to_config_file "global" "default_clone_flag"                   "-iu"
107		__write_property_value_to_config_file "global" "default_info_flags"                   "-h"
108		__write_property_value_to_config_file "global" "default_list_flags"                   "-skdp"
109		__write_property_value_to_config_file "global" "dev_mode"                             "off"
110		__write_property_value_to_config_file "global" "eject_iso_on_n_reboot"                "1"
111		__write_property_value_to_config_file "global" "network_design_mode"                  "auto"
112		__write_property_value_to_config_file "global" "log_mode"                             "dual"
113		__write_property_value_to_config_file "global" "log_to_file"                          "yes"
114		__write_property_value_to_config_file "global" "restrict_new_property_names"          "on"
115		__write_property_value_to_config_file "global" "stdout_level"                         "2"
116		__write_property_value_to_config_file "global" "tap_start_offset"                     "50"
117		__write_property_value_to_config_file "global" "tap_up_by_default"                    "yes"
118		__write_property_value_to_config_file "global" "vlan_iface_base_name"                 "vlan"
119		__write_property_value_to_config_file "global" "uefi_vnc_port_start_offset"           "5900"
120
121		__log 2 "Saving global settings as a snapshot"
122		zfs snapshot $_PRIMARY_POOL/chyves/.config@${_DATE_YMD}-chyves-install-inital-settings-version-${_VERSION}
123
124		__log 2 "Creating null.iso for UEFI use..."
125		touch /tmp/null.iso
126		if [ -e /tmp/null.iso ]; then
127			__log 2 "Importing null.iso into ISO resources..."
128			__resource_import iso /tmp/null.iso
129		fi
130
131		__log 2 "Done setting up primary pool: $_pool"
132	fi
133}
134
135# Incrementally upgrade a datasets from version to version
136__dataset_upgrade() {
137	local _pool="$1"
138
139	__dataset_upgrade_lset_version() {
140		_DATASET_pool_version_int="$( __return_property_value_from_config_file "manual" "dataset_version" "/chyves/$_pool/.config/pool.cfg" )"
141	}
142
143	# Seed value
144	__dataset_upgrade_lset_version
145
146	# Prepare to add new paramaters
147	_RESTRICT_NEW_PROPERTY_NAMES=master-override
148
149	# Added defaults bhyve_net_type
150	if [ "$_DATASET_pool_version_int" = "0001" ]; then
151		__log 1 "Upgrading dataset to version 0002"
152		[ "$_PRIMARY_POOL" = "$_pool" ] && __write_property_value_to_config_file "defaults" "bhyve_net_type" "virtio-net"
153		__load_guest_default_parameters
154		__write_property_value_to_config_file "manual" "dataset_version" "0002" "/chyves/$_pool/.config/pool.cfg"
155		__dataset_upgrade_lset_version
156	fi
157
158	# Updated to include new 'revert_to_snapshot' and 'revert_to_snapshot_method' default properties. Also update default 'bargs' to include '-S' flag on FreeBSD 10.3+ hosts.
159	if [ "$_DATASET_pool_version_int" = "0002" ]; then
160		__log 1 "Upgrading dataset to version 0003"
161
162		# Hosts on FreeBSD 10.3 or are recommended to use the "-S" bhyve wire memory flag otherwise fun stuff happens at memory starvation.
163		if [ "$_OS_VERSION_DATE" -ge "1003000" ]; then
164			__load_guest_default_parameters
165
166			# Check if the initial default value is the same, otherwise do not change.
167			if [ "$_GDP_bargs" = "-A -H -P" ] && [ "$_PRIMARY_POOL" = "$_pool" ]; then
168				__log 1 "Updating default 'bargs' value. "
169				__write_property_value_to_config_file "defaults" "bargs" "-A -H -P -S"
170			else
171				__log 1 "It is recommended to include the '-S' bhyve arguement in the 'bargs' property for guests and defaults."
172			fi
173		fi
174
175		[ "$_PRIMARY_POOL" = "$_pool" ] && __write_property_value_to_config_file "defaults" "revert_to_snapshot" ""
176		[ "$_PRIMARY_POOL" = "$_pool" ] && __write_property_value_to_config_file "defaults" "revert_to_snapshot_method" "off"
177		__write_property_value_to_config_file "manual" "dataset_version" "0003" "/chyves/$_pool/.config/pool.cfg"
178		__dataset_upgrade_lset_version
179	fi
180
181	# Updated to include new 'eject_iso_on_n_reboot' global properties.
182	if [ "$_DATASET_pool_version_int" = "0003" ]; then
183		__log 1 "Upgrading dataset to version 0004"
184		[ "$_PRIMARY_POOL" = "$_pool" ] && __write_property_value_to_config_file "global" "eject_iso_on_n_reboot" "1"
185		__write_property_value_to_config_file "manual" "dataset_version" "0004" "/chyves/$_pool/.config/pool.cfg"
186		__dataset_upgrade_lset_version
187	fi
188
189	# Updated to include new 'bhyveload_flags' default properties.
190	if [ "$_DATASET_pool_version_int" = "0004" ]; then
191		__log 1 "Upgrading dataset to version 0005"
192		[ "$_PRIMARY_POOL" = "$_pool" ] && __write_property_value_to_config_file "defaults" "bhyveload_flags" ""
193		[ "$_PRIMARY_POOL" = "$_pool" ] && __write_property_value_to_config_file "defaults" "bhyve_disk_type" "ahci-hd"
194		__write_property_value_to_config_file "manual" "dataset_version" "0005" "/chyves/$_pool/.config/pool.cfg"
195		__dataset_upgrade_lset_version
196	fi
197
198	# Template for third change.
199	#if [ "$_DATASET_pool_version_int" = "0003" ]; then
200	#	__log 1 "Upgrading dataset to version 0004"
201	#	<ACTION>
202	#	__write_property_value_to_config_file "manual" "dataset_version" "0004" "/chyves/$_pool/.config/pool.cfg"
203	#	__dataset_upgrade_lset_version
204	#fi
205
206	# Upgrade guests on pool to latest chyves guest version if the primary pool is on the latest version.
207	_PRIMARY_pool_version_int="$( __return_property_value_from_config_file "manual" "dataset_version" "/chyves/$_PRIMARY_POOL/.config/pool.cfg" )"
208	if [ "$_PRIMARY_pool_version_int" = "$_VERSION_DATASET" ]; then
209		__guest_upgrade_chyves_guest_version "$( __return_new_line_delimit_as_comma_string $( __return_guest_list $_pool ) )"
210	fi
211}
212