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