简体   繁体   English

如何将平面原始磁盘映像转换为用于 virtualbox 或 vmplayer 的 vmdk?

[英]How to convert flat raw disk image to vmdk for virtualbox or vmplayer?

I have some old images of old Linux filesystems in flat file format.我有一些平面文件格式的旧 Linux 文件系统的旧图像。 they can be used by Bochs , but I need to run them with Virtual Box . Bochs可以使用它们,但我需要使用Virtual Box运行它们。 Virtual Box cannot use images in this format, so I need to convert these images from flat file to .vmdk file format. Virtual Box 无法使用这种格式的图像,因此我需要将这些图像从平面文件转换为 .vmdk 文件格式。 Is there any way to do this?有没有办法做到这一点?

First, install QEMU.首先,安装 QEMU。 On Debian-based distributions like Ubuntu, run:在 Ubuntu 等基于 Debian 的发行版上,运行:

$ apt-get install qemu

Then run the following command:然后运行以下命令:

$ qemu-img convert -O vmdk imagefile.dd vmdkname.vmdk

I'm assuming a flat disk image is a dd -style image.我假设平面磁盘映像是dd样式的映像。 The convert operation also handles numerous other formats.转换操作还处理许多其他格式。

For more information about the qemu-img command, see the output of有关qemu-img命令的更多信息,请参阅输出

$ qemu-img -h

Since the question mentions VirtualBox, this one works currently:由于问题提到了VirtualBox,这个目前有效:

VBoxManage convertfromraw imagefile.dd vmdkname.vmdk --format VMDK

Run it without arguments for a few interesting details (notably the --variant flag):不带参数运行它以获得一些有趣的细节(特别是--variant标志):

VBoxManage convertfromraw

To answer TJJ: But is it also possible to do this without copying the whole file?回答 TJJ:但是是否也可以在不复制整个文件的情况下执行此操作? So, just to somehow create an additional vmdk-metafile, that references the raw dd-image.所以,只是为了以某种方式创建一个额外的 vmdk-metafile,它引用了原始 dd-image。

Yes , it's possible.是的,这是可能的。 Here's how to use a flat disk image in VirtualBox:以下是在 VirtualBox 中使用平面磁盘映像的方法:

First you create an image with dd in the usual way:首先,您以通常的方式使用 dd 创建一个图像:

dd bs=512 count=60000 if=/dev/zero of=usbdrv.img

Then you can create a file for VirtualBox that references this image:然后你可以为 VirtualBox 创建一个引用这个图像的文件:

VBoxManage internalcommands createrawvmdk -filename "usbdrv.vmdk" -rawdisk "usbdrv.img"

You can use this image in VirtualBox as is, but depending on the guest OS it might not be visible immediately.您可以按原样在 VirtualBox 中使用此映像,但根据来宾操作系统的不同,它可能不会立即可见。 For example, I experimented on using this method with a Windows guest OS and I had to do the following to give it a drive letter:例如,我尝试在 Windows 客户机操作系统上使用此方法,并且必须执行以下操作才能为其指定驱动器号:

  • Go to the Control Panel.进入控制面板。
  • Go to Administrative Tools.转到管理工具。
  • Go to Computer Management.转到计算机管理。
  • Go to Storage\\Disk Management in the left side panel.转到左侧面板中的存储\\磁盘管理。
  • You'll see your disk here.您将在此处看到您的磁盘。 Create a partition on it and format it.在其上创建一个分区并对其进行格式化。 Use FAT for small volumes, FAT32 or NTFS for large volumes.小卷使用 FAT,大卷使用 FAT32 或 NTFS。

You might want to access your files on Linux.您可能希望在 Linux 上访问您的文件。 First dismount it from the guest OS to be sure and remove it from the virtual machine.首先从来宾操作系统卸载它以确保并将其从虚拟机中删除。 Now we need to create a virtual device that references the partition.现在我们需要创建一个引用分区的虚拟设备。

sfdisk -d usbdrv.img

Response:回应:

label: dos
label-id: 0xd367a714
device: usbdrv.img
unit: sectors

usbdrv.img1 : start=          63, size=       48132, type=4

