首页 > 其他分享 >磁盘分区操作

磁盘分区操作

时间:2023-04-24 22:35:34浏览次数:55  
标签:nvme1n1p1 vg01 nvme1n1 dev iZbp12hz1b1hpxtkd5epdrZ 操作 磁盘分区 root

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

相关文章

  • Java文件操作必备技能,10个小技巧让你快速掌握!
    前言  在我们日常的开发中,文件操作是一个非常重要的主题。文件读写、文件复制、任意位置读写、缓存等技巧都是我们必须要掌握的。在这篇文章中,我将给你们介绍10个实用的文件操作技巧。使用try-with-resources语句处理文件IO流,确保在使用完毕后自动关闭流。使用java.n......
  • 操作系统基础知识(1)
    什么是管程?当共享资源用共享数据结构表示时,资源管理程序可用对该数据结构进行操作的一组过程来表示,如资源的请求和释放过程request和release。把这样一组相关的数据结构和过程一并归为管程。Hansan为管程所下的定义是:“一个管程定义了一个数据结构和能为并发进程所执行的一组操......
  • ESP32-S3 FLASH 操作
    FLASH读取操作整个分区的读、写、擦除#ESP-IDFPartitionTable#Name,Type,SubType,Offset,Size,Flagsnvs,data,nvs,0x9000,0x4000,otadata,data,ota,,0x2000,phy_init,data,phy,,0x1000,key_data,0x4......
  • 学习《操作系统导论》03
    进程调度:介绍(原书第七章)问题:如何开发调度策略?工作负载假设在具体给出一个目标调度程序之前,先逐步分析,先给出一些列约束,这些约束看上去都非常理想化,不切实际,不过随着后面分析的深入,会逐步放开这些约束,这样最终的方案就是想要的一个比较理想的调度策略了。假设如下:每个工作运......
  • Django 静态文件 request对象方法 pycharm和Django连接MySQL Django模型层初步了解 基
    目录静态文件一、概念静态文件:不经常变化的文件,主要针对html文件所使用到的各种资源。例如:css文件、js文件、img文件、第三方框架文件ps:Django针对静态文件资源需要单独在根目录创建一个static目录统一存放,该目录下的文件类型还有很多,例如:utils目录,plugins目录,li......
  • Apple 的 plist 编辑器入门指南:基础操作与高级功能详解
     PlistEditPro是一款专为macOS编写的最高级属性列表Plist编辑器。对于Mac和IOS开发人员来说,编写应用程序时必须编辑各种列表文件。PlistEditPro通过提供直观且功能强大的界面,使编辑这些文件更加容易。它不仅能够复制和粘贴或拖放属性列表数据,还提供强大的搜索和替换功能,并......
  • TypeScript 学习笔记 — 数组常见的类型转换操作记录(十四)
    获取长度lengthtypeLengthOfTuple<Textendsany[]>=T["length"];typeA=LengthOfTuple<["B","F","E"]>;//3typeB=LengthOfTuple<[]>;//0取第一项FirstItemtypeFirstItem<Textendsany[]>......
  • Windows操作系统网卡显示公用,公用网络改为专用网络
    步骤1.按“Win+R”输入“secpol.msc”,然后点击“确定”打开本地安全策略。步骤2.单击“安全设置”,然后单击“网络列表管理器策略”,找到您的网络名称并双击它。步骤3.在网络属性窗口中,选择“网络位置”选项卡,然后在“位置类型”下选择“专用”,再单击“确定”。 ......
  • C#用dynamic一行代码实现反射操作
    dynamic简介dynamic是.NETFramework4.0的新特性。dynamic的出现让C#具有了弱语言类型的特性。编译器在编译的时候不再对类型进行检查,编译时默认dynamic对象支持你想要的任何特性。dynamic简化反射实现使用dynamic来简化反射实现是一种比较常见的编程技巧,它可以减少代码的复杂......
  • JS高级之手动实现new操作符
    相信大家都很好奇,在js中为什么函数在经过new操作之后会变成一个对象?functionFoot(params){this.name=params}Foot("aa")undefinednewFoot("aa")Foot {name:'aa'}实际上,new是一个语法糖从上面介绍中,我们可以看到new关键字主要做了以下的工作:创建一个新的对......