OiO.lk Blog C# How to find partition number or starting offset given volume attributes?
C#

How to find partition number or starting offset given volume attributes?


To clone a partition using dd.exe which comes with MSYS2 for Windows, we require 3 pieces of information.

  1. physical drive name
  2. partition starting offset (in MiB)
  3. partition size (in MiB)
dd if=physical_drive_name of=backup.img bs=1M skip=starting_offset count=size status=progress

To list available physical disk drives, we can use the command:

wmic diskdrive get Caption,Index,MediaType

On my system, it outputs:

Caption                    Index    MediaType
PC SN530 NVMe WDC 512GB    0        Fixed hard disk media
PNY USB 3.0 FD USB Device  1        Removable Media

I am interested in my NVMe drive, so I will use its index (0) to find requirement #1 which is physical drive name:

wmic diskdrive where "Index=0" get DeviceID,Partitions,Status,MediaLoaded

it outputs:

DeviceID            MediaLoaded  Partitions  Status
\\.\PHYSICALDRIVE0  TRUE         8           OK

The 2 other requirements are partition starting offset and partition size. To list partitions and their attributes, I’ll use the command:

wmic partition where "DiskIndex=0" get Name,Index,Type,StartingOffset,BlockSize,NumberOfBlocks,Size

It outputs:

BlockSize  Index  Name                   NumberOfBlocks  Size          StartingOffset  Type
512        0      Disk #0, Partition #0  614400          314572800     1048576         GPT: System
512        1      Disk #0, Partition #1  727875584       372672299008  449839104       GPT: Basic Data
512        2      Disk #0, Partition #2  225282048       115344408576  373123186688    GPT: Basic Data
512        3      Disk #0, Partition #3  102400          52428800      488467595264    GPT: Basic Data
512        4      Disk #0, Partition #4  2123776         1087373312    488520024064    GPT: Unknown
512        5      Disk #0, Partition #5  2027520         1038090240    489609494528    GPT: Unknown
512        6      Disk #0, Partition #6  39032832        19984809984   490647584768    GPT: Unknown
512        7      Disk #0, Partition #7  2883584         1476395008    510633443328    GPT: Unknown

So now, we can use the starting offset and size of the partition of our choice with dd.exe to clone it. However, this output does not give us a hint on the contents of the partitions. For that, we need to list volumes so that we can view the filesystems and labels of formatted partitions:

wmic volume where "DriveType=3" get Label,DeviceID,DriveLetter,DriveType,SerialNumber,FileSystem,MaximumFileNameLength,BlockSize,Capacity,FreeSpace

It outputs:

BlockSize  Capacity      DeviceID                                           DriveLetter  DriveType  FileSystem  FreeSpace     Label        MaximumFileNameLength  SerialNumber
4096       372672294912  \\?\Volume{41d21340-a487-4021-9735-3e999ccfc734}\  C:           3          NTFS        232339791872  OS           255                    3295242676
                         \\?\Volume{e5ffe5ea-3596-468c-832f-e5bcf2aeb39e}\               3
512        48234496      \\?\Volume{5792630e-1c80-4d8a-a679-82ec4cd0ecdc}\               3          FAT32       39666176      GRUBX64      255                    716055397
4096       1087369216    \\?\Volume{19df495f-4e17-41fb-bcb0-d33d40588153}\               3          NTFS        161165312                  255                    1445116501
4096       1038086144    \\?\Volume{9e1ca148-9cff-4576-ba37-cad1aab53ba1}\               3          NTFS        501415936     WINRETOOLS   255                    4141927052
4096       19984805888   \\?\Volume{0e8ffde2-0844-4d79-ada2-a3ea534dcc6b}\               3          NTFS        136228864     Image        255                    3940616972
4096       1476390912    \\?\Volume{a847a1f9-be5e-4f38-80ff-e5617868e2a1}\               3          NTFS        505286656     DELLSUPPORT  255                    1554162235
4096       310378496     \\?\Volume{9685da03-f3a8-4f89-88f2-1b9dafc3bd38}\               3          FAT32       203812864     ESP          255                    684405683

Now if we can determine the partition number of the volume of our interest, we can refer to the partition table to find the starting offset and size to use them with dd.exe, but how can we do that?

The reliable (unique) volume attributes are serial number, deviceID and (label and drive letter if any). size in general is not reliable as 2 or more partitions may have the same size.

Suppose the volume of our interest is DELLSUPPORT, how do we find its partition number so that we can find its starting offset and size to use with dd.exe?



You need to sign in to view this answers

Exit mobile version