Author Topic: การ extend or resize harddisk size via lvm on centOS  (Read 18944 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
การ extend or resize harddisk size via lvm on centOS
« on: เมษายน 29, 2014, 03:17:53 PM »
ในกรณีที่เราใช้งานระบบ cloud ที่ใช้งาน OS= Centos หรือ Redhat ที่มีการทำ partition เป็น LVM อยู่แล้ว
แล้วก็ติดปัญหาเรื่องพื้นที่ บน harddisk กำลังจะเต็ม เราสามารถทำการเพิ่มหรือขยาย ขนาด harddisk ในระบบที่เป็น virtual space ได้นะครับ ซึ่งทำการเพิ่มจาก ถ้าเป็น esx ก็เพิ่มจาก vSphere
ตามขั้นตอนเหล่านี้ เลยครับ

a) Connect to the VMware ESXi 5 server using the vSphere Client. Edit the VM's properties to increase the hard disk size (VM has to be off) - I won't get into details on that, if you have the experience on managing ESXi you know what to do... I'm assuming the process is the same for ESXi 4. Now restart the VM.

 
b) Login via SSH to the VM and follow this process.

 
- First list all partitions:
$ls -al /dev/sda*

 
- Create new partition using fdisk:
$fdisk /dev/sda

Then:
    type p - to list all your partitions
    type n - to create a new partition
    type l - for "logical"  หรือเลือกเป็น type p (primary) แต่ถ้าเลือกเป็น extend จะไม่สามารถปรับเป็น LVM ได้นะครับ
    then give it a number (e.g. if you got 2 partitions listed as /dev/sda1 & /dev/sda2, for the new partition simply type "3" to create /dev/sda3)
    
    type t - to change the partition type to "Linux LVM"
    provide the partition number you previously created
    type 8e - for the "Linux LVM" type
    type p - to list the new partition table
    type w - to write changes and exit

 
- Reboot server 1 ครั้ง:
$reboot

 
- Assuming you created partition /dev/sda3, let's now create the physical volume in that partition:
$pvcreate /dev/sda3

 
- Now let's extend the server's Volume Group to that physical volume.
$vgdisplay
This will give you the info on your current Volume Group. Note down the entry next to "VG Name". That's your Volume Group name.

 
$vgextend EnterVolumeGroupNameHere /dev/sda3
หรือ
$vgextend /dev/VolGroup00  /dev/sda3

 
=== IMPORTANT NOTE [start] ===
If you get a message saying /dev/sda3 could not be added to your Volume Group, you need to remove the physical volume and recreate it. Metadata might have gotten corrupt and thus the volume cannot be added to your Volume Group. So just do:
$pvremove /dev/sda3
And then again:
$pvcreate /dev/sda3
=== IMPORTANT NOTE [end] ===

 
- Since we're (essentially) extending the main logical volume, let's get the name of that:
$lvdisplay
and note down the entry next to "LV Name". This is your logical volume's name (e.g. /dev/srv/root), which you'll now extend to the newly added partition/physical volume.

 
- Extend the logical volume by X GBs:
$lvextend -L +XG yourLogicalVolumeName
หรือ
$lvextend -L+10G  /dev/VolGroup00/LogVol01

 
Make sure you replace X above with the actual number of GBs you've added in your VM's settings. So if you increased your VM by 20GBs, the command becomes:
$lvextend -L +20G yourLogicalVolumeName

 
- Finally, let's resize the file system to the new allocated space:
$resize2fs yourLogicalVolumeName
หรือ
$resize2fs /dev/VolGroup00/LogVol01
แล้วต้องรอสักพักหนึ่งนะครับ ระบบทำการปรับขนาด disk ให้อยู่
(this may take some time depending on number of GBs added to the file system.

 
- Check the new file system sizes:
$df -hT

ก็จะเห็นเป็นขนาดที่เพิ่มขึ้นของ partition ที่ทำการขยายขนาดไปเมื่อครู่นี้ ครับผม
 
You should now see an increased disk space for your primary logical volume.

 
- Reboot and you're set :)

####more information reference
http://www.howtoforge.com/logical-volume-manager-how-can-i-extend-a-volume-group
http://www.tldp.org/HOWTO/LVM-HOWTO/extendlv.html
http://www.joomlaworks.net/blog/item/168-resizing-the-disk-space-on-ubuntu-server-vms
« Last Edit: เมษายน 30, 2014, 10:00:44 AM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: การ extend or resize harddisk size via lvm on centOS
« Reply #1 on: มีนาคม 13, 2018, 09:29:38 PM »
ถ้าต้องการให้ขยาย เท่ากับขนาดเต็มของ partition ที่แอดเข้ามาใหม่ ในตอนทำ lvextend สามารถทำได้ดังนี้คือ
#lvextend /dev/VolGroup00/LogVol00 /dev/sda3

เสร็จแล้วทำการขยาย block ผ่าน resize2fs หรือใช้ xfs_growfs ก็ได้ครับ หลังจากรัน resize2fs แล้วจะเห็นพื้นที่เพิ่มตอนรัน df -h ครับ
#resize2fs /dev/mapper/centos_sql01-root

#df -h
ก็จะเห็นขนาดพื้นที่เพิ่มขึ้นครับผม : )

