This chapter introduces the introduction of hard disk, how to store hard disk data, how to add hard disk to enterprise production server, partition hard disk, initialize hard disk, mount hard disk, etc.

1 Introduction to computer hard disk

The hard disk is one of the main storage media of a computer. It is composed of one or more disks made of aluminum or glass, which are covered with ferromagnetic materials. The inside of the hard disk is composed of tracks, cylinders, sectors, magnetic heads and other components, as shown in the figure:

/dev/sda, /dev/sda, /dev/sdb, /dev/sdc, /dev/vda. Different hard disk interfaces have different device names identified in the system.

IDE hard disk interface in Linux device name /dev/hda, SAS, SCSI, SATA hard disk interface in Linux device name sda, efficient cloud disk hard disk interface will be identified as /dev/vda, etc.

Files are stored on a hard disk. The smallest unit of storage on a hard disk is called a Sector. Each Sector contains 512 bytes. When the operating system reads a hard disk, it does not read one Sector at a time, which is very inefficient. To improve the reading efficiency, the operating system reads multiple sectors in a row, that is, reads multiple sectors at once, called a Block.

A Block consisting of multiple sectors is the smallest unit of file access. Block sizes include 1KB, 2KB, and 4KB. In Linux, blocks are usually set to 4KB (eight sectors in a row).

The Block size of a Linux partition can be viewed as follows:

[root@superman-vm01 ~]# stat /boot | grep "IO Block" 
  Size: 4096            Blocks: 8          IO Block: 4096   directory
[root@superman-vm01 ~]# 

For example, if a file size is 10Bytes, and the default Block size is 4K, if there are 10000 small files, since each Block can only hold one file, if the file size is larger than the default Block, more blocks will be applied. On the other hand, if the file size is smaller than the default Block, more blocks will be applied. One Block will still be occupied, so the rest of the space will be wasted.

  • 10000×10=100000Bytes=97.65625 Mbytes;
  • 10000x4096Bytes=40960000Bytes= 4000mbytes =40GB;
  • Depending on the actual needs of the enterprise, the Block can be set to 1K at this time to save more space.

2 Hard disk Block and Inode details

Generally speaking, the storage of file data by the operating system includes two parts: file content, permissions and file attributes. Operating system file storage is based on the file system, which stores the actual contents of the file in blocks and information such as permissions and properties in inodes.

In the hard disk partition, there is also a SuperBlock. The SuperBlock records the whole information of the file system, including the Inode, the total number of blocks, the size used, the remaining size, etc. Each Inode has a number corresponding to the Block. Convenient Linux system to locate and find files quickly.

  • SuperBlock: Record the overall information of the file system, including the total number of inodes and blocks, the size used, the remaining size, as well as the format and related information of the file system.
  • Inode: records the file’s properties, permissions, and the block number of the file’s data;
  • Block: Stores the contents of a file. If the file exceeds the default Block size, multiple blocks are automatically occupied.

Because each inode and block are numbered, and each file occupies an inode, the inode contains the block number in which the file’s data is placed. If you can find the inode of the file, you can find the block number of the data placed in the file and read the contents of the file.

When the operating system formats partitions, the operating system automatically divides the hard disk into two regions. One is the data Block, which is used to store file data. The other is the Inode Table area, which holds the meta information that inodes contain.

The default size of each inode is 128Bytes or 256Bytes. The default size of the /boot partition is 128Bytes, and the default size of the other partition is 256Bytes.

[root@superman-vm01 ~]# stat /boot | grep "Inode"   
Device: 801h/2049d      Inode: 64          Links: 5
[root@superman-vm01 ~]# 

3 Hard link introduction

In general, there is a one-to-one relationship between file names and inode numbers, with one file name for each inode number. However, on UNIX/Linux systems, multiple file names can also point to the same inode number. This means that the same content can be accessed with different file names, and changes to the file contents affect all file names. But deleting one file name does not affect the access of the other file name. This is called a hard link.

The command to create the hard link is: ln superman1.txt superman2.txt, where superman1.txt is the source file and superman2.txt is the target file. As mentioned above, the source file and the target file have the same inode number and both point to the same inode. There is an entry in the inode information called “link count”, which records the total number of filenames pointing to the inode. This increases by 1 to 2, as shown in Figure 7-3:

