Skip to content

VM Image to bare metal disk

Henryk Paluch edited this page Aug 24, 2019 · 11 revisions

How to quickly convert and write VM image to physical hard drive.

I have lot of VirtualBox images, but my computer has constrained resources (2 cores, 4GB RAM). When using VirtualBox, only around 1.8GB RAM is available - which is OK for console Linux. But it is too small to run GUI (this applies unfortunately for both Linux and Windows guests).

However I have few old IDE disks (around 40GB and 80GB in size) and USB to IDE adapter. So I decided to convert some of my VMs back to bare metal - using these IDE disks.

Setup

We will need following software to do this task:

  • good image converter to convert *.vdi or *.vmdk VirtualBox images to raw images
  • Windows dd like utility to write *.raw image to physical disk.

Installing Image converter

I have installed QEMU for Windows which contains handy qemu-img.exe utility pretty suitable for this task.

  • install QEMU obvious way (double click on downloaded qemu-w64-setup-20190815.exe and confirm everything).

Installing DD like utility

I decided to use http://hddguru.com/software/HDD-Raw-Copy-Tool/. Just Download portable version: http://hddguru.com/software/HDD-Raw-Copy-Tool/HDDRawCopy1.10Portable.exe

Converting Virtual Disk image to physical disk

At first we use qemu-img.exe to convert virtual disk to RAW disk (which is exactly uncompressed disk image - sector by sector). Here is typical command to do this:

d:\APP\qemu\qemu-img.exe convert -p -f vdi -O raw ^
  m:\VirtualMachines\stm32-cubemx-linux\stm32-cubemx-linux-disk001.vdi ^
  m:\M_TEMP\stm32-cubemx-linux.raw

Please replace all paths to to fit your case. Notes on parameters:

  • -p - show progress
  • -f vdi - input format. NOTE: use vmdk for *.vmdk images
  • -O raw - output format. Must be RAW to be later written to Physical disk.

NOTE: You need enough space on your working disk (where will be *.raw stored) to accommodate complete uncompressed disk image!

Now we can write *.raw image to physical image.

  • Run HDDRawCopy1.10Portable.exe
  • double-click on last entry labeled FILE
  • select your *.raw image
  • click on Continue
  • select your target HDD drive - ensure that it is right-one !!!
  • click on Continue
  • double check that Target drive is correct!!!
  • then click on Start to write *.raw image to physical disk

When done you can try to reboot - press "right" hot-key to select boot menu on BIOS it is typically:

  • F8 (Zotac)
  • F11 (Microstar?)
  • F12 (Dell)

And hope to boot your OS from physical disk.

NOTE: Some operating systems (most Windows) are unable to boot from different hardware (you may need in such case Universal Boot CD (UBCD) - to run fix_hd or similar command - untested).

However modern Linux versions (tested Debian9) should boot fine - because initial RAM-disk (initrd) contains all kernel modules, and root filesystem is typically identified by UUID (which forces kernel to scan all disk to find the right one). You may need to also edit /etc/network/interfaces to correct name of network card, etc...

Bonus: resizing LVM disks

Typically physical HDD are larger than RAW image - so to fully use them we may need to enlarge partitions. Here is typical Debian disk layout as seen by parted:

parted /dev/sda
...
(parted) print
Model: ST340016 A (scsi)
Disk /dev/sda: 40.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  256MB   255MB   primary   ext2         boot
 2      257MB   34.4GB  34.1GB  extended
 5      257MB   34.4GB  34.1GB  logical                lvm

WARNING! Double check correct HDD name it may even sometimes change across reboot (some BIOSes sometimes reorder logical drive order when booting from different drive...)

1 - /dev/sdX1 - /boot

2 - /dev/sdX2 - extended partition

5 - /dev/sdX5 - logical LVM partition

