I have successfully resize /boot partition enlarge it and shrink root partition on Virtual Machine using a ssh connection.
I have /dev/sda1 (/boot)
and /dev/sda2 (/) root
First need to boot on some like live cd. My provider let me use a system-rescue-cd booting system. So on my provider vps control panel I have an option to boot in system-rescue-cd and after I do I can connect to it by ssh at the same ip of my vps.
Before I do it on my host I do:
$ mv /home/<username>/.ssh/known_hosts /home/<username>/.ssh/known_hosts.bak
to avoid my system try to connect to remote host using rsa certificate.
At the end job I do an $ mv /home/<username>/.ssh/known_hosts.bak /home/<username>/.ssh/known_hosts
to restore the previous behaviour.
- I connect in ssh to system-rescue-cd
- I check my partition was not mount by
# umonunt /dev/sda1
# umonunt /dev/sda2
system tell me that they are not mounted.
# e2fsck -f /dev/sda2
to check fs
- my sda2 is 300Gb most empty. I resize it to 200GB:
# resize2fs /dev/sda2 200G
(shrink)
- the previous command have an output like
... The filesystem on /dev/sda2 is now xxxxx (4k) blocks long. ...
where xxxxx is a number. You have to multiple xxxxx * 4 to get yyyyy the new size in k and remember it
- now I have to set the partition table:
# fdisk /dev/sda
(type m for help)
- active d command (delete partition)
- select 2 partition
- active n command (create partition)
- select p (primary partition)
- select first sector: choose default (same of before)
- select last sector: +yyyyyk (if yyyyy is 3746596 choose +3746596k) note the + and the k
- then it ask you:
Partition #2 contains a ext4 signature
Do you want to remove the signature? [Y]es/[N]o:
choose n (otherwise it delete all your data I suppose)
- activate w command to write new partition on disk
Now you have to move the root partition to the right.
You need to define a new_start_sector
to move.
# fdisk -l
and get the last sector of /dev/sda1 (/boot partition) hypothesize number zzz
make 1024*1024*1024/512 and get one GB in sector and add to zzz to get a new_start_sector
(zzz+(1024*1024*1024/512))
make a bash script like this:
#!/bin/sh
partition=/dev/sda2
disk=/dev/sda
sector_size=512
new_start_sector=2584575
exit 1 # drop this line exit 1 after you have change 2584575 using zzz calculate before by you
# opos is right after the new partition and given in bytes
opos=$(($new_start_sector * $sector_size + `blockdev --getsize64 $partition`))
echo "doing dd_rescue -v -r -S $opos $partition $disk"
dd_rescue -v -r -S $opos $partition $disk
Now you have move root partition but have to set the correct partition table.
# fdisk /dev/sda
(type m for help)
- active d command (delete partition)
- select 2 partition
- active n command (create partition)
- select p (primary partition)
- select first sector: choose
new_start_sector
defined before
- select last sector: choose default (last available)
- then it ask you:
Partition #2 contains a ext4 signature
Do you want to remove the signature? [Y]es/[N]o:
choose n (otherwise it delete all your data I suppose)
- activate w command to write new partition on disk
Now resize root partition by:
# e2fsck -f /dev/sda2
to check fs
# resize2fs /dev/sda2
Now delete boot partition and make it bigger:
# fdisk /dev/sda
(type m for help)
- active d command (delete partition)
- select 1 partition
- active n command (create partition)
- select p (primary partition)
- select first sector: choose default
- select last sector: choose
new_start_sector
- 1
- then it ask you:
Partition #1 contains a ext4 signature
Do you want to remove the signature? [Y]es/[N]o:
choose n (otherwise it delete all your data I suppose)
- choose a command (to make partition 1 bootable)
- select 1 partition
- activate w command to write new partition on disk
Now resize boot partition by:
# e2fsck -f /dev/sda1
to check fs
# resize2fs /dev/sda1
I restarted and all was ok.
I do this command on system-rescue-cd and at restart all history is gone so I try to remenber all to write there.
Be careful calculate the sector and byte to use in my example.
All this example is done by search on google various solutions
one
two
three
and more ...
remember
$ mv /home/<username>/.ssh/known_hosts.bak /home/<username>/.ssh/known_hosts
best regards,
Leonardo