How to recover NTFS partition after resizing under Win guest

Discussions related to using VirtualBox on Linux hosts.
Post Reply
elifarley
Posts: 9
Joined: 10. Aug 2007, 04:17
Location: Brazil

How to recover NTFS partition after resizing under Win guest

Post by elifarley »

So I created a raw disk VMDK pointing to an NTFS partition with a command similar to this one:

Code: Select all

VBoxManage internalcommands createrawvmdk -filename /path/to/file.vmdk \
      -rawdisk /dev/sda -partitions 5 -relative
Then I launched a Windows 7 guest and opened a Windows utility to shrink this partition and move it to the end of the existing space it occupied.

As I understand, only data inside the partition itself was changed (the NTFS filesystem), but not the partition table, so I cannot mount the partition anymore.

What would be the best way to fix the partition table so that it correctly point to the new start of the NTFS filesystem?

Thanks!
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to recover NTFS partition after resizing under Win guest

Post by mpack »

Are all the other partitions on the drive still working? By moving a physical partition around on a fictional drive surface there is every possibility of corrupting the other physical (but unmapped) partitions on that drive.

Assuming you avoided that damage, the only repair strategy I can think of is to patch the partition table manually with a sector editor.

I would start however by performing a whole disk backup so things can't be made any worse - or hopefully you did that already before trying something so potentially calamitous? Note however that any new backup must be "dumb" (all sectors), since it can't reliably identify used space on the disk.
elifarley
Posts: 9
Joined: 10. Aug 2007, 04:17
Location: Brazil

Re: How to recover NTFS partition after resizing under Win guest

Post by elifarley »

I'm trusting that VirtualBox has strictly followed the VMDK definition, so it only allowed access to the contents of that specific partition and nothing else outside it, thus I hope all other partitions are safe (I'm currently using the system, and apparently it's working just fine).

Now I'll need to find out where the new NTFS filesystem starting and ending sectors are, and then manually edit the starting and ending sectors of the partition, leaving a hole before it (which I intend to format as a 16 GB swap space, the primary reason I started all this).
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to recover NTFS partition after resizing under Win guest

Post by mpack »

elifarley wrote:I'm trusting that VirtualBox has strictly followed the VMDK definition, so it only allowed access to the contents of that specific partition and nothing else outside it
Good point. I'd expect that to be the case.

Ok then, only the sector editor suggestion remains.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to recover NTFS partition after resizing under Win guest

Post by mpack »

When patching the partition fields do remember to consider both the CHS and LBA address fields. Hopefully the CHS start field will be maxed out (CHS=1023,254,63) and can just be left that way. Otherwise you'll have to determine the drive geometry in order to provide the correct address. The LBA fields are straightforward of course. The MBR spec is available on wikipedia.

And of course I'm assuming throughout this discussion that the host PC uses a classic generic MBR, with max 4 partitions.
elifarley
Posts: 9
Joined: 10. Aug 2007, 04:17
Location: Brazil

Re: How to recover NTFS partition after resizing under Win guest

Post by elifarley »

part5.tar.bz2
part5.vmdk and part5-pt.vmdk
(1.45 KiB) Downloaded 5 times
My laptop HD uses GPT with protective MBR.

Anyways, I've just found out something that could help fix the situation. I started my Win 7 vbox machine (with the VMDK still attached) and the resized partition showed up just fine, with the new (smaller) size, as if nothing wrong had happened.

From this, I believe the smaller partition size information was written not to the host GPT partition table, but to one of the 2 VMDK files I had created. Can anyone versed in VMDK internals confirm that this is what usually happens?

So now I just need to get the start and end addresses of the resized partition from the VMDK files. How can I do this?

I've attached the VMDK files. Below you can find the text part of them:

part5.vmdk:

Code: Select all

# Disk DescriptorFile
version=1
CID=b3f4268b
parentCID=ffffffff
createType="partitionedDevice"

