——观察磁盘分区的状态
lsblk 列出系统上所有的磁盘列表
[root@study ~]# lsblk [dfimpt] device
选项与参数:
-d :仅列出磁盘本身,并不会列出该磁盘的分区数据
-f :同时列出该磁盘内的文件系统名称
-i :使用 ASCII 的线段输出,不要使用复杂的编码 (再某些环境下很有用)
-m :同时输出该设备在 /dev 下面的权限数据 (rwx 的数据)
-p :列出该设备的完整文件名!而不是仅列出最后的名字而已。
-t :列出该磁盘设备的详细数据,包括磁盘伫列机制、预读写的数据量大小等
[root@study ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 2M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 30G 0 part
├─centos-root 253:0 0 10G 0 lvm /
├─centos-swap 253:1 0 1G 0 lvm [SWAP]
└─centos-home 253:2 0 5G 0 lvm /home
sr0 11:0 1 4.3G 0 rom /run/media/fshiyu/CentOS 7 x86_64
NAME:设备的文件名,会省略/dev等前导目录。
MAJ:MIN:核心认识的设备都是通过这两个代码来熟悉的!分别是主要:次要设备代码!
RM:是否为可卸载设备,如光盘、USB 磁盘等等。
SIZE:容量。
RO:是否为只读设备。
TYPE:是磁盘 (disk)、分区 (partition) 还是只读存储器 (rom) 等输出。
MOUTPOINT:就是挂载点!
blkid 列出设备的UUID等参数
[root@study ~]# blkid
/dev/sda2: UUID="3bbadaeb-299b-4876-b2c4-15a78375cf6d" TYPE="xfs" PARTUUID="d50c21ab-d2ce-4934-916d-51983842616c"
/dev/sda3: UUID="xJdUBl-mkyR-l8ML-wiYy-vZkI-PZJ8-bG6JS4" TYPE="LVM2_member" PARTUUID="c32eb2f3-72e4-4634-8766-631c43af2dfc"
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="d976e3eb-8b6c-4750-8a78-c3d675eb0f67" TYPE="xfs"
/dev/mapper/centos-swap: UUID="97a1375e-0529-4a92-a806-5c92aaae2fc8" TYPE="swap"
/dev/mapper/centos-home: UUID="9b6dddf0-e5d4-4869-ae22-3f3c85f25838" TYPE="xfs"
/dev/sda1: PARTUUID="cf87aa36-f354-438f-8604-7d3ce8b3fdd4"
每一行代表一个文件系统,主要列出设备名称、UUID 名称以及文件系统的类型 (TYPE)!
parted 列出磁盘的分区表类型与分区信息
[root@study ~]# parted /dev/sda print
Model: VMware, VMware Virtual S (scsi) # 磁盘的模块名称——厂商
Disk /dev/sda: 42.9GB # 磁盘的总容量
Sector size (logical/physical): 512B/512B # 磁盘的每个逻辑/物理扇区容量
Partition Table: gpt # 分区表的格式GPT/MBR
Disk Flags: pmbr_boot
Number Start End Size File system Name 标志
1 1049kB 3146kB 2097kB bios_grub
2 3146kB 1077MB 1074MB xfs
3 1077MB 33.3GB 32.2GB lvm
——磁盘分区
GPT分区表使用gdisk,MBR使用fdisk。
[root@study ~]# gdisk 设备名称
[root@study ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): # 按?来查看可用命令
b back up GPT data to a file
c change a partition's name
d delete a partition # 删除一个分区
i show detailed information on a partition
l list known partition types
n add a new partition # 增加一个分区
o create a new empty GUID partition table (GPT)
p print the partition table # 打印出分区表(常用)
q quit without saving changes # 不储存分区就直接离开 gdisk
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit # 储存分区操作后离开 gdisk
x extra functionality (experts only)
? print this menu
Command (? for help):
通常通过 lsblk 或 blkid 先找到磁盘,再用 parted /dev/xxx print 来找出内部的分区表类型,之后才用 gdisk 或 fdisk 来操作系统。
Command (? for help): p
Disk /dev/sda: 83886080 sectors, 40.0 GiB # 磁盘文件名/扇区数与总容量
Logical sector size: 512 bytes # 单一扇区大小为 512 Bytes
Disk identifier (GUID): 90923798-5275-4862-B31C-261D9B0970BE # 磁盘的 GPT 识别码
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 18862013 sectors (9.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 6143 2.0 MiB EF02 # 第一个分区数据
2 6144 2103295 1024.0 MiB 0700
3 2103296 65026047 30.0 GiB 8E00
# 分区编号 开始扇区号码 结束扇区号码 容量大小
Command (? for help): q
注意:现在的分区主要是以扇区为最小的单位!
Code:在分区内的可能的文件系统类型。Linux 为 8300,swap 为 8200。不过这个项目只是一个提示而已,不见得真的代表此分区内的文件系统!
Name:文件系统的名称。
用 gdisk 新增分区
例如,新增以下分区:
1GB 的 xfs 文件系统 (Linux)
1GB 的 vfat 文件系统 (Windows)
0.5GB 的 swap (Linux swap)
[root@study ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n # 新增分区的指令
Partition number (4-128, default 4): # 默认即可
First sector (34-83886046, default = 65026048) or {+-}size{KMGTP}: # 默认即可
Last sector (65026048-83886046, default = 83886046) or {+-}size{KMGTP}: +1G # 新增分区大小
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
# 这里在让你选择未来这个分区预计使用的文件系统!默认都是 Linux 文件系统的 8300!
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/sda: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 90923798-5275-4862-B31C-261D9B0970BE
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 16764861 sectors (8.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 6143 2.0 MiB EF02
2 6144 2103295 1024.0 MiB 0700
3 2103296 65026047 30.0 GiB 8E00
4 65026048 67123199 1024.0 MiB 8300 Linux filesystem
Command (? for help):
partprobe 更新 Linux 核心的分区表信息
[root@study ~]# partprobe -s
/dev/sda: gpt partitions 1 2 3 4 5 6
[root@study ~]# cat /proc/partitions # 核心分区记录
major minor #blocks name
8 0 41943040 sda
8 1 2048 sda1
8 2 1048576 sda2
8 3 31461376 sda3
8 4 1048576 sda4
8 5 1048576 sda5
8 6 512000 sda6
11 0 4481024 sr0
253 0 10485760 dm-0
253 1 1048576 dm-1
253 2 5242880 dm-2
用 gdisk 删除一个分区
[root@study ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): d
Partition number (1-6): 6
Command (? for help): p
Disk /dev/sda: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 90923798-5275-4862-B31C-261D9B0970BE
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 14667709 sectors (7.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 6143 2.0 MiB EF02
2 6144 2103295 1024.0 MiB 0700
3 2103296 65026047 30.0 GiB 8E00
4 65026048 67123199 1024.0 MiB 8300 Linux filesystem
5 67123200 69220351 1024.0 MiB 0700 Microsoft basic data
注意:不要去处理一个正在使用中的分区!