How can I write a Windows (NTFS) .img file to a partition and add it to grub?

3

I'm trying some crazy stuff here. First, I had a VirtualBox machine with Windows XP installed, using .vdi format. Then I exported the image to a OVA, extracted the .vmdk and converted it to a raw .img file. Then I've created a new VM into KVM via virt-manager, loaded this image as a hard disk and got it running.

Now, I'm trying to copy this entire image to a disk partition, via dd, and boot the first NTFS partition inside this image on boot, via grub. So far, i've already dd'ed the first partition inside the image (the actual NTFS partition) and it was recognized both by gParted as NTFS and grub as a Windows install, but I can't boot it. So I've tried to copy the entire image, including the partition table, boot sector and stuff, to this /dev/sda4 physical partition. Now the image is booting via KVM, but I can't find a way to add it to Grub. I'm trying to find a way to do it via loop mounting, I just have no idea on how config grub to mount/map/boot this image and access the first (NTFS) partition inside it.

What I want to do:

- Turn my XP image into a valid physical installation to dual boot
- Keep the VM running from this disk partition

This way, if possible, I'll use the VM on Ubuntu, or boot it on grub.

For clarification, the commands I used:

$ sudo su

# mounts the entire image
$ losetup /dev/loop0 /path/to/WindowsXP.img
$ fdisk -l /dev/loop0

> Device        Boot    Start   End         Blocks      Id  System
> /dev/loop0p1  *       63      31439204    15719571    7   HPFS/NTFS/exFAT

# mounts the first NTFS partition inside image (offset 63*512)
$ losetup /dev/loop1 -o 32256 /dev/loop0

At this point, I have both NTFS partition on /dev/loop1 and the full disk (with MBR) on /dev/loop0. Then:

# copied the entire image to /dev/sda4. This is not recognized by GRUB, 
# therefore it must be loopmounted before booted via grub config somehow
$ dd if=/dev/loop0 of=/dev/sda4 bs=10m

# copied the NTFS virtual partition to physical one
# GRUB should recognize and boot it, since is a valid XP install, however it misses the partition table,
# so the virtual machine won't be able to boot it.
$ dd if /dev/loop1 of=/dev/sda4 bs=10m

As an alternative, I thought about a second disk image attached to VM, only with GRUB and minimal info to boot this partition. This way, I could keep the NTFS partition on /dev/sda4 to grub as a dual boot, and boot from a second tiny image on KVM.

Ideas?

Darlan Alves

Posted 2013-09-20T06:36:33.660

Reputation: 133

Answers

0

Disclaimer: this is more of a suggestion, not a well-informed answer.

Since you seem to be able to copy the Windows partition to a legitimate physical partition (/dev/sda4) why don't you just boot /dev/sda in a virtual machine? You could then boot into the Windows partition by choosing the Windows option in GRUB. It appears this sort of setup has been done before: https://bbs.archlinux.org/viewtopic.php?id=68216

Aaron Okano

Posted 2013-09-20T06:36:33.660

Reputation: 136

Thanks for the answer! I didn't know that I could boot /dev/sda directly via qemu /dev/sda. That will make my tests a lot easier. I wanted to boot XP as a dual-boot, to enable direct access to hardware, and play some games, without losing the ability to run it inside a VM. I think the best solution is a fresh install of XP into /dev/sda4 and reinstall grub to enable the dual boot option. I'll try it. – Darlan Alves – 2013-09-22T02:01:25.560