Expand nested partition

Hi,

I have an OpenSuse Leap 15.4 VM on a VMware infrastructure.

The VM is fitted with a 20 GB disk.

The fdisk command returns the following:

srv-intranet:~ # fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (33554431 != 41943039) will be corrected by write.

Command (m for help): p

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
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: gpt
Disk identifier: 1F2FFE00-2217-499A-98F2-73277A00483D

Device        Start      End  Sectors Size Type
/dev/sda1      2048    18431    16384   8M BIOS boot
/dev/sda2     18432 29360127 29341696  14G Linux filesystem
/dev/sda3  29360128 33554398  4194271   2G Linux swap

How can I expand sda2 partition with the unused 6 GB ?

As it is a nested partition between sda1 and sda3, I’m not sure about the way to achieve that.

Thansk for help!

I have no idea how the fact that this is with VMware influences my raw outline that follows. But on a normal system, I probably would go like below. BTW I assume that with “nested” you mean “hemmed in between”. “Nested” means IMHO “one inside another”.

  • Make the swap entry in /etc/fstab into a comment by putting a # before it
  • New boot.
  • Now you can, with e.g. fdisk, remove sda3.
  • Enlarge sda2 to needed size.
  • Create new sda3 for swap after sda2 and write the new partition table and end fdisk.
  • Take note of the new UUID of the new swap partition (e.g. lsblk).
  • change the swap entry in /etc/fstab to the new UUID and remove the # from the begin of the line.
  • Reboot or use swapon to take the swap into use.
  • Use the proper tool for the type of file system on sda2 (you do not tell us what that is) to enlarge the file system to fit the partition.
  • Ready

Just a start for contemplation :wink:

By default, swap partition is used as resume device and if it is missing system probably will not boot (stop in initrd due to missing device). So, in addition to commenting out /etc/fstab it also should be removed from kernel command line (parameter resume=/dev/...) using YaST Bootloader module.

According to fdisk output only 4GB are unused.

Correct. However at boot the device is still there (although not used as swap when the system is started) and I assume the kernel parameter will still be satisfied.
BTW, instead of a boot, using swapoff will also take it out of use (and that is what matters).

I really hope that additions and corrections are added to my suggestion above.

Thanks for all the suggestions! I’m getting ready an operations list so that you can validate it.

In the mean time, one more question: is it possible to position the swap partition in the second place, in order to allow the remaining space to a /dev/sda3 linux system partition (see fdisk command return in my initial post)?

Everything is possible. E.g. by copying the whole of sda2 to somewhere else will free to remove sda2 and sda3. Then create new sda2 for swap, create new sda3. Then copy back from the saved contents. Depending on the way you did the copy, you may have to expand the file system (no detailed plan, just rough outline).
And of course adapt all the UUIDs in fstab and the kernel command line. But why this more complicated approach?

Just in case I need to resize the Linux filesystem partition in the furture, I guess it will be easier if it’s the last one…

In your first post you asked on how to expand sda2 to use all unused space (6GB according to you, 4GB according to fdisk). After that is done there will be no more space to resize into, not even in the future.

OK @hcvv, I get it. Sorry for side notes… Then let’s go in my original post direction.

My filesystem is BTRFS as returned by mount command:

srv-intranet:~ # mount | grep sda
/dev/sda2 on / type btrfs (rw,relatime,space_cache,subvolid=268,subvol=/@/.snapshots/1/snapshot)
/dev/sda2 on /.snapshots type btrfs (rw,relatime,space_cache,subvolid=267,subvol=/@/.snapshots)
/dev/sda2 on /boot/grub2/x86_64-efi type btrfs (rw,relatime,space_cache,subvolid=265,subvol=/@/boot/grub2/x86_64-efi)
/dev/sda2 on /boot/grub2/i386-pc type btrfs (rw,relatime,space_cache,subvolid=266,subvol=/@/boot/grub2/i386-pc)
/dev/sda2 on /opt type btrfs (rw,relatime,space_cache,subvolid=263,subvol=/@/opt)
/dev/sda2 on /home type btrfs (rw,relatime,space_cache,subvolid=264,subvol=/@/home)
/dev/sda2 on /var type btrfs (rw,relatime,space_cache,subvolid=258,subvol=/@/var)
/dev/sda2 on /usr/local type btrfs (rw,relatime,space_cache,subvolid=259,subvol=/@/usr/local)
/dev/sda2 on /srv type btrfs (rw,relatime,space_cache,subvolid=261,subvol=/@/srv)
/dev/sda2 on /tmp type btrfs (rw,relatime,space_cache,subvolid=260,subvol=/@/tmp)
/dev/sda2 on /root type btrfs (rw,relatime,space_cache,subvolid=262,subvol=/@/root)
/dev/sda2 on /opt/sentinelone/rpm_mount type btrfs (ro,relatime,space_cache,subvolid=268,subvol=/@/.snapshots/1/snapshot)

I then suggest the following detailed plan. Does it sound good to you?

srv-intranet:~ # swapoff
srv-intranet:~ # vim /etc/fstab
	# Comment swap line

