Device Start End Sectors Size Type /dev/sdc1 2048 206847 204800 100M Linux filesystem /dev/sdc2 206848 1230847 1024000 500M Linux filesystem /dev/sdc3 1230848 2047966 817119 399M Linux filesystemwhere the last partition is your ex-girlfriend /home and the first partition is almost full of pictures. Now you want to remove the last unnecessary partition and enlarge the first partition. And you don't want to lost data on the first and second partitions.
(You have backup of all data, because you understand that all operations related to the partition table are risky. Right? :-)
Let's delete the last partition:
# sfdisk /dev/sdc --delete 3Now move the second partition to the end of the device. The size of the deleted sdc3 was 817119 sectors. The sfdisk expects input in format
[+]start[,[+]size[,type]]where "+num" means offset or size relative to the original partition setting. If any number is unspecified than default is to use the current setting -- it means that "+817119" is enough. (Note that -N 2 means second partition.)
# echo '+817119' | sfdisk /dev/sdc -N 2 --move-data ... Old situation: Device Start End Sectors Size Type /dev/sdc1 2048 206847 204800 100M Linux filesystem /dev/sdc2 206848 1230847 1024000 500M Linux filesystem New situation: Device Start End Sectors Size Type /dev/sdc1 2048 206847 204800 100M Linux filesystem /dev/sdc2 1023967 2047966 1024000 500M Linux filesystem Data move: typescript file: /root/sfdisk-sdc2.move old start: 206848, new start: 1023967 (move 1024000 sectors)The important detail is --move-data option, it forces sfdisk after partition table modification copy data from old area to the new offsets. The source and target may overlap (sfdisk copy sectors in backward order if necessary). The file /root/sfdisk-sdc2.move is log with details about the change.
The last step is to enlarge the first partition to use all available free space (space originally used by the second partition). The string ",+" keeps start offset unchanged and the size is specified as "+" (all available space).
# echo ',+' | sfdisk /dev/sdc -N 1 ... Old situation: Device Start End Sectors Size Type /dev/sdc1 2048 206847 204800 100M Linux filesystem /dev/sdc2 1023967 2047966 1024000 500M Linux filesystem New situation: Device Start End Sectors Size Type /dev/sdc1 2048 1023966 1021919 499M Linux filesystem /dev/sdc2 1023967 2047966 1024000 500M Linux filesystemAnd now inform filesystem about a new space:
# resize2fs /dev/sdc1and test all by mount:
# mount /dev/sdc1 /mnt/A # mount /dev/sdc2 /mnt/B # lsblk /dev/sdc NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdc 8:32 0 1000M 0 disk ├─sdc1 8:33 0 499M 0 part /mnt/A └─sdc2 8:34 0 500M 0 part /mnt/B