Linux 分配卷宗

1、磁盘分区工具与挂载

1.1 硬盘分区符认识:

MBR概述:全称为Master Boot Record,即硬盘的主引导记录。

硬盘的0柱面、0磁头、1扇区称为主引导扇区(也叫主引导记录MBR)。它由3个本分组成:主引导程序、硬盘分区表DPT(Disk Partition Table)和分区有效标志(55AA)。在总共512字节的主引导扇区里主引导程序(boot loader)占446个字节,第二部分是Partition table(分区表),即DPT,占64个字节,硬盘中分区有多少以及每一分区的大小都记录在其中。第三部分是magic number,占两个自己,固定为55AA。

分区编号:

  • 主分区:1-4
  • 逻辑分区:从5开始

一个硬盘最多能有4个主分区一个扩展分区,一般组成为:4个主分区或者3个主分区和一个扩展分区。

linux规定,逻辑分区必须建立在扩展分区之上,不能在主分区之上

分区作用:

  • 主分区:主要是用来启动操作系统,它主要存放的是操作系统的启动或引导程序,/boot分区最好放在主分区上
  • 扩展分区:扩展分区不能直接使用,它只是作为逻辑分区的容器存在。我们真正存放数据的是主分区和逻辑分区,一般大量的数据都放在逻辑分区

GPT的分区方式,不限制主分区个数

注意:使用分区工具fdisk对磁盘进行操作,分区,格式化

root@pve107:/dev/mapper# ls /dev/sd*
/dev/sda   /dev/sdb   /dev/sdc   /dev/sdd   /dev/sde
/dev/sda1  /dev/sdb1  /dev/sdc1  /dev/sdd1  /dev/sde1

说明:

/dev/sda1
  • /dev:设备文件的目录
  • sd:scsi硬盘
  • a:【a-z】从a开始,sda表示第一块scsi硬盘,第二块为sdb,依次类推
  • 1:分区编号,表示第一块scsi硬盘上的第一个分区。

1.2 使用fdisk管理分区

fdisk:磁盘分区,是linux发行版本中最常用的分区工具

用法:fdisk 【选项】 device

常用选项: -l 查看硬盘分区表

root@pve107:/dev/mapper# fdisk -l
Disk /dev/nvme0n1: 1.1 TiB, 1200243695616 bytes, 2344225968 sectors
Disk model: INTEL SSDPEDMW012T4
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 0D06E540-74D5-4D4A-A8F6-08607A885465

Device           Start        End    Sectors  Size Type
/dev/nvme0n1p1      34       2047       2014 1007K BIOS boot
/dev/nvme0n1p2    2048    1050623    1048576  512M EFI System
/dev/nvme0n1p3 1050624 2344225934 2343175311  1.1T Linux LVM


Disk /dev/sda: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD20EZRZ-00Z
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x0005df83

Device     Boot Start        End    Sectors  Size Id Type
/dev/sda1        2048 3907029167 3907027120  1.8T 83 Linux


Disk /dev/sdb: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD20EZRZ-00Z
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x000d93ca

Device     Boot Start        End    Sectors  Size Id Type
/dev/sdb1        2048 3907029167 3907027120  1.8T 83 Linux


Disk /dev/sdc: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD20EZRZ-00Z
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x000ceb56

Device     Boot Start        End    Sectors  Size Id Type
/dev/sdc1        2048 3907029167 3907027120  1.8T 83 Linux


Disk /dev/sdd: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD20EZRZ-00Z
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x0001f79f

Device     Boot Start        End    Sectors  Size Id Type
/dev/sdd1        2048 3907029167 3907027120  1.8T 83 Linux


Disk /dev/sde: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD20EZRZ-00Z
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 404A301C-47FD-453D-A3E8-70247ED1C2C2

Device     Start        End    Sectors Size Type
/dev/sde1   2048 2147485695 2147483648   1T Linux filesystem


Disk /dev/mapper/pve-swap: 8 GiB, 8589934592 bytes, 16777216 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