I used following command to resize LVM partition and later enlarge swap etc...

  • run parted on right disk, in my case parted /dev/sda
  • resize extended partition (number 2 in my case) using this parted command (replace 40GB with size of your entire disk - as reported on parted startup):
    resizepart 2 40GB
    
  • parted will happily truncate that number to fit on disk. Warning: do not use sizes larger than reported disk size - parted will report error in this case.
  • resize logical partition same way as extended partition - in my case:
    resizepart 5 40GB
    
  • rather print to see what has been done:
    (parted) print
    Model: ST340016 A (scsi)
    Disk /dev/sda: 40.0GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:
    
    Number  Start   End     Size    Type      File system  Flags
     1      1049kB  256MB   255MB   primary   ext2         boot
     2      257MB   40.0GB  39.7GB  extended
     5      257MB   40.0GB  39.7GB  logical                lvm
    
  • then enter quit command to quit parted
  • now reboot OS to ensure that kernel sees new partitions:
    init 6
    

NOTE: After reboot my hard-drive letter suddenly changed from /dev/sda to expected /dev/sdc - do NOT ask me why - but be ready and double check what are you editing...

  • after reboot we need to tell LVM that it can utilise new larger partition - in my case using command:
    # see current Physical Volume sizes
    pvs
    PV         VG          Fmt  Attr PSize  PFree
    /dev/sdc5  debian-9-vg lvm2 a--  31.76g    0
    
    pvresize /dev/sdc5
    Physical volume "/dev/sdc5" changed
    1 physical volume(s) resized / 0 physical volume(s) not resized
    
    # notice that PFree changed from 0 to 5.25g 
    pvs
    PV         VG          Fmt  Attr PSize  PFree
    /dev/sdc5  debian-9-vg lvm2 a--  37.01g 5.25g
  • now you can enlarge any of LVM partition you want - for example to add 2GB to swap:
    lvscan
    ACTIVE            '/dev/debian-9-vg/root' [31.26 GiB] inherit
    ACTIVE            '/dev/debian-9-vg/swap_1' [512.00 MiB] inherit
    
    lvextend -L +2G /dev/mapper/debian--9--vg-swap_1
    Size of logical volume debian-9-vg/swap_1 changed from 512.00 MiB (128 extents) to 2.50 GiB (640 extents).
    Logical volume debian-9-vg/swap_1 successfully resized.
    
    # NOTICE how the swap1 size changed from 512Mib to 2.50GiB
    lvscan
    ACTIVE            '/dev/debian-9-vg/root' [31.26 GiB] inherit
    ACTIVE            '/dev/debian-9-vg/swap_1' [2.50 GiB] inherit
    
  • Now we need to reformat swap - using these steps:
    • turn off all swaps:
      swapoff -a
    • reformat swap - ignore warning:
      mkswap /dev/debian-9-vg/swap_1
      mkswap: /dev/debian-9-vg/swap_1: warning: wiping old swap signature.
      Setting up swapspace version 1, size = 2.5 GiB (2684350464 bytes)
      no label, UUID=d282e354-d0cc-486a-b8b0-641f6c239dff
    • now edit your /etc/fstab and update UUID value on your swap line, in my example:
      UUID=d282e354-d0cc-486a-b8b0-641f6c239dff none            swap    sw              0       0
      
    • now turn on all swaps:
      swapon -a

Fixing creepy suspend: By default Debian attempts to suspend/resume OS to swap partition. When swap partition UUID changes - you may need to also do this (but only if they use UUID):

  • update /etc/initramfs-tools/conf.d/resume - I strongly recommend to completely turn it off using:
    RESUME=none
    
  • update initial ramdisk using:
    update-initramfs -u
    

To resize filesystem we need to use lvresize with -r argument, for example to add 2GB to existing root fs:

lvscan
  ACTIVE            '/dev/debian-9-vg/root' [31.26 GiB] inherit
  ACTIVE            '/dev/debian-9-vg/swap_1' [2.50 GiB] inherit

lvresize -r -L +2G /dev/mapper/debian--9--vg-root
  Size of logical volume debian-9-vg/root changed from 31.26 GiB (8002 extents) to 33.26 GiB (8514 extents).
  Logical volume debian-9-vg/root successfully resized.
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/mapper/debian--9--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/mapper/debian--9--vg-root is now 8718336 (4k) blocks long.

lvscan
  ACTIVE            '/dev/debian-9-vg/root' [33.26 GiB] inherit
  ACTIVE            '/dev/debian-9-vg/swap_1' [2.50 GiB] inherit

Done!

--hp

Clone this wiki locally