Sei sulla pagina 1di 7

How to extend the filesystem on a Vmware ESX guest Red Hat Linux server May 26, 2011 By Andrew

Lin Being the administrator of a large Linux shop, I have had to extend the filesyst ems on the Red Hat Linux server a few times. Each time I had the opportunity to extend the filesystem, I found that I struggled to remember the commands and ste ps. This is because Linx is a very command line driven operating system. All rig ht that is not completely true anymore, some variants of Linux have graphical in terfaces. Such as Red Hat has the graphical Logical Volume Manager (LVM), but I find the LVM to be buggy and unreliable at times. Personally I find it more reli able and easier to extend the filesystem using the command line interface. The sequence to extend the filesystem is as follow. (1) Add the new disk. In my case I was running Red Hat Enterprise 5 Linux as a g uest operating system in Vmware ESX server. I simply had to increase the disk si ze using the Vmware Vcenter interface, under edit setting, disk provisioning. Yo u will need to reboot the guest OS after to recognize the new disk size. (2) Partition the new disk space using fdisk. (3) Initialize the new partition, using the command pvcreate. Run partprobe to u pdate the kernel. (4) Extend the existing volume group with the new partition using the command vg extend. (5) Extend the existing logical volume group using lvextend. (6) Grow the filesystem with the command resize2fs. Take a snapshot of the existing disks and to understand how they are partitioned . The fdisk command will display the configuration of the existing disks. In this example there are two disks, a 10.7 GB /dev/sda and /dev/sdb which is 21.4 GB. D isk sda has two partitions, sda1 and sda2. The second disk sdb has only one part ition sdb1. [root@linux_server ~]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes ice Boot Start End Blocks Id System /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 1305 10377990 8e Linux LVM Disk /dev/sdb: 21.4 GB, 21475491840 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes ice Boot Start End Blocks Id System /dev/sdb1 1 2610 20964793+ 8e Linux LVM You also need to take a snapshot of how the partitions are allocated to the volu me groups. In the below example, the result of pvscan indicates that sda2 is map ped to the volume group VolGroup00. But where is sda1? Well sda1 is mapped to th e /boot filesystem, you will see it when you issue the mount command. You can al so use the command pvs or pvdisplay instead of pvscan.

[root@linux_server ~]# pvscan PV /dev/sdb1 VG gfs_vg lvm2 [19.99 GB / 8.99 GB free] PV /dev/sda2 VG VolGroup00 lvm2 [9.88 GB / 0 free] Total: 2 [29.84 GB] / in use: 2 [29.84 GB] / in no VG: 0 [0 ] The mount command will show you that /dev/sda1 is mounted as /boot. In this exam ple we will be extending the / (root) filesystem. [root@linux_server ~]# mount /dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts typepts (rw,gid=5,mode=620) /dev/sda1 on /boot type ext3 (rw) tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) none on /sys/kernel/config type configfs (rw) /dev/mapper/gfs_vg-gfs_lv on /mnt/gfs type gfs2 (rw,noatime,nodiratime,hostdata= jid=0:id=196609:first=0) Add the new disk or extend the existing disk in Vmware. Expand the disk sda using the VMware Vcenter management console. Under the summa disk provisioning. Simply increa ry tab of the guest server, go to edit settings se the disk size and then reboot the guest OS. Partition the disk After the guest server has been rebooted it should see the size increment in sda . You now need to partition the new disk space using the command fdisk. [root@linux_server ~]# fdisk /dev/sda The number of cylinders for this disk is set to 2610. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): m (select m to display the help screen) Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition 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 (select n to add a new partition)

Command action e extended p primary partition (1-4) p (select p to create a primary partition) Partition number (1-4): 3 (enter 3 for partition number, we already know from th e output of fdisk -l that sda1 and sda2 already exist, therefore the new partiti on will be sda3) First cylinder (1306-2610, default 1306): (hit the enter key to accept the defau lt) Using default value 1306 Last cylinder or +size or +sizeM or +sizeK (1306-2610, default 2610): (press the enter key to accept the default. You can also select something lower than 2610, if you do not want to use all available free disk space) Using default value 2610 Command (m for help): t (select t to change the system id) Partition number (1-4): 3 (select the new partition number) Hex code (type L to list codes): 8e (Enter 8e which is the code for Linux LVM) Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): p (this will print the partition table, verify the changes you made) Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes ice Boot Start End Blocks Id System /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 1305 10377990 8e Linux LVM /dev/sda3 1306 2610 10482412+ 8e Linux LVM Command (m for help): w (this will write the table tto disk committing it) The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16:ice or resource bus y. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks. Note: (May have to hit enter to get command prompt back) Initialize the physical volume Initialize the new physical volume with the command pvcreate /dev/sda3. If you h ave not rebooted the operating system after creating the new partition, then you will need to run partprobe for the kernel to recognize the new partition. Or el se you will get an error. [root@linux_server ~]# pvcreate /dev/sda3 ice /dev/sda3 not found (or ignored by filtering). Run partrobe to update the kernel. [root@linux_server]# partprobe -s /dev/sda: msdos partitions 1 2 3 /dev/sdb: msdos partitions 1 Initialize the partition.

