Debian Tutorials

Debian Tutorials


Step by step tutorials showing you how to install and configure various applications and services on Debian based Linux distros.

April 2018
M T W T F S S
« Mar    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Categories


How to add a new hard disk or partition using UUID and ext4 filesystem

Ástþór IPÁstþór IP

Adding a additional hard disk to your workstation or server is easy and often required. Here’s we’ll step through the process of identifing the newly attached drive, prepare and mount it by referencing UUID which is a preferred method today.

If you have just added a virtual disk to a virtual machine, make sure you restart the virtual machine before mounting the new disk.

1. Figure out the device name for the new device

fdisk -l

This will give you output similar to this:

Disk /dev/sda: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders, total 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000299d1
 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 32088063 16043008 83 Linux
/dev/sda2 32090110 33552383 731137 5 Extended
/dev/sda5 32090112 33552383 731136 82 Linux swap / Solaris
 
Disk /dev/sdb: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders, total 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
 
Disk /dev/sdb doesn't contain a valid partition table

2. Next we’ll partion the new disk using the following command:

cfdisk /dev/sdb
 
> New -> Primary -> Specify size in MB
> Write -> yes
> Quit

3. Format the new disk using the ext4 filessystem

mkfs.ext4 /dev/sdb1

4. You need to create a new directory where the disk will be mounted in the filesystem

mkdir /disk2

You can name the folder whatever your want and place it in a subfolder of another mounting point, for example /var/disk2

5. It’s preferred to use the device UUID (Universally Unique Identifier) instead of directly linking to the device path because while UUID always stays the same, the device path may change. This is how we find the UUID:

blkid

Which shows a list of all partitions and the assigned UUID. The list should look similar to this:

/dev/sda5: UUID="180cab2a-300a-4e3d-8c8e-0e1df46b9bf7" TYPE="swap"
/dev/sda1: UUID="cd0c7b2c-bf50-4557-bc01-0048764a41d2" TYPE="ext4"
/dev/sdb1: UUID="359d90df-f17a-42f6-ab13-df13bf356de7" TYPE="ext4"

6. Add the new disk/partition to fstab to automatically mount it on boot

echo "UUID=359d90df-f17a-42f6-ab13-df13bf356de7 /disk2 ext4 errors=remount-ro 0 1" >> /etc/fstab

Replace the UDID value to the UDID displayed in step 5 for the new disk and replace /disk2 with the path where you want to mount the disk in the filesystem as specified in step 4

7. Manually mount the disk (you can also reboot the machine and it will be automatically mounted)

mount /disk2

/disk2 is the directory created in step 4

Now your new hard disk is mounted and ready to use.

Comments 13
  • Susan
    Posted on

    Susan Susan

    Author

    Thank-you, for taking the time to write this up. It works like a charm.


  • Arun
    Posted on

    Arun Arun

    Author

    Thanks. Works perfectly. You can also do, mount -a which will mount all the partitions.


  • Carl
    Posted on

    Carl Carl

    Author

    Thanks! Works like a charm..


  • John
    Posted on

    John John

    Author

    Thanks for creating a simple, concise guide. Very helpful.


  • Troy
    Posted on

    Troy Troy

    Author

    If you include the “disk label type” step to this it will also apply to Jessie. I went with GPT based on my drive being 4TB. Thanks for the write up.


  • Hua Zhang
    Posted on

    Hua Zhang Hua Zhang

    Author

    Very clear and nice article, thank you so much!


  • Sergei
    Posted on

    Sergei Sergei

    Author

    Thanks, helped a lot!


  • Alex
    Posted on

    Alex Alex

    Author

    Thanks for the detailed and easy-to-follow writeup!


  • Marc
    Posted on

    Marc Marc

    Author

    Thanks! I think this could almost work for Jessie. Main difference is the new cfdisk version first asks what label type the partition should have (gpt, dos, sgi, sun). Any hints?


  • Marc
    Posted on

    Marc Marc

    Author

    To answer my own question above: I think in most cases gpt is the correct type. See http://manpages.ubuntu.com/manpages/utopic/man8/fdisk.8.html


  • Jirka
    Posted on

    Jirka Jirka

    Author

    Thanks! Works Great!


  • Jim
    Posted on

    Jim Jim

    Author

    I changed the name from disk2 to usbbackupdrive, but I have a problem where it says it can’t find /usbbackupdrive/ in /etc/fstab


  • Jeremy
    Posted on

    Jeremy Jeremy

    Author

    Very clear, thanks!