I have a VPS with only a SSH access. This is hosted on OVH.

The VPS is a Debian 9 (stretch). (I don't have LVM partition)

The size limit was a problem so I ordered an additional disk of 100G.

The default size of the disk was 25G and I already installed a few things on it and don't want to reconfigure everything.

Result of lsblk

sda      8:0    0   25G  0 disk
└─sda1   8:1    0   25G  0 part /
sdb      8:16   0  100G  0 disk

I want to extend the /var which is in the volume /dev/sda1. This volume is the bootable one and is currently in use.

What is recommended in my use case?

  • Unless I am not seeing something, you are not dealing with unallocated space on a disk where your sda1 partition could be "extended". The sdb listed is another storage disk alltogether. While it is possible to span a 'volume' across two storage devices, but it is a bit crazy. It becomes a "one-for-all, all-for-one" situation, such that if one of the drives fails, the whole volume goes up in smoke, and data recovery rates will vary between zero, and not enough for this to be a good idea. What you want to do is set-up a new mount point for /var . – BarBar1234 Dec 12 '19 at 16:55

What I believe you are looking is changing the mount point of /var to a new partition on disk sdb, while preserving all of the data in that folder. Since this is a running server, I will be as descriptive as I can, especially with respect to commands used and their flags.

Also, I would highly recommend putting your server in some administrative/stand-by/offline (aside from the current admin connection) mode, so there is no risk to data integrity (close any db connections and other unnecessary sockets at the very least)

Partition your new /dev/sdb disk

First, we need to setup sdb by creating a partition. Here I am going to assume /var is the one that is and will continue growing on your server, and accordingly I am going to dedicate virtually the full capacity of sdb to it (you can obviously adjust the size of the new partition as required for your own server, and leave unallocated space for future use if so desired).

Use parted to create a partion table on sdb

$ sudo parted /dev/sdb mklabel gpt

Next create a primary partition on the disk, aligning on cylinder, with ext4 format. The 1024MB corresponds to the free space in the beginning of the disk, which is way more than enough for writing any MBR (but will allow for some potential rescue mode, if ever needed). The 100% at the end means extend to the end of available space.

$ sudo parted -a cylinder /dev/sdb mkpart primary ext4 1024MB 100%

Check if everything was created correctly (this sample output was created on the fly, but yours should show two disks, with the new partition created and visible where /dev/sdb gets listed)

$ sudo parted -l

Model: [MODEL]
Disk /dev/sda: 25GB
Sector size (logical/physical): [*/*]
Partition Table: [mklabel]
Disk Flags: 

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  25GB  25GB  primary      ext4         lba

Model: [MODEL]
Disk /dev/sdb: 100GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   Type      File system  Flags
 1      1049MB  100GB  100GB  primary      ext4         

If everything looks good partion-wise you can move on.

Sync the data from /varto sdb1 .

  1. What needs to be done
    • make a new /mnt folder for sdb1 and mount it there
    • copy data from /var/*to sdb1
    • check for consistency with diff
    • check diff_var.log for inconsistency info

First, we need to mount our new sdb1 partition. Your system will likely have /mnt present already, but if not the -p flag in the first command will take care of it.

$ sudo mkdir -p /mnt/sdb1
$ sudo mount /dev/sdb1 /mnt/sdb1

  1. Next, we rsync our /var/* data to it, and check for data consistency.

The -a flag is a shortcut of multiple other rsync flags combined together ( with the exception of the hard links -H flag), and means rsync everything there, recursively. The -v flag will show you a list of the files as they are being copied. The folder var will be created on /dev/sdb1

$ sudo rsync -av /var/* /mnt/sdb1

  1. Once that is done, check for consistency between the two folders, on two different partitions with diff with the -r recursive flag . This will be another time-consuming operatio. Also, redirecting the output to a log file is highly advisable.
$ sudo diff -r /var /mnt/sdb1 > ~/diff_var.log

If you check diff_var.log, will likely have some lines resembling the following:

# socket - no point in copying directly
File /var/*/[socket] is a socket while file /mnt/sdb1/var/*/[socket] is a socket

# Self-explanatory, log file update between `diff` and `rsync`*
diff -r /var/log/* /mnt/sdb1/var/log/* differs
< Dec 12 13:05:01 * * *

# Broken symlink
diff: /var/*/*/ABC No such file or directory
diff: /mnt/sdb1/var/*/*/ABC No such file or directory

  1. Unmount sdb1 partition.
$ sudo umount /mnt/sdb1

Add the new entry to /etc/fstab

  1. Backup your current /etc/fstab
$ sudo cp /etc/fstab /etc/fstab.back
  1. This is one is a test, and I highly recommended going through the steps It involves:
    • getting your /dev/sdb1 UUID , and reformatting in /etc/fstab format
    • checking if the new partition mounts correctly from a new /etc/fstab entry
###Getting your UUID
$ sudo blkid -s UUID /dev/sdb1
/dev/sdb1: UUID="94c3f14d-508d-4621-9000-cbd4fc4c7445"

# var that holds a properly formatted line for /etc/fstab
$ my_uuid_sdb1=$(blkid -s UUID /dev/sdb1 | cut -d: -f2 | tr -d '"') 

# echo it to make sure
$ echo $my_uuid_sdb1

If the ouput of the above command looks good on your system, you can append it to /etc/fstab as follows.

$ sudo echo "$my_uuid_sdb1 /mnt/sdb1 ext4 defaults 0 2" >> /etc/fstab

Just to make sure, diff the two fstab files.

# diff /etc/fstab /etc/fstab.back should only report one new line
$ sudo diff /etc/fstab /etc/fstab.back
< UUID=94c3f14d-508d-4621-9000-cbd4fc4c7445 /mnt/sdb1 ext4 defaults 0 2

Test it with mount -a and your new partition should report as successfully mounted like so:

$ sudo mount -av
/                        : ignored
/mnt/sdb1                : successfully mounted

#checking with ls to see if we can see /var folder structure 
$ sudo ls /mnt/sdb1
*******  cache  local  log  *****   run    *****   tmp       
backups  lib    lock   mail  opt    spool  www     ***

Unmount it again

$ sudo umount /mnt/sdb1

Backup current /var to /var.back and change the mount-point of /dev/sdb1 to /var

$ sudo mv /var /var.back

### remake /var dir in /
$ sudo mkdir /var

Edit /etc/fstab again, and ensure the line was propely changed (better yet re-check whole /etc/fstab).

# Use a visual editor and change /mnt/sdb1 to /var in /etc/fstab
# or use awk with gsub and inplace, directly from terminal  
$ sudo awk -i inplace  '/mnt\sdb1/gsub(/mnt\/sdb1/,"var")' /etc/fstab

$ sudo grep var /etc/fstab 
UUID=94c3f14d-508d-4621-9000-cbd4fc4c7445 /var ext4 defaults 0 2

First run mount -av and check the mount-point list with df for sdb1 mounted on /var

$ sudo mount -av 

$ sudo df -h /var
Filesystem      Size   Used   Avail Use% Mounted on
/dev/sdb1       50.7G  6.9G  1.4G   14% /var

If it all looks good, reboot your server.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.