Partition Root Volume of EBS Volume while Creating EC2 Instance (Windows & Linux)

0

When we provision an EC2 instance (say Windows with t2.micro), I get a 30GB EBS volume. Even if the OS takes 10 to 15 GB, I want to create a 10GB partition out of that 30GB and use it as D drive (example). Is this achievable?

I saw a similar question, but it is also not answered. https://repost.aws/questions/QUsg638lebSeet0x6CtXbmQQ/partitioning-the-ebs-root-volume-of-an-aws-ec2-instance

asked 10 months ago655 views
3 Answers
0
Accepted Answer

Hello,

As this is the root volume one option would be to create a golden image (AMI) with your requirements. I have not validated it but in general you can shrink the partition in Windows through the Disk Management. A generic step-by-step process could look like

  1. Back up your data (if this is a new empty instance ignore this step)
  2. Open Disk Management
  3. Shrink the C drive: In Disk Management, right-click on the C drive and select "Shrink Volume." Enter the amount you want to shrink the C drive by (in this case, 20 GB). The utility will calculate the maximum amount of space that can be shrunk based on available free space. After specifying the desired size, click "Shrink."
  4. Create a new partition: You will now have unallocated space on your disk. Right-click on the unallocated space and select "New Simple Volume." The New Simple Volume Wizard will appear. Follow the wizard's instructions to assign a drive letter (e.g., D) and format the new partition using the desired file system (e.g., NTFS). Complete the wizard to create the new D drive.

Then create an AMI and use it for your new instances.

Other approach - NOT TESTED - not sure if works - use it at your own risk - would be to run a PowerShell Script as UserData on launch:

<powershell>
Install-Module -Name Storage

# Get the disk to resize
$disk = Get-Disk | Where-Object { $_.PartitionStyle -eq 'RAW' }

# Get the existing partition (C drive)
$partition = Get-Partition -DiskNumber $disk.Number | Where-Object { $_.DriveLetter -eq 'C' }

# Resize the existing partition to 30GB
$targetSizeGB = 30
Resize-Partition -PartitionNumber $partition.PartitionNumber -Size ($targetSizeGB * 1GB)

# Create a new partition (D drive) with the remaining space
New-Partition -DiskNumber $disk.Number -UseMaximumSize -AssignDriveLetter -IsActive
Format-Volume -DriveLetter D -FileSystem NTFS -Confirm:$false
</powershell>

This would allow on-launch partition resize but as mentioned not tested.

profile picture
answered 10 months ago
profile picture
EXPERT
reviewed a month ago
  • Thanks a lot once again for your quick response. Out of the above 2, first one worked. However, need a way to automate it. The 2nd approach didn't work. I think the script needs some update. I am trying that. Will let you know.

  • Thanks a lot @sauerkraut. However, I will confirm if this is fine.

0

You cannot partition the volume on instance creation. Additionally for something like Windows if you created a 10GB partition you'd fill that very quickly once you started applying Windows updates to the OS.

profile pictureAWS
EXPERT
Rob_H
answered 10 months ago
  • First of all, thanks a lot for your quick response. I am not not creating a 10 GB partition for C Drive. My requirement is that if I have a 30 or 40GB EBS root volume, how can I partition it (say 10 or 15 GB) to create D drive also without attaching another EBS volume. Can I not partition the root volume through User Data? If not, is this possible? If yes, how and at what stage? Please help.

  • Ah, I think you got some info on that in the other answer, so hopefully that's helpful to you.

0
<powershell>
# Get the disk to resize
$disk = Get-Disk | Where-Object { $_.PartitionStyle -eq 'MBR' }

# Get the existing partition (C drive)
$partition = Get-Partition -DiskNumber $disk.Number | Where-Object { $_.DriveLetter -eq 'C' }

# Resize the existing partition to 20GB
$targetSizeGB = 20
Resize-Partition -DiskNumber $disk.Number -PartitionNumber $partition.PartitionNumber -Size ($targetSizeGB * 1GB)

# Create a new partition (D drive) with the remaining space
New-Partition -DiskNumber $disk.Number -UseMaximumSize -AssignDriveLetter -IsActive
Format-Volume -DriveLetter D -FileSystem NTFS -Confirm:$false
</powershell>
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions