Linux磁盘扩容

linux在proxmox虚拟平台上,以前linux的磁盘大小为350G,现在要扩容到650G,增加300G,操作如下:

首先在pve中调整磁盘的大小

 

(上图所示有误:应该选择 Resize disk)

 

接着重新启动linux,查看磁盘

可以看到磁盘的总大小已经为600多G了,但用df命令查看

# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  5.1G   45G  11% /
devtmpfs                devtmpfs   16G     0   16G   0% /dev
tmpfs                   tmpfs      16G   84K   16G   1% /dev/shm
tmpfs                   tmpfs      16G  8.7M   16G   1% /run
tmpfs                   tmpfs      16G     0   16G   0% /sys/fs/cgroup
tmpfs                   tmpfs      16G  4.0K   16G   1% /tmp
/dev/mapper/centos-home xfs       284G  203G   82G  72% /home
/dev/sda1               xfs       497M  265M  233M  54% /boot
tmpfs                   tmpfs     3.2G   16K  3.2G   1% /run/user/42
tmpfs                   tmpfs     3.2G     0  3.2G   0% /run/user/1001

并没有检测到增加的大小,这里使用LVM来扩容。

# 创建分区
# fdisk /dev/sda
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): m  # 查看帮助
Command 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): n  # 新建分区
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p  # 创建主分区
Partition number (3,4, default 3):   # 默认
First sector (734003200-1363148799, default 734003200):   # 默认
Using default value 734003200
Last sector, +sectors or +size{K,M,G} (734003200-1363148799, default 1363148799):   # 默认
Using default value 1363148799
Partition 3 of type Linux and of size 300 GiB is set

Command (m for help): w  # 保存并退出
The 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 at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

# 查看分区
# fdisk -l

Disk /dev/sda: 697.9 GB, 697932185600 bytes, 1363148800 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 label type: dos
Disk identifier: 0x000edbcd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   734003199   366488576   8e  Linux LVM
/dev/sda3       734003200  1363148799   314572800   83  Linux

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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/centos-swap: 16.9 GB, 16911433728 bytes, 33030144 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/centos-home: 304.6 GB, 304615522304 bytes, 594952192 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

同上上面的命令,可以看到已经添加了/dev/sda3,大小为300G。接下来将新分区添加进LVM中。

# fdisk /dev/sda
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): t  # 改变分区系统Id
Partition number (1-3, default 3):   # 默认
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 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 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 b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 8e  # 指定要改成的id号,8e代表LVM
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w  # 保存退出
The 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 at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

# 查看修改后的磁盘
# sudo fdisk -l

Disk /dev/sda: 697.9 GB, 697932185600 bytes, 1363148800 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 label type: dos
Disk identifier: 0x000edbcd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   734003199   366488576   8e  Linux LVM
/dev/sda3       734003200  1363148799   314572800   8e  Linux LVM

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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/centos-swap: 16.9 GB, 16911433728 bytes, 33030144 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/centos-home: 304.6 GB, 304615522304 bytes, 594952192 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

这里将/dev/sda3改变成了LVM,接着格式化新添加的分区,格式化为xfs文件系统。

# partprobe
# mkfs -t xfs /dev/sda3
meta-data=/dev/sda3              isize=256    agcount=4, agsize=19660800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=78643200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=38400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

扩充根分区

# vgdisplay  # 查看当前逻辑卷
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               349.51 GiB
  PE Size               4.00 MiB
  Total PE              89474
  Alloc PE / Size       89458 / 349.45 GiB
  Free  PE / Size       16 / 64.00 MiB
  VG UUID               30vUpr-brgg-yF39-rOmD-LAOt-PPoZ-PuHe3z
   
# vgextend centos /dev/sda3  # 将/dev/sda3添加进逻辑卷
WARNING: xfs signature detected on /dev/sda3 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created
  Volume group "centos" successfully extended
[root@JPDB2 gpadmin]# vgdisplay  # 再次查看
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               649.50 GiB
  PE Size               4.00 MiB
  Total PE              166273
  Alloc PE / Size       89458 / 349.45 GiB
  Free  PE / Size       76815 / 300.06 GiB
  VG UUID               30vUpr-brgg-yF39-rOmD-LAOt-PPoZ-PuHe3z

在逻辑卷当中已经添加了新增加的300G空间,最后需要将/dev/sda3的空间添加进需要扩容的目录,这里为/dev/mapper/centos-home。

# 扩展空间
# lvextend -l+100%FREE /dev/mapper/centos-home /dev/sda3                
  Size of logical volume centos/home changed from 283.70 GiB (72626 extents) to 583.69 GiB (149425 extents).
  Logical volume home successfully resized.

# 调整磁盘
# lvresize -r -l+100%FREE centos/home
  Size of logical volume centos/home changed from 583.69 GiB (149425 extents) to 583.75 GiB (149441 extents).
  Logical volume home successfully resized.
meta-data=/dev/mapper/centos-home isize=256    agcount=4, agsize=18592256 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=74369024, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=36313, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 74369024 to 153027584

# 调整分区
# resize2fs /dev/mapper/centos-home
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-home
Couldn't find valid filesystem superblock.

# 查看结果
# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  5.1G   45G  11% /
devtmpfs                devtmpfs   16G     0   16G   0% /dev
tmpfs                   tmpfs      16G   84K   16G   1% /dev/shm
tmpfs                   tmpfs      16G  8.8M   16G   1% /run
tmpfs                   tmpfs      16G     0   16G   0% /sys/fs/cgroup
tmpfs                   tmpfs      16G  4.0K   16G   1% /tmp
/dev/mapper/centos-home xfs       584G  216G  369G  37% /home
/dev/sda1               xfs       497M  265M  233M  54% /boot
tmpfs                   tmpfs     3.2G   16K  3.2G   1% /run/user/42
tmpfs                   tmpfs     3.2G     0  3.2G   0% /run/user/1001

可以看到这里成功将/home目录扩容了将近300G。