# Extent description
RW 63 FLAT "part5-pt.vmdk" 0
RW 1985 ZERO 
RW 2048000 ZERO 
RW 532480 ZERO 
RW 2048000 ZERO 
RW 262144 ZERO 
RW 148731028 FLAT "/dev/sda5" 0
RW 876 ZERO 
RW 921600 ZERO 
RW 5568512 ZERO 
RW 1101004800 ZERO 
RW 614400000 ZERO 
RW 52428800 ZERO 
RW 25575424 ZERO 
RW 1423 ZERO 
RW 33 FLAT "part5-pt.vmdk" 63

# The disk Data Base 
#DDB

ddb.virtualHWVersion = "4"
ddb.adapterType="ide"
ddb.geometry.cylinders="16383"
ddb.geometry.heads="16"
ddb.geometry.sectors="63"
ddb.uuid.image="4f4f1f24-a8d8-4a86-9e0e-16d53a96ee7a"
ddb.uuid.parent="00000000-0000-0000-0000-000000000000"
ddb.uuid.modification="12e7a9d0-e556-4e02-ae4f-58c6ed8e6990"
ddb.uuid.parentmodification="00000000-0000-0000-0000-000000000000"
ddb.geometry.biosCylinders="1024"
ddb.geometry.biosHeads="255"
ddb.geometry.biosSectors="63"
strings part5-pt.vmdk

Code: Select all

EFI PART
StateApp
ppAetatS
StateApp
ppAetatS
EFI PART
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to recover NTFS partition after resizing under Win guest

Post by mpack »

You provided a dummy/virtual boot sector as one of the VMDK extents when you created the VMDK (looking at your extent list, that would be the 63 sector "part5-pt.vmdk" file, I guess being 63 sectors allows it to contain a grub boot script as well as an MBR in sector 0). Anyhoo, sector 0 contains the MBR and partition map for the virtual drive. But I don't know how that knowledge helps you fix the host MBR.
elifarley
Posts: 9
Joined: 10. Aug 2007, 04:17
Location: Brazil

Re: How to recover NTFS partition after resizing under Win guest

Post by elifarley »

I've installed IM-Magic partition resizer on the guest OS, and it shows the physical sector number and total sector count:
part5.jpg
part5.jpg (21.63 KiB) Viewed 2820 times
I guess I can use it to update the host GPT table...
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to recover NTFS partition after resizing under Win guest

Post by mpack »

The host is using GPT? That complicates matters a lot - a GPT partition table is protected by CRC32, so you can't just hack it. You'll need a specialist editing tool.

But, I didn't think VirtualBox supported raw disk access to selected host partitions if the host uses GPT. But, I guess it's possible if you hand crafted the extent list yourself... which could explain why the "boot sector extent" is 63 sectors instead of the 1 sector I expected.

And no, the guest partition table can't easily be used to inform what you do on the host. You could only do that if host and guest partition tables are supposed to be identical, and I'd have doubts that was ever the case, especially if one is GPT and the other not.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to recover NTFS partition after resizing under Win guest

Post by mpack »

You might find it easier to back up and then fix the problem from the other direction.

1. In the guest, slide the partition back to the start of its allocated place.

2. In the HOST the partition should now be accessible again. Shrink or replace it and slide it aside.

3. Edit the VMDK descriptor to reference the new host partitions properly. This may include inserting a new extent to use as your swap partition. In fact if the sole object was to insert a swap partition then you could just do this and omit step 2.
elifarley
Posts: 9
Joined: 10. Aug 2007, 04:17
Location: Brazil

Re: How to recover NTFS partition after resizing under Win guest

Post by elifarley »

I tried it but it seems that the NTFS filesystem didn't get to the exact same place it was before, so the host system couldn't mount it.
So I copied my files using the guest OS and simply deleted this troublesome partition :)
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: How to recover NTFS partition after resizing under Win guest

Post by socratis »

mpack wrote:But, I didn't think VirtualBox supported raw disk access to selected host partitions if the host uses GPT.
It does. That's how OSX users get access to their Bootcamp partition.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Post Reply