• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Linux分区扩容方式(根分区扩容,SWAP分区扩容,挂载新分区为目录)

    发布者: Error | 发布时间: 2025-6-17 08:19| 查看数: 41| 评论数: 0|帖子模式

    Linux 分区扩容(根分区扩容,SWAP 分区扩容,挂载新分区为目录)

    Linux 系统在运行过程中,出现磁盘空间不足,需要扩容该如何处理?
    本文描述了常见的扩容场景,包括根分区、SWAP 分区以及扩容某个目录。

    1. 根分区扩容


    1.1 标准分区扩容(本站 OVF 默认)

    本例为 CentOS 8 虚机,两块磁盘,磁盘 1 容量 60G 用于根目录(包含
    1. /boot
    复制代码
    ),磁盘 2 容量 4G 用于 SWAP。
    (1)扩容前状态如下:
    1. [root@sysin-c8 ~]# lsblk
    2. NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    3. sda      8:0    0   60G  0 disk
    4. └─sda1   8:1    0   60G  0 part /
    5. sdb      8:16   0    4G  0 disk
    6. └─sdb1   8:17   0    4G  0 part [SWAP]
    7. sr0     11:0    1 1024M  0 rom

    8. [root@sysin-c8 ~]# df -Th
    9. Filesystem     Type      Size  Used Avail Use% Mounted on
    10. devtmpfs       devtmpfs  386M     0  386M   0% /dev
    11. tmpfs          tmpfs     400M     0  400M   0% /dev/shm
    12. tmpfs          tmpfs     400M   11M  389M   3% /run
    13. tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
    14. /dev/sda1      xfs        60G  1.8G   59G   3% /
    15. tmpfs          tmpfs      80M     0   80M   0% /run/user/0
    复制代码
    (2)将虚机中将磁盘 1 容量扩展为 100G,这个过程就不截图了。
    若支持在线添加,可通过命令刷新磁盘状态:
    1. partprobe /dev/sda
    复制代码
    (3)开始扩容根目录:
    1. [root@sysin-c8 ~]# fdisk /dev/sda

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


    5. Command (m for help):
    复制代码
    (4)可以按 m 查看一下帮助:
    1. Command (m for help): m

    2. Help:

    3.   DOS (MBR)
    4.    a   toggle a bootable flag
    5.    b   edit nested BSD disklabel
    6.    c   toggle the dos compatibility flag

    7.   Generic
    8.    d   delete a partition
    9.    F   list free unpartitioned space
    10.    l   list known partition types
    11.    n   add a new partition
    12.    p   print the partition table
    13.    t   change a partition type
    14.    v   verify the partition table
    15.    i   print information about a partition

    16.   Misc
    17.    m   print this menu
    18.    u   change display/entry units
    19.    x   extra functionality (experts only)

    20.   Script
    21.    I   load disk layout from sfdisk script file
    22.    O   dump disk layout to sfdisk script file

    23.   Save & Exit
    24.    w   write table to disk and exit
    25.    q   quit without saving changes

    26.   Create a new label
    27.    g   create a new empty GPT partition table
    28.    G   create a new empty SGI (IRIX) partition table
    29.    o   create a new empty DOS partition table
    30.    s   create a new empty Sun partition table


    31. Command (m for help):
    复制代码
    (5)按 p 查看当前磁盘下的分区:
    1. Command (m for help): p
    2. Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
    3. Units: sectors of 1 * 512 = 512 bytes
    4. Sector size (logical/physical): 512 bytes / 512 bytes
    5. I/O size (minimum/optimal): 512 bytes / 512 bytes
    6. Disklabel type: dos
    7. Disk identifier: 0x7bb4c495

    8. Device     Boot Start       End   Sectors Size Id Type
    9. /dev/sda1  *     2048 125829119 125827072  60G 83 Linux

    10. # 本例该磁盘仅仅一个分区,Boot 下面有个可启动标记 *,/boot 没有独立分区
    复制代码
    (6)按 d 删除 / 分区:
    1. Command (m for help): d
    2. Selected partition 1
    3. Partition 1 has been deleted.

    4. Command (m for help):
    5. # 本例中只有一个分区,所以直接删除了,如果是有多个分区,会提示输入数字选择
    复制代码
    (7)按 n 创建新分区:
    1. Command (m for help): n
    2. Partition type
    3.    p   primary (0 primary, 0 extended, 4 free)
    4.    e   extended (container for logical partitions)
    5. Select (default p): p  #选择 p primary
    6. Partition number (1-4, default 1):  #直接回车默认 1 即 sda1
    7. First sector (2048-209715199, default 2048):  #直接回车默认值 (sysin)
    8. Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): #直接回车默认值,使用全部剩余空间

    9. Created a new partition 1 of type 'Linux' and of size 100 GiB.
    10. Partition #1 contains a xfs signature.

    11. Do you want to remove the signature? [Y]es/[N]o: N  #按 N 保留 xfs 签名,移除的话分区的 UUID 会变更。

    12. The signature will be removed by a write command.

    13. Command (m for help):
    复制代码
    (8)按 p 再次查看状态:
    1. Command (m for help): p
    2. Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
    3. Units: sectors of 1 * 512 = 512 bytes
    4. Sector size (logical/physical): 512 bytes / 512 bytes
    5. I/O size (minimum/optimal): 512 bytes / 512 bytes
    6. Disklabel type: dos
    7. Disk identifier: 0x6a72cc03

    8. Device     Boot Start       End   Sectors  Size Id Type
    9. /dev/sda1        2048 209715199 209713152  100G 83 Linux

    10. Filesystem/RAID signature on partition 1 will be wiped.

    11. Command (m for help):
    复制代码
    (9)重要步骤:按 a 设置可引导:
    本例
    1. /boot
    复制代码
    没有独立分区,需要需要设置 boot flag 即将分区设置为可引导:
    1. /boot
    复制代码
    独立分区的不需要此步骤。
    1. Command (m for help): a
    2. Selected partition 1
    3. The bootable flag on partition 1 is enabled now.

    4. # 按 p 再次确认,Boot 下面有了 * 符号
    5. Command (m for help): p
    6. Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
    7. Units: sectors of 1 * 512 = 512 bytes
    8. Sector size (logical/physical): 512 bytes / 512 bytes
    9. I/O size (minimum/optimal): 512 bytes / 512 bytes
    10. Disklabel type: dos
    11. Disk identifier: 0x6a72cc03

    12. Device     Boot Start       End   Sectors  Size Id Type
    13. /dev/sda1  *     2048 209715199 209713152  100G 83 Linux

    14. Filesystem/RAID signature on partition 1 will be wiped.

    15. Command (m for help):
    复制代码
    (10)按 w 保存:
    1. Command (m for help): w
    2. The partition table has been altered.
    3. Syncing disks.
    复制代码
    (11)重要步骤:同步文件系统中的容量。
    CentOS 7 开始默认使用 xfs 文件系统,使用 xfs_growfs 命令同步文件系统容量。
    如果是 Ext4(包括 2、3),使用 resize2fs 命令。
    1. xfs_growfs /
    2. # 注意 xfs_growfs 使用 mountpoint
    3. #resize2fs /dev/sda1
    4. # resize2fs 则使用 device
    复制代码
    (12)确认分区结果,可以重启一下系统确认是否正常
    1. [root@sysin-c8 ~]# lsblk
    2. NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    3. sda      8:0    0  100G  0 disk
    4. └─sda1   8:1    0  100G  0 part /
    5. sdb      8:16   0    4G  0 disk
    6. └─sdb1   8:17   0    4G  0 part [SWAP]
    7. sr0     11:0    1 1024M  0 rom
    8. [root@sysin-c8 ~]# df -Th
    9. Filesystem     Type      Size  Used Avail Use% Mounted on
    10. devtmpfs       devtmpfs  386M     0  386M   0% /dev
    11. tmpfs          tmpfs     400M     0  400M   0% /dev/shm
    12. tmpfs          tmpfs     400M   11M  389M   3% /run
    13. tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
    14. /dev/sda1      xfs       100G  2.1G   98G   3% /
    15. tmpfs          tmpfs      80M     0   80M   0% /run/user/0
    16. [root@sysin-c8 ~]#
    复制代码
    至此扩容成功完成。

    1.2 LVM 分区扩容

    LVM 名词解释:
    LVM (logical volume manager) 逻辑卷管理器
    其中主要分为这几个概念:

    • 物理卷 - Physical volume 简称 PV
    • 物理卷在逻辑卷管理器中属于最底层的,任何的逻辑卷和卷组都必需依靠物理卷来建立,物理卷可以是一个完整的硬盘,也可以是硬盘中的莫一个分区。
    • 卷组 - Volume group 简称 VG
    • 卷组是建立在物理卷之上,一个卷组中可以包含一个或者多个物理卷。
    • 逻辑卷 - Logical volume 简称 LV
    • 逻辑卷类似于非 LVM 系统中的硬盘分区,在逻辑卷之上可以建立文件系统 (比如
      1. /home
      复制代码
      或者
      1. /usr
      复制代码
      等)。
    一个建立逻辑卷的流程如下:PV -> VG -> LV,物理卷包含卷组,卷组包含逻辑卷。
    本例为 CentOS 7,一块磁盘,独立
    1. /boot
    复制代码
    分区,两个 LVM 分区,如下:
    1. # root @ C7-SYSIN in ~ [12:41:56]
    2. $ lsblk
    3. NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    4. sda               8:0    0  160G  0 disk
    5. ├─sda1            8:1    0    1G  0 part /boot
    6. └─sda2            8:2    0  159G  0 part
    7.   ├─centos-root 253:0    0  155G  0 lvm  /
    8.   └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
    9. sr0              11:0    1 1024M  0 rom

    10. # root @ C7-SYSIN in ~ [12:41:56]
    11. $ df -Th
    12. Filesystem              Type      Size  Used Avail Use% Mounted on
    13. /dev/mapper/centos-root xfs       155G  1.5G  154G   1% /
    14. devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
    15. tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
    16. tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
    17. tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
    18. /dev/sda1               xfs      1014M  142M  873M  14% /boot
    19. tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
    复制代码
    将该虚拟磁盘扩展到 260G。
    1. 说明:也可以添加一块磁盘,可以模拟新增了物理磁盘,只是下面盘符对应修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。
    复制代码
    (1)创建分区
    1. $ fdisk /dev/sda  #对原磁盘 /dev/sda 分区
    2. Welcome to fdisk (util-linux 2.23.2).

    3. Changes will remain in memory only, until you decide to write them.
    4. Be careful before using the write command.


    5. Command (m for help): p  #p 查看当前分区

    6. Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 sectors
    7. Units = sectors of 1 * 512 = 512 bytes
    8. Sector size (logical/physical): 512 bytes / 512 bytes
    9. I/O size (minimum/optimal): 512 bytes / 512 bytes
    10. Disk label type: dos
    11. Disk identifier: 0x000af364

    12.    Device Boot      Start         End      Blocks   Id  System
    13. /dev/sda1   *        2048     2099199     1048576   83  Linux
    14. /dev/sda2         2099200   335544319   166722560   8e  Linux LVM

    15. Command (m for help): n  #n 新建分区
    16. Partition type:
    17.    p   primary (2 primary, 0 extended, 2 free)
    18.    e   extended
    19. Select (default p): p  #p 主分区
    20. Partition number (3,4, default 3):  #默认按顺序,直接回车
    21. First sector (335544320-545259519, default 335544320):  #默认直接回车
    22. Using default value 335544320
    23. Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519):  #默认直接回车使用全部剩余空间
    24. Using default value 545259519
    25. Partition 3 of type Linux and of size 100 GiB is set

    26. Command (m for help): p  #p 再次查看分区

    27. Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 sectors
    28. Units = sectors of 1 * 512 = 512 bytes
    29. Sector size (logical/physical): 512 bytes / 512 bytes
    30. I/O size (minimum/optimal): 512 bytes / 512 bytes
    31. Disk label type: dos
    32. Disk identifier: 0x000af364

    33.    Device Boot      Start         End      Blocks   Id  System
    34. /dev/sda1   *        2048     2099199     1048576   83  Linux
    35. /dev/sda2         2099200   335544319   166722560   8e  Linux LVM
    36. /dev/sda3       335544320   545259519   104857600   83  Linux

    37. Command (m for help): w  #w 保存
    38. The partition table has been altered!

    39. Calling ioctl() to re-read partition table.

    40. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    41. The kernel still uses the old table. The new table will be used at
    42. the next reboot or after you run partprobe(8) or kpartx(8)
    43. Syncing disks.
    复制代码
    (2)刷新分区
    1. # root @ C7-SYSIN in ~ [13:31:00]
    2. $ partprobe /dev/sda

    3. # root @ C7-SYSIN in ~ [13:31:54]
    4. $ lsblk
    5. NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    6. sda               8:0    0  260G  0 disk
    7. ├─sda1            8:1    0    1G  0 part /boot
    8. ├─sda2            8:2    0  159G  0 part
    9. │ ├─centos-root 253:0    0  155G  0 lvm  /
    10. │ └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
    11. └─sda3            8:3    0  100G  0 part
    12. sr0              11:0    1 1024M  0 rom
    复制代码
    (3)创建 PV
    1. $ pvcreate /dev/sda3
    2.   Physical volume "/dev/sda3" successfully created.
    复制代码
    (4)查看 VG
    1. $ vgdisplay
    2.   --- Volume group ---
    3.   VG Name               centos
    4.   System ID
    5.   Format                lvm2
    6.   Metadata Areas        1
    7.   Metadata Sequence No  3
    8.   VG Access             read/write
    9.   VG Status             resizable
    10.   MAX LV                0
    11.   Cur LV                2
    12.   Open LV               2
    13.   Max PV                0
    14.   Cur PV                1
    15.   Act PV                1
    16.   VG Size               <159.00 GiB
    17.   PE Size               4.00 MiB
    18.   Total PE              40703
    19.   Alloc PE / Size       40703 / <159.00 GiB
    20.   Free  PE / Size       0 / 0
    21.   VG UUID               Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw
    复制代码
    VG 名称为 centos
    (5)扩展 VG
    使用 /dev/sda3 PV 扩展到 centos VG 中。
    1. $ vgextend centos /dev/sda3
    2.   Volume group "centos" successfully extended
    复制代码
    (6)查看 LV
    1. $ lvdisplay
    2.   --- Logical volume ---
    3.   LV Path                /dev/centos/root
    4.   LV Name                root
    5.   VG Name                centos
    6.   LV UUID                ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS
    7.   LV Write Access        read/write
    8.   LV Creation host, time localhost, 2021-08-22 14:12:50 +0800
    9.   LV Status              available
    10.   # open                 1
    11.   LV Size                <155.00 GiB
    12.   Current LE             39679
    13.   Segments               1
    14.   Allocation             inherit
    15.   Read ahead sectors     auto
    16.   - currently set to     8192
    17.   Block device           253:0

    18.   --- Logical volume ---
    19.   LV Path                /dev/centos/swap
    20.   LV Name                swap
    21.   VG Name                centos
    22.   LV UUID                jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi
    23.   LV Write Access        read/write
    24.   LV Creation host, time localhost, 2021-08-22 14:12:51 +0800
    25.   LV Status              available
    26.   # open                 2
    27.   LV Size                4.00 GiB
    28.   Current LE             1024
    29.   Segments               1
    30.   Allocation             inherit
    31.   Read ahead sectors     auto
    32.   - currently set to     8192
    33.   Block device           253:1
    复制代码
    需要扩展的根分区 LV 为 /dev/centos/root
    (7)将 VG 中的空闲空间扩展到根分区 LV
    1. $ lvextend -l +100%FREE /dev/centos/root
    2.   Size of logical volume centos/root changed from <155.00 GiB (39679 extents) to 254.99 GiB (65278 extents).
    3.   Logical volume centos/root successfully resized.
    复制代码
    (8)刷新根分区
    如果是 ext4 文件系统(包括 2、3),使用 resize2fs 命令。
    1. xfs_growfs /dev/centos/root
    复制代码
    (9)验证结果
    1. # root @ C7-SYSIN in ~ [13:39:53]
    2. $ lsblk
    3. NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    4. sda               8:0    0  260G  0 disk
    5. ├─sda1            8:1    0    1G  0 part /boot
    6. ├─sda2            8:2    0  159G  0 part
    7. │ ├─centos-root 253:0    0  255G  0 lvm  /
    8. │ └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
    9. └─sda3            8:3    0  100G  0 part
    10.   └─centos-root 253:0    0  255G  0 lvm  /
    11. sr0              11:0    1 1024M  0 rom

    12. # root @ C7-SYSIN in ~ [13:39:53]
    13. $ df -Th
    14. Filesystem              Type      Size  Used Avail Use% Mounted on
    15. /dev/mapper/centos-root xfs       255G  1.5G  254G   1% /
    16. devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
    17. tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
    18. tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
    19. tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
    20. /dev/sda1               xfs      1014M  142M  873M  14% /boot
    21. tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
    复制代码
    可以看到根目录容量扩展成功。

    2. SWAP 分区扩容


    2.1 创建文件作为 SWAP 分区(本站 OVF 默认)

    这是最简单的方式,直接在 / 目录下创建一个文件作为交换分区,可以非常灵活完美的启用和禁用 swap 并在线调整大小。
    (1)创建要作为 swap 分区的文件:
    增加 4GB 大小的交换分区,则命令写法如下,其中的 count 等于想要的块的数量(bs*count = 文件大小)。
    1. # 这里定义为 /swap.img
    2. dd if=/dev/zero of=/swap.img bs=1M count=4096
    3. # 修改权限
    4. chmod 600 /swap.img
    复制代码
    (2)创建 SWAP 分区文件系统:
    1. mkswap /swap.img #mkswap - set up a Linux swap area
    复制代码
    (3)启用交换分区文件:
    1. swapon /swap.img #启用 swap 文件
    复制代码
    此时使用
    1. free -m
    复制代码
    命令可以看到 Swap 的容量等于原有容量加上上述创建文件容量之和。
    (4)编辑
    1. /etc/fstab
    复制代码
    开机自动加载上述 swap 文件:
    增加如下一行。
    1. /swap.img    none    swap    defaults    0 0
    2. #  第二个字段在 CentOS 中默认使用 swap,两者并没有不一样。
    3. /swap.img    swap    swap    defaults    0 0
    4. # 关于字段的风格,使用 tab 或者空格都可以,数量没有要求,通常为了对齐使用多个空格
    复制代码
    直接命令添加:
    1. sudo sh -c "echo '/swap.img    none    swap    defaults    0 0' >> /etc/fstab"
    复制代码
    (5)取消 swap 文件
    1. swapoff /swap.img
    2. rm -r /swap.img
    复制代码
    然后编辑
    1. /etc/fstab
    复制代码
    ,删除上述添加的一行即可。
    Ubuntu 20.04 中默认使用
    1. /swap.img
    复制代码
    文件作为 SWAP 分区,可以直接 swapoff 并删除原文件,然后重新创建同名文件来简单扩容。我们这里借鉴了 Ubuntu 的做法,并推荐使用该种方式。

    2.2 标准分区 SWAP 扩容

    本例为 CentOS 8 虚机,两块磁盘,磁盘 1 容量 60G 用于根目录(包含
    1. /boot
    复制代码
    ),磁盘 2 容量 4G 用于 SWAP。
    (1)扩容前状态如下:
    1. [root@sysin-c8 ~]# lsblk
    2. NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    3. sda      8:0    0   60G  0 disk
    4. └─sda1   8:1    0   60G  0 part /
    5. sdb      8:16   0    4G  0 disk
    6. └─sdb1   8:17   0    4G  0 part [SWAP]
    7. sr0     11:0    1 1024M  0 rom

    8. [root@sysin-c8 ~]# df -Th
    9. Filesystem     Type      Size  Used Avail Use% Mounted on
    10. devtmpfs       devtmpfs  386M     0  386M   0% /dev
    11. tmpfs          tmpfs     400M     0  400M   0% /dev/shm
    12. tmpfs          tmpfs     400M   11M  389M   3% /run
    13. tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
    14. /dev/sda1      xfs        60G  1.8G   59G   3% /
    15. tmpfs          tmpfs      80M     0   80M   0% /run/user/0
    复制代码
    (2)将虚机中将磁盘 2 容量扩展为 16G,这个过程就不截图了。
    刷新磁盘状态:
    1. partprobe /dev/sdb
    复制代码
    (3)关闭 swap
    1. swapoff /dev/sdb1
    复制代码
    (4) 重新创建
    1. /dev/sdb1
    复制代码
    分区
    1. [root@sysin-c8 ~]# fdisk /dev/sdb  #选择 /dev/sdb

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


    5. Command (m for help): p  #查看当然分区
    6. Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 sectors
    7. Units: sectors of 1 * 512 = 512 bytes
    8. Sector size (logical/physical): 512 bytes / 512 bytes
    9. I/O size (minimum/optimal): 512 bytes / 512 bytes
    10. Disklabel type: dos
    11. Disk identifier: 0x316023cd

    12. Device     Boot Start     End Sectors Size Id Type
    13. /dev/sdb1        2048 8388607 8386560   4G 82 Linux swap / Solaris

    14. Command (m for help): d  #删除分区,只有一个分区不会提示选择数字
    15. Selected partition 1
    16. Partition 1 has been deleted.

    17. Command (m for help): n  #新建分区
    18. Partition type
    19.    p   primary (0 primary, 0 extended, 4 free)
    20.    e   extended (container for logical partitions)
    21. Select (default p): p  #主分区,输入 P 或者直接回车
    22. Partition number (1-4, default 1):   #直接回车按顺序 1
    23. First sector (2048-33554431, default 2048):   #直接回车
    24. Last sector, +sectors or +size{K,M,G,T,P} (2048-33554431, default 33554431):   #直接回车使用全部可用空间

    25. Created a new partition 1 of type 'Linux' and of size 16 GiB.
    26. Partition #1 contains a swap signature.

    27. Do you want to remove the signature? [Y]es/[N]o: N  # 选择 N 不要移除 swap 签名,磁盘 UUID 不变

    28. The signature will be removed by a write command.

    29. Command (m for help): t  #更改分区类型
    30. Selected partition 1
    31. Hex code (type L to list all codes): L  #输入 L,查看可用代码

    32. 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
    33. 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
    34. 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
    35. 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
    36. 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
    37. 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
    38. 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
    39. 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
    40. 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
    41. 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
    42. a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
    43. b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
    44. c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
    45. e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs
    46. f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT
    47. 10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
    48. 11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
    49. 12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor
    50. 14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor
    51. 16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary
    52. 17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS
    53. 18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE
    54. 1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
    55. 1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep
    56. 1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT
    57. Hex code (type L to list all codes): 82  #输入 82,更改为 SWAP
    58. Changed type of partition 'Linux' to 'Linux swap / Solaris'.

    59. Command (m for help): p  #再次查看分区,已经是 swap
    60. Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 sectors
    61. Units: sectors of 1 * 512 = 512 bytes
    62. Sector size (logical/physical): 512 bytes / 512 bytes
    63. I/O size (minimum/optimal): 512 bytes / 512 bytes
    64. Disklabel type: dos
    65. Disk identifier: 0x316023cd

    66. Device     Boot Start      End  Sectors Size Id Type
    67. /dev/sdb1        2048 33554431 33552384  16G 82 Linux swap / Solaris

    68. Filesystem/RAID signature on partition 1 will be wiped.

    69. Command (m for help): w  #保存退出
    70. The partition table has been altered.
    71. Calling ioctl() to re-read partition table.
    72. Re-reading the partition table failed.: Device or resource busy

    73. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
    复制代码
    最后提示需要重启或者运行 partprobe、kpartx 命令生效,这里执行命令:
    1. partprobe /dev/sdb1
    复制代码

    笔者在一次测试中执行 partprobe 命令报错,可能是分区过程中选择移除了 swap 签名,分区的 UUID 变更,直接重启,要多等一会儿才能进入系统,但是不影响正常运行。
    (5) 创建 swap 文件系统
    1. mkswap /dev/sdb1
    2. # 提示如下
    3. Setting up swapspace version 1, size = 16 GiB (17178816512 bytes)
    4. no label, UUID=df11dbb4-9665-4732-b865-03913713fa5e
    5. # 注意这里的 UUID 变更,需要修改替换 /etc/fstab
    复制代码
    (6) 打开 swap
    1. swapon /dev/sdb1
    复制代码
    (7)更改
    1. /etc/fstab
    复制代码
    如果在重新创建分区的过程中移除了 swap 签名,则磁盘 UUID 变更,需要编辑
    1. /etc/fstab
    复制代码
    做相应变更。
    即使没有移除 swap 签名,使用 mkswap 命令后 UUID 也变更了,也需要编辑
    1. /etc/fstab
    复制代码
    做相应变更。
    要查看分区的 UUID 使用命令
    1. blkid
    复制代码
    或者
    1. lsblk -f
    复制代码
    1. #UUID=c154479a-28e1-40b2-b10c-646b41693f51 swap                    swap    defaults        0 0 #原有
    2. UUID=df11dbb4-9665-4732-b865-03913713fa5e swap                    swap    defaults        0 0
    复制代码
    当然也可以新建额外的分区作为增加的 swap 分区,原有 swap 分区不变,最后编辑
    1. /etc/fstab
    复制代码
    自动加载即可。
    (8)更改
    1. /etc/default/grub
    复制代码
    grub 配置文件中有个 resume 选项,指向 swap 分区,如果标准分区默认使用的是 swap 分区的 UUID。
    1. vi /etc/default/grub
    2. # 找到如下行
    3. GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=c154479a-28e1-40b2-b10c-646b41693f51 rhgb quiet"  #原有
    4. # 修改如下
    5. GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=df11dbb4-9665-4732-b865-03913713fa5e rhgb quiet"
    6. # 重建配置
    7. grub2-mkconfig -o /boot/grub2/grub.cfg
    8. # UEFI boot mode:
    9. #grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
    复制代码
    2.3 LVM SWAP 扩容

    本例为 CentOS 7,一块磁盘,独立
    1. /boot
    复制代码
    分区,两个 LVM 分区,如下:
    1. # root @ C7-SYSIN in ~ [12:41:56]
    2. $ lsblk
    3. NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    4. sda               8:0    0  160G  0 disk
    5. ├─sda1            8:1    0    1G  0 part /boot
    6. └─sda2            8:2    0  159G  0 part
    7.   ├─centos-root 253:0    0  155G  0 lvm  /
    8.   └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
    9. sr0              11:0    1 1024M  0 rom

    10. # root @ C7-SYSIN in ~ [12:41:56]
    11. $ df -Th
    12. Filesystem              Type      Size  Used Avail Use% Mounted on
    13. /dev/mapper/centos-root xfs       155G  1.5G  154G   1% /
    14. devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
    15. tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
    16. tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
    17. tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
    18. /dev/sda1               xfs      1014M  142M  873M  14% /boot
    19. tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
    复制代码
    将该虚拟磁盘容量增加 12G。
    说明:也可以添加一块磁盘,可以模拟新增了物理磁盘,只是下面盘符对应修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。
    (1)关闭 swap
    查看
    1. /etc/fstab
    复制代码
    可以看到 swap 的 LV 名称:
    1. $ cat /etc/fstab | grep swap
    2. /dev/mapper/centos-swap swap                    swap    defaults        0 0
    复制代码
    将其关闭:
    1. swapoff /dev/mapper/centos-swap
    复制代码
    (2)创建分区
    1. $ fdisk /dev/sda  #对原磁盘 /dev/sda 分区,也可以是新增的磁盘,按顺序使用 /dev/sdb 操作方法是一样的。
    2. Welcome to fdisk (util-linux 2.23.2).

    3. Changes will remain in memory only, until you decide to write them.
    4. Be careful before using the write command.


    5. Command (m for help): p  #查看当前分区

    6. Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 sectors
    7. Units = sectors of 1 * 512 = 512 bytes
    8. Sector size (logical/physical): 512 bytes / 512 bytes
    9. I/O size (minimum/optimal): 512 bytes / 512 bytes
    10. Disk label type: dos
    11. Disk identifier: 0x000af364

    12.    Device Boot      Start         End      Blocks   Id  System
    13. /dev/sda1   *        2048     2099199     1048576   83  Linux
    14. /dev/sda2         2099200   335544319   166722560   8e  Linux LVM

    15. Command (m for help): n  #新建分区
    16. Partition type:
    17.    p   primary (2 primary, 0 extended, 2 free)
    18.    e   extended
    19. Select (default p):  #直接回车,默认主分区
    20. Using default response p
    21. Partition number (3,4, default 3):   #直接回车默认按顺序 3
    22. First sector (335544320-360710143, default 335544320):   #直接回车
    23. Using default value 335544320
    24. Last sector, +sectors or +size{K,M,G} (335544320-360710143, default 360710143):   #直接回车,使用全部剩余空间
    25. Using default value 360710143
    26. Partition 3 of type Linux and of size 12 GiB is set   #12G 没错

    27. Command (m for help): p  #再次查看分区

    28. Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 sectors
    29. Units = sectors of 1 * 512 = 512 bytes
    30. Sector size (logical/physical): 512 bytes / 512 bytes
    31. I/O size (minimum/optimal): 512 bytes / 512 bytes
    32. Disk label type: dos
    33. Disk identifier: 0x000af364

    34.    Device Boot      Start         End      Blocks   Id  System
    35. /dev/sda1   *        2048     2099199     1048576   83  Linux
    36. /dev/sda2         2099200   335544319   166722560   8e  Linux LVM
    37. /dev/sda3       335544320   360710143    12582912   83  Linux

    38. Command (m for help): w  #保存退出
    39. The partition table has been altered!

    40. Calling ioctl() to re-read partition table.

    41. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    42. The kernel still uses the old table. The new table will be used at
    43. the next reboot or after you run partprobe(8) or kpartx(8)
    44. Syncing disks.
    45. You have new mail.
    复制代码
    (3)刷新分区
    1. # root @ C7-SYSIN in ~ [13:31:00]
    2. $ partprobe /dev/sda

    3. # root @ C7-SYSIN in ~ [13:31:54]
    4. $ lsblk
    5. NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    6. sda               8:0    0  172G  0 disk
    7. ├─sda1            8:1    0    1G  0 part /boot
    8. ├─sda2            8:2    0  159G  0 part
    9. │ ├─centos-root 253:0    0  155G  0 lvm  /
    10. │ └─centos-swap 253:1    0    4G  0 lvm
    11. └─sda3            8:3    0   12G  0 part
    12. sr0              11:0    1 1024M  0 rom
    复制代码
    (4)创建 PV
    1. $ pvcreate /dev/sda3
    2.   Physical volume "/dev/sda3" successfully created.
    复制代码
    (5)查看 VG
    1. $ vgdisplay
    2.   --- Volume group ---
    3.   VG Name               centos
    4.   System ID
    5.   Format                lvm2
    6.   Metadata Areas        1
    7.   Metadata Sequence No  3
    8.   VG Access             read/write
    9.   VG Status             resizable
    10.   MAX LV                0
    11.   Cur LV                2
    12.   Open LV               2
    13.   Max PV                0
    14.   Cur PV                1
    15.   Act PV                1
    16.   VG Size               <159.00 GiB
    17.   PE Size               4.00 MiB
    18.   Total PE              40703
    19.   Alloc PE / Size       40703 / <159.00 GiB
    20.   Free  PE / Size       0 / 0
    21.   VG UUID               Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw
    复制代码
    VG 名称为 centos
    (6)扩展 VG
    使用 /dev/sda3 PV 扩展到 centos VG 中。
    1. $ vgextend centos /dev/sda3
    2.   Volume group "centos" successfully extended
    复制代码
    (7)查看 LV
    1. $ lvdisplay
    2.   --- Logical volume ---
    3.   LV Path                /dev/centos/root
    4.   LV Name                root
    5.   VG Name                centos
    6.   LV UUID                ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS
    7.   LV Write Access        read/write
    8.   LV Creation host, time localhost, 2021-08-22 14:12:50 +0800
    9.   LV Status              available
    10.   # open                 1
    11.   LV Size                <155.00 GiB
    12.   Current LE             39679
    13.   Segments               1
    14.   Allocation             inherit
    15.   Read ahead sectors     auto
    16.   - currently set to     8192
    17.   Block device           253:0

    18.   --- Logical volume ---
    19.   LV Path                /dev/centos/swap
    20.   LV Name                swap
    21.   VG Name                centos
    22.   LV UUID                jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi
    23.   LV Write Access        read/write
    24.   LV Creation host, time localhost, 2021-08-22 14:12:51 +0800
    25.   LV Status              available
    26.   # open                 2
    27.   LV Size                4.00 GiB
    28.   Current LE             1024
    29.   Segments               1
    30.   Allocation             inherit
    31.   Read ahead sectors     auto
    32.   - currently set to     8192
    33.   Block device           253:1
    复制代码
    需要扩展的根分区 LV 为 /dev/centos/swap
    (8)将 VG 中的空闲空间扩展到根分区 LV
    1. $ lvextend -l +100%FREE /dev/centos/swap
    2.   Size of logical volume centos/swap changed from 4.00 GiB (1024 extents) to <16.00 GiB (4095 extents).
    3.   Logical volume centos/swap successfully resized.
    复制代码
    (9)刷新 swap 分区
    1. partprobe /dev/centos/swap
    复制代码
    (10)验证结果
    1. # root @ C7-SYSIN in ~ [15:54:13]
    2. $ lsblk
    3. NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    4. sda               8:0    0  172G  0 disk
    5. ├─sda1            8:1    0    1G  0 part /boot
    6. ├─sda2            8:2    0  159G  0 part
    7. │ ├─centos-root 253:0    0  155G  0 lvm  /
    8. │ └─centos-swap 253:1    0   16G  0 lvm
    9. └─sda3            8:3    0   12G  0 part
    10.   └─centos-swap 253:1    0   16G  0 lvm
    11. sr0              11:0    1 1024M  0 rom

    12. # root @ C7-SYSIN in ~ [15:54:13]
    13. $ df -Th
    14. Filesystem              Type      Size  Used Avail Use% Mounted on
    15. /dev/mapper/centos-root xfs       155G  1.5G  154G   1% /
    16. devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
    17. tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
    18. tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
    19. tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
    20. /dev/sda1               xfs      1014M  142M  873M  14% /boot
    21. tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
    复制代码
    可以看到根目录容量扩展成功。
    (11)创建 swap 文件系统
    1. $ mkswap /dev/mapper/centos-swap
    2. mkswap: /dev/mapper/centos-swap: warning: wiping old swap signature.
    3. Setting up swapspace version 1, size = 16773116 KiB
    4. no label, UUID=fb85ef4d-59aa-4554-a3bd-b4cc37746c51
    复制代码
    (12)开启 swap
    1. swapon /dev/mapper/centos-swap
    复制代码
    使用
    1. free -m
    复制代码
    可以查看 swap 分区容量成功变为 16G,
    1. /etc/fstab
    复制代码
    无需修改。

    3. 挂载新的磁盘到新的分区

    本例使用 Ubuntu 20.04,分区如下,我们新增一块 100G 的磁盘,挂载为
    1. /data
    复制代码
    ,同时新增一块 100G 的磁盘,用来替换原有的
    1. /var
    复制代码
    目录(假定原有磁盘分区不够用)。当然我们也可以直接扩容根分区参看本文上述描述。这里我们分别创建独立的
    1. /data
    复制代码
    1. /var
    复制代码
    分区。
    创建独立的
    1. /data
    复制代码
    1. /var
    复制代码
    分区也可以使用 LVM 的方式,具体操作不同但是逻辑步骤是一样的,可以参看其他文章。本文使用标准分区。
    1. $ lsblk
    2. NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    3. sda                         8:0    0  160G  0 disk
    4. ├─sda1                      8:1    0    1M  0 part
    5. ├─sda2                      8:2    0    1G  0 part /boot
    6. └─sda3                      8:3    0  159G  0 part
    7.   └─ubuntu--vg-ubuntu--lv 253:0    0  159G  0 lvm  /
    8. sr0                        11:0    1 1024M  0 rom

    9. # sa @ U20 in ~ [16:14:05]
    10. $ df -Th
    11. Filesystem                        Type      Size  Used Avail Use% Mounted on
    12. udev                              devtmpfs  1.9G     0  1.9G   0% /dev
    13. tmpfs                             tmpfs     391M  1.1M  390M   1% /run
    14. /dev/mapper/ubuntu--vg-ubuntu--lv ext4      156G  6.0G  142G   5% /
    15. tmpfs                             tmpfs     2.0G     0  2.0G   0% /dev/shm
    16. tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
    17. tmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
    18. /dev/sda2                         ext4      976M  103M  806M  12% /boot
    19. tmpfs                             tmpfs     391M     0  391M   0% /run/user/1000
    复制代码
    Ubuntu 比较特别的,这里有个 1M 的分区为 bios_grub,无论使用 LVM 或是标准分区都会自动创建。
    新增硬盘后,如下,如果看不到新增磁盘需要重启或者执行 partprobe 命令。
    1. $ lsblk
    2. NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    3. sda                         8:0    0  160G  0 disk
    4. ├─sda1                      8:1    0    1M  0 part
    5. ├─sda2                      8:2    0    1G  0 part /boot
    6. └─sda3                      8:3    0  159G  0 part
    7.   └─ubuntu--vg-ubuntu--lv 253:0    0  159G  0 lvm  /
    8. sdb                         8:16   0  100G  0 disk
    9. sdc                         8:32   0  100G  0 disk
    10. sr0                        11:0    1 1024M  0 rom
    复制代码
    3.1 将磁盘挂载为新的目录

    (1)创建分区
    1. # sa @ U20 in ~ [16:23:43] C:1
    2. $ sudo fdisk /dev/sdb  #将 /dev/sdb 创建分区
    3. [sudo] password for sa:

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

    7. Device does not contain a recognized partition table.
    8. Created a new DOS disklabel with disk identifier 0x9a7ae73a.

    9. Command (m for help): n  #n 新建分区
    10. Partition type
    11.    p   primary (0 primary, 0 extended, 4 free)
    12.    e   extended (container for logical partitions)
    13. Select (default p):   #直接回车,主分区

    14. Using default response p.
    15. Partition number (1-4, default 1):   #直接回车,按顺序编号 1
    16. First sector (2048-209715199, default 2048):   #直接回车
    17. Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199):   #直接回车,使用全部可用空间

    18. Created a new partition 1 of type 'Linux' and of size 100 GiB.

    19. Command (m for help): p  #查看分区
    20. Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
    21. Disk model: VMware Virtual S
    22. Units: sectors of 1 * 512 = 512 bytes
    23. Sector size (logical/physical): 512 bytes / 512 bytes
    24. I/O size (minimum/optimal): 512 bytes / 512 bytes
    25. Disklabel type: dos
    26. Disk identifier: 0x9a7ae73a

    27. Device     Boot Start       End   Sectors  Size Id Type
    28. /dev/sdb1        2048 209715199 209713152  100G 83 Linux

    29. Command (m for help): w  #保存退出
    30. The partition table has been altered.
    31. Calling ioctl() to re-read partition table.
    32. Syncing disks.
    复制代码
    (2)创建文件系统(格式化分区)
    1. sudo mkfs -t ext4 /dev/sdb1
    2. # 上述使用 ext4,也可以使用 xfs
    复制代码
    (3)挂载到
    1. /data
    复制代码
    目录
    1. sudo mkdir /data
    2. sudo mount /dev/sdb1  /data
    复制代码
    (4)开机自动加载
    编辑
    1. /etc/fstab
    复制代码
    ,添加如下一行:
    1. /dev/sdb1 /data ext4 defaults 0 0
    复制代码
    (5)验证
    重启后查看是否正确加载了:
    1. $ df -Th
    2. Filesystem                        Type      Size  Used Avail Use% Mounted on
    3. udev                              devtmpfs  1.9G     0  1.9G   0% /dev
    4. tmpfs                             tmpfs     391M  1.2M  390M   1% /run
    5. /dev/mapper/ubuntu--vg-ubuntu--lv ext4      156G  6.0G  142G   5% /
    6. tmpfs                             tmpfs     2.0G     0  2.0G   0% /dev/shm
    7. tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
    8. tmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
    9. /dev/sda2                         ext4      976M  103M  806M  12% /boot
    10. tmpfs                             tmpfs     391M     0  391M   0% /run/user/1000
    11. /dev/sdb1                         ext4       98G   61M   93G   1% /data
    复制代码
    3.2 将磁盘挂载原有目录

    (1)创建分区
    1. # sa @ U20 in ~ [16:23:43] C:1
    2. $ sudo fdisk /dev/sdc  #将 /dev/sdc 创建分区
    3. [sudo] password for sa:

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

    7. Device does not contain a recognized partition table.
    8. Created a new DOS disklabel with disk identifier 0x9a7ae73a.

    9. Command (m for help): n  #n 新建分区
    10. Partition type
    11.    p   primary (0 primary, 0 extended, 4 free)
    12.    e   extended (container for logical partitions)
    13. Select (default p):   #直接回车,主分区

    14. Using default response p.
    15. Partition number (1-4, default 1):   #直接回车,按顺序编号 1
    16. First sector (2048-209715199, default 2048):   #直接回车
    17. Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199):   #直接回车,使用全部可用空间

    18. Created a new partition 1 of type 'Linux' and of size 100 GiB.

    19. Command (m for help): p  #查看分区
    20. Disk /dev/sdc: 100 GiB, 107374182400 bytes, 209715200 sectors
    21. Disk model: VMware Virtual S
    22. Units: sectors of 1 * 512 = 512 bytes
    23. Sector size (logical/physical): 512 bytes / 512 bytes
    24. I/O size (minimum/optimal): 512 bytes / 512 bytes
    25. Disklabel type: dos
    26. Disk identifier: 0x9a7ae73a

    27. Device     Boot Start       End   Sectors  Size Id Type
    28. /dev/sdc1        2048 209715199 209713152  100G 83 Linux

    29. Command (m for help): w  #保存退出
    30. The partition table has been altered.
    31. Calling ioctl() to re-read partition table.
    32. Syncing disks.
    复制代码
    (2)创建文件系统(格式化分区)
    1. sudo mkfs -t xfs /dev/sdc1
    2. # 上述使用 xfs,也可以使用 ext4
    复制代码
    (3)挂载到
    1. /varnew
    复制代码
    目录
    1. sudo mkdir /varnew
    2. sudo mount /dev/sdc1  /varnew
    复制代码
    (4)关键步骤:拷贝
    1. /var
    复制代码
    下的所有内容到新的硬盘
    1. sudo cp -a /var/** /varnew
    复制代码
    释义:
    “-a” 选项相当于 “-d、-p、-r” 选项,使用 “-a” 选项时,目标文件和源文件的所有属性都一致,包括源文件的所有者,所属组、时间和软链接性。
    这里使用了两个 “**”,确保如果 Bash 开启了 Globstar 选项后可以复制所有文件。
    (5)重命名当前
    1. /var
    复制代码
    目录(可选,也可以直接删除)
    1. sudo mv /var /varold
    复制代码
    (6)重新挂载硬盘到
    1. /var
    复制代码
    目录
    1. sudo umount /dev/sdc1
    2. sudo mkdir /var
    3. sudo mount /dev/sdc1 /var
    复制代码
    (7)设置开机启动自动挂载
    1. #sudo vi /etc/fstab
    2. #在文件的最后增加一行
    3. /dev/sdc1 /var xfs defaults 0 0
    复制代码
    (8)删除原有
    1. /var
    复制代码
    目录
    经过确认没有问题后,彻底删除原有
    1. /var
    复制代码
    1. sudo rm -rf /varold
    复制代码
    4. 小结

    使用标准分区扩容相比 LVM 似乎要简单一些,特别是在虚机环境中,虚拟磁盘是可以直接扩展容量的,并不需要使用 LVM 的特性。
    而在物理机环境中,新增物理磁盘扩容,使用 LVM 就发挥了优势。
    所以推荐在虚机或者云环境中使用标准分区(独立 swap 虚拟磁盘或者使用 swap 文件更便捷),物理机特别是数据量大的应用场景,可以考虑优先使用 LVM。

    5.附加


    MBR与GPT对比

    类别主引导方式主分区数量最大容量支持多少位系统分区方法MBRBIOS+MBR42T32和64fdiskGPTUEFI+GPT12818EB(1EB=1024PB=1048576TB)64parted
    Parted分区和创建逻辑卷LVM

    当对大于2TB硬盘进行分区时时,fdisk不再适用,需要使用parted对硬盘进行分区。
    Parted分区过程:
    1. parted -l  #查看所有磁盘状态
    2. parted /dev/vdb   #通过parted工具来创建大于2T的分区
    3. mklabel gpt   #创建创建磁盘标签
    4. mkpart primary 0% 100% #创建整个分区
    5. q #退出

    6. #其他命令
    7. -------------------
    8. (parted) mklabel    #创建创建磁盘标签
    9. New disk labeltype? gpt
    10. (parted) p  #查看分区状态
    11. (parted) mkpart
    12. Partition name? []? gpt2t   #指定分区名称
    13. File system type? [ext2]ext3    #指定分区类型
    14. Start? 1    #指定开始位置
    15. End? 2190GB #指定结束位置
    16. (parted) P  #显示分区信息
    17. (parted) Q  #退出
    复制代码
    创建逻辑卷的过程
    1. fdiks -l #查看分区
    2. pvcreate /dev/vdb1  #创建pv物理卷
    3. vgcreate vgdata /dev/vdb1  #创建vg卷组
    4. lvcreate -l +100%FREE -n lvdata vgdata #创建lv逻辑卷
    5. mkfs.xfs /dev/mapper/vgdata-lvdata #格式化逻辑卷
    6. mkdir /data   #创建数据文件夹
    7. mount /dev/mapper/vgdata-lvdata /data #将逻辑卷挂载到/data
    8. vim /etc/fastab  #添加开机挂载
    9. /dev/mapper/vgdata-lvdata /data  xfs  defaults 0 0
    10. mount -a #检查挂载
    复制代码
    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    来源:https://www.jb51.net/server/339409wxj.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表