Also deleting a superman2.txt file will decrease the number of links in the superman1.txt inode node by 1. If the inode value drops to 0, indicating that no file name refers to the inode, the inode number and its corresponding block area are retrieved, as shown in the figure below:

Useful tip: Hard links do not cross partition links. Hard links only work for files, not directories. That is, directories cannot create hard links. Hard-linked source files and target files share the same inode value, in a sense, saving inode space. Whether the source file is deleted separately or the target file is deleted, the contents of the file always exist. At the same time, the linked files do not occupy excess space in the system.

4. Soft link introduction

In addition to hard links, there is another kind of link – soft links. The inode number of the file superman1.txt is different from that of the file superman2.txt, but the contents of the file superman2.txt are the path of the file superman1.txt. When the file superman2.txt is read, the system automatically directs the visitor to the file superman1.txt.

Whichever file you open, you end up reading the file superman1.txt. At this point, the file superman2.txt is called the “soft link” or “symbolic link” of the file superman1.txt.

The superman2.txt file is dependent on the superman1.txt file. If you delete the superman1.txt file, opening the superman2.txt file will get an error: “No such file or directory”.

The biggest difference between a soft link and a hard link is that the file superman2.txt points to the file name superman1.txt instead of the inode number of the file superman1.txt, so the inode number of the file superman1.txt does not change, as shown in the figure below:

Useful tip: Soft links can be linked across partitions. Soft links support links to directories as well as files. Soft-linked source files are not the same as the target inodes and, in a sense, consume Inode space. Whether the source file is deleted or the system is restarted, the soft link still exists, but the contents of the file will be lost. Once the source file name is created, the soft link file will return to normal.

5 Linux disk combat operation command

Enterprise real scene due to a large number of hard disk read and write all the year round, often appear bad disk, need to replace the hard disk. The new hard disk needs to be formatted and partitioned before it can be used by the Linux system. The virtual machine CentOS 7 Linux imitates the real server to add a new hard disk. It does not need to shut down, but can be directly inserted into the hard disk. The operation process of adding a new hard disk in the enterprise is as follows:

5.1 fdisk partition command parameters

Common parameters include m, n, p, e, d, w.

B. Edit BSD disklabel; C Switch DOS compatibility flag; D delete a partition; Create a new empty GPT partition table; Create an IRIX (SGI) partition table; L Lists known partition types; M Print Help Menu; N Add a new partition; O Create a new empty DOS partition table; P Print partition table information; Q exits without saving changes; Create a new empty Sun disk label; T changes the partition's system ID; U Change display/input unit; V Verify partition table; W writes the partition table to disk and exits; X Extra features.

5.2 Hard disk devices for detection and identification

The new disk is identified as /dev/sdb. If there are more than one disk, the device name is identified as /dev/sdc, /dev/sdd, etc., as shown in the figure:

fdisk -l

5.3 Create disk partitions

Create a disk partition /dev/sdb1 based on the new /dev/sdb device, as shown in the figure:

fdisk /dev/sdb

5.4 Create /dev/sdb1 partition method

Fdisk/dev/SDB, and then press n – p – 1 – Enter – Enter to Enter – w, and finally execute fdisk -l | head – 10, as shown in figure (a), shown in figure (b) :

(a) fdisk /dev/sdb creates /dev/sdb1 partition

(b) fdisk — l to see /dev/sdb1 partition

5.5 Formatted Partitions

Mkfs.xfs /dev/sdb1 format the disk partition as shown in the figure:

5.6 mount partitions

After the /dev/sdb1 partition is formatted, mount it to /data/ and add /etc/fstab to boot, as shown below:

Mount /dev/sdb1 /data mount /dev/sdb1 /data mount /dev/sdb1 /data mount /dev/sdb1 /data mount /dev/sdb1 /data mount /dev/sdb1 /data "/dev/sdb1 /data xfs defaults 0 0" >>/etc/fstab

Disk partitioning based on GPT format

The MBR partitioning standard determines that the MBR only supports hard disks under 2TB. In order to support the use of more than 2T hard disk space, the GPT format should be used for partitioning. To create partitions larger than 2TB, use the parted tool.

In a real enterprise environment, usually a server has multiple hard disks, and the entire capacity of the hard disks may be 10T. Therefore, the 10T hard disks need to be partitioned based on the GTP format. The operation steps are as follows:

We parted /dev/sdb mkpart primary 0 100% for /dev/sdb. We parted /dev/sdb mkpart primary 0 100% for /dev/sdb Mount /dev/sdb1 /data mount /dev/sdb1 /data mount /dev/sdb1 /data mount /dev/sdb1 /  xfs defaults 0 0" >>/etc/fstab

6.1 Hard disk devices detected and identified

fdisk -l

As shown in the figure, if /dev/sdb is a 10T hard disk, use GPT format to format the disk:

6.2 Set the partition type to GPT format

6.2.1 Non-interactive mode

Parted -s /dev/sdb mklabel GPT and parted /dev/sdb mkpart primary 0 100%

6.2.2 Interaction mode

Parted command line can also be partitioned, as shown in Figure (a), (b), and (c) :

parted > select /dev/sdb > mklabel gpt > mkpart primary 0 -1 > print > quit

6.3 Formatting Disk

The disk is formatted based on mkfs.xfs /dev/sdb, as shown in the figure:

6.4 mount partition

Create the mount directory and mount it:

7 Mount the command tool

The Mount command tool is mainly used to Mount the device or partition into the Linux system directory. When the Linux system partitioning, it also mounts the /dev/sda partition into the system directory based on the Mount mechanism. After the device and directory are mounted, the Linux operating system can store files.

7.1 Mount command parameters detail

Here are some of the common parameters of the Mount command in the enterprise:

mount [-Vh] mount -a [-fFnrsvw] [-t vfstype] mount [-fnrsvw] [-o options [,...]] device | dir mount [-fnrsvw] [-t Vfstype] [-o options] device dir-v: mount mount version number; -l: Display a list of loaded file systems; -h: Display help information and exit; -v: Output instruction execution details; -n: Load file system not written to /etc/mtab; -r: Loads the file system into read-only mode; -A: Load all filesystems configured in /etc/fstab; -o: The system will update the document time every time it reads a document; -o: The system will update the document time every time it reads a document. -o noatime: The system does not update the document time each time it reads a document; -o defaults: use default options for rw, suid, dev, exec, auto, nouser, etc.; -o exec allows execution files to be executed; -o user, -o nouser: mount/umount -o remount: mount the mounted system partition again in another remount mode; -o: mount in read-only mode; -o rw: mount in read-write mode; -o loop uses loop mode to mount files as devices to the system directory. -t: Specifies mount device type, common type NFS, NTFS-3G, VFAT, ISO9660, etc., where -t related instructions are as follow: ISO9660 disc or disc image; MSDOS FAT16 file system; VFAT FAT32 file system; NTFS; NTFS; NTFS-3G recognition of mobile hard disk format; SMBFS mounts Windows file network share; NFS UNIX /Linux file network sharing.

7.2 Common Mount cases in enterprises

Examples of Mount are as follows:

7.2.1 mount /dev/sdb1 partition to /data/ directory

mount /dev/sdb1 /data

7.2.2 mount CDROM to/MNT directory

mount /dev/cdrom /mnt

7.2.3 mount /dev/sdb1 mobile hard disk to /data directory

mount -t ntfs-3g /dev/sdb1 /data

Note: If prompted for unknown file system type “NTFS”, please execute “yum install -y ntfs-3G” to install the ntfs-3G package.

7.2.4 Re-mount/system in read-write mode

mount -o remount,rw /

7.2.5 mount the centos-7-x86_64-DVD-1810.iso image file to/MNT directory

mount -t iso9660 -o loop CentOS-7-x86_64-DVD-1810.iso /mnt

7.2.6 mount the usb disk /dev/sdd1 to/MNT /

mount -t vfat /dev/sdb1 /mnt

7.2.7 Mount the remote 192.168.1.182:/data directory to the local /data directory

Mount -t NFS 192.168.1.182:/data/ /data

Welcome to pay attention to my WeChat public number [super brother’s IT private food] for more technical dry goods!

If you have any jokes or feedback, just tell me! I will solve the problem you said, further better service you oh! Tips: If the QR code fails, you can also directly add WeChat ID: YSC13803862469!