[LINUX] Beginners add disks and use them as a file system (´ ・ ω ・ `)

About this article

Target Target: ** IT beginner Shobon-kun: ** </ font color> (´ ・ ω ・ `) ▶ ︎For beginners, an article whose purpose is to be an image-grabbing opportunity ▶ ︎ It may not be accurate due to the broken understanding.

Shobon-kun no Boyaki ** Shobon-kun: ** </ font color> (´ ・ ω ・ `)" I'll add a disk on Linux "

Introduction

This time, we will organize and implement the prerequisite knowledge for adding disks in Linux.

-[Add disk on Linux] -[About the file system](https://qiita.com/Pana/items/3925e3f650971202bfc0#%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82% B7% E3% 82% B9% E3% 83% 86% E3% 83% A0% E3% 81% AB% E3% 81% A4% E3% 81% 84% E3% 81% A6) -[Partition allocation](https://qiita.com/Pana/items/3925e3f650971202bfc0#%E3%83%91%E3%83%BC%E3%83%86%E3%83%BC%E3%82% B7% E3% 83% A7% E3% 83% B3% E3% 81% AE% E5% 89% B2% E3% 82% 8A% E5% BD% 93% E3% 81% A6) -[Format (logical)](https://qiita.com/Pana/items/3925e3f650971202bfc0#%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83 % 83% E3% 83% 88% E8% AB% 96% E7% 90% 86) -Mount

About the file system

The file system is one of the basic OS services that efficiently manages storage media such as HDDs and provides a means to use them as files. In most cases, adding a disk means making it available as a file system.

** Shobon-kun: ** </ font color> (´ ・ ω ・ `)" It is thanks to the file system that data can be handled as a file. "

I don't usually have a chance to mention the types of file systems, but Linux alone has "ext2, ext3, ext4, xfs ...". Currently, the de facto standard on Linux is ext4, an extension of ext3.

ext4 Maximum file size is 16TiB, maximum volume size is 1EiB The date range is from December 14, 1901 to April 25, 2514.

image.png

In the file system, the base VFS (Virtual File System) is used to determine and control which file system manages the partition of the storage medium, and to check the access right in the background. File processing such as write wait judgment is also performed.

image.png

A partition is one of the areas of the physical storage medium divided into logical areas.

** Shobon-kun: ** </ font color> (´ ・ ω ・ `)" Is it managed by the file system for each partition? "

To use a new storage medium as a file system, such as by adding a disk, you need to assign a partition and format it.

Partition allocation

To allocate a partition, let's make sure that the added storage medium is recognized and a device name is assigned.

On Linux, the device name of the hard disk is recognized as / dev / sd *.

Check partition information.


$ sudo fdisk -l

Disk /dev/xvdb: 2 GiB, 2147483648 bytes, 4194304 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				
Disklabel type: dos				
Disk identifier: 0x00025cdb				
				
Device     Boot Start     End Sectors Size Id Type				
/dev/xvdb1         63 4192964 4192902   2G 82 Linux swap / Solaris				
				
				
Disk /dev/xvda: 25 GiB, 26843701248 bytes, 52429104 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				
Disklabel type: dos				
Disk identifier: 0xe8275ee7				
				
Device     Boot  Start      End  Sectors  Size Id Type				
/dev/xvda1 *      2048   526335   524288  256M 83 Linux				
/dev/xvda2      526336 52428799 51902464 24.8G 83 Linux				
				
	
Disk /dev/xvdc: 100 GiB, 107374182400 bytes, 209715200 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				

Partition information exists on the disk used as the file system. Taking / dev / xvda as an example: 下書き一覧_-_Qiita.png

Since the disk without this information will be the disk added this time, Disk / dev / xvdc at the bottom will be the device name of the newly recognized disk in the above example. Also refer to the added disc size and confirm that it is the same disc. This time I added a 100GB disk.

You can also check the device name of the disk you are currently using with the following command. You may also want to make sure that it is not included below.

Check disk free space.


$ df -h

In order to use the newly added disk as a file system, it is necessary to set the partition first.

** Shobon-kun: ** </ font color> (´ ・ ω ・ `)" Because the file system is for each partition "

Partition creation is set interactively from the following command.

Creating a partition.


$ sudo fdisk /dev/xvdc

###Enter the key when the following is displayed. You can see help with m to see what keys are available
Command (m for help):m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


###Display partition information before setting
Command (m for help): p					
	Disk /dev/xvdc: 100 GiB, 107374182400 bytes, 209715200 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				
	Disklabel type: dos				
	Disk identifier: 0x144852f7	

###Creating a partition( n   add a new partition )
Command (m for help): n					
	Partition type				
	   p   primary (0 primary, 0 extended, 4 free)				
	   e   extended (container for logical partitions)

###Select partition type(This time the default primary)
Select (default p): p

###Select the number of partitions(Leave default Enter)
Partition number (1-4, default 1): 

###Size setting(Leave default Enter)
First sector (2048-209715199, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): 		
		

###Check partition information
Command (m for help): p					
	Disk /dev/xvdc: 100 GiB, 107374182400 bytes, 209715200 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				
	Disklabel type: dos				
	Disk identifier: 0x144852f7				
					
	Device     Boot     Start       End Sectors  Size Id Type				
	/dev/xvdc1      209715199 209715199       1  512B 83 Linux											
###End
Command (m for help): q

You have now created a partition that uses the additional disk in one area.

Format (logical)

Then format it and apply the file system on the partition. This is called a logical format. The format for using a storage medium as a storage medium is called a physical format, but it is different.

The logical format rewrites the file system management information instead of erasing the data.

Logical format.


$ mkfs -t ext4 /dev/xvdc1		

** Shobon-kun: ** </ font color> (´ ・ ω ・ `)" You are ready to use the added disk as a file system. 』\

mount

In order to actually use it as a file system, it is necessary to mount the created partition.

Linux has a tree-structured directory structure with the / (root) directory as the apex, but mounting is basically to make the partition that can be used as a file system part of the / directory. Say.

I think it's hard to get an image, so I made a diagram. For example, the image to mount the disk added this time to / mnt is as follows.

image.png

The mount command is: I checked if it can be mounted with a temporary mount and set a permanent mount.

Temporary mount.


$ sudo mount /dev/xvdc1 /mnt --types=ext4 --option=rw

Check the mount.


$ df -hT
<font color="DeepSkyBlue">**Shobon-kun:(´ ・ ω ・`)**</font color>"Additional disks can now be used as a file system! 』\

Will mount even after reboot/etc/Add to fstab.


#### **`Persistent mount.`**

$ sudo vi /etc/fstab

Add the following

/dev/xvdc1 /mnt ext4 defaults 0 0



Example of how to write fstab
!["Beginners add disks on Linux_´ ・ ω ・`_Edit_-_Qiita.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/147613/9c99b1ed-4a5a-5806-f22d-32a0dfb8585b.png)

<font color="DeepSkyBlue">**Shobon-kun:**</font color>(´ ・ ω ・`)"I wasn't aware of it until now, but I feel like I can be aware of which partition I'm using now."


With this disk addition, we separated partitions, formatted and mounted them in one physical device. Another option is to think of multiple devices as a single volume, separate them, and mount them.

#LVM(logical volume manager)




END