linux下扩展卷组大小

首先查看一下根目录的剩余可用空间大小,具体我的虚拟机来说,目前只有3.2GB可用,已经没有多少剩余了,现在需要做的就是扩展一下空间,如果到了可用空间为0,将会导致无法进入KDE(我用的是KDE,如果你用的是其他桌面环境,也将无法进入)桌面环境,但是仍然可以用CTRL+F2还是CTRL+SHIFT+F2来着进入终端环境进行操作,但是到那时已经有点晚了,我在工位的虚拟机就只能使用那种方式操作了。以下是在另一台虚拟机上演示的,顺便扩展一下这个虚拟机的空间。具体如下:

[zzz@zzfedora23 ~]$ df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 860M     0  860M   0% /dev
tmpfs                    872M  280K  871M   1% /dev/shm
tmpfs                    872M  1.5M  870M   1% /run
tmpfs                    872M     0  872M   0% /sys/fs/cgroup
/dev/mapper/fedora-root   18G   14G  3.2G  81% /
tmpfs                    872M   76K  872M   1% /tmp
/dev/sda1                477M  191M  257M  43% /boot
tmpfs                    175M   28K  175M   1% /run/user/42
tmpfs                    175M   24K  175M   1% /run/user/1000
[zzz@zzfedora23 ~]$

再查看一下当前系统识别出来的的磁盘设备:

[zzz@zzfedora23 ~]$ sudo fdisk -l
[sudo] password for zzz:
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd1e562e0

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048  1026047  1024000  500M 83 Linux
/dev/sda2       1026048 41943039 40916992 19.5G 8e Linux LVM


Disk /dev/mapper/fedora-root: 17.5 GiB, 18798870528 bytes, 36716544 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/fedora-swap: 2 GiB, 2147483648 bytes, 4194304 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
[zzz@zzfedora23 ~]$

可以看到这里实际的物理磁盘只有sda这一个,这是当时在创建虚拟机时创建的磁盘,它分为2个区,分别为sda1和sda2,且sda2为LVM卷,有关LVM卷的相关定义请自行查找资料。简单说就是/dev/mapper/fedora-root 这个卷组的空间不够了,又得知它是挂载到 根目录“/“上的,现在需要扩展,而/dev/mapper/fedora-root所对应的实际物理空间又是从/dev/sda2上划分出来的,可sda2只有19.5G那么大,要怎么扩展/dev/mapper/fedora-root呢? 这里使用的解决方法是:新创建一个磁盘,然后把它的空间也映射添加到/dev/mapper/fedora-root上去。具体方法或流程如下:

1.创建一个新的硬盘
由于使用的是虚拟机,所以需要先关掉虚拟机系统,然后使用虚拟机软件提供的功能添加一个新的硬盘。具体操作步骤就不说了,下面是创建了一个20GB的磁盘之后的操作。

[zzz@zzfedora23 ~]$ sudo fdisk -l
[sudo] password for zzz:
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd1e562e0

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048  1026047  1024000  500M 83 Linux
/dev/sda2       1026048 41943039 40916992 19.5G 8e Linux LVM


Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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/fedora-root: 17.5 GiB, 18798870528 bytes, 36716544 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/fedora-swap: 2 GiB, 2147483648 bytes, 4194304 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
[zzz@zzfedora23 ~]$

可以看到新添加的硬盘为sdb,容量为20G,还没有分区和格式化.

先分区,创建一个/dev/sdb1分区:

[root@zzfedora23 zzz]# fdisk /dev/sdb

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

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

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 0
Value out of range.
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):

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

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@zzfedora23 zzz]#

查看分区结果:

[root@zzfedora23 zzz]# fdisk -l
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd1e562e0

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048  1026047  1024000  500M 83 Linux
/dev/sda2       1026048 41943039 40916992 19.5G 8e Linux LVM


Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x537420c6

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 41943039 41940992  20G 83 Linux


Disk /dev/mapper/fedora-root: 17.5 GiB, 18798870528 bytes, 36716544 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/fedora-swap: 2 GiB, 2147483648 bytes, 4194304 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

注意,此处的分区类型是0x83,对应着Linux,而我们期望的是Linux LVM类型,也就是上面sda2的分区类型,只有把sdb1的分区类型也搞成Linux LVM才能添加进卷。
下面修改分区类型:

[root@zzfedora23 zzz]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.28).
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: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x537420c6

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 41943039 41940992  20G 83 Linux

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 1
Changed type of partition 'Linux' to 'FAT12'.

Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 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 or  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 ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT
Partition type (type L to list all types): 8e
Changed type of partition 'FAT12' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@zzfedora23 zzz]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.28).
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: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x537420c6

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 41943039 41940992  20G 8e Linux LVM

