Skip to main content

[How-To] Format and Mount Data Drive on Debian Server

Purpose

This doc shows how to take an additional virtual hard drive on a linux vm and make a mount point on the main drive with it for nextcloud data. 

Prerequisites

List of prerequisites:

  • Root user or sudo user
  • Debian 12 LXC or VM

Instructions

Step 1: Create a Partition on the New Drive

  1. Use fdisk to partition the drive:
    sudo fdisk /dev/sdb
  2. Inside the fdisk utility:
    • Press n to create a new partition.
    • Select p for a primary partition.
    • Accept the default values for the first and last sectors (this will use the entire disk).
    • Press w to write the changes and exit.

Step 2: Format the Partition

  1. Verify the partition name (it should be /dev/sdb1 after partitioning):
    lsblk
  2. Format the partition with the ext4 filesystem:
    sudo mkfs.ext4 /dev/sdb1

Step 3: Mount the Partition

  1. Create a directory to mount the drive (e.g., /mnt/nextcloud-data):
    sudo mkdir /mnt/nextcloud-data
  2. Mount the partition to the directory:
    sudo mount /dev/sdb1 /mnt/nextcloud-data

Step 4: Make the Mount Persistent

  1. Get the UUID of the new partition:
    sudo blkid /dev/sdb1
    Example output:
    /dev/sdb1: UUID="abcd-1234-efgh-5678" TYPE="ext4"
  2. Edit the /etc/fstab file:
    sudo nano /etc/fstab
  3. Add the following line to the file:
    UUID=abcd-1234-efgh-5678 /mnt/nextcloud-data ext4 defaults 0 2
  4. Save and exit the editor.

Step 5: Update Nextcloud Configuration

  1. Move existing Nextcloud data to the new drive:

    sudo rsync -a /path/to/current/nextcloud/data/ /mnt/nextcloud-data/

    Replace /path/to/current/nextcloud/data/ with the actual path to your current Nextcloud data directory.

  2. Update the datadirectory path in Nextcloud's configuration file (config.php):

    sudo nano /var/www/nextcloud/config/config.php

    Find the datadirectory line and change it to:

    'datadirectory' => '/mnt/nextcloud-data',
  3. Set the correct permissions:

    sudo chown -R www-data:www-data /mnt/nextcloud-data

Step 6: Restart Services

  1. Restart the web server and PHP:

    sudo systemctl restart apache2
  2. Ensure everything works:

    • Log in to your Nextcloud instance.
    • Verify that the new data directory is being used.