Shrinking/ Resizing Partition using linux commands
Hi All,
I am a new user of Linux, and I actually had a task that is the ability to resize (specifically Shrink) storage of Virtual Machine, I was thinking that the best to start on is to know how to resize partition in linux using command line since our VM runs in Linux environment. Any help will be very much appreciated. Thank you |
Quote:
http://gparted.sourceforge.net/livecd.php You download the .iso file and burn it to a CD. There is not just one command line tool to do it all. Second, shrinking a virtual machines disk is just a file on a linux partition, and has nothing to do with the size of your linux partition. Of course the virtual disk can not exceed the real size of the linux partition. If you do decide to resize a partition, make sure you have any data backed up, and know how to restore it, before you play with the partitions. |
The basic method here, assuming you are not using LVM (logical volume management), is you would want to create another partition which is about 110% of your consumed disk space on the volume you wish to resize. Meaning, if your 10GB volume only has 1GB used, make a new partition of about 1.1GB (or slightly more) and copy all of the files over to that new partition. Resize the old partition using fdisk, or gparted, and then copy all of your data back over.
This would be the safest way to do it, but requires you have enough disk space to create a new partition slightly larger than the data you wish to retain. An easier method (but one I cannot necessarily vouch for) is to use a tool like gparted (or qtparted) - it's a nice GUI tool that wraps the command-line "parted" program with nice features to resize partitions and whatnot. I say I cannot vouch for it, because I have never used it for such purposes. |
Quote:
If you are using LVM on Red Hat or Fedora Core linux distributions (other distros may be the same but I'm not familiar with others so can't say for sure) you just issue the following commands to add space to a filesystem: [root@tortui tmp]# #Here are my volumes: [root@tortui tmp]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 12G 9.6G 1.6G 87% / /dev/mapper/VolGroup00-LogVol03 9.5G 7.3G 1.7G 82% /usr/local /dev/mapper/VolGroup00-LogVol04 9.7G 168M 9.3G 2% /m1 /dev/mapper/VolGroup00-LogVol02 9.5G 3.1G 5.9G 35% /home /dev/mapper/VolGroup00-u01 20G 11G 8.7G 55% /u01 /dev/sda1 99M 24M 70M 26% /boot tmpfs 2.0G 1.0G 1004M 51% /dev/shm [root@tortui tmp]# #Show free disk space on VolGroup00 [root@tortui tmp]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup00 2 7 0 wz--n- 148.88G 69.66G [root@tortui tmp]# #There's almost 70GB free [root@tortui tmp]# #Let's make u01 larger [root@tortui tmp]# lvresize -L +5G /dev/VolGroup00/u01 /dev/hda: open failed: No medium found Extending logical volume u01 to 25.31 GB Logical volume u01 successfully resized [root@tortui tmp]# #That adds 5GB to the Logical Volume u01 but we still need to enlarge the [root@tortui tmp]# # underlying file system. resize2fs will automatically enlarge the file system to fill the LV. [root@tortui tmp]# resize2fs /dev/VolGroup00/u01 resize2fs 1.39 (29-May-2006) Filesystem at /dev/VolGroup00/u01 is mounted on /u01; on-line resizing required Performing an on-line resize of /dev/VolGroup00/u01 to 6635520 (4k) blocks. The filesystem on /dev/VolGroup00/u01 is now 6635520 blocks long. [root@tortui tmp]# #That does it. You don't even need to unmount the file system. [root@tortui tmp]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 12G 9.6G 1.6G 87% / /dev/mapper/VolGroup00-LogVol03 9.5G 7.3G 1.7G 82% /usr/local /dev/mapper/VolGroup00-LogVol04 9.7G 168M 9.3G 2% /m1 /dev/mapper/VolGroup00-LogVol02 9.5G 3.1G 5.9G 35% /home /dev/mapper/VolGroup00-u01 25G 11G 14G 44% /u01 /dev/sda1 99M 24M 70M 26% /boot tmpfs 2.0G 1.0G 1004M 51% /dev/shm That's all there is to it. You can even enlarge the root file system while live. Shrinking is more work because you can't shrink a mounted file system. Here is an example: I want to reduce the size of /m1 from 19G to 10G: 1. df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 11G 5.3G 4.9G 52% / /dev/mapper/VolGroup00-LogVol03 9.5G 888M 8.1G 10% /usr/local /dev/mapper/VolGroup00-LogVol04 20G 176M 19G 1% /m1 /dev/mapper/VolGroup00-LogVol02 9.5G 3.1G 6.0G 34% /home /dev/sda1 99M 24M 70M 26% /boot 2. umount /m1 a. unmount the file system 3. e2fsck -f /dev/VolGroup00/LogVol04 a. check it for errors 4. resize2fs /dev/VolGroup00/LogVol04 9G a. Reduce it to a size smaller than the target size 5. e2fsck -f /dev/VolGroup00/LogVol04 a. Check for errors again 6. mount /m1 7. lvresize -L 10G /dev/VolGroup00/LogVol04 a. Reduce the Logical Volume to the target size 8. resize2fs /dev/VolGroup00/LogVol04 a. Resize the file system to fill the LV. resize2fs without a size parameter will automatically enlarge the filesystem to fit the LV. |
@ rexjustin_02
BTW, I suggest you backup your files before resizing. Sometimes resizing can go wrong. |
Hi,
Quote:
I guess 'parted' is not installed on your machine? 'parted' is a command that can be used from the cli; Quote:
"Knowledge is of two kinds. We Know a subject ourselves, or we know where we can find information upon it."- Samuel Johnson |
I also second using a live cd. I realize that you're working with virtual machines, but it's still probably the easiest way to do it. (stop the vm, restart it booting from CD, possibly as a virtual drive)
It's been a while since I've used GParted, but I had success with it a while back. It's probably on the Ubuntu installation CD (which can be run as a live cd, without installing). Note that if ntfsprogs is installed, it can even resize Windows volumes. (With some restrictions. For instance, if you resize the first partition, you can no longer boot an XP install in the second partition; stupid stuff like that.) |
korkiley thanks for your clear explanation. You make my life easy! Thanks.
|
All times are GMT -5. The time now is 11:38 PM. |