Expanding the C: drive on a server
Reference: https://thedxt.ca/2023/06/moving-windows-recovery-partition-correctly/
(Note: This has been tested on Windows Server 2022. Not sure that it will work on lower versions of Windows Server)
I recently ran across an issue where I had to increase the space on the C: drive on a Server 2022 virtual machine because it was running low. I increased the size of the disk in the VM settings, but it didn't increase the size of the C drive. Even after I went in and tried to extend the partition, I couldn't. The reason? The recovery partition was in the way.
So, how do you fix this? You move the recovery partition.
To be able to extend the partition for the C: drive, the drive space you want to extend it to must be next to the partition.
Here's how you do it:
Before you begin, make a snapshot of the VM (do not include the memory)
1) Shut down the VM and increase the size of the drive then power the VM back on
2) Open the command prompt as admin
3) Disable the Windows Recovery Partition
- Run: reagentc /disable
You should get a message that the operation was successful
You should also see these two files in the C:\Windows\System32\Recovery (they may be hidden)
- ReAgent.xml
- Winre.wim
4) Remove the recovery partition
- Run: diskpart
Execute the following commands:
- Run: list disk
- This will show you all of the disks on the machine - most likely you will be modifying Disk 0
- Take note if the disk you are trying to modify is in GPT format (There will be a * in the GPT column)
- Run: select disk 0
- This selects the disk you want to work with
- Run: list partition
- This lists the partitions of the disk. It should look something like this
You should see these four partitions on the disk. The partition that we will be working with is Partition 4 - Recovery.
- Run: select partition 4
Because it is a protected partition, we must force delete it to remove it.
- Run: delete partition override
At this point, you can minimize the command prompt window and go back into Disk Management. You should no longer see the recovery partition. The free drive space that you want to extend your C: drive to should be next to the C: drive itself.
5) Extend your volume, but leave enough room for the recovery partition (Approx. 1 GB to accommodate future updates).
- Note: Windows works with drive sizes in MB so you need to leave 1024 MB for the recovery partition.
When you have expanded your drive, you should see something similar to this in Disk Management
6) Recreate the recovery partition.
- Create a New Simple Volume with the remaining unallocated space
- You can name the partition if you want. In the example, the partition was named New Recovery
- Be sure the file system is NTFS and the Allocation unit size is set to Default.
- Be sure to check the box to perform a quick format
- Do not check the box "Enable file and folder compression"
- Do not give the partition a drive letter or drive path
Disk Management should now look something like this
7) Re-enable the recovery partition
Return to Diskpart (relaunch as admin if you closed it and run back through the steps shown above to select the disk)
- Run: list partition
- Run: select partition 4
- This is the 1GB partition we created for recovery
GPT or MBR Disk - Based on which type of boot record your disk is using, you will need to run the appropriate commands
For GPT Disks:
Change the partition ID to set it as a recovery partition
- Run: set id=de94bba4-06d1-4d40-a16a-bfd50179d6ac
- You should get a message that the ID was set successfully
Next you need to hide the drive and flag it as a required partition
- Run: gpt attributes=0x8000000000000001
- (BTW, that is 14 zeros between the 8 and the 1)
- You should get a message that the attributes were successfully assigned
MBR Disk (Use this only if the the boot record is not in GPT format)
Set the recovery partition ID
- Run: set id=27
- You should get a message that the ID was set successfully
You may now exit Diskpart
- Re-enable the Windows Recovery Partition
- Run: reagentc /enable
- You should get a success message
Disk Management should look something like this now
If everything worked like it should, you should now have an extended C: drive and a recovery partition.
More info on deploying the Windows Recovery Partition can be found here.
More info on the reagentc command can be found here.