Resize Root Partition Without Reinstalling Linux – How to Avoid Data Loss

debianpartition

I have started using a machine that has both Debian and Windows 7 installed. However, after installing some programs I started getting a message that there is not enough space. I knew that the system had more than 1TB of hard disk space in total and did some research. It seems that the root partition is only 5GB.

Is there any way to allocate more disk space to the specific partition without reinstalling Linux?

Below you can find the results of a couple of commands that I executed:

fdisk -l

Disk /dev/sda: 640.1 GB, 640135028736 bytes
255 heads, 63 sectors/track, 77825 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4a47e2fd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400    7  HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2              13       53507   429687500    7  HPFS/NTFS
Partition 2 does not end on cylinder boundary.
/dev/sda3           53507       77826   195340289    5  Extended
Partition 3 does not end on cylinder boundary.
/dev/sda5           53507       53537      243712   83  Linux
/dev/sda6           53538       77826   195095552   8e  Linux LVM

Disk /dev/sdb: 640.1 GB, 640135028736 bytes
255 heads, 63 sectors/track, 77825 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4a47e2fe

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               2       62261   500097657+   f  W95 Ext'd (LBA)
/dev/sdb5               2       62261   500097656+   7  HPFS/NTFS

Disk /dev/dm-0: 4999 MB, 4999610368 bytes
255 heads, 63 sectors/track, 607 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-0 doesn't contain a valid partition table

Disk /dev/dm-1: 1996 MB, 1996488704 bytes
255 heads, 63 sectors/track, 242 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-1 doesn't contain a valid partition table

Disk /dev/dm-2: 20.0 GB, 19998441472 bytes
255 heads, 63 sectors/track, 2431 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-2 doesn't contain a valid partition table

df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/HU-root   4.6G  4.4G   32M 100% /
tmpfs                 2.0G     0  2.0G   0% /lib/init/rw
udev                  2.0G  220K  2.0G   1% /dev
tmpfs                 2.0G  356K  2.0G   1% /dev/shm
/dev/sda5             231M   22M  198M  10% /boot
/dev/mapper/HU-home    19G  751M   17G   5% /home

Edit (extra info based on comment):

# mount
/dev/mapper/HU-root on / type ext3 (rw,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/sda5 on /boot type ext3 (rw)
/dev/mapper/HU-home on /home type ext3 (rw)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda6
  VG Name               HU
  PV Size               186.06 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              47630
  Free PE               41194
  Allocated PE          6436
  PV UUID               wmEFAc-eSb6-r3qo-jIjy-vuKH-v9JK-eQfJFZ

As a sidenote, I would also appreciate any suggestions for good books that explain these subjects in depth (file systems, how the various operating systems understand the file systems etc).

Best Answer


Warning: doing anything to your filesystems without a known-restorable backup is ill-advised.


Do not run any of the following steps if you're not sure your / is clean. If you're not sure, run the following (as root):

# touch /forcefsck

and reboot. This will do an fsck of all your partitions, to be on the safe side.


That being said, since you're using LVM for your root device and an ext3 filesystem, you can extend it online. Before you start make sure you have resize2fs installed. If not, it's usually in a package called e2fsprogs. (If you can't install it because you don't have enough room, try to do a bit of cleanup in /var/log for instance.)

First you extend the underlying volume with (as root):

# lvextend -L+2G /dev/mapper/HU-root

(Adjust the 2G part as you wish - it's the amount of space you want to add.)

Then you need to resize the filesystem. This can be done online with resize2fs:

# resize2fs /dev/mapper/HU-root

This can take a few seconds/minutes. Don't interrupt. Do another reboot at the end, possibly with another /forcefsck, if you want to. It's not necessary, but I often do it anyway.

Related Question