Disk /dev/mapper/pve-root: 1.2 TiB, 1288490188800 bytes, 2516582400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
root@pve107:/dev/mapper#


示例:
磁盘 /dev/sda:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000033f0
 
   设备 Boot  Start End  Blocks   Id  System
/dev/sda1   *2048 2099199 1048576   83  Linux
/dev/sda2 20992008388607940893440   8e  Linux LVM

fdisk常用的参数:

root@pve107:/dev/pve# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD20EZRZ-00Z
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x000d93ca

Device     Boot Start        End    Sectors  Size Id Type
/dev/sdb1        2048 3907029167 3907027120  1.8T 83 Linux

Command (m for help): q

root@pve107:/dev/pve# mkfs.ext4 /dev/sde1
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 268435456 4k blocks and 67108864 inodes
Filesystem UUID: 20609837-8e11-45d8-bf55-44d6767666d3
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000, 214990848

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

root@pve107:/dev/pve# mkfs.ext4 /dev/sdb1
mke2fs 1.44.5 (15-Dec-2018)
/dev/sdb1 contains a ext4 file system
        created on Wed Jan 13 13:44:58 2021
Proceed anyway? (y,N) y
Creating filesystem with 488378390 4k blocks and 122101760 inodes
Filesystem UUID: 0f986223-f255-4108-9d98-1e2d1440b106
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000, 214990848

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

说明:

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
 
Device does not contain a recognized partition table
使用磁盘标识符 0x4cf6a6ac 创建新的 DOS 磁盘标签。
 
命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition    ##删除分区
   g   create a new empty 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 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   ##改变分区类型
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit   ##写分区信息到硬盘,保存操作并退出
   x   extra functionality (experts only)

1.3 使用fdisk添加一个新的分区

fdisk /dev/sda
 
命令(输入 m 获取帮助):p   ##列出当前磁盘的分区信息
  
磁盘 /dev/sda:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000033f0
 
   设备 Boot  Start End  Blocks   Id  System
/dev/sda1   *2048 2099199 1048576   83  Linux
/dev/sda2 20992008388607940893440   8e  Linux LVM
 
 
命令(输入 m 获取帮助):n  ##新建一个分区
 
Partition type:
   p   primary (2 primary, 0 extended, 2 free)p:主分区
   e   extended e:扩展分区
Select (default p): --直接默认
Using default response p
Partition number (1,4, default 3):   ---直接默认
First sector (1230848-41943039, default 1230848):   ---直接默认 
Using default value 1230848
Last sector, +sectors or +size{K,M,G} (1230848-41943039, default 41943039): +1G  输入分区大小
Partition 3 of type Linux and of size 1 GiB is set
Command (m for help): w   保存退出

2)让新划分的分区生效

注意:对已经在使用的磁盘进行分区后,要让新分区生效,可以使用:

partx -a /dev/sde /dev/sde1
#这里我操作的时候一直报错,直接跳到 格式化也是可以的

root@pve107:/dev/mapper# parted /dev/sde 直接使用这个即可
GNU Parted 3.2
Using /dev/sde
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted)
(parted) m
align-check TYPE N check partition N for TYPE(min|opt)
alignment
help [COMMAND] print general help, or help on
COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition
table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table,
available devices, free space, all found partitions, or a particular
partition
quit exit program
rescue START END rescue a lost partition near START
and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected
device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition
NUMBER
unit UNIT set the default unit to UNIT
version display the version number and
copyright information of GNU Parted
(parted) q

最好的办法是进行设备重启。

init 6

3)格式化新分区


root@pve107:/dev/mapper# mkfs -t ext4 /dev/sdb1
mke2fs 1.44.5 (15-Dec-2018)
/dev/sdb1 contains a LVM2_member file system
Proceed anyway? (y,N) y
Creating filesystem with 488378390 4k blocks and 122101760 inodes
Filesystem UUID: 1fa50970-eab3-4cfb-bd73-033c27bc1972
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848


Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done


