Linux 系统磁盘扩容全攻略

艺帆风顺 发布于 2025-04-24 28 次阅读


链接:https://www.cnblogs.com/coderxueshan/p/17933299.html

Linux系统磁盘扩容

在控制台添加完磁盘后,

添加分区

重新启动系统后,执行以下操作

磁盘分区

[root@oracle ~]# fdisk /dev/sdaWelcome 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 forhelp): mCommand action   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)Command (m for help): nPartition type:   p   primary (3 primary, 0 extended, 1 free)   e   extendedSelect (default e): pSelected partition 4First sector (41943040-83886079, default 41943040):Using default value 41943040Last sector, +sectors or +size{K,M,G} (41943040-83886079, default 83886079):Using default value 83886079Partition 4 of type Linux and of size 20 GiB is setCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks. 

查看新的磁盘分区类型

[root@oracle ~]# fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000a9a6f   Device Boot      Start         End      Blocks   Id  System/dev/sda1            2048        6143        2048   83  Linux/dev/sda2   *        6144     2103295     1048576   83  Linux/dev/sda3         2103296    41943039    19919872   8e  Linux LVM/dev/sda4        41943040    83886079    20971520   83  LinuxDisk /dev/mapper/centos-root: 19.3 GB, 19323158528 bytes, 37740544 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes

修改磁盘分区类型

通过fdisk -l 查看到/dev/sdb3的分区Id为8e,所以,/dev/vdb4的id也修改与其一致。

[root@oracle ~]# fdisk /dev/sdaWelcome 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 forhelp): tPartition number (1-4, default 4):Hex code (type L to list all codes): L 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT- 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT- 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT- 4  FAT16  5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / . 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor14  Hidden FAT16 16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT1e  Hidden W95 FAT1 80  Old MinixHex code (type L to list all codes): 8eChanged type of partition 'Linux' to 'Linux LVM' Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks. 

查看修改后分区类型

[root@oracle ~]# fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000a9a6f   Device Boot      Start         End      Blocks   Id  System/dev/sda1            2048        6143        2048   83  Linux/dev/sda2   *        6144     2103295     1048576   83  Linux/dev/sda3         2103296    41943039    19919872   8e  Linux LVM/dev/sda4        41943040    83886079    20971520   8e  Linux LVMDisk /dev/mapper/centos-root: 19.3 GB, 19323158528 bytes, 37740544 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes

磁盘扩容

重启系统后,开始扩充分区

查看磁盘分区卷组

[root@oracle ~]# lvs  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert  root centos -wi-ao----   swap centos -wi-ao----   1.00g[root@oracle ~]# vgdisplay  --- Volume group ---  VG Name               centos  System ID  Format                lvm2  Metadata Areas        1  Metadata Sequence No  3  VG Access             read/write  VG Status             resizable  MAX LV                0  Cur LV                2  Open LV               2  Max PV                0  Cur PV                1  Act PV                1  VG Size                 PE Size               4.00 MiB  Total PE              4863  Alloc PE / Size       4863 /   Free  PE / Size       0 / 0  VG UUID               gp2KXC-VUTG-0C01-aAed-31UC-HkVO-qi3gus[root@oracle ~]#

扩展逻辑卷

  • 创建物理卷

[root@oracle ~]# pvcreate  /dev/sda4  Physical volume "/dev/sda4" successfully created.

如果出现 “Device /dev/sda4 not found."可以使用 partprobe 命令使系统获知新的分区表的情况

[root@oracle ~]# partprobeWarning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。[root@oracle ~]# pvcreate /dev/sda4Physical volume "/dev/sda4" successfully created.
  • 扩充卷组

[root@oracle ~]# vgextend centos /dev/sda4  Volume group "centos" successfully extended [root@oracle ~]# vgdisplay  --- Volume group ---  VG Name               centos  System ID  Format                lvm2  Metadata Areas        2  Metadata Sequence No  4  VG Access             read/write  VG Status             resizable  MAX LV                0  Cur LV                2  Open LV               2  Max PV                0  Cur PV                2  Act PV                2  VG Size               38.99 GiB  PE Size               4.00 MiB  Total PE              9982  Alloc PE / Size       4863 /   Free  PE / Size       5119 /   VG UUID               gp2KXC-VUTG-0C01-aAed-31UC-HkVO-qi3gus
  • 扩充逻辑卷

[root@oracle ~]# lvextend -l +100%FREE /dev/centos/root /dev/sda4  Size of logical volume centos/root changed from   Logical volume centos/root successfully resized.

修改文件系统大小

  • 查看文件系统类型

[root@oracle ~]# df -ThFilesystem              Type      Size  Used Avail Use% Mounted on/dev/mapper/centos-root xfs        37G   14G   24G  37% /devtmpfs                devtmpfs  898M     0  898M   0% /devtmpfs                   tmpfs     910M     0  910M   0% /dev/shmtmpfs                   tmpfs     910M  9.6M  901M   2% /runtmpfs                   tmpfs     910M     0  910M   0% /sys/fs/cgroup/dev/sda2               xfs      1014M  148M  867M  15% /boottmpfs                   tmpfs     182M     0  182M   0% /run/user/0
  • 修改文件系统大小

[root@oracle ~]# xfs_growfs /dev/mapper/centos-rootmeta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1179392 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=1        finobt=0 spinodes=0data     =                       bsize=4096   blocks=4717568, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=1log      =internal               bsize=4096   blocks=2560, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0data blocks changed from 4717568 to 9567232

注:如果文件系统是ext4,使用resize2fs修改文件系统大小。

扩容完毕!



春招已经开始啦,大家如果不做好充足准备的话,招很难找到好工作。


送大家一份就业大礼包,大家可以突击一下春招,找个好工作!