How to Increase swap space in a CentOS VM
Solution 1
These are the steps for both swap (the question) or a normal partition.
When logical volumes (LVM) are concerned, ignore answers you may find on the Internet about using gparted to resize partitions - but why would you when you have logical volumes? Instead we're gonna make an extra new partition and extend our logical volume to include it.
In VirtualBox, open the Virtual Media Manager. Find the related *.vdi file and ensure its Virtual Size is big enough for the extra space you want, slightly larger because the filesystem-structures need space. For the rest of this example we're gonna choose 64GB of extra space, so in this step I'll arbitrarily choose 65G.
Then, in your CentOS VM:
- Find the '/dev/mapper/?' entry of the logical volume you're expanding. For swap use
sudo fdisk -l
, but for enlarging normal mount-points it's easier to usedf -h
and look at Filesystem. The rest of this example we'll pretend it was '/dev/mapper/cl-root'. - Use
sudo lsblk
to find the drive (actually your vdi) that the volume lives on. For this example I'll pretend it was '/dev/sda'. sudo fdisk /dev/sda
. Use commandsn
(add partition) thenp
(primary). Accept all the defaults but for "Last sector" enter our choice of+64G
, or accept its default if you want all remaining space. Use commandsw
to write and quit. Reboot if asked.- In the last step we made a new partition, so if you hadn't rebooted then add that to the /dev table. Eg.
pvcreate /dev/sda3
. You may now be asked to reboot for the kernel to use the new table, so do it. sudo vgscan
to learn the name of the Volume Group. In the question it is 'cl'- Allow/extend the Volume Group to include the partition we made:
sudo vgextend cl /dev/sda3
- Get your intended logical volume to use the amount we want from it:
sudo lvextend -L +64G /dev/mapper/cl-root
. If it can't fit what you desire, it'll nicely tell you the max 'extents' and you can instead usesudo lvextend -l [extents] /dev/mapper/cl-root
- Skip this step for swap. Expand the filesystem into that area. For ext4 you'll need resize2fs, for xfs you'll need
sudo xfs_growfs /dev/mapper/cl-root
Solution 2
You can increase your swap space by first shrinking the root logical volume to free up space, which you can then reallocate to the swap logical volume. You need to increase your swap LV by 769 MB to get it up to 2048 MB, and your root LV has 3.1 GB free, so you can try the following:
- Shrink your root LV. The
-r
option causes the filesystem to be resized as well:lvresize -L -769M -r c1/root
- Temporarily turn off your swap, since you're going to be modifying it:
swapoff
- Resize your swap LV. Alternatively, delete it and recreate it using contiguous allocation so that the physical extends are adjacent to each other for potentially better performance:
lvresize -L +769M c1/swap
- Turn swap back on:
swapon
Solution 3
This is what I had to do:
With the VM halted, add a new virtual hard disk from Virtualbox by right clicking on the machine and then Configuration -> Storage. Click on the icon of a hard drive with a + sign and add a new disk of 2Gb fixed size.
After adding the new hard disk start the machine and once logged in type:
sudo vgdisplay
this will display the volume group information showing something like: VG Name cl
Then execute
sudo fdisk -l
to list the available drives. You should get in the list /dev/hdb or /dev/sdb depending on whether you are using spinning or solid states physical drives.
Now execute:
sudo pvcreate /dev/sdb
sudo vgextend cl /dev/sdb
sudo lvextend -L+2G /dev/cl/swap
this will add 2Gb to the swap space.
Related videos on Youtube
Serg M Ten
Updated on September 18, 2022Comments
-
Serg M Ten over 1 year
While trying to RPM install Oracle XE in a CentOS 7 VM running inside Virtualbox 5.1 I get the following error message:
This system does not meet the minimum requirements for swap space. Based on the amount of physical memory available on the system, Oracle Database 11g Express Edition requires 2048 MB of swap space. This system has 1279 MB of swap space. Configure more swap space on the system and retry the installation.
How can I increase the swap space?
So far I have tried with
lvm
following the instructions here and here without success. I must start with an already provisioned minimal CentOS VM so tweaking the original image is not an option.A
swapon -s
command informs me:Filename Type Size Used Priority /dev/dm-1 partition 1310716 0 -1
and
lvdisplay
output is:--- Logical volume --- LV Path /dev/cl/swap LV Name swap VG Name cl LV UUID rdNgH4-Sili-NpwP-Ny3S-xtU9-MlEC-ttE5JI LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-12-19 22:50:56 +0000 LV Status available # open 2 LV Size 1.25 GiB Current LE 320 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path /dev/cl/root LV Name root VG Name cl LV UUID H737Lq-OMhE-F9Lf-BlTg-3PL1-0T7O-1Ciusi LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-12-19 22:50:56 +0000 LV Status available # open 1 LV Size 12.39 GiB Current LE 3172 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
and
df -h
Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 13G 9.4G 3.1G 76% / devtmpfs 907M 0 907M 0% /dev tmpfs 920M 4.0K 920M 1% /dev/shm tmpfs 920M 8.7M 912M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 1014M 239M 776M 24% /boot tmpfs 184M 0 184M 0% /run/user/1002 tmpfs 184M 0 184M 0% /run/user/1001 tmpfs 184M 12K 184M 1% /run/user/1000 tmpfs 184M 0 184M 0% /run/user/2 /dev/sr0 57M 57M 0 100% /run/media/vagrant/VBOXADDITIONS_5.1.33_120529 vagrant 102G 77G 26G 76% /vagrant
Edit:
Content of
/etc/fstab
/dev/mapper/cl-root / xfs defaults 0 0 UUID=41388da5-fc65-4a34-a3da-58bb81daf744 /boot xfs defaults 0 0 /dev/mapper/cl-swap swap swap defaults 0 0
Output of
vgs
VG #PV #LV #SN Attr VSize VFree cl 1 2 0 wz--n- 13.64g 4.00m
-
fpmurphy about 6 yearsPlease provide contents of
/etc/fstab
-
Atul Vekariya about 6 yearsAdd new LV and create it swap. And provide the output of
vgs
command -
Serg M Ten about 6 yearsI added /etc/fstab and vgs. I think my first problem is VFree=4.00m right? Because all the available disk space is already taken I can't grow the swap file.
-
fpmurphy about 6 yearsI seem to recall that Oracle XE requires a swap file of at least twice the size of memory. How much memory have you configured for your VM?
-
Raman Sailopal about 6 yearsYou will need to add another disk to the virtual guest from hard drive space on the host. This disk will then need adding to the volume group on the guest and then allocated to the swap logical volume
-
-
Serg M Ten about 6 yearsThanks @emmanuel-rosa This would be a very good solution. Unfortunately, when I try
lvresize -L -769M -r /dev/mapper/cl-root
I getfsadm: Xfs filesystem shrinking is unsupported
and according to this reply it is impossible to shrink an XFS filesystem. SO I guess I'll have no ther choice than following @raman-sailopal advise and add another disk :(