[root@linux_server]# pvcreate /dev/sda3 Physical volume /dev/sda3? successfully created Add new physical volume to the volume group VolGroup00 The command vgdisplay will show you the specifics of the existing volume groups. Note the VG size is 9.88 GB. You can also use vgs and vgscan to display a diffe rent view of all volume groups. [root@linux_server ~]# vgdisplay Volume group VG Name gfs_vg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 7 VG Access read/write VG Status resizable Clustered yes Shared no MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 19.99 GB PE Size 4.00 MB Total PE 5118 Alloc PE / Size 2816 / 11.00 GB Free PE / Size 2302 / 8.99 GB VG UUID ws74W5-bejg-UUqe-ReFJ-ip6i-4Ixy-CAKkDC Volume group VG Name VolGroup00 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 9.88 GB PE Size 32.00 MB Total PE 316 Alloc PE / Size 316 / 9.88 GB Free PE / Size 0 / 0 VG UUID KxKjSV-yOQl-T0gR-vse1-Z0RO-nJfM-FQxMZ0 Add sda3 to VolGroup00 [root@linux_server]# vgextend /dev/VolGroup00 /dev/sda3 Volume group VolGroup00? successfully extended Note that the VG size has increased to 19.84 GB.

[root@linux_server]# vgdisplay Volume group VG Name gfs_vg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 7 VG Access read/write VG Status resizable Clustered yes Shared no MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 19.99 GB PE Size 4.00 MB Total PE 5118 Alloc PE / Size 2816 / 11.00 GB Free PE / Size 2302 / 8.99 GB VG UUID ws74W5-bejg-UUqe-ReFJ-ip6i-4Ixy-CAKkDC Volume group VG Name VolGroup00 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 19.84 GB PE Size 32.00 MB Total PE 635 Alloc PE / Size 316 / 9.88 GB Free PE / Size 319 / 9.97 GB VG UUID KxKjSV-yOQl-T0gR-vse1-Z0RO-nJfM-FQxMZ0 Extend the Logical Volume Group /dev/VolGroup00/LogVol00 which is mounted as the root filesystem. Display the specifics for all logical volume with lvdisplay. You can also use lv scan or lvs to display a summarized view. [root@linux_server]# lvdisplay Logical volume LV Name /dev/gfs_vg/gfs_lv VG Name gfs_vg LV UUID NZrcfq-6Shf-z5cZ-fWJM-fQVi-wOTa-kPhUVG LV Write Access read/write LV Status available # open 1 LV Size 10.00 GB Current LE 2560

Segments 1 Allocation inherit Read ahead sectors auto currently set to 256 Blockice 253:2 Logical volume LV Name /dev/gfs_vg/quorum_lv VG Name gfs_vg LV UUID Pr27bq-X1AM-U6WC-ceJd-dlIu-AipK-pFRfAE LV Write Access read/write LV Status available # open 1 LV Size 1.00 GB Current LE 256 Segments 1 Allocation inherit Read ahead sectors auto currently set to 256 Blockice 253:3 Logical volume LV Name /dev/VolGroup00/LogVol00 VG Name VolGroup00 LV UUID n4QGnw-R9Wj-RHXU-Q1QO-X2aa-vKdx-Js3fwp LV Write Access read/write LV Status available # open 1 LV Size 8.88 GB Current LE 284 Segments 1 Allocation inherit Read ahead sectors auto currently set to 256 Blockice 253:0 Logical volume LV Name /dev/VolGroup00/LogVol01 VG Name VolGroup00 LV UUID qqyXcr-nys3-K7MO-LsbU-5CL8-d2hR-MyliKF LV Write Access read/write LV Status available # open 1 LV Size 1.00 GB Current LE 32 Segments 1 Allocation inherit Read ahead sectors auto currently set to 256 Blockice 253:1 Extend /dev/VolGroup00/LogVol00 using lvextend. The option -l +100%FREE will use up all available disk space. You can also specify the size with the option -L18 G, which will extend the logical volume to 18 GB. The option -L+1G will add anot her gigabyte to the logical volume. [root@linux_server]# lvextend -l +100%FREE /dev/VolGroup00/LogVol00 Extending logical volume LogVol00 to 18.84 GB Logical volume LogVol00 successfully resized

Extend the filesystem We are almost done, the last step is to extend the filesystem. Record the size of the existing filesystems with the df -h command. [root@linux_server]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 8.6G 8.0G 205M 98% / /dev/sda1 99M 19M 75M 21% /boot tmpfs 490M 0 490M 0% /dev/shm /dev/mapper/gfs_vg-gfs_lv 9.8G 584M 9.2G 6% /mnt/gfs Expand the filesystem /dev/mapper/VolGroup00-LogVol00 which is mounted on /. The command resize2fs can be used to enlarge or shrink ext2 or ext3 filesystems tha t are unmounted. On Linux kernel version that is equal or greater than 2.6, resi ze2fs can expand a mounted ext3 filesystem only. [root@linux_server]# resize2fs /dev/mapper/VolGroup00-LogVol00 resize2fs 1.39 (29-May-2006) Filesystem at /dev/mapper/VolGroup00-LogVol00 is mounted on /; on-line resizing required Performing an on-line resize of /dev/mapper/VolGroup00-LogVol00 to 4939776 (4k) blocks. The filesystem on /dev/mapper/VolGroup00-LogVol00 is now 4939776 blocks long. Verify that /dev/mapper/VolGroup00-LogVol00 has increased in size. [root@linux_server]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 19G 8.0G 9.4G 46% / /dev/sda1 99M 19M 75M 21% /boot tmpfs 490M 0 490M 0% /dev/shm /dev/mapper/gfs_vg-gfs_lv 9.8G 584M 9.2G 6% /mnt/gfs

Potrebbero piacerti anche