大网数据广告活动
服务器运维问答

Shrink partition to exactly fit the underlying filesystem size

· 52CE 我爱测

Recently i shrinked an ext4 filesystem size to 500GB df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 493G 64G 404G 14% / now i want to shrink the partition size to fit exactly to the filesystem size. I tried to used pa…

问题原文

原帖链接 · Vikelidis Kostas · CC BY-SA 3.0

Recently i shrinked an ext4 filesystem size to 500GB

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       493G   64G  404G  14% /

now i want to shrink the partition size to fit exactly to the filesystem size.

I tried to used parted and the resizepart command. The problem is when parted asks for the new size. If i choose 500GB the resulting partition is smaller from 500GB and as a result the underlying filesystem cannot fit on this partition. Any hint on how to do the correct size calculations?

已采纳解答

原帖链接 · David · CC BY-SA 3.0

Sizes reported by df will be incorrect as they account only for data blocks and miss blocks used internally by the filesystem as well as the reserved blocks.

The easy way is to shrink your filesystem to be smaller than you want by at least 10%. Resize the partition to the size you want then grow the filesystem with resize2fs.

If you want to calculate it by hand you have to know how large the filesystem is internally. Check this with tune2fs -l /dev/sda2 and multiply the Block count by the Block size. When resizing the partition in parted switch the units to sectors with unit s and print the table to get the start sector and logical sector size. Divide the total size in bytes from above by the sector size. Round up to the nearest multiple of 2048 and resize to this number of sectors (end sector = size in sectors + start sector - 1).

Equation (runable in python just fill in the first 4 values):

block_count = N
block_size = N
sector_size = N
start_sector = N
fs_size = block_count * block_size
fs_sectors = fs_size/sector_size
part_sectors = ((fs_sectors-1)/2048+1)*2048
end_sector = start_sector + part_sectors - 1
print "Partition start: %d end: %d size: %d"%(start_sector,end_sector,part_sectors)
print "Resize in parted with: \nresizepart <number> %ds"%(end_sector)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。