Skip to content

Instantly share code, notes, and snippets.

@icyleaf
Last active February 8, 2020 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icyleaf/e28f267de77236541d7ecea4ff599574 to your computer and use it in GitHub Desktop.
Save icyleaf/e28f267de77236541d7ecea4ff599574 to your computer and use it in GitHub Desktop.
Convert img file to vbox format
#!/usr/bin/env bash
#
# icyleaf
# 2020-02-03
#
VM_NAME=$1
IMG_FILE=$2
IMG_PATH=`dirname $IMG_FILE`
IMG_NAME=`basename $IMG_FILE`
IMG_NAME="${IMG_NAME%.*}"
VDI_FILE="${IMG_PATH}/${IMG_NAME}.vdi"
echo "============ DEBUG ==========="
echo $VM_NAME
echo $IMG_FILE
echo $VDI_FILE
echo "============ DEBUG ==========="
VBoxManage unregistervm ${VM_NAME} --delete
rm ${VDI_FILE}
echo "Creating VM ... ${VM_NAME}"
VBoxManage createvm --name ${VM_NAME} \
--ostype Linux_64 \
--register
echo ""
echo "Configuring VM ... ${VM_NAME}"
VBoxManage modifyvm ${VM_NAME} --boot1 disk --cpus 2 --memory 128 --vram 12
VBoxManage modifyvm ${VM_NAME} --nic1 bridged --bridgeadapter1 en0
VBoxManage modifyvm ${VM_NAME} --nic2 nat
VBoxManage setextradata ${VM_NAME} GUI/ScaleFactor 3
# hdd
VBoxManage storagectl ${VM_NAME} --name "SATA Controller" --add sata --bootable on
VBoxManage convertfromraw --format VDI ${IMG_FILE} ${VDI_FILE}
VBoxManage storageattach ${VM_NAME} --storagectl "SATA Controller" \
--port 0 --device 0 --type hdd --medium ${VDI_FILE}
echo ""
echo "Showing VM ... ${VM_NAME}"
VBoxManage showvminfo ${VM_NAME}
echo
echo "Runing VM ... ${VM_NAME}"
VBoxManage startvm ${VM_NAME}
# echo
# echo "Delete VM ... #{VM_NAME}"
# VBoxManage unregistervm ${VM_NAME} --delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment