Initial format of new physical disk and mounting on file system

I will explain how to format a new disc initially. If you add a new physical disk, you need to perform the initial format before using it.

Refer to the following for how to check the newly added physical disk.

Check the physically attached disk --fdisk -l

Create and format partition

First of all, create a partition. A partition is a logical disk.

One physical disk can be treated as multiple logical disks, or multiple physical disks can be treated as one large-sized logical disk.

You need to create a partition even if there is only one partition for the disk.

This section describes how to allocate one partition for a new physical disk.

First, use the parted command to partition and format the physical disk.

You can also use fdisk, but here we use a command called parted that allows you to execute commands instead of interactively.

In the following explanation, the newly added physical disk is referred to as "/ dev / sdc".

The file system type is Ubuntu's default ext4. (This is just a hint on the partition, the next step is to specify the actual file system.)

sudo parted / dev / sdc --script mklabel gpt mkpart ext4part ext4 0%100%

Let's confirm that the partition has been created.

sudo fdisk -l

A partition called "/ dev / sdc1" will be created.

Disk / dev / sdc: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical / physical): 512 bytes / 4096 bytes
I / O size (minimum / optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: D5DDBFFA-278E-417C-9DA7-7FF50891E1FD

Device Start End Sectors Size Type
/ dev / sdc1 2048 2147481599 2147479552 1024G Linux filesystem

Creating a Linux file system

Next, create a Linux file system for the partition.

Use the mkfs command to create a Linux file system. There is a command called "mkfs.ext4" for the XFS file, so use this.

Specify the partition as an argument.

sudo mkfs.ext4 /dev/sdc1

This is a sample output result.

meta-data = / dev / sdc1 isize = 512 agcount = 4, agsize = 67108736 blks
         = sectsz = 4096 attr = 2, projid32bit = 1
         = crc = 1 finobt = 1, sparse = 0, rmapbt = 0, reflink = 0
data = bsize = 4096 blocks = 268434944, imaxpct = 25
         = sunit = 0 swidth = 0 blks
naming = version 2 bsize = 4096 ascii-ci = 0 ftype = 1
log = internal log bsize = 4096 blocks = 131071, version = 2
         = sectsz = 4096 sunit = 1 blks, lazy-count = 1
realtime = none extsz = 4096 blocks = 0, rtextents = 0

Use partprobe to immediately recognize it as a file system.

sudo partprobe / dev / sdc1

Is the partition recognized as a Linux file system? Look at "/ dev" with the ls command Let's do it.

ls / dev | grep sd

"/ Dev / sdc1" is recognized as a file system.

sda
sda1
sda14
sda15
sdb
sdb1
sdc
sdc1

Mount to file system

The last is mounting to the file system. "/ Dev / sdc1" is a device file that points to the partition. This is a special file and cannot be treated as a normal file system directory.

You need to bind the device files to a regular directory. This is called a mount.

The directory name can be decided freely, but here it is "/ datadrive".

First, create an empty directory.

sudo mkdir / datadrive

Then mount "/ dev / sdc2" with "/ datadrive".

sudo mount / dev / sdc1 / datadrive

Use the df command to see if the mount was successful.

df -h

"/ Dev / sdc1" is tied to "/ datadrive".

/ dev / sdc1 1.0T 1.1G 1023G 1%/ datadrive

That's it. However, the mount is temporary and requires additional steps to mount automatically when Ubuntu restarts.

Auto mount settings

To mount automatically when Ubuntu is restarted, you first need to know an ID called UUID that can uniquely identify the partition.

Use the blkid command to get the UUID.

sudo blkid
/ dev / sda1: LABEL = "cloudimg-rootfs" UUID = "bdf328db-9606-49ba-8e44-5e1299ae56f0" TYPE = "ext4" PARTUUID = "179b1e79-bd71-4241-9557-8be63cbb8857"
/ dev / sda15: LABEL = "UEFI" UUID = "54D1-0F3A" TYPE = "vfat" PARTUUID = "ec4144dd-3a40-4774-bfc2-73488a3f8e39"
/ dev / sdb1: UUID = "61c57ec5-96f9-4c18-8d7e-d7b05dd48b76" TYPE = "ext4" PARTUUID = "a252cd85-01"
/ dev / sdc1: UUID = "c85b796f-b9ff-4b4d-9091-47327999d8aa" TYPE = "ext4" PARTLABEL = "ext4part" PARTUUID = "d2295ab7-22fb-49a2-a3d0-1a9819207562"
/ dev / sda14: PARTUUID = "bddfd6d3-8ef9-40d2-8fda-0083f3e0df9b"

Copy the UUID double quotes of the newly added partition "sdc1" and save it somewhere.

Next, make a backup of "/ etc / fstab" in case you get into a situation where you cannot restart the OS.

sudo cp /etc/fstab /etc/fstab.original

Edit "/ etc / fstab". By writing to this config file, it will be mounted automatically on reboot.

sudo vi / etc / fstab

Add the following line to "/ etc / fstab" using the UUID you copied in your environment.

UUID = c85b796f-b9ff-4b4d-9091-47327999d8aa / datadrive ext4 defaults, nofail 1 2

If you specify nofail, you can restart even if the mount fails, so you can rest assured. 1 in the 5th column is 1 because it is a data disk whether to dump or not. 2 (2 or more) in the 6th column is specified when the file system is not the root etc. and normal data is stored.

If you make an incorrect setting for fstab, the OS will fail to restart with an error message like the one below, and you will not be able to start it, so be careful.

The disk drive for / datadrive is not ready yet or not present.
Continue to wait, or Press S to skip mounting or M for manual recovery

Let's check how to recover from fstab error in VPS and each cloud service before doing it.

In VPS and each cloud service, it seems that the procedure is to skip the mount from the console, restart the OS and fix "/ etc / fstab".

How to use the new data area for other directories?

What if you want to use a new "/ datadrive" for "/ var / backup", for example?

Use symbolic links.

Create a directory called "/ var / backup" in "/ datadrive" and create a symbolic link from "/ var / backup" to "/ datadrive / var / backup".

mkdir -p / datadrive / var / backup
ls -s / datadrive / var / backup / var / backup

If you make a mistake and want to undo it

This is an explanation when you make a mistake in the procedure and want to restore it.

Unmount

Use the umount command to unmount.

sudo umount / datadrive

If you are in a directory or using that directory with a cd etc., it will be busy and you will not be able to unmount it.

If someone is using it, you can check it with the following command.

fuser -muv / datadrive

Make sure no one is inside before unmounting.

Repartition

After dismounting, you can recreate the partition by rerunning the parted command.

Recreate file system

The mkfs command also asks if you want to recreate the file system, so press y to proceed.

Associated Information