Newer
Older
# VirtualBox VM Scripts - vm-start

Alberto LIVIO BECCARIA
committed
VERSION="18.5.8"
#include common code
. "${SRC_DIR}/settings/common"
###############################################################################
# Title
##############
title () {
echo -e "$APP_NAME by $AUTHOR"
echo "v.$VERSION"
echo
}
###############################################################################
###############################################################################
# Debug info
##################
debug() {
if [[ ${DEBUG} == 1 ]]; then
echo "Debug info:"
echo -e "\tvm_id = $VM_ID"
echo -e "\tfullscreen = $FULLSCREEN"
echo -e "\tno-init = $NO_INIT"
echo -e "\tno-run = $NO_RUN"
echo -e "\tdiscard-state = $DISCARD_STATE"

Alberto LIVIO BECCARIA
committed
echo -e "\thd-autoreset = $HD_AUTORESET"
}
###############################################################################
###############################################################################
# Usage
#######
usage () {
if [[ "$@" != "" ]]; then
echo >&2 "$@"
echo
fi

Alberto LIVIO BECCARIA
committed
echo "Usage: $0 <vm_id | vm_name> [--gui | --fullscreen] [--no-init] [--no-run] [--discard-state] [--debug] [--hd-autoreset=on|off]"
echo
print_vm_list
echo
exit 1
}
###############################################################################

