基本语法:fdisk [选项] 设备名
常用操作:
# 查看磁盘分区信息
fdisk -l
# 对指定磁盘进行分区操作
fdisk /dev/sdb
常用交互命令:
#fdisk /dev/vda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):m
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a newempty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a newempty DOS partition table
p print the partition table
q quit without saving changes
s create a newempty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
基本语法:gdisk [选项] 设备名
常用操作:
# 查看磁盘分区信息,设备不可省略
gdisk -l /dev/sdb
# 对指定磁盘进行分区操作
gdisk /dev/sdb
常用交互命令和fdisk基本一样,但帮助是“?”
Command (? forhelp): ?
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
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
x extra functionality (experts only)
? print this menu
用法:
lsblk[选项][<设备> ...]
常用选项:
-a, --all 打印所有设备
-f, --fs 输出文件系统信息
-h, --help 使用信息(此信息)
-l, --list 使用列表格式的输出
-p, --paths 打印完整设备路径
例
[07:05:45 root@localhost\ [ ~]#lsblk -l
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:014.4G 0 rom
vda 252:0010G 0 disk
vda1 252:101G 0 part /boot
vda2 252:209G 0 part
centos-root 253:008G 0 lvm /
centos-swap 253:101G 0 lvm [SWAP]
vdb 252:1605G 0 disk
#↑发现5G磁盘
MBR分区方式
[root@localhost ~] fdisk /dev/sdb
Command(m for help): n
Partition type:
p primary(0 primary, 0 extended, 4 free)
e extended(container for logical drives)
Select(default p): p
Partition number(1-4, default1): 1
First sector(2048-20971519, default2048):
Last sector, +sectors or +size{K,M,G,T,P}: +8G
Command(m for help): n
Partition type:
p primary(1 primary, 0 extended, 3 free)
e extended(container for logical drives)
Select(default p): p
Partition number(2-4, default2): 2
First sector(16779264-20971519, default16779264):
Last sector, +sectors or +size{K,M,G,T,P}:
Command (m for help): w
GPT分区方式
[root@localhost ~]# gdisk /dev/vdb
GPT fdisk (gdisk) version 0.8.10
Command (? for help): n
Partition number (1-128, default1):
First sector (34-10485726, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-10485726, default = 10485726) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/vdb: 10485760 sectors, 5.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): CAD5CEFE-CCD0-4BBC-910A-007CBA9F96D6
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 10485726
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
12048104857265.0 GiB 8300 Linux filesystem
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/vdb.
The operation has completed successfully.
parted方式创建GPT分区,parted是一种支持MBR和GPT分区表的磁盘分区命令.
[root@localhost ~]#parted /dev/vdb
(parted) mklabel gpt #分区方式,需要MBR格式则这里写mbr
(parted) mkpart vdb1 0% 100%
(parted) quit
parted交互方式
(parted) mkpart vdb1
分区类型? primary/主分区/extended/扩展分区? p
文件系统类型? [ext2]? xfs
起始点? 0
结束点? 100%
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
1512B 5369MB 5369MB primary
LVM(Logical Volume Manager)是Linux下的逻辑卷管理器,它提供了比传统分区更灵活的磁盘管理方式,它可以动态调整容量。
[root@localhost ~]# pvcreate /dev/vdb1 #如果创建多个pv可以继续往后写如/dev/vdb2
Physical volume "/dev/vdb1" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <9.00g 0#这个是操作系统安装时自动创建的
/dev/vdb1 lvm2 --- <5.00g <5.00g
[root@localhost ~]# vgcreate vg1 /dev/vdb1 #如果有多个pv可以继续往后写
Volume group "vg1" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 120 wz--n- <9.00g 0
vg1 100 wz--n- <5.00g <5.00g
#-l按比例分配,-L也可按大小分配
[root@localhost ~]# lvcreate -l 100%FREE -n lv1 vg1
Logical volume "lv1" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <8.00g
swap centos -wi-ao---- 1.00g
lv1 vg1 -wi-a----- <5.00g
# 创建文件系统
mkfs.xfs /dev/vg1/lv1
# 创建挂载点
mkdir /data
# 挂载
mount /dev/vg1/lv1 /data
# 验证
df -h
#输出类似以下则成功
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 8.0G 1.5G 6.5G 19% /
/dev/vda1 1014M 140M 875M 14% /boot
/dev/mapper/vg1-lv1 5.0G 33M 5.0G 1% /data
配置自动挂载
# 编辑/etc/fstab
vim /etc/fstab
# 添加以下内容
/dev/mapper/vg1-lv1 /data xfs defaults 0 0
# 测试配置,配置正确则没有任何输出
mount -a
一个完整的文件系统结构如下所示
文件系统(fs)
↑
逻辑卷(Logical Volume, LV)
↑
卷组(Volume Group, VG)
↑
物理卷(Physical Volume, PV)
↑
分区(part)
↑
物理设备(硬盘)
依据实际情况可有所裁剪,根据实践有如下建议: