Quote:
Originally Posted by rexjustin_02
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.
|
ARE you using LVM? I highly recommend it. It makes resizing file systems very easy. Of course you must have free space available in order to add space to a file system.
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.