###Reference
https://ma.ttias.be/increase-a-vmware-disk-size-vmdk-formatted-as-linux-lvm-without-rebooting/
https://christophermonzon.wordpress.com/2017/03/16/how-to-increase-the-size-of-a-linux-lvm-by-expanding-the-virtual-machine-disk/

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: การ extend or resize harddisk size via lvm on centOS
« Reply #2 on: พฤษภาคม 24, 2021, 03:53:06 PM »
== How to Extend disk in LVM

==df -h for extend disk "ubuntu--vg-ubuntu--lv "
/dev/mapper/ubuntu--vg-ubuntu--lv   66G  5.9G   30G  10% /

==check vg display
vgdisplay ubuntu-vg
  Alloc PE / Size       8832 / 34.50 GiB
  Free  PE / Size       8831 / <34.50 GiB

==extend +32G disk space
lvextend -L +32G /dev/mapper/ubuntu--vg-ubuntu--lv

==resize
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

==check disk again after extend successful
/dev/mapper/ubuntu--vg-ubuntu--lv   66G  5.9G   57G  10% /

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: การ extend or resize harddisk size via lvm on centOS
« Reply #3 on: พฤษภาคม 24, 2021, 04:25:55 PM »
==== add new disk (sdb) to vg LVM
fdisk /dev/sdb
n
p
1

=change type to LVM as 8e and write save config
t
8e
w

==create pv
pvcreate /dev/sdb1
y
vgextend ubuntu-vg /dev/sdb1
lvextend -L +32G /dev/mapper/ubuntu--vg-ubuntu--lv
xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv

« Last Edit: มิถุนายน 19, 2021, 08:40:25 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: การ extend or resize harddisk size via lvm on centOS
« Reply #4 on: ธันวาคม 03, 2022, 02:29:13 PM »
ในกรณีถ้ามีการปรับขยาย disk size  ของก้อนที่ใช้งานอยู่เดิมอยู่แล้ว บนระบบ ESX-i หรือ Proxmox
อาจจะทำให้ MBR (Master boot record) size เพี้ยนไปตอนที่รันเชคด้วย command #fdisk -l (แอลเล็ก , small L)

GPT PMBR size mismatch (209715199 != 262143999) will be corrected by w(rite)

สามารถแก้ไขปัญหาด้วย command parted -l (แอลเล็ก , small L)
# parted -l
Warning: Not all of the space available to /dev/sda appears to be used, you can
fix the GPT to use all of the space (an extra 52428800 blocks) or continue with
the current setting?
Fix/Ignore? Fix                                                           
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 134GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  106GB   106GB
 3      106GB   107GB   1074MB  xfs


Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  4800MB  4799MB  xfs          primary


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/ubuntu--vg-lv--0: 106GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:

Number  Start  End    Size   File system  Flags
 1      0.00B  106GB  106GB  xfs


เท่านี้ก็เป็นอันที่เรียบร้อย แล้ว
หลังจากนั้นให้ทำการ reboot os 1 รอบแล้วทำการ fdisk ตาม process ได้เลย
fdisk /dev/sda
n
p
4

=change type to LVM as 8e and write save config
t
8e
w

==create pv
pvcreate /dev/sda4
y
vgextend ubuntu-vg /dev/sda4
lvextend -L +25G /dev/mapper/ubuntu--vg-ubuntu--lv
xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv
« Last Edit: ธันวาคม 03, 2022, 02:31:50 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: การ extend or resize harddisk size via lvm on centOS
« Reply #5 on: เมษายน 03, 2023, 09:02:10 AM »
=== change mount /dev/sdb1 to mount with UUID
หลังจากทำ fdisk เสร็จให้ทำการ format เป็น
mkfs.xfs /dev/sdb1

แล้วทำการกำหนด blkid
sudo blkid /dev/sdb1
หรือ
sudo vol_id --uuid /dev/sdb1
จะได้ UUID ของ /dev/sdb1 ออกมา
แล้วทำการ mount ไว้ใน /etc/fstab ดังนี้
UUID="xxxxx-xxxxxx-xxxxxx" /nsm2 xfs defaults 0 1

แล้วสั่ง
#mount -a
ก็จะมี /nsm2 ถูก mount ขึ้นมากับบน UUID ของ /dev/sdb1
« Last Edit: เมษายน 03, 2023, 09:17:33 AM by golfreeze »