Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a CentOS 5 virtual machine and I added 20GB more to disk size. How do I initialize and extend the available space?

I'm new to Linux, so what should I do?

I tried loading GParted as well, but the drive is locked; how do I unlock it?

enter image description here

enter image description here

share|improve this question

6 Answers 6

You're not going to be able to use GParted because the filesystem is on LVM and GParted does not support that.

First, TAKE A BACKUP OF THE VM. Then perform the following as "root" from a command line.

  1. It looks like you've already rebooted but just in case, ensure the kernel recognizes the larger disk

    echo 1 > /sys/class/scsi_disk/0:0:0:0/device/rescan
    
  2. Resize the second partition with fdisk

    fdisk /dev/sda
    p     # print current
    d     # delete
    - 2
    n     # create
    - p   # primary partition
    2     # partition number
    - default first/last cylinders
    t     # change partition id
    - 2
    - 8e (hex code)
    p     # print the current again to verify it's expanded
    w     # write changes
    
  3. Reboot the virtual machine

    shutdown -r now
    
  4. Once it's back up, resize the LVM physical volume

    pvresize /dev/sda2
    
  5. Now resize the LVM logical volume (NOTE: I'm unable to decipher what your logical volumes name is based on the screenshots and thus am assuming LogVol00. You can determine this by running lvdisplay.)

    lvresize -l +100%FREE /dev/VolGroup00/LogVol00
    
  6. Finally, lets resize the filesystem

    resize2fs -p /dev/VolGroup00/LogVol00
    
  7. Verify the filesystem is larger

    df -h
    
  8. Done

share|improve this answer
    
No reboot neded after fdisk. Use partprobe there. The SCSI-rescan is interesting - does it work on a disk in use? –  Nils Jul 13 '12 at 20:27
1  
Late to the party :p I've had issues with partprobe not re-reading partitions that were in use so I end up rebooting in almost all cases. The SCSI rescan works just fine with disks in use. You can run dmesg to confirm the disk is seen as the new size –  TheDude Sep 19 '12 at 18:52
    
sad that there is nothing lika a scsi-rescan for xen-pv-disks. But that`s another topic... –  Nils Sep 19 '12 at 20:42
1  
Thank you very much, your instructions worked perfectly! –  Chris Haas Oct 11 '12 at 14:18
    
What should I be looking for after step 1 (echo&rescan)? How can I tell if the kernel recognizes the extra space or not? –  Jeff Feb 8 '13 at 0:42

Easy. Go into your VM as root. Type "fdisk -l" - if you already see the new disk size - good.

If not - try partprobe - if you still do not see the new disk size - reboot.

Now fdisk /dev/sda

  1. Write down you starting cylinder for the second partition.
  2. "Delete" the second partiton
  3. Recreate the second partiton, same starting cylinder, last cylinder for end
  4. set the type of the recreated partition to 8e (LVM)
  5. write the changes and quit fdisk
  6. partprobe
  7. pvresize /dev/sda2

Now your VG VolGroup00 has 20 GB more space. Do whatever you like with it. Just as you would do on a physical system (resize LVs, then resize the FS for example).

share|improve this answer

Download the Gparted live CD ISO. Then attach the ISO to CentOS and boot from live cd (go to the virtual machine settings, select “CD/DVD” and select “Use ISO” from the right-side panel). Then select the partition you want to resize. Click on the partition menu (top menu bar) then click on “Resize”. Then select “Apply all operations” from “Edit” menu.

share|improve this answer
    
I loaded, but the drive has the lock icon.. I attached the image –  Andrew Jul 12 '12 at 16:58
    

Here I am taking a sample partition to expand the disk from 5GB to 7GB with out losing Data. You can change according to your free space available.

Before increasing the disk, I have taken the following details from the Linux VM:

# df -h
 Filesystem            Size  Used Avail Use% Mounted on
 /dev/sda3             6.8G  2.1G  4.5G  32% /
 /dev/sda1              99M   12M   83M  13% /boot
 none                  252M     0  252M   0% /dev/shm
 /dev/sdb1             5.0G   39M  4.7G   1% /mnt/test
# fdisk -s /dev/sdb
Disk /dev/sdb: 5368 MB, 5368709120 bytes
 255 heads, 63 sectors/track, 652 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
 /dev/sdb1               1         652     5237158+  83  Linux

Shut the VM down now.

Now I am going to increase the existing virtual disk (/dev/sdb) from VMware the command prompt. Note: this task should be performed only when the VM is powered off.

Go to the location of virtual disk location.

# vmware-vdiskmanager -x  7Gb RedHatEnterprise_Linux_4-0.vmdk

Now it will grow the virtual disk to 7Gb

Now you need to power on the VM. Log into the VM using PuTTY or the console as the root user.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
 /dev/sda3             6.8G  2.1G  4.5G  32% /
 /dev/sda1              99M   12M   83M  13% /boot
 none                  252M     0  252M   0% /dev/shm
 /dev/sdb1             5.0G   39M  4.7G   1% /mnt/test

It still shows the old size. Unmount the partition which you are going to increase.

# umount /dev/sdb1

Run the file system repair on the disk

# fsck -n /dev/sdb1

Now were are goig to remove the journal from the disk to make it into an ext2 filesystem.

# tune2fs -O ^has_journal /dev/sdb1

Now start partitioning /dev/sdb to assign the increased space to /dev/sdb1. Note: Don’t Panic, you will not lose data here.

I am removing the existing /dev/sdb1 and recreating along with available free space.

# fdisk /dev/sdb
Command (m for help): m
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): p
Disk /dev/sdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         652     5237158+  83  Linux

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-913, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-913, default 913):
Using default value 913

Command (m for help): p
Disk /dev/sdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         913     7333641   83  Linux

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

Now restart the Linux machine to take effect of the partition (we can also use partprobe, it's strictly recommended to restart the machine).

# shutdown now -r

Now once again check the extended file system, if you skip this step, resize2fs command will recommend running it.

# e2fsck -f /dev/sdb1
e2fsck 1.35 (28-Feb-2004)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 13/655360 files (7.7% non-contiguous), 22121/1309289 blocks

Now resize the file system.

# resize2fs /dev/sdb1
resize2fs 1.35 (28-Feb-2004)
Resizing the filesystem on /dev/sdb1 to 1833410 (4k) blocks.
The filesystem on /dev/sdb1 is now 1833410 blocks long.

Now check the file system.

# fsck -n /dev/sdb1
fsck 1.35 (28-Feb-2004)
e2fsck 1.35 (28-Feb-2004)
/dev/sdb1: clean, 13/917504 files, 30538/1833410 blocks

Now recreate the partition with the ext3 format.

# tune2fs -j /dev/sdb1
tune2fs 1.35 (28-Feb-2004)
Creating journal inode: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Restart the Linux machine once to effectively use the ext3 filesystem.

# shutdown -r now

Once again you login to Linux box using root previleges. Now you can find the partition increased to 7 GB.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             6.8G  2.1G  4.5G  32% /
/dev/sda1              99M   12M   83M  13% /boot
none                  252M     0  252M   0% /dev/shm
/dev/sdb1             6.9G   39M  6.7G   1% /mnt/test
share|improve this answer
    
Why do you remove then reinstate the journal? This is not necessary, an ext3 filesystem can be extended just as well a an ext2 filesystem. Furthermore, you're missing one big piece of the puzzle: the partition to extend is on LVM. –  Gilles Jul 12 '12 at 21:16

I just did this, in VMware, with CentOS 6.4. Answer number 1 is correct, but is missing one step, that I discovered.

In Gparted, mine also had the lock icon.

On the row with the partition, that has the lock icon, right-click and select "de-activate". That makes the lock go away.

Then you can right-click and change the partition size.

Then apply the change.

Then right-click and select "activate".

share|improve this answer

You can make steps 5 and 6 to be one by adding the -r switch to lvresize. That will make lvresize run resize2fs automatically with the required params:

lvresize -r -l +100%FREE /dev/VolGroup00/LogVol00
share|improve this answer
    
Welcome to the *nix StackExchange! –  paraxor Aug 7 '13 at 21:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.