When I set up Qemu and ran Windows XP inside, I had XP updated to Service Pack 2 from a cdrom. How to mount a cdrom in Qemu is available here, but I wanted to upgrade to Service Pack 3 and didn't want to download the SP3 file inside the virtual Windows since it is considerably slower. So I went to the Microsoft web-site and downloaded the necessary file, assuming I could just put it inside the "windows.img" file.
Well, I can, and I have to mount the file as a drive. But how do you do that? I didn't know, but now I do:
The essential command is this:
sudo mount -o loop,offset=32256 -t ntfs /folder/windows.img /media/windows
You will probably need to make the /media/windows directory first:
sudo mkdir /media/windows
Okay, here is the explanation of the directory making:
sudo
: You can't be a normal user to do this, you have to be an administratormkdir
: This is the command to make the directory/media/windows
: This is the folder you are making. You could also make it /mnt/windows
And here is the explanation of the mounting command:
sudo
: See abovemount
: This "connects" the fake drive to the computer-o loop,offset=32256
: These are special commands, set using -o
, like you can make the fake drive read-only, or other tricks. The loop flag is used for fake drives, aka, img files. Any other command is seperated with a comma, an example would be -o loop,rw,auto,noexec
. I will explain the offset just below.-t ntfs
: This is the type of hard-drive that is faked. In this case, it is Windows, which typically uses the NTFS file structure. Other common ones are fat16
and fat32
./folder/windows.img
: This is the folder where the fake disk, windows.img
, is located./media
: This is the folder to mount the fake drive. It is the one you made earlier.Okay, so here is where the real trick is: The
offset=32256
flag is what tells the "mount" program where to look in the file. See, the "windows.img" fake drive file has some other information at the start which confuses the "mount" program, so it needs to know where in the file to start reading the fake drive. If you made your fake drive like I made mine, or if you only have Windows on it, the value of "32256" should be fine, otherwise try the following.You will need to find the numerical value of the offset. Run the "fdisk" command, just be careful because if you mistype it you can easily destroy EVERYTHING! Run it like this:
fdisk -l /folder/windows.img
fdisk
: The fdisk command is the swiss army knife to poke at drives-l
: This lists the drive information/folder/windows.img
: This is the fake driveIt will probably print something like this:
You must set cylinders.
You can do this from the extra functions menu.
Disk /folder/windows.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x1ff71ff7
Device Boot Start End Blocks Id System
/folder/windows.img 1 * 1 1273 10225341 7 HPFS/NTFS
Partition 1 has different physical/logical endings:
phys=(1023, 254, 63) logical=(1272, 254, 63)
The two things to notice are the number which falls under the "Start". In mine, that is just 1. Note also the "63 sectors/track". The number to offset is: 1*63*512=32256
Now, once I mounted the "windows.img" I was able to poke around in the folders. I took the Windows Service Pack 3 file and copied it to my Desktop. Now I can install SP3 without having to download it in virtual Windows XP, this saves a lot of time!
For my next trick, I will pull a rabbit out of my hat:
Try mounting the Fake drive, and booting XP in Qemu together! (Mount first, then run Qemu) Rock on!
P.s. You may have trouble unmounting that fake drive? Type in
df
and see which thing on the left corresponds to the fake drive on the right. In my case it was /dev/loop0
. Now, you may have a trouble unmounting it still? In this case, you will need to exit any programs accessing the drive, I even had to exit the terminal I was in. Now go back and try again.