srv-intranet:~ # fdisk /dev/sda

	#Delete sda3 partition
	Command:			d
	Partition number:	3

	#Delete sda2 partition
	Command:			d
	Partition number:	2
	
	# Create sda2 partition again (larger)
	Command:			n
	Partition type: 	p
	Partition number:	2
	First sector:		18432
	Last sector:		+18G
	
	# Type of new sda2 partition (Linux filesystem)
	Command:			t
	Partition number:	2
	Hex code:			20
	
	# Create sda3 partition again (swap)
	Command:			n
	Partition type:		p
	Partition number:	3
	First sector:		Default
	Last sector:		Default

	# Type of new sda3 partition (swap)
	Command:			t
	Partition number:	3
	Hex code:			19

	# Save changeset
	Command:			w

# Update kernel
srv-intranet:~ # partprobe

# Extend filesystem to new sda3 partition
srv-intranet:~ # btrfs filesystem resize max /

# Note UUID of new swap partition
srv-intranet:~ # lsblk

# Update fstab file with new UUID and uncomment swap line
srv-intranet:~ # vim /etc/fstab

# Format swap partition
srv-intranet:~ # mkswap /dev/sda3

# Enable swap
srv-intranet:~ # swapon

# Restart system
srv-intranet:~ # reboot

Thanks again!

Looks like a plan.

A few random remarks.

I do not use Btrfs and I am not sure if you can increase the btrfs / file system while it is active. Please check that.

I never used (or heard of) partprobe. Just read the man page. As I see it, you use it to avoid a boot and then can switch the resizing and the swap actions when compared with my first suggestion.

Out of the discussion with arvidjaar I think the conclusion is that you also have to adapt the kernel command in grub, because it has the UUID of the swap partition for the resume parameter, before you boot with the new swap partition.

Thanks a lot!

However, I don’t feel at ease with grub configuration. I’m working on a server with no GUI, so command lines only (YaST is a GUI as far as I know). I can’t find this resume parameter among config files… Would you be able to give me some more details on this topic?

First, YaST also functions in the terminal with a ncurses interface:

yast

The interface might be something to get used to (a lot of Tab-ing and Return and/or use the Alt-key with the underscored character).
Just give it a try to look around a bit before using in earnest.

I think in this case you can also change the configuration in /boot/grub2/grub.cfg because I do not think that a new initrd is needed for this.

I am really a bit amazed that you, using a text only openSUSE system, did not know about using YaST. For me YaST is one of the main reasons of using openSUSE, text only or not :wink:

This is the case anyway :face_with_diagonal_mouth:

I eventually managed to make the needed changes following this plan (nearly identical to the anticipated one):

srv-intranet:~ # swapoff -all
srv-intranet:~ # vim /etc/fstab
	# Comment swap line

srv-intranet:~ # fdisk /dev/sda

	#Delete sda3 partition
	Command:			d
	Partition number:	3

	#Delete sda2 partition
	Command:			d
	Partition number:	2
	
	# Create sda2 partition again (larger)
	Command:			n
	Partition number:	2
	First sector:		18432
	Last sector:		+18G
	Partition #2 contains a btrfs signature.
	Do you want to remove the signature? [Y]es/[N]o: N
	
	# Type of new sda2 partition (Linux filesystem)
	Command:			t
	Partition number:	2
	Hex code:			20
	
	# Create sda3 partition again (swap)
	Command:			n
	Partition type:		p
	Partition number:	3
	First sector:		Default
	Last sector:		Default

	# Type of new sda3 partition (swap)
	Command:			t
	Partition number:	3
	Hex code:			19

	# Save changeset
	Command:			w

# Update kernel (vérification avant modifs)
srv-intranet:~ # partprobe -s

# Update kernel
srv-intranet:~ # partprobe

# Extend filesystem to new sda3 partition
srv-intranet:~ # btrfs filesystem resize max /
Resize device id 1 (/dev/sda2) from 13.99GiB to max

# Format swap partition
srv-intranet:~ # mkswap /dev/sda3
Setting up swapspace version 1, size = 2 GiB (2138021888 bytes)
no label, UUID=cbd5bbd4-7a91-4a6e-bcca-08851951f285

# Update fstab file with new UUID and uncomment swap line
srv-intranet:~ # vim /etc/fstab

# Enable swap
srv-intranet:~ # swapon

# Restart system
srv-intranet:~ # reboot

After reboot:

srv-intranet:~ # lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   20G  0 disk
├─sda1   8:1    0    8M  0 part
├─sda2   8:2    0   18G  0 part /var
│                               /usr/local
│                               /tmp
│                               /srv
│                               /root
│                               /home
│                               /opt
│                               /boot/grub2/x86_64-efi
│                               /boot/grub2/i386-pc
│                               /.snapshots
│                               /
└─sda3   8:3    0    2G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom

and

srv-intranet:~ # fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
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: gpt
Disk identifier: 1F2FFE00-2217-499A-98F2-73277A00483D

Device        Start      End  Sectors Size Type
/dev/sda1      2048    18431    16384   8M BIOS boot
/dev/sda2     18432 37767167 37748736  18G Linux filesystem
/dev/sda3  37767168 41943006  4175839   2G Linux swap

Many thanks again for help!

Congratulations!

No thanks, glad to be of help, if only as listening and commenting person.

1 Like