Add swap partition to running VM

Hi,

is it possible to add swap partition to running VM ? If yes, can you advice what attributes i have to set when attaching this partition to running VM ?

I tried various combination of parameters, but adding always fails.

BR

The swap partition can be added(or extended) to the existing file-system(swap) online or offline.

It is better to have partitions that has been configured over Logical Volume Management(LVM) as well.

To check further, please issue the command:
$ cat /etc/fstab
If the line starts with UUID=<some alphanumeric code) or /dev/sdaX, this means it’s a physical partition.
If the line starts with /dev/mapper/xyz, this means it’s a LVM partition.
$ lvdisplay
(to get logical volume related information)

Now from Root(#), create a partition using fdisk command. Let us assume that this partition is called /dev/sdc1(say)

# fdisk /dev/sdc (provided you have empty space in your HDD)

# mkswap /dev/sdc1 (making a swap file system)

# swapon /dev/sdc1 (enable the newly created swap partition)

# cat /etc/fstab (open this file and enter the below line for permanent effect after reboot - save it)
/dev/sdc1 swap swap defaults 0 0

# swapon -s (check whether the newly created swap area is available)
//you may also like to do swapoff -s and then again swapon -s if you do NOT prefer to reboot the system//

free -k

Thanks for explanation, I am familiar with LVM and also with swapon …
But I want to add new disk as device to serve as swap, like this in VMs Storage TAB ->

This will provide new disk to VM provisioned as SWAP partition with Contextualization scripts automatically. Unfortunately I was not able to add this new disk to running machine, it only works in state SHUTDOWN. When VM is in state RUNNING.

Problem is > error: Operation not supported: disk bus ‘ide’ cannot be hotplugged.

I have to set some other disk bus than “ide” so it can be hotplugged, but dunno what option and where to edit in Opennebula.

BR

not sure, but when attaching a new drive use a virtio or scsi which support hotplug (you will need to have the interface first)

you can find this option under the advance option when attaching a disk.

Which field is right ? There is no scsi / virtio option in Image mapping driver dropdown.

Hi

You should mention the version of OpenNebula that you are using.
In your case set “Device prefix” to sd for virtio-scsi or vd for virtio-blk.

Kind Regards,
Anton Todorov

1 Like

Thanks, it worked! I am using OpenNebula 5.x

Hi @atodorov_storpool,

do you know if it is possible to re-run contextualization scripts ? Based on Device prefix field, I was able to add new disk to running VM, but it will get provisioned as SWAP only after VM reboot. I suppose that these scripts are fired only when machine is booting.

Thanks.

Hi Martin,

To activate the swap you should enable the swap (assuming the swap disk is /dev/vdb) by appending a line to /etc/fstab and activate the swap:

echo '/dev/vdb swap swap defaults 0 0' >>/etc/fstab
swapon -a

Then you can verify the activation of the swap device:

cat /proc/swaps
Filename                                Type              Size      Used     Priority
/dev/vdb                                 partition         1048572  0        -1

Kind Regards,
Anton Todorov

Yes, understand. This is manual approach. But when I add disk like described in this thread and restart/reboot VM, OpenNebula vmcontext scripts create SWAP partition on vdb automatically at boot. This is what I want to do but without rebooting.

Is it possible ?