Command (m for help): q

再验证一下:


[root@zzfedora23 zzz]# fdisk -l
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd1e562e0

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048  1026047  1024000  500M 83 Linux
/dev/sda2       1026048 41943039 40916992 19.5G 8e Linux LVM


Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x537420c6

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 41943039 41940992  20G 8e Linux LVM


Disk /dev/mapper/fedora-root: 17.5 GiB, 18798870528 bytes, 36716544 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/fedora-swap: 2 GiB, 2147483648 bytes, 4194304 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

分完区了,开始给这个分区进行格式化,文件系统为ext4:

root@zzfedora23 zzz]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 5242624 4k blocks and 1310720 inodes
Filesystem UUID: 89f273c1-1cf2-4bfd-b6e0-72ebb4f4c5b8
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000

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

此时再检查一下:

[root@zzfedora23 zzz]# fdisk -l
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd1e562e0

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048  1026047  1024000  500M 83 Linux
/dev/sda2       1026048 41943039 40916992 19.5G 8e Linux LVM


Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x537420c6

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 41943039 41940992  20G 8e Linux LVM


Disk /dev/mapper/fedora-root: 17.5 GiB, 18798870528 bytes, 36716544 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/fedora-swap: 2 GiB, 2147483648 bytes, 4194304 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
[root@zzfedora23 zzz]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 860M     0  860M   0% /dev
tmpfs                    872M  260K  871M   1% /dev/shm
tmpfs                    872M  1.6M  870M   1% /run
tmpfs                    872M     0  872M   0% /sys/fs/cgroup
/dev/mapper/fedora-root   18G   14G  3.2G  81% /
tmpfs                    872M   76K  872M   1% /tmp
/dev/sda1                477M  191M  257M  43% /boot
tmpfs                    175M   28K  175M   1% /run/user/42
tmpfs                    175M   24K  175M   1% /run/user/1000
[root@zzfedora23 zzz]#

/dev/sda2 和 /dev/sdb1 分区类型都是Linux LVM,而/dev/mapper/fedora-root 的空间也还是18G,没有变化,这是因为到目前为止还没有将sdb1加入到/dev/mapper/fedora-root中去。接下来需要使用/dev/sdb1创建一个物理卷。

[root@zzfedora23 zzz]# 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@zzfedora23 zzz]#

创建完之后把这个物理卷加入到/dev/mapper/fedora-root所在的fedora组:
[root@zzfedora23 zzz]# vgextend fedora /dev/sdb1
  Volume group "fedora" successfully extended
[root@zzfedora23 zzz]#

此时可以看一下”fedora“这个卷组的信息:

[root@zzfedora23 zzz]# vgdisplay
  --- Volume group ---
  VG Name               fedora
  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               39.50 GiB
  PE Size               4.00 MiB
  Total PE              10113
  Alloc PE / Size       4994 / 19.51 GiB
  Free  PE / Size       5119 / 20.00 GiB
  VG UUID               o9cyeW-23WV-42f1-yv70-HvC2-7Rl2-xnDWrY

[root@zzfedora23 zzz]#

它的Size已经增加到39.5GiB了。抱歉在添加进入vg之前忘了使用vgdisplay查看卷组信息了,那时的卷组大小应该是19.5GiB。

到这里还没有完,还需要把sdb1加入到/dev/mapper/fedora-root中去:

[root@zzfedora23 zzz]# lvextend /dev/mapper/fedora-root /dev/sdb1
  Size of logical volume fedora/root changed from 17.51 GiB (4482 extents) to 37.50 GiB (9601 extents).
  Logical volume root successfully resized.
[root@zzfedora23 zzz]#

加完之后还需要重新更新一下设备大小:

[root@zzfedora23 zzz]# resize2fs /dev/mapper/fedora-root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/fedora-root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/fedora-root is now 9831424 (4k) blocks long.

[root@zzfedora23 zzz]#

最后再使用df命令查看一下:

[root@zzfedora23 zzz]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 860M     0  860M   0% /dev
tmpfs                    872M  260K  871M   1% /dev/shm
tmpfs                    872M  1.5M  870M   1% /run
tmpfs                    872M     0  872M   0% /sys/fs/cgroup
/dev/mapper/fedora-root   37G   14G   23G  38% /
tmpfs                    872M   72K  872M   1% /tmp
/dev/sda1                477M  191M  257M  43% /boot
tmpfs                    175M   28K  175M   1% /run/user/42
tmpfs                    175M   24K  175M   1% /run/user/1000
[root@zzfedora23 zzz]#

到此,根目录已经成功扩展了20G。
下一步重启一下就行了。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值