Alberto LIVIO BECCARIA
committed
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
###############################################################################
# vbox_remove_media
###################
vbox_remove_media () {
if [[ "${DISK_IMAGE_1}" != "" ]]; then
echo "Removing disk 1..."
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 1 --device 0 --medium none
sleep 1
TMP_DIFF=`vboxmanage showhdinfo "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_1}" | grep -m1 "Child UUIDs:" | awk '{print $3}'`
if [ "${TMP_DIFF}" != "" ]; then
vboxmanage closemedium disk "${TMP_DIFF}" --delete
fi
vboxmanage closemedium disk "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_1}"
fi
if [[ "${DISK_IMAGE_2}" != "" ]]; then
echo "Removing disk 2..."
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 2 --device 0 --medium none
vboxmanage closemedium disk "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_2}"
fi
if [[ "${DISK_IMAGE_3}" != "" ]]; then
echo "Removing disk 3..."
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 3 --device 0 --medium none
vboxmanage closemedium disk "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_3}"
fi
if [[ "${DISK_IMAGE_4}" != "" ]]; then
echo "Removing disk 4..."
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 4 --device 0 --medium none
vboxmanage closemedium disk "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_4}"
fi
}
###############################################################################
###############################################################################
# vbox_attach_media
###################
vbox_attach_media () {
if [[ "${DISK_IMAGE_1}" != "" ]]; then
echo "Attaching disk 1..."
VBoxManage modifyhd "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_1}" --type immutable
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 1 --device 0 --type hdd --medium "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_1}"
fi
if [[ "${DISK_IMAGE_2}" != "" ]]; then
echo "Attaching disk 2..."
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 2 --device 0 --type hdd --medium "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_2}"
fi
if [[ "${DISK_IMAGE_3}" != "" ]]; then
echo "Attaching disk 3..."
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 3 --device 0 --type hdd --medium "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_3}"
fi
if [[ "${DISK_IMAGE_4}" != "" ]]; then
echo "Attaching disk 4..."
vboxmanage storageattach "${VM_NAME}" --storagectl "${STORAGE_CONTROLLER_NAME}" --port 4 --device 0 --type hdd --medium "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_4}"
fi
}
###############################################################################
###############################################################################
# vm_start_init
###############
vm_start_init () {
# create temporary folder

Alberto LIVIO BECCARIA
committed
if [ ! -d $USER_TMP_DIR ]; then
mkdir $USER_TMP_DIR
# remove files older than 12 hours

Alberto LIVIO BECCARIA
committed
#find ${USER_TMP_DIR} -name "*" -type f -mmin +720 -delete
# copy diff file (if needed)
if [ "${DIFF_DISK_FILE}" != "" ]; then
echo -n "Setting up differential disk... "

Alberto LIVIO BECCARIA
committed
cp "${TEMPLATE_DIR}/${DIFF_DISK_FILE}" "${USER_TMP_DIR}"
chmod 755 "${USER_TMP_DIR}/${DIFF_DISK_FILE}"
echo "OK"
fi
echo -n "Setting up media folder... "
# create local media folder and symlink if not existing
if [ ! -d "${SF_MEDIA_TARGET}" ]; then
mkdir -p "${SF_MEDIA_TARGET}"
fi
if [ ! -e "${SF_MEDIA_TARGET}/${USER}" ]; then
if [ ! -h "${SF_MEDIA_TARGET}/${USER}" ]; then
ln -s "${SYSTEM_MEDIA_FOLDER}" "${SF_MEDIA_TARGET}/${USER}"
fi
fi
echo "OK"
# create network interface
echo -n "Removing network interface: "
VBoxManage hostonlyif remove vboxnet0
echo -n "Creating network interface: "
VBoxManage hostonlyif create
# >/dev/null 2>&1
# restore snapshot if needed
if [ "${SNAPSHOT_NAME}" != "" ]; then
VBoxManage snapshot "${VM_NAME}" restore "${SNAPSHOT_NAME}"

Alberto LIVIO BECCARIA
committed
if [ "${HD_AUTORESET}" == "off" ]; then

Alberto LIVIO BECCARIA
committed
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
if [ "${DISK_IMAGE_1}" != "" ]; then
TMP_DIFF=`vboxmanage showhdinfo "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_1}" | grep -m1 "Child UUIDs:" | awk '{print $3}'`
if [ "${TMP_DIFF}" != "" ]; then
echo "Setting differential disk ${TMP_DIFF}: autoreset=${HD_AUTORESET}"
VBoxManage modifyhd ${TMP_DIFF} --autoreset ${HD_AUTORESET}
fi
fi
if [ "${DISK_IMAGE_2}" != "" ]; then
TMP_DIFF=`vboxmanage showhdinfo "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_2}" | grep -m1 "Child UUIDs:" | awk '{print $3}'`
if [ "${TMP_DIFF}" != "" ]; then
echo "Setting differential disk ${TMP_DIFF}: autoreset=${HD_AUTORESET}"
VBoxManage modifyhd ${TMP_DIFF} --autoreset ${HD_AUTORESET}
fi
fi
if [ "${DISK_IMAGE_3}" != "" ]; then
TMP_DIFF=`vboxmanage showhdinfo "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_3}" | grep -m1 "Child UUIDs:" | awk '{print $3}'`
if [ "${TMP_DIFF}" != "" ]; then
echo "Setting differential disk ${TMP_DIFF}: autoreset=${HD_AUTORESET}"
VBoxManage modifyhd ${TMP_DIFF} --autoreset ${HD_AUTORESET}
fi
fi
if [ "${DISK_IMAGE_4}" != "" ]; then
TMP_DIFF=`vboxmanage showhdinfo "${IMG_HOME}/${VM_ID}/${DISK_IMAGE_4}" | grep -m1 "Child UUIDs:" | awk '{print $3}'`
if [ "${TMP_DIFF}" != "" ]; then
echo "Setting differential disk ${TMP_DIFF}: autoreset=${HD_AUTORESET}"
VBoxManage modifyhd ${TMP_DIFF} --autoreset ${HD_AUTORESET}
fi
fi

Alberto LIVIO BECCARIA
committed
else
#if [ "${HD_AUTORESET}" == "on" ]; then
vbox_remove_media
vbox_attach_media
#fi

Alberto LIVIO BECCARIA
committed
fi
if [ "${LAST_HD_UUID}" != "" ]; then
TMP_DIFF=`vboxmanage showhdinfo "${LAST_HD_UUID}" | grep -m1 "Child UUIDs:" | awk '{print $3}'`
if [ "${TMP_DIFF}" != "" ]; then
if [ "${AUTORESET}" -eq 0 ]; then
echo "Differential disk ${TMP_DIFF}: autoreset=off"
VBoxManage modifyhd ${TMP_DIFF} --autoreset off
else
echo "Differential disk ${TMP_DIFF}: autoreset=on"
fi
if [ "${LAST_HD2_UUID}" != "" ]; then
TMP_DIFF=`vboxmanage showhdinfo "${LAST_HD2_UUID}" | grep -m1 "Child UUIDs:" | awk '{print $3}'`
if [ "${TMP_DIFF}" != "" ]; then
if [ "${AUTORESET}" -eq 0 ]; then
echo "Differential disk ${TMP_DIFF}: autoreset=off"
VBoxManage modifyhd ${TMP_DIFF} --autoreset off
else
echo "Differential disk ${TMP_DIFF}: autoreset=on"
fi
###############################################################################
###############################################################################
# discard_state
###############

Alberto LIVIO BECCARIA
committed
vm_discard_state() {
echo -n "Discard state... "

Alberto LIVIO BECCARIA
committed
echo "OK."
}
###############################################################################

Alberto LIVIO BECCARIA
committed
###############################################################################
# vm_start_run
##############
vm_start_run() {
if [[ ${FULLSCREEN} == 1 ]]; then
# VBoxManage has no option for fullscreen mode, use VBoxSDL (no menu available in fullscreen)
# VBoxSDL --startvm "${VM_NAME}" --fullscreen
# options below could be useful in setting the fullscreen resolution, when known
# --fullscreenresize --fixedmode 1152 864 32
#
# the following command can also be used to start the VM in fullscreen (GUI mode)
/usr/bin/VirtualBox --startvm "${VM_NAME}" --fullscreen
else
VBoxManage startvm "${VM_NAME}"
fi
###############################################################################
# print title
title
###############################################################################
# Parse arguments
#################
[[ "$#" -ge 1 ]] || usage "ERROR: one argument required ($# provided)."
VM_ID=""
DEBUG=0
FULLSCREEN_CL=-1
FULLSCREEN=0
NO_RUN=0
NO_INIT=0
DISCARD_STATE=0

Alberto LIVIO BECCARIA
committed
HD_AUTORESET=on
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
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case ${PARAM} in
-h | --help)
usage ""
exit 0
;;
-g | --gui)
FULLSCREEN_CL=0
shift
;;
-f | --fullscreen)
FULLSCREEN_CL=1
shift
;;
-i |--no-init)
NO_INIT=1
shift
;;
-r |--no-run)
NO_RUN=1
shift
;;
-s | --discard-state)
DISCARD_STATE=1
shift
;;
-d | --debug)
DEBUG=1
shift
;;

