In the case of Windows OS, it seems that the operation to the disk is a very high hurdle for Linux beginners because it is easy to do most of the work from "disk management".
In this article, I will introduce the work flow and the commands that appear in each process, assuming a scenario of "adding one HDD drive to a computer and saving data in that drive".
This is an excerpt from an article about a model called Dell OptiPlex 7050. OptiPlex 7050 Small Form Factor-Owner's Manual
In my environment, Linux is running on a virtual machine configured on the virtual environment, so in the figure below, it is confirmed that 3 virtual hard drives are installed as "VMware Virtual SCSI Hard Drive (0: x)". It's done.
This time, we will work on saving data for "VMware Virtual SCSI Hard Drive 0: 1". The appearance of the BIOS screen will change depending on the product you have, so if you don't know how to check it, you can ask the support of the product vendor or rely on Google image search.
ls -la /dev | grep sd
Commentary/A command to search for files and directories that contain the letters sd under dev
In the figure below, three hard drives are recognized. The image is as follows. LinuxOS L [HDD # 1 sda] "Partition sda1 / Partition sda2 / Partition sda3" L [HDD # 2 sdb] "No partition" L [HDD # 3 sdc] "No partition" Also, the "lsblk" command makes it easier to understand the interaction between drives and partitions from a hierarchical perspective as shown above. I'll say it after writing it, but this is easier to check than the above method. The description starting with this sd is an identifier that is usually assigned to a drive that is communicating with a connection standard such as SCSI / SATA / USB. There are other patterns, but please refer to the following page for them. [Chapter 3 Overview of Persistent Naming Attributes --Red Hat](https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/html/managing_storage_devices/assembly_overview-of-persistent-naming-attributes_managing- storage-devices) [Device File-Wikipedia](https://ja.wikipedia.org/wiki/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%83%95% E3% 82% A1% E3% 82% A4% E3% 83% AB) fdisk --LinuC Aegis
There are several partitioning commands in the Linux OS. The following are typical ones. [Fdisk] command-create and delete partition [Gdisk] command-Create and delete GPT-compatible partition [Parted] command-create and delete GPT-compatible partition
At this point, many beginners are wondering "Which one should I use?", But at the time of posting this article, "parted" is a good choice. The reason is that drives like HDDs and SSDs are now treated as either MBR format or GPT format and connected to the system. fdisk is a dedicated command for MBR format drives. gdisk is a dedicated command for GPT format drives. parted supports both. I personally recommend parted because parted is the latest command and there are many turns.
From here, I will illustrate the flow up to creating a partition using parted. First, in the figure below, I reconfirmed the recognition of the current drive with lsblk, and confirmed that the second drive confirmed by the BIOS was recognized as "sdb".
parted /dev/sdb print
Explanation Command to check the drive information of sdb,Also available by replacing sdb with another device
If you look at the third line from the bottom, it says "Partition Table: unknown". This is an uninitialized drive. First, perform initialization processing in GPT format. In the above figure, initialization is completed in GPT format with "parted / dev / sdb mklabel". In the second line from the top of the above figure, "New disk label type?", Press the tab key twice to display a list of formats that can be specified, which is convenient. In the 4th line from the bottom of the above figure, the GPT characters could be confirmed by the "print" command executed again.
Then create a partition. Work with the following command.
This time, by ending with mkpart, I entered the partition name / type / size specification interactively. When asked for the format type, you can check the type as shown above by pressing the tab key twice. To specify the size, enter the partition start position and end position on the drive, but enter the start position in% and specify the end position as a unit. You can also specify the unit for the start position, but if you set this as the start position from 0, the following warning will be displayed. A search for this error message "Warning: The resulting partition is not properly aligned for best performance." Has resulted in many reports. This is a warning to specify the sector start position with the recently introduced 4K drive in mind. This article was used as a reference as the root cause. [What to do if parted gives a performance warning](https://www.xmisao.com/2013/09/27/parted-warning-the-resulting-partition-is-not-properly-aligned-for -best-performance.html) This time, we will introduce a general drive expansion method, so we will not pursue this point in depth.
The simplest way to avoid the above error is to specify the starting position at 0%. Note that the 0% specification cannot be used when creating the second and subsequent partitions on one drive. In this case, enter a number that is close to the size of the previous partition. In the figure below, the second partition is created and the start position is specified as "10.1G". The layout is beautiful.
At this point, the rest is formatting and mounting. Proceed to the next step.
mkfs. ○○ format command. The corresponding file system name is entered in ○○. It was confirmed that the description of ext4 was added to the two partitions.
This time, I created directories named test01 and test02 under / mnt.
In the figure below, mount is executed and lsblk is used to confirm that the two partitions are mounted in the directories under / mnt.
You can now save the file. I created empty files named rhcsa and rhce in each directory.
After remounting, I checked with ls and confirmed that the file was alive.
In order for a particular file system to be automatically mounted in the specified directory after a reboot, it must be listed in a configuration file called "/ etc / fstab".
Added lines for automatic mounting to the 2nd and 3rd lines from the bottom in the figure below. (Edited in vi / etc / fstab.) On the far left is the partition identifier called UUID that sdb1 and sdb2 have. After that, the mount specification location and file system information will be posted, but please refer to the following for the description rules. Linux bean knowledge 175 "/ etc / fstab"-LinuC The meaning of the numbers in / etc / fstab-@IT
You can check the UUID for each partition with lsblk -f.
That's all for the work. After that, the file system specified by fstab will be mounted automatically even if you reboot.
If it is a reboot after editing fstab, do not panic and check the fstab file on the CLI screen. At the first prompt, you will be able to type the command after entering the root password.
If you check with cat / etc / fstab, do you know what is wrong?
In the above figure, I deleted "UUID =" from the beginning of the second line from the bottom and tried to cause a pseudo failure. You can also modify with the vi command from this screen. Here is the state after the correction.
After that, save it and restart it.
Recommended Posts