Take note of the start position of the partition: 63. In the command below I used loop4 because it was the first available loop device in my case.记下分区的起始位置:63。在下面的命令中,我使用了 loop4,因为在我的情况下它是第一个可用的循环设备。

sudo losetup -o $((63*512)) loop4 usbdrv.img
mkdir usbdrv
sudo mount /dev/loop4 usbdrv
ls usbdrv -l

Response:回应:

total 0
-rwxr-xr-x. 1 root root 0 Apr  5 17:13 'Test file.txt'

Yay!耶!

On windows, use https://github.com/Zapotek/raw2vmdk to convert raw files created by dd or winhex to vmdk.在 Windows 上,使用https://github.com/Zapotek/raw2vmdk将 dd 或 winhex 创建的原始文件转换为 vmdk。 raw2vmdk v0.1.3.2 has a bug - once the vmdk file is created, edit the vmdk file and fix the path to the raw file (in my case instead of D:\\Temp\\flash_16gb.raw (created by winhex) the generated path was D:Tempflash_16gb.raw). raw2vmdk v0.1.3.2 有一个错误 - 创建 vmdk 文件后,编辑 vmdk 文件并修复原始文件的路径(在我的情况下,而不是 D:\\Temp\\flash_16gb.raw(由 winhex 创建)生成的路径是 D:Tempflash_16gb.raw)。 Then, open it in a vmware virtual machine version 6.5-7 (5.1 was refusing to attach the vmdk harddrive).然后,在 vmware 虚拟机版本 6.5-7(5.1 拒绝附加 vmdk 硬盘)中打开它。 howgh!哇!

Maybe you should try using Starwind V2V Converter, you can get it from here - http://www.starwindsoftware.com/converter .也许您应该尝试使用 Starwind V2V 转换器,您可以从这里获得它 - http://www.starwindsoftware.com/converter It also supports IMG disk format and performs sector-by sector conversion between IMG, VMDK or VHD into and from any of them without making any changes to source image.它还支持 IMG 磁盘格式,并在 IMG、VMDK 或 VHD 之间进行逐个扇区的转换,而无需对源图像进行任何更改。 This tool is free :)这个工具是免费的:)

krosenvold's answer inspired the following script which does the following: krosenvold 的回答启发了以下脚本,它执行以下操作:

  • get the dd dump via ssh from a remote server (as gz file)从远程服务器通过 ssh 获取 dd 转储(作为 gz 文件)
  • unzip the dump解压缩转储
  • convert it to vmware将其转换为 vmware

the script is restartable and checks the existence of the intermediate files.该脚本可重新启动并检查中间文件是否存在。 It also uses pv and qemu-img -p to show the progress of each step.它还使用 pv 和 qemu-img -p 来显示每一步的进度。

In my environment 2 x Ubuntu 12.04 LTS the steps took:在我的环境 2 x Ubuntu 12.04 LTS 中,步骤如下:

  • 3 hours to get a 47 GByte disk dump of a 60 GByte partition 3 小时获得 60 GByte 分区的 47 GByte 磁盘转储
  • 20 minutes to unpack to a 60 GByte dd file 20 分钟解压到 60 GByte dd 文件
  • 45 minutes to create the vmware file 45 分钟创建 vmware 文件
#!/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

Just to give you an another option, you could use https://sourceforge.net/projects/dd2vmdk/ as well.只是为了给您另一种选择,您也可以使用https://sourceforge.net/projects/dd2vmdk/ dd2vmdk is a *nix-based program that allows you to mount raw disk images (created by dd, dcfldd, dc3dd, ftk imager, etc) by taking the raw image, analyzing the master boot record (physical sector 0), and getting specific information that is need to create a vmdk file. dd2vmdk 是一个基于 *nix 的程序,它允许您通过获取原始映像、分析主引导记录(物理扇区 0)并获取特定信息来挂载原始磁盘映像(由 dd、dcfldd、dc3dd、ftk 映像器等创建)创建 vmdk 文件所需的信息。

Personally, imo Qemu and the Zapotek's raw2vmdk tools are the best overall options to convert dd to vmdks.就个人而言,imo Qemu 和 Zapotek 的 raw2vmdk 工具是将 dd 转换为 vmdks 的最佳整体选择。

Disclosure: I am the author of this project.披露:我是这个项目的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM