如何缩减Linux VM数据盘容量?测试与生产环境实操问询
Got it, let's walk through this step by step—since you're testing first on your Standard disk VM before touching the Premium production one, we'll make sure every step is safe and verifiable.
Prerequisites
- Your test VM has an OS disk + a 1TB data disk (only <15GB used)
- You have access to your cloud provider's VM/disk management console
- Backup first! Even in test, take a snapshot of the data disk or run
rsync -av /mnt/data /path/to/backup/locationto copy all data to another safe place.
Step-by-Step Shrink Process
Stop the Test VM
Don't just shut down via SSH—go to your cloud console, find the test VM, and select "Stop" (this ensures the disk is fully offline and avoids filesystem corruption).Detach the Data Disk (Optional but Recommended)
In the console, navigate to the VM's disk settings, find the data disk, and detach it. This prevents any accidental conflicts while resizing.Resize the Disk in the Console
Go to the standalone disk management page, select your data disk, and edit its capacity to your desired size (e.g., 20GB—leave a little buffer just in case). Save the changes.Reattach the Disk & Start the VM
Go back to your test VM's disk settings, reattach the resized data disk, then start the VM.Verify Disk Size & Fix Partition/FileSystem
SSH into the VM and run these commands:- Check the raw disk size:
lsblk(you'll see the disk now shows your new smaller size, e.g.,/dev/sdcas 20G) - If you're using a partitioned disk (most setups):
- Unmount the disk first:
umount /mnt/data - Use
fdisk /dev/sdcto adjust the partition:- Type
dto delete the old partition (double-check you're deleting the right one!) - Type
nto create a new primary partition, using the full available space of the resized disk - Type
wto write the changes
- Type
- Critical: Adjust the filesystem
- For ext4/ext3:
resize2fs /dev/sdc1(this shrinks the filesystem to match the new partition) - For XFS: Note that XFS doesn't support shrinking natively. You'll need to backup the data, format the new partition with
mkfs.xfs /dev/sdc1, then restore the data from your backup.
- For ext4/ext3:
- Unmount the disk first:
- Check the raw disk size:
Remount & Validate
- Mount the disk again:
mount /dev/sdc1 /mnt/data - Check the new capacity:
df -h—you should see the disk now shows your target size, and all your data is intact.
- Mount the disk again:
Notes for Production Premium Disk
- Backup everything first! Take a full snapshot of the Premium data disk before any changes—Premium disks are critical for production, so no shortcuts here.
- The process is almost identical to the test environment, but double-check your cloud provider supports resizing Premium disks (most major providers do, but confirm this first).
- If your production disk uses LVM, the steps will differ slightly: you'll need to shrink the logical volume first (
lvreduce), then the physical volume (pvresize), before resizing the disk in the console.
内容的提问来源于stack exchange,提问作者Jeremy

