Add new partitions to an existing system
From ArchWiki
Contents |
Introduction
You may find yourself in the situation where you either:
- want to create a new partition to give yourself more flexibility for backup operations, etc.
- are forced to create new partitions because your original partitions are full and you need to move data from the full partition to a new partition to free up space and allow your existing system to grow. (such as moving /srv and /var to separate partitions to free up space on your / (root) partition and provide more space for your webserver, etc.)
This can be accomplished fairly easily provided you have additional free space on your disk or raid array. If you have no additional room on your present disk out of which to create the new partitions, you can always just add another hard drive and create the new partitions on a separate disk using the exact same procedures.
Recently, I was faced with the problem of the root partition filling to capacity due to the size of my website files in /srv and having packages for both kdemod3 and kde-unstable filling /var/cache/pacman/pkg. Since I had available unpartitioned space on the dmraid array holding my Archlinux install, I decided to move /srv and /var to their own separate partitions to solve the problem. By doing this, I will free up the space currently taken by /srv and /var on the / partition while at the same time providing additional space on disk for both /srv and /var.
Synopsis
In the periodic checking of drive status I do, I was a bit surprised to find my root partition full:
[15:41 archangel:/home/david] # df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/nvidia_ecaejfdip5 19G 18G 64M 100% / none 2.0G 0 2.0G 0% /dev/shm /dev/mapper/nvidia_ecaejfdip6 114M 17M 92M 15% /boot /dev/mapper/nvidia_ecaejfdip7 37G 12G 24G 32% /home
Note: I am running dmraid where my partitions ( /dev/mapper/nvidia_ecaejfdip<X> ) are raid arrays comprised of partitions from /dev/sdb and /dev/sdd. If you are not running any type of raid setup, the you will just see /dev/sda, /dev/sdb, etc.. for each of your disks instead of /dev/mapper/Nameof_DeviceNode.. Thankfully, adding new partitions works the same regardless. For additional information on dmraid, see: http://wiki.archlinux.org/index.php/Installing_with_Fake-RAID
Looking at the problem in kdiskfree, the source was found to be the collection of files offered on my website (/srv/www) and the pacman cache (/var/cache/pacman). So, the decision was made to:
- create new partitions for each /srv and /var;
- mount the new partitions in a temporary location;
- copy the existing files from /srv and /var to the newly created partitions in their temporary locations;
- after confirming a good copy, delete the files under the original /srv and /var directories;
- move the new /srv and /var from their temporary mount points to their permanent homes; and
- update fstab accordingly.
Simple enough.
Creating the New Partitions
To create your new partitions you will need to UNMOUNT your filesystem. This means that to make changes and add new partitions to the free space on your disk or to create free space by shrinking partitions you will need to either boot from your install CD into "Rescue" mode or use a similar environment provided by booting from a distribution live CD or from the GParted live CD.
If you are not familiar with creating partitions and filesystems from the command line with fdisk, cfdisk, sfdisk or the like, I would recommend downloading the GParted iso and booting from it to manage the creation of new partitions. It provides a nice gui and additional checks to make sure what you are doing is OK. You can find GParted:
Main Download Page:
http://gparted.sourceforge.net/download.php
See the note about its use on some HP Pavilion Computers
Direct Download (as of 7/1/2009)
http://sourceforge.net/projects/gparted/files/gparted-live-testing/gparted-live-0.4.5-3.iso
WARNING
Changing, Resizing and/or Creating partitions has the VERY REAL potential to cause DATA LOSS Use common sense. BACKUP ANYTHING YOU DO NOT WANT TO LOSE!
With that cautionary statement, you can take comfort know that changing, resizing, creating and removing partitions is what each of the partitioning tools were created to do. They work. Just make sure you know what your are doing with the software and don't do anything strange (like clicking buttons willy-nilly or radomly trying out all of the commands available in the packages and you should be just fine.
Extended Growth & Logical Partitioning
Partitions come in three main flavors: Primary, Extended and Logical. An Extended partition is for the most part just a "wrapper" to contain logical partitions.
If you have a typical Linux partition scheme, then you probably have a single "extended" partition with "logical" partitions of '/', '/home' and possibly a '/boot' partition. Most likely your logical partitions will completely fill the extended partition they reside in. Before you can add additional partitions, you must grow the current extended partition to make room for your new logical partitions, or you can create a new extended partition. I prefer to grow the extended partition then add the new logical partitions. I find no need for primary partitions unless dual booting windows. Otherwise, don't use them.
Regardless of how you create the new partitions, you will need to create a filesystem on the new partitions with 'mkfs -t <fstype>'. If you use gparted to create the partitions, it allows you to select the filesystem when you define your partition, so it will help take care of the details for you. If you don't know what type of filesystem you are using, then from the command line "df -hT" and look under the type column.
Now go create your new partitions...
Moving Existing Data to the New Partitions
Before you can benefit from the new space offered by the newly created partitions, you have to integrate the new partitions into your filesystem. In my case I was replacing the directories /srv and /var with the new partitions /srv and /var. The solution:
- copy everything from the existing directories to the new partitions
- delete the contents of the existing /srv and /var directories
- mount the new partitions under /srv and /var
You do not want files being written to the parts of the filesystem you are moving during this process. The safest way to accomplish the copy and delete is to either boot from your install cd into rescue mode and create mount points to hold your / filesystem and the new partitions, or, make sure you have shutdown anything that could write to or read from the existing directories. /srv wasn't an issue, but for /var, shutdown syslog-ng. (when booting from the install CD into resue mode, if using dmraid, don't forget to issue "dmraid -ay" to activate your raid sets)
Here my new partitions were /dev/mapper/nvidia_ecaejfdip9 and /dev/mapper/nvidia_ecaejfdip10, you will most likely have something like /dev/sda8, and /dev/sda9 or something similar. Just substitute as necessary. The basic process (as root):
# Stop any processes that might write to the old directories /etc/rc.d/syslog-ng stop # Create temporary mount points for your new partitions mkdir /mnt/newsrv mkdir /mnt/newvar # Mount the new partitions on the temporary mount points mount /dev/mapper/nvidia_ecaejfdip9 /mnt/newsrv mount /dev/mapper/nvidia_ecaejfdip10 /mnt/newvar # Copy the information from the old directories to the new partitions cp -a /srv/* /mnt/newsrv cp -a /var/* /mnt/newvar # Confirm the information was written to the new partitions with ls, diff, etc.. # then delete the information in the old directories rm -r /srv/* rm -r /var/*
Remounting the New Partitions on the Filesystem
At this point you have all of the information on your new partitions and you have deleted the information from the / filesystem to free-up space, but the new partitions are not in the right place within the filesystem and are still mounted in the temporary locations. It's time to unmount the new partitions from their temporary location and move them to their proper place in the filesystem.
# Unmount the new partitions from the temporary mount point umount /mnt/newsrv umount /mnt/newvar # Mount the new partitions as /srv and /var mount /dev/mapper/nvidia_ecaejfdip9 /srv mount /dev/mapper/nvidia_ecaejfdip10 /var
Now the new partitions are in the proper location in the filesystem and you can confirm the new room you have created on / with "df -h"
[16:19 archangel:/home/david] # df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/nvidia_ecaejfdip5 19G 11G 7.3G 59% / none 2.0G 0 2.0G 0% /dev/shm /dev/mapper/nvidia_ecaejfdip6 114M 17M 92M 15% /boot /dev/mapper/nvidia_ecaejfdip7 37G 12G 24G 32% /home /dev/mapper/nvidia_ecaejfdip9 15G 165M 14G 2% /mnt/var /dev/mapper/nvidia_ecaejfdip10 29G 7.4G 20G 27% /srv
Making the Changes Permanent
All that remains is to make the new changes permanent by adding the new mount configuration to /etc/fstab. Using your favorite editor as root, open /etc/fstab and add something similar to the following:
# New Partitions, /srv and /var on separate partitions /dev/mapper/nvidia_ecaejfdip9 /var ext3 defaults 0 1 /dev/mapper/nvidia_ecaejfdip10 /srv ext3 defaults 0 1
If you have disabled any running processes, go restart or enable them, check the files on your new partitions to insure all is well, check the logs for any permission errors, then reboot to box to make sure it all works as expected. If so, your done.
Note: you are not limited to running your filesystem from a single disk. If you are out of space on your original disk and need more, you can get the new space you need by installing a new hard drive and creating your new partitions there. No need to move your entire system to the new drive, as in this example, I could have created the new /srv and /var partitions on a completely separate drive and then mounted the partitions on the new drive as /srv and /var in my existing system. The only thing you need to insure when using a separate drive is that it is mounted before anything tries to write to it (e.g. syslog in /var in my case)