TECH / Servers & Websites 4 min read

How to Add a Dedicated Disk for Swap in Ubuntu (Hyper-V & KVM)

Antonio NEMCD Author
Published Oct 5, 2019 · 4 min

When a Linux VM or server starts running tight on RAM, adding swap space keeps services from crashing under load spikes. If your root disk is already nearly full, the cleanest solution is attaching a separate virtual or physical drive dedicated to swap. Here are both ways to set it up on Ubuntu in Hyper-V (the exact same Linux commands work on KVM, Proxmox, and VMware).

Adding a dedicated second disk for Swap in Ubuntu and Hyper-V

Quick answer: attach a second disk to the VM, find its device name with lsblk (usually /dev/sdb), initialize it with sudo mkswap /dev/sdb, enable it with sudo swapon /dev/sdb, and add its UUID to /etc/fstab so it mounts automatically after reboot.

Step 1. Attach a Second Disk in Hyper-V and Find It in Ubuntu

In your Hyper-V virtual machine settings, add a new virtual hard disk of the desired size (32 GB in my example). I also removed the unused virtual DVD drive so the device list stays clean.

Adding a second virtual hard disk to Ubuntu in Hyper-V

Boot the VM, log into Ubuntu, and list attached block devices:

sudo lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
# or classic output:
sudo fdisk -l

You will see your OS disk (/dev/sda) alongside the brand-new unpartitioned drive (/dev/sdb):

Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk /dev/sdb: 32 GiB, 34359738368 bytes, 67108864 sectors

Warning: double-check the device name – everything on the target drive will be wiped.

Method 1 (Recommended): Use the Entire Second Disk for Swap

If the whole disk is dedicated strictly to swap, there is no reason to format ext4 and create a file on top of it – initializing the entire disk as swap is faster and cleaner:

sudo mkswap /dev/sdb
sudo swapon /dev/sdb

Verify that the new swap area is active:

swapon --show
free -h

Example output from swapon --show:

NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   1G   0B   -1
/dev/sdb  partition  32G   0B   -2

Make It Persist After Reboot (Using UUID)

To enable the swap disk automatically on every boot, add it to /etc/fstab. Using its permanent UUID is much safer than hardcoding /dev/sdb, which can shift if you add or remove virtual disks later. Grab the UUID first:

sudo blkid /dev/sdb

Copy the UUID="..." value and append this line to /etc/fstab (the nofail option prevents the VM from stalling at boot if the disk is ever detached):

UUID=your-uuid-from-blkid none swap sw,nofail 0 0

(If your VM disk layout never changes, /dev/sdb none swap sw,nofail 0 0 also works.)

Method 2: Create a Swap File on the Second Disk (If You Need Shared Storage)

If you want to store normal files on the second disk alongside swap, format it as ext4, mount it, and allocate a swap file:

sudo mkfs.ext4 /dev/sdb
sudo mkdir -p /swapdisk
sudo mount /dev/sdb /swapdisk
sudo fallocate -l 16G /swapdisk/swapfile
sudo chmod 600 /swapdisk/swapfile
sudo mkswap /swapdisk/swapfile
sudo swapon /swapdisk/swapfile

Setting chmod 600 is required so non-root users cannot read swapped memory pages. To persist both mounts across reboots, add both lines to /etc/fstab (disk mount first, then swap file):

/dev/sdb /swapdisk ext4 defaults,nofail 0 2
/swapdisk/swapfile none swap sw,nofail 0 0

Quick Server Tuning Tip: vm.swappiness

Ubuntu defaults to vm.swappiness=60, which starts swapping fairly early. On servers where swap is mainly a safety net against OOM Killer during traffic spikes, setting it to 10 is a solid default:

sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf

On budget Linux VPS plans with modest RAM (like entry-level servers at Fornex with 7% off or Vultr), a few gigabytes of NVMe-backed swap regularly saves databases and web servers from sudden crashes. And if you want a faster way to browse and edit configs like /etc/fstab, see my guide on installing Midnight Commander (mc) on Ubuntu.

Disclosure: the Fornex and Vultr links are affiliate links – your price stays standard or discounted, while the blog earns a small commission.

For a dedicated swap disk on Hyper-V or KVM, stick with Method 1 using mkswap and a UUID entry in fstab – fewer moving parts and rock-solid across reboots.

Related posts:

Leave a Comment