[Linux] Disk Management and File System

1. Disk Basics

1.1 Disk structure

硬盘的物理结构

platter: The hard disk has multiple platters,2 sides per platter

magnetic head:one head per side

insert image description here

硬盘的数据结构

sector: The disk is divided into multiple sectors, each sectorstore 512 bytesThe data,The smallest storage unit of a hard disk.

track: same platterConcentric circles of different radii, is the circular trajectory drawn by the magnetic head on the surface of the disk.

cylinder:Different discs have the same radiusA cylindrical surface consisting ofIt consists of multiple tracks of the same radius circle.

insert image description here

  • Hard disk storage capacity = number of heads x number of tracks (cylinders) x number of sectors per track x number of bytes per sector (512 bytes)

  • Can usecylinder/head/sectorComeunique positioningEvery region on the disk

  • Disk Interface Type

● IDE, SATA, SCSI, SAS, Fiber Channel

Interface Type Features
IDE Parallel data cableConnect the motherboard and hard disk, the anti-interference is too poor, and the cable takes up a lot of space, which is not conducive to the internal heat dissipation of the computer.Has been gradually replaced by SATA
SATA Strong anti-interference,Support hot plug and other functions, fast speed, strong error correction ability
SCSI Small computer system interface, SCSI hard disk widelyWorkstation-class PCs and serversLow CPU usage and fast speed during data transmission, support hot swapping etc.
SAS It is a new generation of SCSI technology, which is the same as SATA hard disk.Adopt serial technology for higher transmission speed, up to 6Gb/s

1.2 MBR

Master Boot Record (MBR: Master Boot Record)

MBR is located on the hard diskat the first physical sector

MBR contains the hard disk's master boot program and hard disk partition table

● partition tableThere are 4 partition record areas, and each partition record area occupies 16 bytes

● Linux AdmiralDevices such as hard disks and partitions are represented as files

insert image description here

1.3 Disk partition structure

in the hard diskThe number of primary partitions is only 4

The sequence numbers of primary partitions and extended partitions are limited to 1~4

Extended partitions are subdivided into logical partitions

The serial number of the logical partition will always start from 5

insert image description here

2. File system type

2.1 XFS file system

● storagePartitioning of file and directory data

High-performance journaling file system, especially good at handling large files, can support millions of terabytes of storage space

The file system used by default in the CentOS 7 system

2.2 SWAP, swap file system

● Create a swap partition for the Linux system

● generalSet to 1.5 to 2 times the physical memory

Other Filesystem Types Supported by Linux

● EXT4、FAT32、NTFS、LVM

注意: CentOS 6The default file system used isEXT4/3; CentOS 7The default file system used isXFS.

2.3 fdisk command

fdisk command

View or manage disk partitions

fdisk -l [disk device] or fdisk [disk device]

交互模式中的常用指令

m、p、n、d、t、 w、q

[root@clr ~]# fdisk -l (/dev/sda) #查看系统中磁盘分区情况,sd表示使用SCSI设备

磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00099d02

   设备 Boot      Start         End      Blocks(块大小)   Id(系统对应ID号)  System(系统类型)
/dev/sda1   *(引导分区)     2099200    79771647    38836224   83  Linux
/dev/sda2       117698560   125829119     4065280    5  Extended
/dev/sda5       117700608   125829119     4064256   82  Linux swap / Solaris(交换分区)

2.4 Create a new hard disk

新增一块硬盘,步骤如下所示:

Settings - Hard Disk - Add;

insert image description here

Hard disk, and then select Next;

insert image description here

insert image description here
insert image description here

Modify the disk size to 40GB;

insert image description here

Finalize it;

insert image description here

Reboot reboot, check the newly added hard disk partition after refreshing;

[root@clr ~]# fdisk -l

磁盘 /dev/sdb:42.9 GB, 42949672960 字节,83886080 个扇区 #新增加的第二块硬盘sdb,b代表是第二块
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00099d02

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *     2099200    79771647    38836224   83  Linux
/dev/sda2       117698560   125829119     4065280    5  Extended
/dev/sda5       117700608   125829119     4064256   82  Linux swap / Solaris

Refresh the disk partition status online;

[root@clr ~]# echo "- - -" > /sys/class/scsi_host/host0/scan  #在线刷新磁盘命令
[root@clr ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@clr ~]# echo "- - -" > /sys/class/scsi_host/host2/scan

[root@clr ~]# fdisk -l

磁盘 /dev/sdb:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00099d02

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *     2099200    79771647    38836224   83  Linux
/dev/sda2       117698560   125829119     4065280    5  Extended
/dev/sda5       117700608   125829119     4064256   82  Linux swap / Solaris

