LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-01-2010, 12:18 PM   #1
rexjustin_02
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
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
 
Old 12-01-2010, 01:11 PM   #2
camorri
LQ Guru
 
Registered: Nov 2002
Location: Somewhere inside 9.9 million sq. km. Canada
Distribution: Slackware 14.1, 14.2
Posts: 5,151

Rep: Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504
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.
 
Old 12-01-2010, 01:15 PM   #3
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 12-01-2010, 01:57 PM   #4
korkiley
LQ Newbie
 
Registered: May 2008
Location: South Burlington, Vermont
Distribution: RHEL 4, RHEL 5, Fedora Core 7
Posts: 3
Blog Entries: 1

Rep: Reputation: 0
Quote:
Originally Posted by rexjustin_02 View Post
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.

Last edited by korkiley; 12-01-2010 at 02:02 PM. Reason: Added that my instructions are for RHEL and Fedora
 
Old 12-01-2010, 06:41 PM   #5
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
@ rexjustin_02

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

Last edited by dv502; 12-01-2010 at 10:32 PM.
 
Old 12-01-2010, 07:43 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,727
Blog Entries: 27

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,
Quote:
Originally Posted by camorri View Post
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

 
Old 12-01-2010, 10:31 PM   #7
gd2shoe
Member
 
Registered: Jun 2004
Location: Northern CA
Distribution: Debian
Posts: 835

Rep: Reputation: 49
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.)
 
Old 02-02-2011, 05:06 AM   #8
mhy
LQ Newbie
 
Registered: Nov 2010
Posts: 1

Rep: Reputation: 0
Thumbs up

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dual Boot / Vista and Linux - Question on Shrinking Partition Morgandy Linux - Newbie 4 12-18-2009 10:34 AM
Shrinking the ext3 partition during the boot and creation of new partition saleem_ak Linux - Newbie 2 05-05-2009 01:52 AM
NTFS resizing from linux commands Ricio Linux - Hardware 12 04-20-2008 09:16 PM
Problem Resizing Linux Partition From Partition magic John_Emad SUSE / openSUSE 5 02-16-2005 12:42 PM
Shrinking Linux Partition w/ Partition Magic jrdioko Linux - Newbie 11 12-21-2003 03:46 PM


All times are GMT -5. The time now is 10:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration