在这篇文章中,将介绍添加新磁盘的步骤,该磁盘可以添加到现有卷组中以扩展逻辑卷,但我们没有使用卷组,那么我们必须使用 fdisk 或者 parted 在 Linux 中扩展或者缩小分区。
但是如果你想减少(缩小)一个现有的逻辑卷并使用另外的空间来扩展另一个逻辑卷,请按照下面的链接
如何在 Linux 中逐步减小 LVM 大小(无需重启在线)
我们可以看到我的根分区位于大小为 5.8Gb 的 LVM 上。
我想用 +1GB 扩展我的根文件系统。
现在这里可以有两种情况
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-root 5.8G 4.1G 1.4G 76% / tmpfs 504M 260K 504M 1% /dev/shm /dev/sda1 194M 26M 158M 15% /boot
- 我们想使用另外分区来扩展大小
- 我们想使用现有卷组中的空间扩展大小
将介绍使用这两种场景执行相同操作所需的步骤
场景2
如果卷组中已经有足够的空间来扩展根文件系统,那么我们不需要创建新的物理卷。
我们可以按照面所示的扩展 LVM 的步骤进行操作
场景1 使用添加分区扩展 LVM
# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes/512 bytes I/O size (minimum/optimal): 512 bytes/512 bytes Disk identifier: 0x000cf049 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 1306 10279936 8e Linux LVM Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes/512 bytes I/O size (minimum/optimal): 512 bytes/512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/VolGroup-root: 6278 MB, 6278873088 bytes 255 heads, 63 sectors/track, 763 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes/512 bytes I/O size (minimum/optimal): 512 bytes/512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/VolGroup-swap: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes/512 bytes I/O size (minimum/optimal): 512 bytes/512 bytes Disk identifier: 0x00000000
如我们所见,我添加了另外的分区硬盘 /dev/sdb 和 2GB 空间。
让我们用它来扩展我们的根文件系统。
执行步骤
- 为 /dev/sdb 创建一个 1 GB 存储分区
- 创建物理卷
- 扩展现有卷组
- 扩展 LVM
- 验证更改
使用 /dev/sdb 创建分区
# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x55693454. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-261, default 1): 1 Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): +1G Command (m for help): p Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes/512 bytes I/O size (minimum/optimal): 512 bytes/512 bytes Disk identifier: 0x55693454 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux Command (m for help): t Selected partition 1 Hex code (type L to list codes): L 0 Empty 24 NEC DOS 81 Minix/old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap/So c1 DRDOS/sec (FAT 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M/CTOS/. 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS/HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes/512 bytes I/O size (minimum/optimal): 512 bytes/512 bytes Disk identifier: 0x55693454 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
# partprobe /dev/sdb1
现在在这个阶段,我们的分区被创建为 /dev/sdb1 和 LVM 类型。
创建物理卷
# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created
扩展卷组
在扩展现有卷组的大小之前,让我们验证相同的大小
# vgdisplay --- Volume group -- VG Name VolGroup System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 5 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 9.80 GiB PE Size 4.00 MiB Total PE 2509 Alloc PE/Size 2009/7.85 GiB Free PE/Size 500/1.95 GiB VG UUID uH5AP5-b24E-92h7-nL8b-7Bio-fXe3-pstWIW
所以我们的卷组包含 9.80 GB
接下来使用以下命令扩展卷组
# vgextend VolGroup /dev/sdb1 Volume group "VolGroup" successfully extended
检查是否已更改
# vgdisplay --- Volume group -- VG Name VolGroup System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 6 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 10.81 GiB PE Size 4.00 MiB Total PE 2767 Alloc PE/Size 2009/7.85 GiB Free PE/Size 758/2.96 GiB VG UUID uH5AP5-b24E-92h7-nL8b-7Bio-fXe3-pstWIW
我们可以验证更改,例如:另外的 1 GB 已添加到我们的卷组中,我们可以使用它来扩展根文件系统
扩展 LVM
在扩展根 fs 之前让我们验证现有的大小
# lvdisplay /dev/VolGroup/root --- Logical volume -- LV Path /dev/VolGroup/root LV Name root VG Name VolGroup LV UUID Qn8TnI-TLNm-rl4Y-ORnd-zU3p-2Kj1-ALSLAg LV Write Access read/write LV Creation host, time , LV Status available # open 1 LV Size 5.85 GiB Current LE 1497 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
所以我们现有的根 LVM 大小是 5.85GB
让我们用 +1GB 扩展大小
# lvextend -L +1G /dev/VolGroup/root Extending logical volume root to 6.85 GiB Logical volume root successfully resized
验证更改
# lvdisplay /dev/VolGroup/root --- Logical volume -- LV Path /dev/VolGroup/root LV Name root VG Name VolGroup LV UUID Qn8TnI-TLNm-rl4Y-ORnd-zU3p-2Kj1-ALSLAg LV Write Access read/write LV Creation host, time , LV Status available # open 1 LV Size 6.85 GiB Current LE 1753 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
让我们看看更改是否使用 df 反映
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-root 5.8G 4.1G 1.4G 76% / tmpfs 504M 260K 504M 1% /dev/shm /dev/sda1 194M 26M 158M 15% /boot
但它仍然显示 root 的旧大小。
原因是因为我们需要使用 LVM 的新值更新分区表,这可以使用以下命令完成
# resize2fs /dev/VolGroup/root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/VolGroup/root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/VolGroup/root to 1795072 (4k) blocks. The filesystem on /dev/VolGroup/root is now 1795072 blocks long.
如果我们注意到上面最初的命令抛出错误,因为我们试图在已安装的分区上调整大小,但由于我们的文件系统是 ext4,我们获得了在线调整大小的添加功能,我们不需要卸载文件系统来扩展它的大小
NOTE: We can resize the file system online if following conditions are met
- 2.6.x 内核系列
- 必须格式化为 ext3 或者以上的文件系统
现在重新验证更改
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-root 6.8G 4.2G 2.3G 65% / tmpfs 504M 260K 504M 1% /dev/shm /dev/sda1 194M 26M 158M 15% /boot
所以一切都按预期进行。