Alberto LIVIO BECCARIA
committed
-a | --hd-autoreset)
HD_AUTORESET=$VALUE
shift
shift
;;
*)
if [[ "${VM_ID}" == "" ]]; then
VM_ID=${PARAM}
shift
else
usage "ERROR: unknown argument \"${PARAM}\"."
exit 1
fi
;;
esac
done
###############################################################################
# include global settings
. "${SRC_DIR}/settings/settings"
# VM_ID lookup in settings files by ID or NAME
if [ ! -f "$SRC_DIR/settings/${VM_ID}.settings" ]; then
result=`get_vm_id_by_name "${VM_ID}"`
VM_ID=$result
if [ ! -f "$SRC_DIR/settings/${VM_ID}.settings" ]; then
usage "ERROR: wrong vm_id or name."
# include VM settings
. "${SRC_DIR}/settings/${VM_ID}.settings"
# override VM fullscreen setting if set on command line
if [[ ${FULLSCREEN_CL} != -1 ]]; then
FULLSCREEN=${FULLSCREEN_CL}
fi
# print debug info if needed
debug
VBoxManage showvminfo "${VM_NAME}" &> /dev/null
[ "$?" -eq 0 ] || die "ERROR: VM not configured. Run 'vm-setup ${VM_ID}' and try again."

Alberto LIVIO BECCARIA
committed
# check if snapstot folder is the one defined by us
current_snapshot_folder=`VBoxManage showvminfo "${VM_NAME}" | grep -m1 "Snapshot folder" | awk '{print $3}'`
if [ "$current_snapshot_folder" != "${USER_TMP_DIR}" ]; then
# non si può cambiare al volo se ci sono degli snapshot...
#VBoxManage modifyvm "${VM_NAME}" --snapshotfolder "${USER_TMP_DIR}"
echo "Configurazione obsoleta. Per favore riesegui:"
echo "${SRC_DIR}/vm-setup ${VM_ID}"
exit 1
fi
if [[ ${NO_INIT} == 0 ]]; then
vm_start_init
fi

Alberto LIVIO BECCARIA
committed
# --discard-state?
if [[ ${DISCARD_STATE} == 1 ]]; then

Alberto LIVIO BECCARIA
committed
vm_discard_state
fi
if [[ ${NO_RUN} == 0 ]]; then
vm_start_run
fi