root@pve107:/dev/mapper# mkfs -t ext4 /dev/sda1
mke2fs 1.44.5 (15-Dec-2018)
/dev/sda1 contains a LVM2_member file system
Proceed anyway? (y,N) y
Creating filesystem with 488378390 4k blocks and 122101760 inodes
Filesystem UUID: 42f79bc5-57b7-47c2-8ce2-d12a628e1cd7
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848


Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done


root@pve107:/dev/mapper# mkfs -t ext4 /dev/sde1
mke2fs 1.44.5 (15-Dec-2018)
/dev/sde1 contains a LVM2_member file system
Proceed anyway? (y,N) y
Creating filesystem with 268435456 4k blocks and 67108864 inodes
Filesystem UUID: 25149c8e-a6c8-4063-af18-de79b63c0805
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848


Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done



#注意格式化类型可以根据自己需要进行选择
root@pve107:/dev/mapper# pvscan
  PV /dev/nvme0n1p3   VG pve             lvm2 [1.09 TiB / 15.99 GiB free]
  PV /dev/sda1                           lvm2 [<1.82 TiB]
  Total: 2 [2.91 TiB] / in use: 1 [1.09 TiB] / in no VG: 1 [<1.82 TiB]
root@pve107:/dev/mapper# vgs
  VG  #PV #LV #SN Attr   VSize VFree
  pve   1   3   0 wz--n- 1.09t 15.99g
root@pve107:/dev/mapper# lvscan
  ACTIVE            '/dev/pve/swap' [8.00 GiB] inherit
  ACTIVE            '/dev/pve/root' [96.00 GiB] inherit
  ACTIVE            '/dev/pve/data' [977.36 GiB] inherit
root@pve107:/dev/mapper# pvcreate /dev/sde1
WARNING: ext4 signature detected on /dev/sde1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sde1.
  Physical volume "/dev/sde1" successfully created.
root@pve107:/dev/mapper# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
root@pve107:/dev/mapper# pvscan
  PV /dev/nvme0n1p3   VG pve             lvm2 [1.09 TiB / 15.99 GiB free]
  PV /dev/sda1                           lvm2 [<1.82 TiB]
  PV /dev/sdb1                           lvm2 [<1.82 TiB]
  PV /dev/sde1                           lvm2 [1.00 TiB]
  Total: 4 [<5.73 TiB] / in use: 1 [1.09 TiB] / in no VG: 3 [<4.64 TiB]

创建lv

root@pve107:/dev/mapper# vgcreate kycx-pve /dev/sdb1 /dev/sda1 /dev/sde1
  /etc/lvm/archive/.lvm_pve107_21541_1693306552: write error failed: No space left on device

vgextend pve /dev/sda1
/etc/lvm/archive/.lvm_pve107_23845_1665177330: write error failed: No space left on device

这里提示空间不足

查看空间

root@pve107:/dev/mapper# df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   63G     0   63G   0% /dev
tmpfs                  13G  1.3G   12G  11% /run
/dev/mapper/pve-root   94G   94G     0 100% /
tmpfs                  63G   37M   63G   1% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
tmpfs                  63G     0   63G   0% /sys/fs/cgroup
/dev/nvme0n1p2        511M  312K  511M   1% /boot/efi
/dev/fuse              30M   16K   30M   1% /etc/pve
tmpfs                  13G     0   13G   0% /run/user/0

发现

/dev/mapper/pve-root 使用率100%

使用下面的命令进行扩展

root@pve107:/dev/mapper# lvresize -A n -L +100G /dev/pve/root #扩展
  Insufficient free space: 25600 extents needed, but only 4094 available
root@pve107:/dev/mapper# resize2fs -p /dev/pve/root #更新
resize2fs 1.44.5 (15-Dec-2018)
The filesystem is already 25165824 (4k) blocks long.  Nothing to do!

根据上面提示蒋剩余空间分配扩展

