4.文件系统
本章同步视频:https://edu.51cto.com/sd/e4874
4.5 文件系统管理
4.6 分区、格式化与挂载
如果我们想要在系统里面新增一颗磁碟时,应该有哪些动作需要做的呢:
n 对该 partition 进行格式化 (format),以建立系统可用的 filesystem;
n 若想要仔细一点,则可对刚刚建立好的 filesystem 进行检验;
n 在 Linux 系统上,需要建立挂载点 (亦即是目录),并将他挂载上来;
4.6.1 查看分区状况
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
├─sda1 8:1 0 500M 0 part /boot
├─rhel-root 253:0 0 9.8G 0 lvm /
├─rhel-swap 253:1 0 2G 0 lvm [SWAP]
└─rhel-home 253:2 0 2G 0 lvm /home
[root@localhost tmp]# lsblk -d
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
[root@localhost tmp]# lsblk -p
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
├─/dev/sda1 8:1 0 500M 0 part /boot
└─/dev/sda2 8:2 0 13.7G 0 part
├─/dev/mapper/rhel-root 253:0 0 9.8G 0 lvm /
├─/dev/mapper/rhel-swap 253:1 0 2G 0 lvm [SWAP]
└─/dev/mapper/rhel-home 253:2 0 2G 0 lvm /home
2.blkid - locate/print block device attributes
/dev/sda1: UUID="decc164a-f28b-4b7f-ae37-9fc38a347d94" TYPE="xfs"
/dev/sda2: UUID="Ijm3mK-onGH-ht6u-tGqw-qYYS-QrmH-dxbqkr" TYPE="LVM2_member"
/dev/mapper/rhel-root: UUID="f02db577-90a3-4ae2-a434-b43c4ab34d7e" TYPE="xfs"
/dev/mapper/rhel-swap: UUID="145d4801-311d-4efe-b188-7620c7a9ada9" TYPE="swap"
/dev/mapper/rhel-home: UUID="6ef1eb27-38b9-46bd-b6d4-abc3557b8f8c" TYPE="xfs"
3.GNU Parted - a partition manipulation program
[root@localhost tmp]# parted /dev/sda print
Model: VMware, VMware Virtual S (scsi)
Sector size (logical/physical): 512B/512B
Number Start End Size Type File system Flags
1 1049kB 525MB 524MB primary xfs boot
2 525MB 15.2GB 14.7GB primary lvm
4.fdisk - manipulate disk partition table
[root@localhost tmp]# fdisk -l /dev/sda
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 29714431 14344192 8e Linux LVM
4.6.2 分区1-fdisk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
├─sda1 8:1 0 500M 0 part /boot
├─rhel-root 253:0 0 9.8G 0 lvm /
├─rhel-swap 253:1 0 2G 0 lvm [SWAP]
└─rhel-home 253:2 0 2G 0 lvm /home
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): m #输入m,查看可使用的命令
c toggle the dos compatibility flag
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types #查看分区类型
o create a new empty DOS partition table
p print the partition table #列出分区情况
q quit without saving changes #不保存退出
s create a new empty Sun disklabel
t change a partition's system id #修改分区id
w write table to disk and exit #保存退出
x extra functionality (experts only)
Command (m for help): n #新建一个分区
p primary (2 primary, 0 extended, 2 free)
Select (default p): p #创建一个主分区
Partition number (3,4, default 3):
First sector (29714432-41943039, default 29714432):
Last sector, +sectors or +size{K,M,G} (29714432-41943039, default 41943039): +200M
Partition 3 of type Linux and of size 200 MiB is set
Command (m for help): n #新建一个分区
p primary (3 primary, 0 extended, 1 free)
Select (default e): e #选择分区类型为扩展分区,直接回车采用默认值e
Selected partition 4 #分区号为4(只剩下4了,没的选了)
First sector (30124032-41943039, default 30124032):
Last sector, +sectors or +size{K,M,G} (30124032-41943039, default 41943039): +4G
Partition 4 of type Extended and of size 4 GiB is set
#注:主分区可以直接被格式化后使用,而扩展分区不能,需要再在扩展分区中划分逻辑分区,然后格式化逻辑分区才能使用。
Command (m for help): n #新建一个分区
All primary partitions are in use #没有主分区可选
Adding logical partition 5 #只能直接创建逻辑分区,分区号是5
First sector (30126080-38512639, default 30126080):
Last sector, +sectors or +size{K,M,G} (30126080-38512639, default 38512639): +500M
Partition 5 of type Linux and of size 500 MiB is set
Command (m for help): p #列出分区表
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 29714431 14344192 8e Linux LVM
/dev/sda3 29714432 30124031 204800 83 Linux
/dev/sda4 30124032 38512639 4194304 5 Extended
/dev/sda5 30126080 31150079 512000 83 Linux
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
[root@localhost ~]# partprobe /dev/sda
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
├─sda1 8:1 0 500M 0 part /boot
│ ├─rhel-root 253:0 0 9.8G 0 lvm /
│ ├─rhel-swap 253:1 0 2G 0 lvm [SWAP]
│ └─rhel-home 253:2 0 2G 0 lvm /home
Partition number (1-5, default 5): 5 #指定要删除的分区号
#注:删除分区后也要进行保存(7)和更新内核信息(8)的操作
4.6.3 分区2-gdisk
[root@localhost ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.6
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if
you don't want to convert your MBR partitions to GPT format!
***************************************************************
Command (? for help): ? #查看可用命令
i show detailed information on a partition
o create a new empty GUID partition table (GPT)
p print the partition table #显示分区
q quit without saving changes #不保存退出
r recovery and transformation options (experts only)
t change a partition's type code
w write table to disk and exit #保存退出
x extra functionality (experts only)
Disk /dev/sda: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): FAE2CF35-09B0-4F2C-B43D-B8E6DCD48693
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 11820989 sectors (5.6 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1026047 500.0 MiB 8300 Linux filesystem
2 1026048 29714431 13.7 GiB 8E00 Linux LVM
3 29714432 30124031 200.0 MiB 8300 Linux filesystem
Partition number (4-128, default 4): #设置分区号,回车使用默认值
First sector (34-41943006, default = 30124032) or {+-}size{KMGTP}:
Last sector (30124032-41943006, default = 41943006) or {+-}size{KMGTP}: +300M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Partition number (1-4): 4 #指定要删除的分区号
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
Do you want to proceed? (Y/N): y #输入y,否则会退出
OK; writing new GUID partition table (GPT) to /dev/vda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
[root@localhost ~]# partprobe /dev/sda