<center></center>node
在平時的開發工做中,常常會建立磁盤不足夠大的虛擬機,而後往集羣裏面寫一些數據致使磁盤滿了。手動編輯虛擬機的磁盤大小是不會文件系統識別的,大多數同窗只能無奈的從新裝OS
,這裏我介紹一種基於LVM
實現動態的方式。app
原文地址: LVM動態擴展ide
LVM
是邏輯盤卷管理(LogicalVolumeManager
)的簡稱,它是Linux
環境下對磁盤分區進行管理的一種機制,LVM
是創建在硬盤和分區之上的一個邏輯層,來提升磁盤分區管理的靈活性。經過LVM系統管理員能夠輕鬆管理磁盤分區,如:將若干個磁盤分區鏈接爲一個整塊的卷組(volumegroup
),造成一個存儲池。管理員能夠在卷組上隨意建立邏輯卷組(logicalvolumes
),並進一步在邏輯卷組上建立文件系統。管理員經過LVM
能夠方便的調整存儲卷組的大小,而且能夠對磁盤存儲按照組的方式進行命名、管理和分配。ui
當前默認只有一個採用lvm
的分區,一開始sda
磁盤容量爲16G
,後來發現不夠用了,編輯磁盤大小爲50G
,可是能夠發現這50G
並無起到擴展分區容量的效果。spa
[root@tony-play ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_tonyplay-lv_root 14G 3.4G 9.6G 26% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 477M 42M 410M 10% /boot [root@tony-play ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 1024M 0 rom sda 8:0 0 50G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 15.5G 0 part ├─vg_tonyplay-lv_root (dm-0) 253:0 0 13.9G 0 lvm / └─vg_tonyplay-lv_swap (dm-1) 253:1 0 1.6G 0 lvm [SWAP]
能夠經過新增一塊其餘磁盤來擴容,我這邊採起的是增大當前磁盤的容量實現擴容。code
有時候由於系統設備處於繁忙狀態,因此分區須要重啓後纔會生效。orm
[root@tony-play ~]# fdisk /dev/sda 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): 3 First cylinder (2089-6527, default 2089): // 直接回車,用默認值就能夠了 Using default value 2089 Last cylinder, +cylinders or +size{K,M,G} (2089-6527, default 6527): // 直接回車,用默認值就能夠了 Using default value 6527 Command (m for help): w The partition table has been altered! # 能夠看到新建的分區sda3已結被建立出來了,採起默認值會將剩餘全部空間都分到分區中 [root@tony-play ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 1024M 0 rom sda 8:0 0 50G 0 disk ├─sda1 8:1 0 500M 0 part /boot ├─sda2 8:2 0 15.5G 0 part │ ├─vg_tonyplay-lv_root (dm-0) 253:0 0 13.9G 0 lvm / │ └─vg_tonyplay-lv_swap (dm-1) 253:1 0 1.6G 0 lvm [SWAP] └─sda3 8:3 0 34G 0 part
當前文件系統爲ext4
ip
[root@tony-play ~]# mount /dev/mapper/vg_tonyplay-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
[root@tony-play ~]# sudo mkfs.ext4 /dev/sda3 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 2228224 inodes, 8912727 blocks 445636 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 272 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@tony-play ~]# vgdisplay --- Volume group --- VG Name vg_tonyplay // 卷組名在下面擴展中會用到 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 15.51 GiB PE Size 4.00 MiB Total PE 3970 Alloc PE / Size 3970 / 15.51 GiB Free PE / Size 0 / 0 VG UUID Y9usSM-nDU5-ZAUd-Y3Te-u5Pd-uFBr-gcYHf0
[root@tony-play ~]# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created
vgextend vg_tonyplay /dev/sda3 // 卷組名在查看卷組信息中 Volume group "vg_tonyplay" successfully extended
/dev/vg_tonyplay/lv_root
就是根分區,也是咱們要擴展的分區。開發
[root@tony-play ~]# lvdisplay --- Logical volume --- LV Path /dev/vg_tonyplay/lv_root // 根分區 LV Name lv_root VG Name vg_tonyplay LV UUID IPd7lm-Sx8g-pe7k-llNL-j1wc-mbA2-2cAdsy LV Write Access read/write LV Creation host, time tony-play, 2017-04-10 17:58:53 -0400 LV Status available # open 1 LV Size 13.91 GiB Current LE 3561 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/vg_tonyplay/lv_swap LV Name lv_swap VG Name vg_tonyplay LV UUID qX637q-iD6i-8blp-hmmS-MvLy-xZ0y-b4D0BF LV Write Access read/write LV Creation host, time tony-play, 2017-04-10 17:59:07 -0400 LV Status available # open 1 LV Size 1.60 GiB Current LE 409 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
[root@tony-play ~]# lvextend /dev/vg_tonyplay/lv_root /dev/sda3 Size of logical volume vg_tonyplay/lv_root changed from 13.91 GiB (3561 extents) to 47.91 GiB (12264 extents). Logical volume lv_root successfully resized
ext4
用resize2fs
,xfs
用xfs_growfs
rem
[root@tony-play ~]# resize2fs /dev/vg_tonyplay/lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg_tonyplay/lv_root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 3 Performing an on-line resize of /dev/vg_tonyplay/lv_root to 12558336 (4k) blocks. The filesystem on /dev/vg_tonyplay/lv_root is now 12558336 blocks long.
能夠發現/dev/mapper/vg_tonyplay-lv_root
已經從開始的14G
擴展到了48G
。ok,這就說明大功告成了,不再用經過重裝系統這種蹩腳的方式擴容了
[root@tony-play ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_tonyplay-lv_root 48G 3.4G 42G 8% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 477M 42M 410M 10% /boot
至此,lvm
擴容工做的過程應該是比較清楚了,以後有機會的話我會再補充一下LVM
的壓縮、刪除等操做過程。