QEMU guest system starts off /dev/sdc but won’t start from disk image file

1

I have a copy of Windows XP installed on a hard drive which I would like to run with QEMU.

When I start it directly from the HDD it runs fine:

qemu -m 256 -hda /dev/sdc
Starting Windows...etc...etc...

Although the HDD capacity is 320GB, my system actually lives in a 4GB partition at the beginning of the disk:

/sbin/fdisk -l /dev/sdc

Disk /dev/sdc: 320 GB, 320070320640 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1   *           1         510     4096543    b  FAT32
Warning: Partition 1 does not end on cylinder boundary.

So I figured I could only copy the partition table and the first partition (8192000 sectors) to a raw image for use with QEMU:

dd if=/dev/sdc of=winxp.img count=8192000

Of course, since winxp.img is a copy of /dev/sdc, it has exactly the same partition layout:

/sbin/fdisk -l winxp.img

Disk /mnt/windata/winxp.raw: 4 GB, 4186667520 bytes
255 heads, 63 sectors/track, 509 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

                 Device Boot      Start         End      Blocks   Id  System
/mnt/windata/winxp.raw1   *           1         510     4096543    b  FAT32
Warning: Partition 1 does not end on cylinder boundary.

However, when I try to use the raw image with QEMU, the guest system won’t start:

qemu -m 256 -hda winxp.img
Booting from hard disk...
NTLDR is missing

What settings could I change to make my image file compatible with QEMU? Should I change something in the partition table, or in the partition itself, to make it work? Is there a QEMU option that could help?

Or is it perhaps possible to use a copy of /dev/sdc1 with QEMU instead of a copy of /dev/sdc?

Dmitry Grigoryev

Posted 2015-04-01T16:05:18.617

Reputation: 7 505

1

Trying to apply wisdom I have found here: http://manuel.kiessling.net/2013/03/19/converting-a-running-physical-machine-to-a-kvm-virtual-machine/

– Dmitry Grigoryev – 2015-04-02T09:38:55.580

Tried to align the partition to cylinder boundary. Nothing changed: QEMU is still able to start Windows off the HDD, but not from the image file. – Dmitry Grigoryev – 2015-04-04T12:31:53.367

Tried to mount the image file on /dev/loop0 and start QEMU with that. Didn't work either. – Dmitry Grigoryev – 2015-04-04T12:36:08.673

So, booting from sdc is fine - why did you copy sdb then? And why only 8192000 blocks? Did you try to copy sdc as a whole? Also, as per your fdisk output, disk "geometry" changed slightly, I wouldn't be surprised for NTLDR to barf :-\ Another thing: does qemu start when you use sdc1? If so, try copying sdc1 only, not the whole disk. – ckujau – 2015-04-08T01:10:42.950

(1) I copied sdc of course, typo fixed. (2) I want to only copy the first 4GB because that's where my system lives, I don't have space on my linux machine to copy the entire 320 GB. I will try this with an external HDD. (3) What did change in the geometry exactly, apart from disk size? The partition of interest still starts on cylinder 1, ends on cylinder 510 and includes 4096543 of 1K blocks. (4) Already tried. QEMU need the partition table, so using sdc1 is impossible. – Dmitry Grigoryev – 2015-04-08T08:32:18.250

1Try to experiment with qemu -hdachs option to set proper geometry. – baf – 2015-04-09T07:46:22.307

@baf this looks like the root cause of my problem, however, QEMU won't allow me to set heads to more than 16, and I have 255 (to be honest I don't quite understand why is this limitation enforced). Is it possible to somehow convert the disk geometry without losing the data, or perhaps switch to LBA mode? And should I convert my image to 16 heads, will I need reconvert it back to 255 heads if I decide to write it back on the disk? Is there a setup which will work on QEMU and physical disk alike? – Dmitry Grigoryev – 2015-04-09T11:21:29.277

Answers

0

It appears that SeaBIOS (used by QEMU when emulating PC targets) has no manual configuration and always decides drive geometry automatically. Since my partial image is quite small, its CHS geometry is detected as 8129 * 16 * 63 rather than 510 * 255 * 63. This is done for compatibility with old drives which didn't understand LBA and thus were limited to 16 heads.

Because of the mismatch between the drive geometry in the MBR and BIOS, the bootloader fails to boot windows properly.

Theoretically, images of more than 8GB should not be affected (I know my 320GB image worked), since drives with more than 8GB have to support LBA. Unfortunately, I don't have that image anymore to test this hypothesis.

Dmitry Grigoryev

Posted 2015-04-01T16:05:18.617

Reputation: 7 505

0

if you can 'mount' your image file to a windows machine (XP preferrably), you can use Microsoft tools against it to see what the problem might be. the partition has to be 'active', it has to be an MBR (not GPT) formatted disk, and it has to have the correct boot sector.

tools like diskpart and bootsect /nt52 /mbr have been useful for me in the past. I'm not familiar with booting windows from a Linux partition.

goofology

Posted 2015-04-01T16:05:18.617

Reputation: 370

Which tools do you suggest? I will try OSFMount and Partition Commander. Though I'm pretty sure the partition table of the image is the same as on the disk I copy the image from.

– Dmitry Grigoryev – 2015-04-07T09:13:52.287

a quick google for 'Mount DD image windows' lists a few options. I can't recommend any since i have not used them. Just suggesting that troubleshooting a windows boot problem is much easier with windows tools (diskpart/bootsect), which would require you mounting the dd image in windows – goofology – 2015-04-08T02:11:31.643

OSFMount running under Windows XP recognizes my image file and successfully mounts the partition. Of course, the NTLDR file is present in the root folder. diskpart doesn't see the mounted disk and is unable to work with raw image files. – Dmitry Grigoryev – 2015-04-08T16:23:09.083

0

Just a guess: you are not getting the MBR as I think you figured out already. The whole disk is too big. so....

1) grab a USB drive with enough space
get a working disk image;
convert to qcow2;
shrink it.

2) use gparted or parted to shrink the original.
e2fsck and tune2fs *may* be helpful, also resize2fs.

John P. Fisher

Posted 2015-04-01T16:05:18.617

Reputation: 173

The MBR is located in the beginning of the disk space, and I have already used gparted on the original drive to remove every partition but the first. – Dmitry Grigoryev – 2015-04-10T07:45:48.867

I have copied a full 320 GB disk image (which runs with QEMU by the way), but converting it to qcow2 fails. qemu-img convert -O qcow2 full.img full.qcow says error while writing sector 8495104: File too large – Dmitry Grigoryev – 2015-04-10T21:59:32.433

Hmm. running at 32 bits? – John P. Fisher – 2015-04-11T19:03:47.260

Yes, 2-core x86 processor with 2GB of RAM. – Dmitry Grigoryev – 2015-04-11T21:14:09.243

32 bit OS limits file size. – John P. Fisher – 2015-04-12T02:34:16.390