0

I have an Ubuntu 19.04 Virtual Machine and I connect to it by ssh.

I have to resize boot partition.

# fdisk -l
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc59d8598

Device     Boot  Start       End   Sectors   Size Id Type
/dev/sda1  *      2048    487423    485376   237M 83 Linux
/dev/sda2       487424 629143551 628656128 299.8G 83 Linux

I like to resize sda1 to 1024M and cannot use live cd.
on my VM I am root user.

I think I have to resize root partition sda2 and move it forward and then resize sda1.

Need a little help to do this operation.

I haven't find any question like mine: only example on one root partition (sda2) and without the need to move it by make space to boot partition (sda1)

can you help?

best regards,
Leonardo

P.S. I haven't to lost data on partition :-)

Update

I discover that my provider let me start a system-rescue-cd on my vm so I have all vm partition unmounted but I am in a command line.

Can I do the job now?

  • There’s a possible way of doing this here https://unix.stackexchange.com/a/227318/11266 . Alternatively can you add a disk to the vm, copy your partitions and resize. Not sure how you would proceed from there though and get it to boot flawlessly – PonJar Nov 07 '19 at 12:37
  • "I like to resize sda1 to 1024M and cannot use live cd." Why can't you use a LiveCD? You attach it to the VM as the ISO file it was downloaded as. You cannot resize partitions while they are in use when you are booted to the OS. – Terrance Nov 07 '19 at 14:00
  • @Terrance can you add some detail? I download the iso on my remote vm disk and then ... – Leonardo Saracini Nov 07 '19 at 14:11
  • You download it to the host system then attach it as a CD device to the VM. Do you have access to the host system? – Terrance Nov 07 '19 at 14:15
  • I guess though if you don't have access to the host system, then you would have to attach the ISO within the VM by adding it as a loop device to the grub then boot to it. However, it looks like with 19.10 that grub 2.04 does not support that https://askubuntu.com/questions/1186942/how-to-downgrade-grub-from-2-04-to-2-02-on-ubuntu-19-04 so you might be at an impasse here with trying to resize the partition. You might have to backup your files then redo the installation. – Terrance Nov 07 '19 at 14:20
  • ok thank Terrance and PonJar – Leonardo Saracini Nov 07 '19 at 14:23

1 Answers1

0

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.

  1. I connect in ssh to system-rescue-cd
  2. I check my partition was not mount by
    # umonunt /dev/sda1
    # umonunt /dev/sda2
    system tell me that they are not mounted.
  3. # e2fsck -f /dev/sda2 to check fs
  4. my sda2 is 300Gb most empty. I resize it to 200GB:
    # resize2fs /dev/sda2 200G (shrink)
  5. 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
  6. now I have to set the partition table:
    1. # fdisk /dev/sda (type m for help)
    2. active d command (delete partition)
    3. select 2 partition
    4. active n command (create partition)
    5. select p (primary partition)
    6. select first sector: choose default (same of before)
    7. select last sector: +yyyyyk (if yyyyy is 3746596 choose +3746596k) note the + and the k
    8. 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)
    9. 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.

  1. # fdisk /dev/sda (type m for help)
    1. active d command (delete partition)
    2. select 2 partition
    3. active n command (create partition)
    4. select p (primary partition)
    5. select first sector: choose new_start_sector defined before
    6. select last sector: choose default (last available)
    7. 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)
    8. 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:

  1. # fdisk /dev/sda (type m for help)
    1. active d command (delete partition)
    2. select 1 partition
    3. active n command (create partition)
    4. select p (primary partition)
    5. select first sector: choose default
    6. select last sector: choose new_start_sector - 1
    7. 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)
    8. choose a command (to make partition 1 bootable)
    9. select 1 partition
    10. 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