If you've worked with cloud servers, you've certainly encountered this need: the initial disk space is full and you need to add a new disk to the server. In the physical world, this means buying a new hard drive and opening the server case; but in the cloud environment, the standard solution is using a block disk. A block disk is an independent storage volume that you can attach to your server, format, and use as a separate partition or as part of a larger logical volume. In this article, you'll learn step by step how to attach a block disk to your cloud server, format it, mount it permanently, and finally resize it without losing data.
What is a block disk and why do you need it?
Block Storage is a low-level storage volume that attaches to your server as a block device (such as /dev/vdb). Unlike the system disk space created with the server, a block disk is an independent resource that you can move between servers, back up separately, and enlarge without needing to modify the operating system.
Common use cases for block disks include:
- Storing large databases (MySQL, PostgreSQL) separately from the system disk
- Holding user-uploaded files and media content
- Log files and backups that shouldn't occupy the main disk space
- Transferring data between servers without copying over the network
Important note: A block disk is raw by default and must be formatted before use. If you don't properly register it in the /etc/fstab file, the disk will become unavailable after a server restart.
Attaching a block disk to the server
After creating a block disk in the management panel (for example, in the ServerNet cloud service), you need to attach it to your server. This is usually done through the panel, and after attachment, the disk appears as a new device in the system.
Identifying the new device
After attaching, first make sure the system has detected the new device. On Linux, you can use the following command:
lsblk
You'll see output similar to this:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 20G 0 disk
└─vda1 252:1 0 20G 0 part /
vdb 252:16 0 100G 0 disk
In this example, vdb is the new block disk with 100GB capacity. If you don't see the device, wait a few seconds and run the command again. On some hypervisors, you may need to rescan the SCSI bus:
echo "- - -" > /sys/class/scsi_host/host0/scan
Common error: Confusing devices
Before taking any action, make sure you've selected the correct device. If you format the system disk (vda), you'll lose all your server data. Always use lsblk and check the disk size to ensure it's the device you want.
Formatting and mounting a block disk
After identifying the device, you need to format it. The choice of filesystem depends on your needs:
- ext4: The default and reliable option for most general purposes
- xfs: Suitable for large files and high performance, but shrinking it is difficult
- btrfs: Advanced features like built-in snapshots and compression
Formatting with ext4
mkfs.ext4 /dev/vdb
This command creates an ext4 filesystem on the disk. If the disk was previously used and has data, you'll be prompted for confirmation. Be aware that this operation erases all data on the disk.
Creating a mount directory and temporary mounting
mkdir -p /mnt/data
mount /dev/vdb /mnt/data
Now you can use the /mnt/data directory for storage. However, this mount is temporary and will be lost after a restart.
Permanent mounting with fstab
To have the disk automatically mounted after every restart, you need to register it in the /etc/fstab file. First, find the disk's UUID:
blkid /dev/vdb
Output similar to:
/dev/vdb: UUID="a1b2c3d4-..." TYPE="ext4"
Then add the following line to the end of /etc/fstab:
UUID=a1b2c3d4-... /mnt/data ext4 defaults,nofail 0 2
The nofail option is important; if the disk is unavailable, the server will still boot and won't wait during startup. After editing, verify the file's correctness with the following command:
mount -a
If there are no errors, the filesystem will be mounted correctly. Otherwise, fix the error before restarting.
Common error: Using device names in fstab
Never use device names (like /dev/vdb) in fstab. Device names may change after a restart (for example, to vdc) and the system won't be able to mount the disk. Always use the UUID.
Resizing a block disk without data loss
One of the most important advantages of a block disk is the ability to increase its size without deleting and recreating it. This is done in two stages: increasing the size in the management panel, then expanding the partition and filesystem in the operating system.
Step 1: Increasing the size in the panel
In your cloud server management panel (for example, in ServerNet), select the desired block disk and click the Resize option. Enter the new size (for example, from 100GB to 200GB) and confirm the operation. This is usually done without downtime, and the disk is enlarged hot.
Step 2: Expanding the partition in the operating system
After increasing the size in the panel, the operating system doesn't yet know the new size. First, rescan the device:
partprobe /dev/vdb
Then check the new size with lsblk. Now you need to enlarge the partition. If you formatted the disk without partitioning (directly on the device), you just need to expand the filesystem:
resize2fs /dev/vdb
If you used partitioning (for example, /dev/vdb1), first enlarge the partition. Use fdisk:
fdisk /dev/vdb
Inside fdisk:
- Press
pto view the partitions - Press
dto delete the partition (only the partition, not the data) - Press
nand create a new partition with the same number and starting point as before - Press
wto save the changes
After this, the partition is enlarged, but the filesystem is still small. Now expand the filesystem:
resize2fs /dev/vdb1
Finally, verify the new size:
df -h /mnt/data
Common error: Forgetting to expand the filesystem
Many users expect the space to be immediately available after increasing the size in the panel. But until resize2fs is executed, the system will still show the previous size. Always complete the partition and filesystem expansion steps after increasing the size in the panel.
Advanced management: Combining multiple disks with LVM
If you need to combine multiple block disks into a single volume, use LVM (Logical Volume Manager). This allows you to turn multiple disks into one large logical volume and later add new disks without stopping the service.
Creating physical volumes and a volume group
pvcreate /dev/vdb /dev/vdc
vgcreate vg_data /dev/vdb /dev/vdc
lvcreate -L 180G -n lv_data vg_data
Then create a filesystem on the logical volume:
mkfs.ext4 /dev/vg_data/lv_data
mkdir -p /mnt/data
mount /dev/vg_data/lv_data /mnt/data
For permanent mounting, use blkid /dev/vg_data/lv_data and register the UUID in fstab.
Resizing with LVM
When the volume group runs out of space, add a new disk and extend the volume group:
pvcreate /dev/vdd
vgextend vg_data /dev/vdd
lvextend -L 280G /dev/vg_data/lv_data
resize2fs /dev/vg_data/lv_data
With this method, you can add capacity indefinitely without your service being interrupted for even a moment.
Final tips and troubleshooting
In this section, we'll mention a few important tips that will help you in your daily work with block disks:
- Before any important operation (such as resizing or changing the filesystem), back up your data. You can save a snapshot of the block disk in the panel.
- If
resize2fsgives a "Device or resource busy" error after resizing, unmount the disk withumount /mnt/data, perform the operation, and mount it again. - For heavy databases, use the xfs filesystem, but remember that shrinking xfs is nearly impossible.
- Always include the
nofailoption in fstab so that if the disk is unavailable, the server still boots and you can fix the issue.
If you're using the ServerNet cloud service, you can easily create a block disk from the management panel and attach it to your server, and from the very beginning, by following the tips in this article, have a flexible and scalable storage infrastructure.
Proper block disk management is one of the essential skills for any cloud server administrator. By practicing these steps, you can increase your storage space whenever needed without worrying about data loss and take full advantage of the flexibility of your cloud infrastructure.