如何将平面原始磁盘映像转换为虚拟机或vmplayer的vmdk ?

时间:2021-10-01 21:24:53

I have some images of old linux distributes in flat file format, they can be used by bochs virtual machines, but I need to run them with Sun Virtual Box. Virtual Box cannot use images in this format, so I need to convert these images from flat file to .vmdk file format. Is there any way to do this?

我有一些旧的linux图片以平面文件格式分发,它们可以被bochs虚拟机使用,但是我需要用Sun虚拟机运行它们。虚拟盒不能使用这种格式的图像,所以我需要将这些图像从平面文件转换为.vmdk文件格式。有什么办法吗?

6 个解决方案

#1


75  

apt-get install qemu (installs QEMU on debian/ubuntu)

安装qemu(在debian/ubuntu上安装qemu)

Then run the following command: qemu-img convert -O vmdk imagefile.dd vmdkname.vmdk

然后运行以下命令:qemu-img转换-O vmdk imagefile。dd vmdkname.vmdk

I'm assuming a flat disk image is a dd-style image. The convert operation also handles numerous other formats.

我假设一个扁平的磁盘映像是一个ddstyle图像。转换操作还处理许多其他格式。

#2


72  

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):

运行它时,不需要为一些有趣的细节(特别是——变体标志)进行参数:

VBoxManage convertfromraw

#3


6  

On windows, use https://github.com/Zapotek/raw2vmdk to convert raw files created by dd or winhex to 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). Then, open it in a vmware virtual machine version 6.5-7 (5.1 was refusing to attach the vmdk harddrive). howgh!

在windows上,使用https://github.com/Zapotek/raw2vmdk将由dd或winhex创建的原始文件转换为vmdk。raw2vmdk v0.1.3.2有一个bug——一旦创建了vmdk文件,编辑vmdk文件并修复原始文件的路径(在我的例子中,不是D:\Temp\flash_16gb)。raw(由winhex创建)生成的路径是D:Tempflash_16gb.raw)。然后,在vmware虚拟机版本6.5-7(5.1拒绝附加vmdk harddrive)中打开它。howgh !

#4


1  

Maybe you should try using Starwind V2V Converter, you can get it from here - 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. This tool is free :)

也许您应该尝试使用Starwind V2V转换器,您可以从这里得到它——http://www.starwindsoftware.com/converter。它还支持IMG磁盘格式,并在IMG、VMDK或VHD之间进行扇区转换,而不需要对源映像进行任何更改。这个工具是免费的:

#5


1  

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)
  • 从远程服务器(如gz文件)通过ssh获取dd转储
  • unzip the dump
  • 解压转储
  • convert it to 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
  • 获得一个60 GByte分区的47个GByte磁盘转储3小时。
  • 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://*.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

#6


0  

To answer TJJ: But is it also possible to do this without copying the whole file? So, just to somehow create an additional vmdk-metafile, that references the raw dd-image.

要回答TJJ:但是不复制整个文件也可以这样做吗?所以,仅仅是为了创建一个附加的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. 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:

您可以在VirtualBox中使用此映像,但是根据来宾操作系统,它可能不会立即可见。例如,我试着用这个方法使用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.
  • 你会看到你的磁盘。在上面创建一个分区并格式化它。为小体积,FAT32或NTFS使用FAT,用于大容量。

You might want to access your files on 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.

您可能想要在Linux*问您的文件。首先从客户操作系统中卸载它,并将其从虚拟机中删除。现在我们需要创建一个引用该分区的虚拟设备。

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!

耶!

#1


75  

apt-get install qemu (installs QEMU on debian/ubuntu)

安装qemu(在debian/ubuntu上安装qemu)

Then run the following command: qemu-img convert -O vmdk imagefile.dd vmdkname.vmdk

然后运行以下命令:qemu-img转换-O vmdk imagefile。dd vmdkname.vmdk

I'm assuming a flat disk image is a dd-style image. The convert operation also handles numerous other formats.

我假设一个扁平的磁盘映像是一个ddstyle图像。转换操作还处理许多其他格式。

#2


72  

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):

运行它时,不需要为一些有趣的细节(特别是——变体标志)进行参数:

VBoxManage convertfromraw

#3


6  

On windows, use https://github.com/Zapotek/raw2vmdk to convert raw files created by dd or winhex to 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). Then, open it in a vmware virtual machine version 6.5-7 (5.1 was refusing to attach the vmdk harddrive). howgh!

在windows上,使用https://github.com/Zapotek/raw2vmdk将由dd或winhex创建的原始文件转换为vmdk。raw2vmdk v0.1.3.2有一个bug——一旦创建了vmdk文件,编辑vmdk文件并修复原始文件的路径(在我的例子中,不是D:\Temp\flash_16gb)。raw(由winhex创建)生成的路径是D:Tempflash_16gb.raw)。然后,在vmware虚拟机版本6.5-7(5.1拒绝附加vmdk harddrive)中打开它。howgh !

#4


1  

Maybe you should try using Starwind V2V Converter, you can get it from here - 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. This tool is free :)

也许您应该尝试使用Starwind V2V转换器,您可以从这里得到它——http://www.starwindsoftware.com/converter。它还支持IMG磁盘格式,并在IMG、VMDK或VHD之间进行扇区转换,而不需要对源映像进行任何更改。这个工具是免费的:

#5


1  

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)
  • 从远程服务器(如gz文件)通过ssh获取dd转储
  • unzip the dump
  • 解压转储
  • convert it to 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
  • 获得一个60 GByte分区的47个GByte磁盘转储3小时。
  • 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://*.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

#6


0  

To answer TJJ: But is it also possible to do this without copying the whole file? So, just to somehow create an additional vmdk-metafile, that references the raw dd-image.

要回答TJJ:但是不复制整个文件也可以这样做吗?所以,仅仅是为了创建一个附加的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. 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:

您可以在VirtualBox中使用此映像,但是根据来宾操作系统,它可能不会立即可见。例如,我试着用这个方法使用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.
  • 你会看到你的磁盘。在上面创建一个分区并格式化它。为小体积,FAT32或NTFS使用FAT,用于大容量。

You might want to access your files on 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.

您可能想要在Linux*问您的文件。首先从客户操作系统中卸载它,并将其从虚拟机中删除。现在我们需要创建一个引用该分区的虚拟设备。

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!

耶!