Difference between revisions of "Add new partitions to an existing system"

From ArchWiki
Jump to: navigation, search
(Introduction)
(45 intermediate revisions by 10 users not shown)
Line 1: Line 1:
[[Category:File systems (English)]]
+
[[Category:File systems]]
{{Poor writing}}
+
{{Related articles start}}
 +
{{Related|Partitioning}}
 +
{{Related|Gparted}}
 +
{{Related articles end}}
 +
You may find yourself in the situation where you either want to create a new partition to give yourself more flexibility (for example, for backup operations), or you are forced to use new partition(s) because your original is full and to free up space you need to move data from the full partition to a new one.
  
== Introduction ==
+
As an easy alternative, you can always download a copy of another operating system such as [http://www.ubuntu.com Ubuntu] and boot into a live media version ([[USB Installation Media|Arch USB Installation Media guide]]). Newer versions of Ubuntu include [[Gparted]].
 +
{{Warning|Be sure to at least read through all the steps in this guide before making any real changes. There is always a risk for losing your data when adjusting partitions.}}
  
You may find yourself in the situation where you either:
+
== Preamble ==
*want to create a new partition to give yourself more flexibility, for example for backup operations.
+
*are forced to create new partition(s) because your original partition is full and you need to move data from the full partition to a new one to free up space and allow your existing system to grow.
+
  
New partitions can be created either on previously unpartitioned sections of an existing disk or raid array or on a new drive added to the system.
+
Here are the steps involved with adding new partitions:
 
+
# Create new partitions.
In my case which I will use as an example, I developed a full root partition due to the size of my website files in {{Filename|/srv}} and many packages such as kdemod3 and kde-unstable filling {{Filename|/var/cache/pacman/pkg}}. Since I had available unpartitioned space on the dmraid array holding my Arch Linux install, I decided to move {{Filename|/srv}} and {{Filename|/var}} to their own separate partitions to solve the problem. This would free up space which had taken by {{Filename|/srv}} and {{Filename|/var}} on the {{Filename|/}} partition and provide greater disk space for {{Filename|/srv}} and {{Filename|/var}} on their own partitions.
+
# Mount the new partitions in a temporary location.
 
+
# Copy the existing files from old partition to the newly created partitions in their temporary locations.
== Synopsis ==
+
# Delete the files under the original directories
 
+
# Move new partition from their temporary mount points to their permanent homes
After a routine drive status check, I was a bit surprised to find my root partition full:
+
# Update fstab accordingly.
 
+
[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 (  {{Filename|/dev/mapper/nvidia_ecaejfdip<X>}} ) are raid arrays comprised of partitions from {{Filename|/dev/sdb}} and {{Filename|/dev/sdd}}. If you are not running any type of raid setup, the you will just see {{Filename|/dev/sda}}, {{Filename|/dev/sdb}}, etc.. for each of your disks instead of {{Filename|/dev/mapper/Nameof_DeviceNode}}.. Thankfully, adding new partitions works the same regardless. For additional information on dmraid, see: [[Installing with Fake RAID]]
+
 
+
I diagnosed the problem with kdiskfree, but you could use another [https://wiki.archlinux.org/index.php/Common_Applications#Disk_Usage_Display_Programs|other disk usage display program]. The source was found to be the collection of files offered on my website ({{Filename|/srv/www}}) and the pacman cache ({{Filename|/var/cache/pacman}}) so the decision was made to:
+
 
+
# create new partitions for each {{Filename|/srv}} and {{Filename|/var}};
+
#*at this stage be careful that other partitions are not mistakenly corrupted
+
# mount the new partitions in a temporary location;
+
# copy the existing files from {{Filename|/srv}} and {{Filename|/var}} to the newly created partitions in their temporary locations;
+
#*check to confirm a good copy
+
# after confirming a good copy, delete the files under the original {{Filename|/srv}} and {{Filename|/var}} directories;
+
#*before doing this ensure that the system does not still need read or write access to these folders
+
# move the new {{Filename|/srv}} and {{Filename|/var}} from their temporary mount points to their permanent homes; and
+
# update fstab accordingly.
+
 
+
Simple enough.
+
  
 
== Creating the New Partitions ==
 
== Creating the New Partitions ==
 +
{{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!}}
  
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.
+
New partitions can be created either on previously unpartitioned sections of an existing disk (or raid array in my case) or simply on an additional newly installed drive.  
  
If you are unfamiliar with creating partitions and filesystems from the command line with utilites such as fdisk, cfdisk or sfdisk, I would recommend downloading the [[Gparted-Live]] 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 okay. You can download GParted here:
+
You will need your filesystems to be '''UNMOUNTED''' when you make changes such as adding new partitions to the free space on your disk or shrinking partitions. Therefore you will need to boot into an environment such as provided by the install CD in '''Rescue''' mode.  
  
http://gparted.sourceforge.net/download.php
+
Command line utilities such as fdisk, cfdisk or sfdisk can be used, but if you are unfamiliar with creating partitions and filesystems [[Gparted-Live]] iso is recommended. It provides a nice gui and additional checks to make sure what you are doing is okay. See also [[GParted]] for how to use GParted.
  
'''See the note about its use on some HP Pavilion Computers'''
+
=== Extended Growth & Logical Partitioning ===
  
=== WARNING ===
+
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.
  
'''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!'''
+
If you have a typical Linux partition scheme, then you probably have a single "extended" partition with "logical" partitions of '/', '{{ic|/home}}' and possibly a '{{ic|/boot}}' partition. Your logical partitions will probably 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.  
  
With that cautionary statement, you can take comfort knowing that changing, resizing, creating and removing partitions is what the partitioning tools were created for. 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 randomly trying out all of the commands available in the packages) and you should be just fine.
+
You will need to create a filesystem on the new partitions with 'mkfs -t <fstype>' or you may select the desired filesystem when defining your partition with Gparted. If you do not know which filesystem you are using, then from the command line type "df -hT" and check the type column.
  
=== Extended Growth & Logical Partitioning ===
+
== Moving Existing Data to the New Partition ==
  
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.
+
To benefit from the extra space provided by the new partitions, the new partitions need to be integrated into the  filesystem.  
  
If you have a typical Linux partition scheme, then you probably have a single "extended" partition with "logical" partitions of '/', '{{Filename|/home}}' and possibly a '{{Filename|/boot}}' partition. Your logical partitions will probably 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.  
+
Files should not be 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 {{ic|/}} filesystem and the new partitions, or alternatively shutdown anything that could write to or read from the existing directories. For example, for {{ic|/var}}, syslog-ng needed to be shutdown. If using dmraid, issue "dmraid -ay" to activate your raid sets when booting from the install CD into rescue mode.
  
You will need to create a filesystem on the new partitions with 'mkfs -t <fstype>' or you may select the desired filesystem when defining your partition with gparted. If you don't know which filesystem you are using, then from the command line type "df -hT" and check the type column.
+
The basic process (as root) is:
  
== Moving Existing Data to the New Partitions ==
+
Stop any processes that might write to the old directories.
 +
# /etc/rc.d/syslog-ng stop
  
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 {{Filename|/srv}} and {{Filename|/var}} with the new partitions {{Filename|/srv}} and {{Filename|/var}}. The solution:
+
Create temporary mount points for your new partitions.
#copy everything from the existing directories to the new partitions
+
# mkdir /mnt/newtemp
#delete the contents of the existing {{Filename|/srv}} and {{Filename|/var}} directories
+
#mount the new partitions under {{Filename|/srv}} and {{Filename|/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 {{Filename|/}} filesystem and the new partitions, or, make sure you have shutdown anything that could write to or read from the existing directories. {{Filename|/srv}} wasn't an issue, but for {{Filename|/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)
+
Mount the new partitions on the temporary mount points.
 +
# mount /dev/sda5 /mnt/newtemp
  
Here my new partitions were {{Filename|/dev/mapper/nvidia_ecaejfdip9}} and {{Filename|/dev/mapper/nvidia_ecaejfdip10}}, you will most likely have something like {{Filename|/dev/sda8}}, and {{Filename|/dev/sda9}} or something similar. Just substitute as necessary. The basic process (as root):
+
{{Warning|Before doing this ensure that the system does not still need read or write access to these folders}}
 +
Copy the information from the old directories to the new partitions.
 +
# cp -a /olddir /mnt/newtemp
  
# Stop any processes that might write to the old directories.
+
Confirm the information was written to the new partitions with ls, diff, etc. When you are certain the information was written correctly to the new partitions delete the information from the old directories.
/etc/rc.d/syslog-ng stop
+
  # rm -r /olddir
+
# 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..
+
# When you are certain the information was written correctly to the new partitions delete  
+
# ... the information from the old directories.
+
  rm -r /srv/*
+
rm -r /var/*
+
  
 
== Remounting the New Partitions on the Filesystem ==
 
== 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 {{Filename|/}} 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.
+
Next the new partition(s) should be unmounted from their temporary location(s) and assigned their proper place in the filesystem.
  
# Unmount the new partitions from the temporary mount point
+
=== Manually remounting and checking ===
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 {{Filename|/}} with "df -h"
+
Unmount the new partitions from the temporary mount point
 +
umount /mnt/newtemp
  
[16:19 archangel:/home/david] # df -h
+
Mount the new partitions as /newdir
Filesystem            Size  Used Avail Use% Mounted on
+
  mount /dev/sda5 /newdir
  /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 ==
+
Now the new partitions are in the proper location in the filesystem and you can confirm the new room with {{ic|df -h}}
  
All that remains is to make the new changes permanent by adding the new mount configuration to {{Filename|/etc/fstab}}. Using your favorite editor as root, open {{Filename|/etc/fstab}} and add something similar to the following:
+
=== Permanent remounting ===
  
# New Partitions, {{Filename|/srv}} and {{Filename|/var}} on separate partitions
+
Finally make the new changes permanent by adding the new mount configuration to {{ic|/etc/fstab}}. As root edit {{ic|/etc/fstab}} adding something similar to the following corresponding to your own new partitions:
 
   
 
   
  /dev/mapper/nvidia_ecaejfdip9 /var ext3 defaults 0 1
+
  /dev/sda5 /newdir ext3 defaults 0 2
/dev/mapper/nvidia_ecaejfdip10 /srv ext3 defaults 0 1
+
  
 
(See the [[fstab]] article for more details.)
 
(See the [[fstab]] article for more details.)
  
If you have disabled any running processes, restart or enable them, check the files on your new partitions to insure all is well and check the logs for any permission errors. Then reboot and make sure it all works as expected.
+
If you have disabled any running processes, restart or enable them, check the files on your new partitions to insure all is well and check the logs for any permission errors. Then reboot and make sure it all works as expected.
 
+
'''Note:''' you are not limited to running your filesystem from a single disk. If you run out of space on your original drive, you may install a new one and create new partitions there. No need to move your entire system to the new drive, as in this example, I could have created the new {{Filename|/srv}} and {{Filename|/var}} partitions on a completely separate drive and then mounted the partitions on the new drive as {{Filename|/srv}} and {{Filename|/var}} in my existing system. However, when using a separate drive it must be mounted before anything needs to write to it (e.g. syslog in {{Filename|/var}} in my case).
+

Revision as of 02:41, 2 January 2014

Related articles

You may find yourself in the situation where you either want to create a new partition to give yourself more flexibility (for example, for backup operations), or you are forced to use new partition(s) because your original is full and to free up space you need to move data from the full partition to a new one.

As an easy alternative, you can always download a copy of another operating system such as Ubuntu and boot into a live media version (Arch USB Installation Media guide). Newer versions of Ubuntu include Gparted.

Warning: Be sure to at least read through all the steps in this guide before making any real changes. There is always a risk for losing your data when adjusting partitions.

Preamble

Here are the steps involved with adding new partitions:

  1. Create new partitions.
  2. Mount the new partitions in a temporary location.
  3. Copy the existing files from old partition to the newly created partitions in their temporary locations.
  4. Delete the files under the original directories
  5. Move new partition from their temporary mount points to their permanent homes
  6. Update fstab accordingly.

Creating the New Partitions

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!

New partitions can be created either on previously unpartitioned sections of an existing disk (or raid array in my case) or simply on an additional newly installed drive.

You will need your filesystems to be UNMOUNTED when you make changes such as adding new partitions to the free space on your disk or shrinking partitions. Therefore you will need to boot into an environment such as provided by the install CD in Rescue mode.

Command line utilities such as fdisk, cfdisk or sfdisk can be used, but if you are unfamiliar with creating partitions and filesystems Gparted-Live iso is recommended. It provides a nice gui and additional checks to make sure what you are doing is okay. See also GParted for how to use GParted.

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. Your logical partitions will probably 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.

You will need to create a filesystem on the new partitions with 'mkfs -t <fstype>' or you may select the desired filesystem when defining your partition with Gparted. If you do not know which filesystem you are using, then from the command line type "df -hT" and check the type column.

Moving Existing Data to the New Partition

To benefit from the extra space provided by the new partitions, the new partitions need to be integrated into the filesystem.

Files should not be 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 alternatively shutdown anything that could write to or read from the existing directories. For example, for /var, syslog-ng needed to be shutdown. If using dmraid, issue "dmraid -ay" to activate your raid sets when booting from the install CD into rescue mode.

The basic process (as root) is:

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/newtemp

Mount the new partitions on the temporary mount points.

# mount /dev/sda5 /mnt/newtemp
Warning: Before doing this ensure that the system does not still need read or write access to these folders

Copy the information from the old directories to the new partitions.

# cp -a /olddir /mnt/newtemp

Confirm the information was written to the new partitions with ls, diff, etc. When you are certain the information was written correctly to the new partitions delete the information from the old directories.

# rm -r /olddir

Remounting the New Partitions on the Filesystem

Next the new partition(s) should be unmounted from their temporary location(s) and assigned their proper place in the filesystem.

Manually remounting and checking

Unmount the new partitions from the temporary mount point

umount /mnt/newtemp

Mount the new partitions as /newdir

mount /dev/sda5 /newdir

Now the new partitions are in the proper location in the filesystem and you can confirm the new room with df -h

Permanent remounting

Finally make the new changes permanent by adding the new mount configuration to /etc/fstab. As root edit /etc/fstab adding something similar to the following corresponding to your own new partitions:

/dev/sda5 /newdir ext3 defaults 0 2

(See the fstab article for more details.)

If you have disabled any running processes, restart or enable them, check the files on your new partitions to insure all is well and check the logs for any permission errors. Then reboot and make sure it all works as expected.