[英]How to convert flat raw disk image to vmdk for virtualbox or vmplayer?
我有一些平面文件格式的舊 Linux 文件系統的舊圖像。 Bochs可以使用它們,但我需要使用Virtual Box運行它們。 Virtual Box 無法使用這種格式的圖像,因此我需要將這些圖像從平面文件轉換為 .vmdk 文件格式。 有沒有辦法做到這一點?
首先,安裝 QEMU。 在 Ubuntu 等基於 Debian 的發行版上,運行:
$ apt-get install qemu
然后運行以下命令:
$ qemu-img convert -O vmdk imagefile.dd vmdkname.vmdk
我假設平面磁盤映像是dd
樣式的映像。 轉換操作還處理許多其他格式。
有關qemu-img
命令的更多信息,請參閱輸出
$ qemu-img -h
由於問題提到了VirtualBox,這個目前有效:
VBoxManage convertfromraw imagefile.dd vmdkname.vmdk --format VMDK
不帶參數運行它以獲得一些有趣的細節(特別是--variant
標志):
VBoxManage convertfromraw
回答 TJJ:但是是否也可以在不復制整個文件的情況下執行此操作? 所以,只是為了以某種方式創建一個額外的 vmdk-metafile,它引用了原始 dd-image。
是的,這是可能的。 以下是在 VirtualBox 中使用平面磁盤映像的方法:
首先,您以通常的方式使用 dd 創建一個圖像:
dd bs=512 count=60000 if=/dev/zero of=usbdrv.img
然后你可以為 VirtualBox 創建一個引用這個圖像的文件:
VBoxManage internalcommands createrawvmdk -filename "usbdrv.vmdk" -rawdisk "usbdrv.img"
您可以按原樣在 VirtualBox 中使用此映像,但根據來賓操作系統的不同,它可能不會立即可見。 例如,我嘗試在 Windows 客戶機操作系統上使用此方法,並且必須執行以下操作才能為其指定驅動器號:
您可能希望在 Linux 上訪問您的文件。 首先從來賓操作系統卸載它以確保並將其從虛擬機中刪除。 現在我們需要創建一個引用分區的虛擬設備。
sfdisk -d usbdrv.img
回應:
label: dos
label-id: 0xd367a714
device: usbdrv.img
unit: sectors
usbdrv.img1 : start= 63, size= 48132, type=4
記下分區的起始位置:63。在下面的命令中,我使用了 loop4,因為在我的情況下它是第一個可用的循環設備。
sudo losetup -o $((63*512)) loop4 usbdrv.img
mkdir usbdrv
sudo mount /dev/loop4 usbdrv
ls usbdrv -l
回應:
total 0
-rwxr-xr-x. 1 root root 0 Apr 5 17:13 'Test file.txt'
耶!
在 Windows 上,使用https://github.com/Zapotek/raw2vmdk將 dd 或 winhex 創建的原始文件轉換為 vmdk。 raw2vmdk v0.1.3.2 有一個錯誤 - 創建 vmdk 文件后,編輯 vmdk 文件並修復原始文件的路徑(在我的情況下,而不是 D:\\Temp\\flash_16gb.raw(由 winhex 創建)生成的路徑是 D:Tempflash_16gb.raw)。 然后,在 vmware 虛擬機版本 6.5-7(5.1 拒絕附加 vmdk 硬盤)中打開它。 哇!
也許您應該嘗試使用 Starwind V2V 轉換器,您可以從這里獲得它 - http://www.starwindsoftware.com/converter 。 它還支持 IMG 磁盤格式,並在 IMG、VMDK 或 VHD 之間進行逐個扇區的轉換,而無需對源圖像進行任何更改。 這個工具是免費的:)
krosenvold 的回答啟發了以下腳本,它執行以下操作:
該腳本可重新啟動並檢查中間文件是否存在。 它還使用 pv 和 qemu-img -p 來顯示每一步的進度。
在我的環境 2 x Ubuntu 12.04 LTS 中,步驟如下:
#!/bin/bash
# get a dd disk dump and convert it to vmware
# see http://stackoverflow.com/questions/454899/how-to-convert-flat-raw-disk-image-to-vmdk-for-virtualbox-or-vmplayer
# Author: wf 2014-10-1919
#
# get a dd dump from the given host's given disk and create a compressed
# image at the given target
#
# 1: host e.g. somehost.somedomain
# 2: disk e.g. sda
# 3: target e.g. image.gz
#
# http://unix.stackexchange.com/questions/132797/how-to-use-ssh-to-make-a-dd-copy-of-disk-a-from-host-b-and-save-on-disk-b
getdump() {
local l_host="$1"
local l_disk="$2"
local l_target="$3"
echo "getting disk dump of $l_disk from $l_host"
ssh $l_host sudo fdisk -l | egrep "^/dev/$l_disk"
if [ $? -ne 0 ]
then
echo "device $l_disk does not exist on host $l_host" 1>&2
exit 1
else
if [ ! -f $l_target ]
then
ssh $l_host "sudo dd if=/dev/$disk bs=1M | gzip -1 -" | pv | dd of=$l_target
else
echo "$l_target already exists"
fi
fi
}
#
# optionally install command from package if it is not available yet
# 1: command
# 2: package
#
opt_install() {
l_command="$1"
l_package="$2"
echo "checking that $l_command from package $l_package is installed ..."
which $l_command
if [ $? -ne 0 ]
then
echo "installing $l_package to make $l_command available ..."
sudo apt-get install $l_package
fi
}
#
# convert the given image to vmware
# 1: the dd dump image
# 2: the vmware image file to convert to
#
vmware_convert() {
local l_ddimage="$1"
local l_vmwareimage="$2"
echo "converting dd image $l_image to vmware $l_vmwareimage"
# convert to VMware disk format showing progess
# see http://manpages.ubuntu.com/manpages/precise/man1/qemu-img.1.html
qemu-img convert -p -O vmdk "$l_ddimage" "$l_vmwareimage"
}
#
# show usage
#
usage() {
echo "usage: $0 host device"
echo " host: the host to get the disk dump from e.g. frodo.lotr.org"
echo " you need ssh and sudo privileges on that host"
echo "
echo " device: the disk to dump from e.g. sda"
echo ""
echo " examples:
echo " $0 frodo.lotr.org sda"
echo " $0 gandalf.lotr.org sdb"
echo ""
echo " the needed packages pv and qemu-utils will be installed if not available"
echo " you need local sudo rights for this to work"
exit 1
}
# check arguments
if [ $# -lt 2 ]
then
usage
fi
# get the command line parameters
host="$1"
disk="$2"
# calculate the names of the image files
ts=`date "+%Y-%m-%d"`
# prefix of all images
# .gz the zipped dd
# .dd the disk dump file
# .vmware - the vmware disk file
image="${host}_${disk}_image_$ts"
echo "$0 $host/$disk -> $image"
# first check/install necessary packages
opt_install qemu-img qemu-utils
opt_install pv pv
# check if dd files was already loaded
# we don't want to start this tedious process twice if avoidable
if [ ! -f $image.gz ]
then
getdump $host $disk $image.gz
else
echo "$image.gz already downloaded"
fi
# check if the dd file was already uncompressed
# we don't want to start this tedious process twice if avoidable
if [ ! -f $image.dd ]
then
echo "uncompressing $image.gz"
zcat $image.gz | pv -cN zcat > $image.dd
else
echo "image $image.dd already uncompressed"
fi
# check if the vmdk file was already converted
# we don't want to start this tedious process twice if avoidable
if [ ! -f $image.vmdk ]
then
vmware_convert $image.dd $image.vmdk
else
echo "vmware image $image.vmdk already converted"
fi
只是為了給您另一種選擇,您也可以使用https://sourceforge.net/projects/dd2vmdk/ 。 dd2vmdk 是一個基於 *nix 的程序,它允許您通過獲取原始映像、分析主引導記錄(物理扇區 0)並獲取特定信息來掛載原始磁盤映像(由 dd、dcfldd、dc3dd、ftk 映像器等創建)創建 vmdk 文件所需的信息。
就個人而言,imo Qemu 和 Zapotek 的 raw2vmdk 工具是將 dd 轉換為 vmdks 的最佳整體選擇。
披露:我是這個項目的作者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.