lvresize -A n -L +4G /dev/pve/root  
Size of logical volume pve/root changed from 96.00 GiB (24576 extents) to 100.00 GiB (25600 extents).
WARNING: This metadata update is NOT backed up.
Logical volume pve/root successfully resized.
root@pve107:/dev/mapper# resize2fs -p /dev/pve/root
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/pve/root is mounted on /; on-line resizing required
old_desc_blocks = 12, new_desc_blocks = 13
The filesystem on /dev/pve/root is now 26214400 (4k) blocks long.

再次查看空间

root@pve107:/dev/mapper# df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   63G     0   63G   0% /dev
tmpfs                  13G  1.4G   12G  11% /run
/dev/mapper/pve-root   98G   94G     0 100% /
tmpfs                  63G   37M   63G   1% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
tmpfs                  63G     0   63G   0% /sys/fs/cgroup
/dev/nvme0n1p2        511M  312K  511M   1% /boot/efi
/dev/fuse              30M   16K   30M   1% /etc/pve
tmpfs                  13G     0   13G   0% /run/user/0
root@pve107:/dev/mapper# vgextend pve /dev/sda1
  Volume group "pve" successfully extended
root@pve107:/dev/mapper# vgdisplay
  --- Volume group ---
  VG Name               pve
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               2.91 TiB
  PE Size               4.00 MiB
  Total PE              762962
  Alloc PE / Size       282961 / <1.08 TiB
  Free  PE / Size       480001 / 1.83 TiB
  VG UUID               xh2x4j-B6OA-4HoT-WbfG-Y3yk-yd4r-XNEWsu

root@pve107:/dev/mapper# lvextend -L +1000G  /dev/pve/root
  Size of logical volume pve/root changed from 100.00 GiB (25600 extents) to 1.07 TiB (281600 extents).
  Logical volume pve/root successfully resized.
root@pve107:/dev/mapper# vgdisplay
  --- Volume group ---
  VG Name               pve
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               2.91 TiB
  PE Size               4.00 MiB
  Total PE              762962
  Alloc PE / Size       538961 / <2.06 TiB
  Free  PE / Size       224001 / 875.00 GiB
  VG UUID               xh2x4j-B6OA-4HoT-WbfG-Y3yk-yd4r-XNEWsu

root@pve107:/dev/mapper# df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   63G     0   63G   0% /dev
tmpfs                  13G  1.4G   12G  11% /run
/dev/mapper/pve-root   98G   94G     0 100% /
tmpfs                  63G   40M   63G   1% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
tmpfs                  63G     0   63G   0% /sys/fs/cgroup
/dev/nvme0n1p2        511M  312K  511M   1% /boot/efi
/dev/fuse              30M   16K   30M   1% /etc/pve
tmpfs                  13G     0   13G   0% /run/user/0
root@pve107:/dev/mapper# lvresize -A n -L +1000G /dev/pve/root
  Insufficient free space: 256000 extents needed, but only 224001 available
root@pve107:/dev/mapper# lvresize -A n -L +100G /dev/pve/root
  Size of logical volume pve/root changed from 1.07 TiB (281600 extents) to 1.17 TiB (307200 extents).
  WARNING: This metadata update is NOT backed up.
  Logical volume pve/root successfully resized.
root@pve107:/dev/mapper# resize2fs -p /dev/pve/root
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/pve/root is mounted on /; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 150
The filesystem on /dev/pve/root is now 314572800 (4k) blocks long.

root@pve107:/dev/mapper# df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   63G     0   63G   0% /dev
tmpfs                  13G  1.4G   12G  11% /run
/dev/mapper/pve-root  1.2T   94G  1.1T   9% /
tmpfs                  63G   43M   63G   1% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
tmpfs                  63G     0   63G   0% /sys/fs/cgroup
/dev/nvme0n1p2        511M  312K  511M   1% /boot/efi
/dev/fuse              30M   16K   30M   1% /etc/pve
tmpfs                  13G     0   13G   0% /run/user/0

至此磁盘扩展完成

原文地址:https://www.cnblogs.com/ningy1009/p/14859164.html