LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shrinking/ Resizing Partition using linux commands (https://www.linuxquestions.org/questions/linux-newbie-8/shrinking-resizing-partition-using-linux-commands-847733/)

rexjustin_02 12-01-2010 12:18 PM

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

camorri 12-01-2010 01:11 PM

Quote:

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.
To resize linux partitions, I would suggest you look at gparted live. Here is a link.

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.

indienick 12-01-2010 01:15 PM

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.

korkiley 12-01-2010 01:57 PM

Quote:

Originally Posted by rexjustin_02 (Post 4177266)
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.

dv502 12-01-2010 06:41 PM

@ rexjustin_02

BTW, I suggest you backup your files before resizing. Sometimes resizing can go wrong.

onebuck 12-01-2010 07:43 PM

Hi,
Quote:

Originally Posted by camorri (Post 4177310)
To resize linux partitions, I would suggest you look at gparted live. Here is a link.

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.


I guess 'parted' is not installed on your machine? 'parted' is a command that can be used from the cli;
Quote:

excerpt from 'man parted';

NAME
GNU Parted - a partition manipulation program

SYNOPSIS
parted [options] [device [command [options...]...]]

DESCRIPTION
parted is a disk partitioning and partition resizing program. It allows you
to create, destroy, resize, move and copy ext2, linux-swap, FAT, FAT32, and
reiserfs partitions. It can create, resize, and move Macintosh HFS parti-
tions, as well as detect jfs, ntfs, ufs, and xfs partitions. It is useful
for creating space for new operating systems, reorganising disk usage, and
copying data to new hard disks.

This manual page documents parted briefly. Complete documentation is dis-
tributed with the package in GNU Info format; see near the bottom.
...
A LiveCD would prevent problems with a mounted filesystem.

"Knowledge is of two kinds. We Know a subject ourselves, or we know where we can find information upon it."- Samuel Johnson


gd2shoe 12-01-2010 10:31 PM

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.)

mhy 02-02-2011 05:06 AM

korkiley thanks for your clear explanation. You make my life easy! Thanks.


All times are GMT -5. The time now is 11:38 PM.