Linux命令大全:https://www.linuxcool.com
注:fdisk
命令只支持MBR分区,MBR分区表最大支撑2T的磁盘,parted
工具可以划分单个分区大于2T的GPT格式的分区,也可以划分普通的MBR分区。
1. 磁盘分区-fdisk
命令介绍:https://www.linuxcool.com/fdisk
这里使用lsblk
命令查看,一共有两块磁盘,一块系统盘,一块裸磁盘我们需要对裸磁盘进行分区
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lsblk #查看硬盘信息
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 200G 0 disk
└─nvme0n1p1 259:2 0 200G 0 part /
nvme1n1 259:1 0 200G 0 disk #准备分区的磁盘
1.1 Linux硬盘分区操作
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# fdisk /dev/nvme1n1
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): n
Partition number (1-128, default 1):
First sector (34-419430366, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-419430366, default 419430366): +50G
Created partition 1
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# 查看分区
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 200G 0 disk
└─nvme0n1p1 259:2 0 200G 0 part /
nvme1n1 259:1 0 200G 0 disk
├─nvme1n1p2 259:4 0 50G 0 part
# 示例:初始化分区
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# mkfs -t ext4 /dev/nvme1n1p1
mke2fs 1.43.5 (04-Aug-2017)
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: 9a69c240-cac9-4634-a106-357e96f8b447
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424
Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done
# 挂载
mount /dev/nvme1n1p1 /home/t4
df -h
/dev/nvme1n1p1 49G 53M 47G 1% /home/t4
# 永久挂载
echo "/dev/nvme1n1p1 /home/t4 ext4 defaults 0 0" >> /etc/fstab
-
m: 显示帮助菜单
-
n: 新建分区
-
P: 显示分区列表
-
q: 不保存退出
-
w: 保存退出
1.2 Linux硬盘卸载操作
umount /home/t4
# 删除分区
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# fdisk /dev/nvme1n1
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): d
Partition number (1,2, default 2): 1 # 删除分区1
Partition 1 is deleted
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lsblk # 查看磁盘,分区已删除。
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 200G 0 disk
└─nvme0n1p1 259:2 0 200G 0 part /
nvme1n1 259:1 0 200G 0 disk
# 删除/etc/fstab内容
sed -i s"#/dev/nvme1n1p1 /home/t4 ext4 defaults 0 0##"g /etc/fstab
2. 磁盘分区-parted
命令介绍:https://www.linuxcool.com/parted
2.1 Linux硬盘分区操作
显示系统中的磁盘分区信息:
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# parted -l
Model: NVMe Device (nvme)
Disk /dev/nvme0n1: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 215GB 215GB primary ext4 boot
Model: NVMe Device (nvme)
Disk /dev/nvme1n1: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
parted
命令准备对磁盘进行分区:
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# parted /dev/nvme1n1
GNU Parted 3.1
Using /dev/nvme1n1
Welcome to GNU Parted! Type 'help' to view a list of commands.
创建分区:
(parted) mklabel # 创建分区
New disk label type? gpt # 我们要正确分区大于2TB的磁盘,应该使用gpt方式的分区表。
Warning: The existing disk label on /dev/nvme1n1 will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) mkpart
Partition name? []? db01 # 分区名称db01
File system type? [ext2]? ext4
Start? 0% # 200G硬盘的划分硬盘大小范围0-100G,直接写start:0-End:100G也可以。
End? 50%
查看分区情况:
(parted) print # 打印分区情况
Model: NVMe Device (nvme)
Disk /dev/nvme1n1: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 107GB 107GB ext4 db01
(parted) quit # 退出
Information: You may need to update /etc/fstab.
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lsblk # 查看分区情况
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 200G 0 disk
└─nvme0n1p1 259:2 0 200G 0 part /
nvme1n1 259:1 0 200G 0 disk
└─nvme1n1p1 259:3 0 100G 0 part
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# mkfs.ext4 /dev/nvme1n1p1 # 磁盘系统初始化操作
mke2fs 1.43.5 (04-Aug-2017)
/dev/nvme1n1p1 contains a ext4 file system
last mounted on Mon Apr 24 18:51:53 2023
Proceed anyway? (y,N) y
Creating filesystem with 26214144 4k blocks and 6553600 inodes
Filesystem UUID: 531b744f-24f3-4d7f-80ea-af4cac7d2a61
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done
挂载:
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# mount /dev/nvme1n1p1 /home/t4
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 61G 0 61G 0% /dev
tmpfs 61G 0 61G 0% /dev/shm
tmpfs 61G 772K 61G 1% /run
tmpfs 61G 0 61G 0% /sys/fs/cgroup
/dev/nvme0n1p1 197G 27G 163G 14% /
tmpfs 13G 0 13G 0% /run/user/0
/dev/nvme1n1p1 98G 61M 93G 1% /home/t4
# 永久写入/etc/fstab
echo "/dev/nvme1n1p1 /home/t4 ext4 defaults 0 0" >> /etc/fstab
2.2 Linux硬盘卸载操作
umount /home/t4 # 卸载
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# parted /dev/nvme1n1
GNU Parted 3.1
Using /dev/nvme1n1
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) rm 1 # 选择分区number
(parted) print
Model: NVMe Device (nvme)
Disk /dev/nvme1n1: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) quit
Information: You may need to update /etc/fstab.
2.3 非交互方式
parted /dev/nvme1n1 mklabel gpt
parted /dev/nvme1n1 mkpart primary 0% 100%
parted /dev/nvme1n1 print
lsblk
mkfs -t ext4 /dev/nvme1n1p1
mount /dev/nvme1n1p1 /home/t4
vim /etc/fstab
3. lvm逻辑卷
参考:http://c.biancheng.net/view/911.html
PP(物理区域) --> PV(物理卷,处于LVM最底层) --> VG(卷组,建立在PV之上,可以含有一个到多个PV)--> LV(逻辑卷,建立在VG之上,相当于原来分区的概念)
3.1 创建卷组过程
创建分区:
fdisk -l
fdisk /dev/nvme1n1
lsblk
创建逻辑卷:
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# pvcreate /dev/nvme1n1p2 /dev/nvme1n1p1
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
WARNING: ext4 signature detected on /dev/nvme1n1p1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/nvme1n1p1.
Physical volume "/dev/nvme1n1p2" successfully created.
Physical volume "/dev/nvme1n1p1" successfully created.
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# pvscan
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
PV /dev/nvme1n1p1 lvm2 [50.00 GiB]
PV /dev/nvme1n1p2 lvm2 [50.00 GiB]
Total: 2 [100.00 GiB] / in use: 0 [0 ] / in no VG: 2 [100.00 GiB]
创建卷组:
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# vgcreate vg01 /dev/nvme1n1p1 /dev/nvme1n1p2
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
Volume group "vg01" successfully created
vgscan
创建逻辑卷:
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lvcreate -L 50G -n lv001 vg01
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
Logical volume "lv001" created.
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lvscan
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
ACTIVE '/dev/vg01/lv001' [50.00 GiB] inherit
-
-L:加容量
初始化挂载:
mkfs.ext4 /dev/vg01/lv001
mount /dev/vg01/lv001 /home/t4
df -h
/dev/mapper/vg01-lv001 49G 53M 47G 1% /home/t4
vim /etc/fstab
echo "/dev/vg01/lv001 /home/t4 ext4 defaults 0 0" >> /etc/fstab
3.2 创建新lv逻辑卷
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# vgs
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
VG #PV #LV #SN Attr VSize VFree
vg01 2 2 0 wz--n- 99.99g 0
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lvcreate -l 100%Free -n lv02 vg01
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
Logical volume "lv02" created.
-
-l:加百分比
逻辑卷初始化:
mkfs.ext4 /dev/vg01/lv02
mke2fs 1.43.5 (04-Aug-2017)
Creating filesystem with 13105152 4k blocks and 3276800 inodes
Filesystem UUID: a47a2fba-2951-4d47-aff0-0ebd1b95f632
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424
Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done
mkdir /home/t5
mount /dev/vg01/lv02 /home/t5
df -h
vim /etc/fstab
echo "/dev/vg01/lv02 /home/t5 ext4 defaults 0 0" >> /etc/fstab
3.3 扩容逻辑卷
[root@iZbp12hz1b1hpxtkd5epdrZ ~]# lvextend -l +100%free -r /dev/vg01/lv001
WARNING: Failed to connect to lvmetad. Falling back to device scanning.
Size of logical volume vg01/lv001 changed from 50.00 GiB (12800 extents) to 99.99 GiB (25598 extents).
Logical volume vg01/lv001 successfully resized.
resize2fs 1.43.5 (04-Aug-2017)
Filesystem at /dev/mapper/vg01-lv001 is mounted on /home/t4; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 13
The filesystem on /dev/mapper/vg01-lv001 is now 26212352 (4k) blocks long.
df -h
3.4 删除LVM
# 1.卸载目录 --> 2.卸载lv --> 3.卸载vg --> 4.卸载pv
umount
lvdisplay #查看当前lv卷轴名称
lvremove +lv名称
vgdisplay #查看vg名称
vgremove +vg名称
pvdisplay #查看pv名称
pvremove +pv名称
标签:nvme1n1p1,vg01,nvme1n1,dev,iZbp12hz1b1hpxtkd5epdrZ,操作,磁盘分区,root From: https://www.cnblogs.com/-k8s/p/17351192.html