磁盘 /dev/sdc:21.5 GB, 21474836480 字节,41943040 个扇区 #新增加的第三块磁盘分区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

Partition the disk /dev/sdb again;

insert image description here
insert image description here
insert image description here
insert image description here

3. Create a file system

3.1 mkfs

After partitioning the new hard disk, you need to partitionto format(that is, create a file system), and mount it to the Linux systemspecified directoryThen it can be used to store data such as files and directories.

mkfs命令
insert image description here
示例
insert image description here

[root@clr ~]# ls /sbin/mkfs* #查看以mkfs开头的所有文件
/sbin/mkfs        /sbin/mkfs.cramfs  /sbin/mkfs.ext3  /sbin/mkfs.fat    /sbin/mkfs.msdos  /sbin/mkfs.xfs
/sbin/mkfs.btrfs  /sbin/mkfs.ext2    /sbin/mkfs.ext4  /sbin/mkfs.minix  /sbin/mkfs.vfat

mkfs -t xfs [specified partition]: format the specified partition as xfs type;

[root@clr ~]# mkfs -t 
btrfs   cramfs  ext2    ext3    ext4    fat     minix   msdos   vfat    xfs     

[root@clr ~]# mkfs -t xfs /dev/sdb1 #将/dev/sdb指定为xfs类型的文件系统
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

mkfs.ext4 [specified partition]: format the specified partition as ext4 type;

[root@clr ~]# mkfs.
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat    
mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs     
[root@clr ~]# mkfs.ext4 /dev/sdb5 #将/dev/sdb5指定为ext4类型的文件系统
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 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

Allocating group tables: 完成                            

mkswap command

insert image description here

mkswap [partition device]: format the file system for the specified swap partition;

[root@clr ~]# mkswap /dev/sdc1  #为交换分区/dev/sdc1创建文件系统
正在设置交换空间版本 1,大小 = 5242876 KiB
无标签,UUID=90f22d9c-46b4-4faa-a4ae-7ee42ee0a439

free -m : View the system memory, -m unified unit, view memory in megabytes

[root@clr ~]# free -m #查看系统内存情况,-m统一单位,以兆为单位查看内存
              total        used        free      shared  buff/cache   available
Mem:           3931         772        2446          17         713        2921
Swap:          3968           0        3968

free -h : View the memory situation in a humane way

[root@clr ~]# free -h #以人性化方式,查看内存情况
              total        used        free      shared  buff/cache   available
Mem:           3.8G        772M        2.4G         17M        713M        2.9G
Swap:          3.9G          0B        3.9G

swapon [partition device]: enable the newly added swap partition;

