Shrink partition with resize2fs
'resize2fs' to get the desired filesystem size
'tune2fs -l' to determine the filesystem look for 'Block size' and 'Block count'
tune2fs -l /dev/sdb1
'parted' to determine the partition's 'Start' sector (first set 'unit' to 's'), unit s,
Compute the partition 'End' sector:
'End' = 'Start' + (('Block size' * 'Block count') / 512) - 1
'parted' to resize the partition using the computed 'End' value (first set 'unit' to 's')
'parted /dev/sdX resizepart 2 <new_last_sector_number>s
(See 'man resize2fs' and 'man parted' for command details)
blkid, vim /etc/fstab, systemctl daemon-reload, lsblk
resize2fs /dev/sdb1 250G
tune2fs -l /dev/sdb1 - Block count: 65536000, First block: 0, Block size: 4096
qalc
2048 + ((4096 × 65536000) / 512) − 1 = 524290047
parted
parted /dev/sdb, unit s, print, (parted) resizepart 1 524290047, e2fsck -f /dev/sdb1
modify image
inspection;
fdisk -l raw_img_file.img
mount -o loop offset=1048576(start sector*512) raw_img_file.img /mnt/img
umount /mnt/img,
Add space to the image;
df -h /mnt/img
parted raw_img_file.img
print free
quit
dd if=/dev/zero bs=1M count=512 >> raw_img_file.img pated raw_img_file.img, unit s, resizepart 1 2444222s (last sector 's')
print free
kpartx -av raw_img_file.img
ls /dev/mapper
e2fsck -f -y -v -C 0 /dev/mapper/loop0p1
resize2fs -p /dev/mapper/loop0p1
kpartx -d raw_img_file.img
umount /mnt/img
kpartx -d raw_img_file.img
kpartx -av raw_img.file.img, mount /dev/mapper/loop0p1 /mnt/img
chroot short version
mount -t proc proc /mnt/img/proc
mount -t sysfs sys /mnt/img/sys
mount -o bind /dev /mnt/img/dev
mount -t devpts pts /mnt/img/dev/pts
chroot /mnt/img
exit
umount /mnt/img/dev/pts
umount /mnt/img/dev
umount /mnt/img/sys
umount /mnt/img/proc
umount /mnt/img
kpartx -d raw_img_file.img
shrinking .img
umount /dev/sdc1
umount /mnt/sdc2
dd if=/dev/sdc of=my_image.img
modprobe loop
losetup -f
losetup /dev/loop0 my_image.img
prtprobe /dev/loop0
gparted /dev/loop0
resize, losetup -d/dev/loop0 (deattach)
find last sector fdisk -l my_image.img
The last block was 9181183 and block-numbers start at 0. That means we need (9181183+1)*512 bytes. truncate --size=$[(9181183+1+33)*512] my_image.img,
– 33 extra sectors are needed for the backup GPT partition tablem use gdisk my_image.img and then use the w command to repair the partition table.
– if GPT backup partition table at the end of the disk was already removed, there is fix. Add 33 sectors with dd if=/dev/zero bs=512 count=33 >> myimage.img. And fix it with gdisk myimage.img and use w command.
or parted /dev/loop0, print, unit s,
resizepart 2(1) 111111s (to sector), quit
Leave a Reply