Format, and Mount a Disk in Linux - Intermediate

Follow

The Below article, will assume that you have one of the following hardware configurations;

  • Stand-Alone Drive which presents itself to the OS as a device name like /dev/sdc
  • RAID Array configured on a RAID device, which presents itself to OS as a device name like /dev/sdc

Use the below command to initiate fdisk for formatting the disk. If the disk is over 3TB, and you're running an older version of FDISK, you'll need to use "parted" instead!!

root@server:~# fdisk /dev/sdc

Now, we'll have an fdisk shell. We want to print the current disk's information, to see if it's empty. In this case, it's a 480GB SSD Drive.

Command (m for help): p
Disk /dev/sdc: 480.1 GB, 480103981056 bytes
255 heads, 63 sectors/track, 58369 cylinders, total 937703088 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: 0x80ed6cbb
Device Boot Start End Blocks Id System

We've now confirmed we have the right disk. Now we move towards creating. The commands you need to create a simple full-size partition is included below. __ denotes "default - Hit Enter"

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): __
Using default value 1
First sector (2048-937703087, default 2048): __
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-937703087, default 937703087):
Using default value 937703087

Check the partition has shown up correctly.

Command (m for help): p
Disk /dev/sdc: 480.1 GB, 480103981056 bytes
255 heads, 63 sectors/track, 58369 cylinders, total 937703088 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: 0x80ed6cbb
Device Boot Start End Blocks Id System
/dev/sdc1 2048 937703087 468850520 83 Linux

All looks good, now we will go ahead and WRITE it!

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

So now, we have our partition, we want to create an ext4 filesystem on it.

root@server:~# mkfs.ext4 /dev/sdc1
mke2fs 1.42 (29-Nov-2011)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
29310976 inodes, 117212630 blocks
5860631 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
3578 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Now that the filesystem has been created, we will move ahead, and locate the disk's UUID, create a mount point, mount it, and configure it for mounting on boot.

Locate our Disk UUID, we'll only need UUID="9262f711-a6ba-4aad-dac4-a73eb92225e0" from this example.

root@server:~# blkid /dev/sdc1
/dev/sdc1: UUID="9262f711-a6ba-4aad-dac4-a73eb92225e0" TYPE="ext4"

In this example, we're going to configure to auto-mount it to "/db2" in the root file directory. Below is a default value, You will need to set the UUID, mount point, and filesystem type.

root@server:~# echo "UUID=9262f711-a6ba-4aad-dac4-a73eb92225e0 /db2 ext4 defaults 0 0" >> /etc/fstab

Now we want to create the mount point, and mount our new drive!

root@server:~# mkdir /db2
root@server:~# mount /db2

As you can see below, it's now mounted, and available in the below directory!

root@server:~# df -h /db2
Filesystem Size Used Avail Use% Mounted on
/dev/sdc1 441G 199M 418G 1% /db2

Have more questions? Submit a request

Comments

Powered by Zendesk