[root@clr ~]# swapon /dev/sdc1  #启用新增加的交换分区/dev/sdc1
[root@clr ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3931         775        2442          17         713        2917
Swap:          9088           0        9088
[root@clr ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.8G        775M        2.4G         17M        713M        2.8G
Swap:          8.9G          0B        8.9G

swapon -s: Check which devices provide the swap partition in the system;

[root@clr ~]# swapon -s  #查看系统中的交换分区由哪几个设备提供
文件名				类型		大小	已用	权限
/dev/sda5                              	partition	4064252	0	-2
/dev/sdc1                              	partition	5242876	0	-3

swapoff [partition device]: disable the specified swap partition device;

[root@clr ~]# swapoff /dev/sdc1 #停用指定的交换分区设备/dev/sdc1
[root@clr ~]# swapon -s #查看系统中的交换分区由哪几个设备提供
文件名				类型		大小	已用	权限
/dev/sda5                              	partition	4064252	0	-2

swapoff -a : Disable all swap partitions in the system;

[root@clr ~]# swapoff -a  #停用系统中所有的交换分区
[root@clr ~]# free -h #查看系统中的交换分区由哪几个设备提供
              total        used        free      shared  buff/cache   available
Mem:           3.8G        771M        2.4G         17M        714M        2.9G
Swap:            0B          0B          0B #此时可以看到洗系统中的交换分区为0

3.2 Mount and unmount file system

mount命令

insert image description here

[root@clr ~]# mkdir /data1
[root@clr ~]# ls /
admin  boot   dev  home  lib64  mnt  proc  run   srv  tmp  var
bin    data1  etc  lib   media  opt  root  sbin  sys  usr
[root@clr ~]# mount /dev/sdb1 /data1/ #将/dev/sdb1硬盘分区挂载到/data1目录下
[root@clr ~]# df #查看磁盘挂载情况
文件系统          1K-块    已用     可用 已用% 挂载点
devtmpfs        1997348       0  1997348    0% /dev
tmpfs           2013076       0  2013076    0% /dev/shm
tmpfs           2013076   12736  2000340    1% /run
tmpfs           2013076       0  2013076    0% /sys/fs/cgroup
/dev/sda1      38817264 5684776 33132488   15% /
tmpfs            402616      24   402592    1% /run/user/0
/dev/sr0        4600876 4600876        0  100% /run/media/root/CentOS 7 x86_64
/dev/sdb1      20961280   32992 20928288    1% /data1 #已成功将/dev/sdb1 挂载到/data1目录下

mount -o loop [ISO image file] [mount point directory]: Mount the image file to the specified directory;

[root@clr /mnt]# cd /opt #切换到/opt目录

[root@clr /opt]# mount -o loop CentOS-7-x86_64-DVD-2009-7.9.iso /mnt #将镜像文件挂载到指定目录/mnt
mount: /dev/loop0 写保护,将以只读方式挂载
[root@clr /opt]# df -h
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        2.0G     0  2.0G    0% /dev
tmpfs           2.0G     0  2.0G    0% /dev/shm
tmpfs           2.0G   13M  2.0G    1% /run
tmpfs           2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/sda1        38G  9.4G   28G   26% /
tmpfs           394M   12K  394M    1% /run/user/42
tmpfs           394M     0  394M    0% /run/user/0
/dev/sdb1        20G   33M   20G    1% /data
/dev/sdb5       9.8G   37M  9.2G    1% /data2
/dev/loop0      4.4G  4.4G     0  100% /mnt #此处查看可以看到,镜像文件已经挂载成功

[root@clr /opt]# cd /mnt #切换到挂载点所在目录
[root@clr /mnt]# ls  #查看,以下都是新挂载的ISO镜像文件
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

umount -lf [mount point directory]: means to forcibly uninstall the specified directory;

[root@clr /mnt]# cd /data
[root@clr /data]# umount /data
umount: /data:目标忙。
        (有些情况下通过 lsof(8) 或 fuser(1) 可以
         找到有关使用该设备的进程的有用信息)
[root@clr /data]# umount -lf /data
[root@clr /data]# df -h
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        2.0G     0  2.0G    0% /dev
tmpfs           2.0G     0  2.0G    0% /dev/shm
tmpfs           2.0G   13M  2.0G    1% /run
tmpfs           2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/sda1        38G  9.4G   28G   26% /
tmpfs           394M   12K  394M    1% /run/user/42
tmpfs           394M     0  394M    0% /run/user/0
/dev/sdb5       9.8G   37M  9.2G    1% /data2
/dev/loop0      4.4G  4.4G     0  100% /mnt

insert image description here

Through configuration files, permanent mounting can be realized;

#
# /etc/fstab
# Created by anaconda on Thu Mar 23 00:41:12 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=bafe91da-4fb0-4d72-b66e-4f98f1645b49 /                       xfs     defaults        0 0
UUID=54c26300-4b67-4510-a83f-07da2560c5a8 swap                    swap    defaults        0 0
/dev/sdb1                                /data                    xfs     defaults        0 0
UUID=b53750a3-dacd-490e-8efd-d57c7f079ab9 /data2                  ext4     defaults        0 0
/dev/sr0                                  /mnt                    iso9660  defaults       0  0

Files used to store commands automatically executed at startup;

[root@clr /]# vim /etc/fstab   #将需要开机自启动的参数放置在/etc/fstab文件中
[root@clr /]# vim /etc/profile
[root@clr /]# vim ~/.bash_profile
[root@clr /]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 45 17:34 /etc/rc.local -> rc.d/rc.local
[root@clr /]# vim /etc/rc.local
[root@clr /]# chmod +x /etc/rc.d/rc.local
[root@clr /]# ll !$  #能够自动识别上条命令中的最后一个字段/一列内容

insert image description here

insert image description here

3.3 View disk usage

df命令

df [options] [file]

示例

insert image description here

lsblk: View the mounting status of all current partition devices;

[root@clr /data]# lsblk  #lsblk:查看当前所有分区设备的挂载情况;
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   60G  0 disk 
├─sda1   8:1    0   37G  0 part /
├─sda2   8:2    0    1K  0 part 
└─sda5   8:5    0  3.9G  0 part [SWAP]  #交换分区
sdb      8:16   0   40G  0 disk 
├─sdb1   8:17   0   20G  0 part /data #sdb1挂载到/data目录下
├─sdb2   8:18   0    1K  0 part 
└─sdb5   8:21   0   10G  0 part /data2  #sdb5挂载到/data2目录下
sdc      8:32   0   20G  0 disk 
└─sdc1   8:33   0    5G  0 part [SWAP]
sr0     11:0    1  4.4G  0 rom  
loop0    7:0    0  4.4G  0 loop /mnt  #镜像文件挂载到/mnt目录下

Guess you like

Origin blog.csdn.net/cailirong123/article/details/129937210