|
#!/bin/bash |
|
|
|
# mkvmdkdev: Create simple VirtualBox VMDK disks from physical block devices |
|
# Copyright (c) 2017 John Lettman |
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
# of this software and associated documentation files (the "Software"), to deal |
|
# in the Software without restriction, including without limitation the rights |
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
# copies of the Software, and to permit persons to whom the Software is |
|
# furnished to do so, subject to the following conditions: |
|
|
|
# The above copyright notice and this permission notice shall be included in all |
|
# copies or substantial portions of the Software. |
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
# SOFTWARE. |
|
|
|
############################## |
|
# Usage and help information # |
|
############################## |
|
|
|
read -d "" help <<- EOH |
|
Usage: $(basename $0) device [output] |
|
Create simple VirtualBox VMDK disks from physical block devices |
|
|
|
device the target raw block device |
|
output the output path of the respective VMDK file |
|
-h, --help show this message |
|
|
|
Exit codes: |
|
09) missing command |
|
1x) device error |
|
10) device does not exist |
|
11) device is not a block device |
|
2x) output error |
|
20) output directory creation failure |
|
3x) create error |
|
30) creation failure |
|
|
|
Report bugs to: John Lettman <the@johnlettman.com> |
|
EOH |
|
|
|
# Print the usage and help information if the arguments are invalid or the |
|
# help, -h, or --help argument is provided |
|
if [ $# -lt 1 ] || [[ $1 == "help" ]] || [[ $1 == "--help" ]] || [[ $1 == "-h" ]]; then |
|
echo "$help" |
|
exit 0 |
|
fi |
|
|
|
#################### |
|
# Argument parsing # |
|
#################### |
|
|
|
# !!! NOTE !!! |
|
# Argument parsing must remain above permission elevation to retain possible |
|
# automatically generated paths |
|
|
|
# Parse arguments into variables |
|
device=$1 |
|
output=${2-${HOME}/Disks/Virtual/raw-${device##*/}} |
|
vboxmanage=${VBOXMANAGE:=VBoxManage} |
|
|
|
# Ensure the output path retains the required VMDK extension |
|
[[ $output != *.vmdk ]] && output="${output}.vmdk" |
|
|
|
# Automatically elevate permissions when not currently operating under root |
|
if [ $EUID -ne 0 ]; then |
|
echo "mkvmdkdev: not root: elevating permissions" |
|
sudo -E $0 $device $output |
|
exit $? |
|
fi |
|
|
|
################### |
|
# Sanity checking # |
|
################### |
|
|
|
# Ensure VBoxManage is installed and accessible |
|
command -v $vboxmanage >/dev/null 2>&1 || { |
|
echo "mkvmdkdev: error: missing VBoxManage command: $vboxmanage" >&2 |
|
echo "mkvmdkdev: is VirtualBox installed?" >&2 |
|
exit 9 |
|
} |
|
|
|
# Ensure the target device exists; issue an error otherwise |
|
if [ ! -e $device ]; then |
|
echo "mkvmdkdev: error: device does not exist at: $device" >&2 |
|
exit 10 |
|
fi |
|
|
|
# Ensure the target device is a block device; issue an error otherwise |
|
if [ ! -b $device ]; then |
|
echo "mkvmdkdev: error: device is not a block device at: $device" >&2 |
|
exit 11 |
|
fi |
|
|
|
# Obtain the directory of the output path |
|
output_dir=$(dirname $output) |
|
|
|
# Ensure the directory for the output path exists |
|
if [ ! -d $output_dir ]; then |
|
echo "mkvmdkdev: creating output directory: $output_dir" |
|
|
|
mkdir -p $output_dir |
|
result=$? |
|
|
|
# Check the result for success; issue an error otherwise |
|
if [ $result -ne 0 ]; then |
|
echo "mkvmdkdev: error: output directory creation failure at: $output_dir" >&2 |
|
echo "mkvmdkdev: code: $result" >&2 |
|
exit 20 |
|
fi |
|
fi |
|
|
|
######## |
|
# Main # |
|
######## |
|
|
|
# Output parsed arguments |
|
echo "mkvmdkdev: device: $device" |
|
echo "mkvmdkdev: output: $output" |
|
|
|
# Execute VBoxManage to create the VMDK |
|
$vboxmanage internalcommands createrawvmdk -rawdisk $device -filename $output |
|
result=$? |
|
|
|
# Check the result for success; issue an error otherwise |
|
if [ $result -ne 0 ]; then |
|
echo "mkvmdkdev: error: creation failure" >&2 |
|
echo "mkvmdkdev: code: $result" >&2 |
|
exit 30 |
|
fi |
|
|
|
exit 0 |