Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Partitioning in action: Consolidating data

Moving /tmp and /var to their own shared partition

Daniel Robbins (drobbins@gentoo.org), President and CEO, Gentoo Technologies
Residing in Albuquerque, New Mexico, Daniel Robbins is the Chief Architect of the Gentoo Project, CEO of Gentoo Technologies, Inc., the mentor for the Linux Advanced Multimedia Project (LAMP), and a contributing author for the Macmillan books Caldera OpenLinux Unleashed, SuSE Linux Unleashed, and Samba Unleashed. Daniel has been involved with computers in some fashion since the second grade, when he was first exposed to the Logo programming language as well as a potentially dangerous dose of Pac Man. This probably explains why he has since served as a Lead Graphic Artist at SONY Electronic Publishing/Psygnosis. Daniel enjoys spending time with his wife, Mary, who is expecting a child this spring. He can be reached at drobbins@gentoo.org.

Summary:  In this second tip on changing partition layout on a running system, Daniel Robbins shows you how to move /tmp and /var to their own shared partition. He also covers several tricks of the trade to minimize downtime and avoid making costly mistakes.

Date:  01 May 2000
Level:  Introductory

Comments:  

In my last tip, we successfully moved /home to a new partition. Now, it's time to learn how to consolidate often-modified files onto a new partition. Why would we want to do this? Here's an example. Often-modified files contribute heavily to fragmentation. One of the best ways to contain fragmentation is to store often-modified files on their own partition. That way, the other partitions are unaffected by the fragmentation caused by the heavily modified files. In concept, this is easy to understand, but how do you actually go about doing it?

Consolidating data

First, you must create a new partition for the specific purpose of storing frequently modified files. You might want to locate this partition on a separate disk to enhance performance. Then, I'll walk you through the steps required to move both /tmp and /var to this new partition.

WARNING: The following technique describes how to move a partition(s). Although this technique is designed specifically so that you can "back out" of a failed partition move, it doesn't protect against user error. In other words, any time that you format partitions or copy large numbers of files, there's a possibility that you will type the wrong thing, causing lots of data to be destroyed. For this reason, it's highly recommended that you take appropriate steps to back up all critical files before proceeding.


1. Create a filesystem on the new partition

The first step of this process is to create a new partition that's big enough to hold /var and /tmp, with a little extra space left over. You'll need either an additional drive, or a spare (unused) partition that will house the often-modified files. If you do need to use fdisk or cfdisk to create the partition, you'll need to reboot once. Then, it's time to format the new partition as follows (it's OK to be in multiuser mode while you do this; I'll let you know when to switch to single-user):

# mkfs.ext2 /dev/???


2. Mount it to /mnt/rwstorage

As in my previous tip, ??? should be replaced with the device name for the new, empty partition that you are creating. Accidentally typing the wrong name will destroy data on an existing partition, so be careful! After typing this command, you'll have a brand-new ext2 filesystem on the new partition. We're almost ready to mount it, but first, let's make a new mount point.

# mkdir /mnt/rwstorage

I chose the name "rwstorage" to remind us that this particular partition is going to be specifically used to house files that are read from and written to frequently. To mount the partition, type:

# mount /dev/??? /mnt/rwstorage


3. Creating a new /tmp

The partition is now mounted and we're ready to create our new /tmp directory:

# cd /mnt/rwstorage
# mkdir tmp
# chmod 1777 tmp


4. Drop to single-user mode

Our new directory at /mnt/rwstorage/tmp has the right permissions for a temporary directory. Now, drop to single-user mode, since we must copy over /var. As usual, we've delayed our drop to single-user mode to the last possible moment. Right now, we don't want any programs reading or writing files in /var, so we have to stop all daemons, disconnect all users, and do some quick maintenance by typing:

# init 1

If you're prompted to enter a password to perform system maintanance, do so. You should now have a root shell, and all unnecessary daemons will be stopped. Create a new location for our /var files by typing:

# cd /mnt/rwstorage
# mkdir var


5. Copy /var

Default permissions on our new /mnt/newstorage/var directory should be correct, so now we're ready to copy all of our original /var data over to the new partition:

# cd /var 
# cp -ax * /mnt/rwstorage/var


6. Back up and create symlinks

After this command completes, you'll have an exact copy of /var at /mnt/rwstorage/var. Now, you may be asking how exactly we get Linux to use /mnt/rwstorage/var and /mnt/rwstorage/tmp instead of the defaults in the root directory. This is easily done by the use of symbolic links -- we'll create the new symbolic links, /tmp and /var, which point to the correct directories in /mnt/rwstorage. First, let's back up the original directories:

# cd /
# cp var var.old
# cp tmp tmp.old

The last line probably isn't necessary, since it's very likely that you don't have anything important in /tmp, but we're playing it safe. Now, let's create the symlinks:

# cd /
# ln -s /mnt/rwstorage/var /var
# ln -s /mnt/rwstorage/tmp /tmp


7. Finishing touches to /etc/fstab

Now, when any user or program uses /var, they'll automatically be transported to /mnt/rwstorage/var! Likewise for /tmp. We have one step left; however, it can be safely performed in multiuser mode. It's time to get apache running again, and to allow all your users to log back in. Exit from runlevel 1 by pressing CTRL-D. The system should start up normally. Log in as root.

The final thing we must do is configure /etc/fstab so that /dev/??? is mounted at /mnt/rwstorage. You must add a line like the following to your /etc/fstab:

/dev/???        /mnt/rwstorage  ext2    defaults       1     2

Important note: If you are using a kernel version in the 2.3+ range, it's very likely that you have a line in your /etc/fstab that looks like this:

none            /var/shm        shm     defaults       0     0    

This line enables shared memory on your system, and by default it gets mounted in /var. In order for this line to work properly, it must appear after the line you just added. That way, when Linux starts up, /mnt/rwstorage will get mounted first (enabling /var). Then, and only then, will the shm device get mounted at /var/shm, which is really /mnt/rwstorage/var/shm. Make sure the lines are in this order:

/dev/???        /mnt/rwstorage  ext2    defaults       1     2
none            /var/shm        shm     defaults       0     0 

After saving the changes to /etc/fstab, your system has been successfully upgraded! After verifying that everything is working properly, you'll want to remove the /tmp.old and /var.old backup directories. Congratulations -- you've successfully reconfigured your system's partitions for optimum performance.


Resources

About the author

Residing in Albuquerque, New Mexico, Daniel Robbins is the Chief Architect of the Gentoo Project, CEO of Gentoo Technologies, Inc., the mentor for the Linux Advanced Multimedia Project (LAMP), and a contributing author for the Macmillan books Caldera OpenLinux Unleashed, SuSE Linux Unleashed, and Samba Unleashed. Daniel has been involved with computers in some fashion since the second grade, when he was first exposed to the Logo programming language as well as a potentially dangerous dose of Pac Man. This probably explains why he has since served as a Lead Graphic Artist at SONY Electronic Publishing/Psygnosis. Daniel enjoys spending time with his wife, Mary, who is expecting a child this spring. He can be reached at drobbins@gentoo.org.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux
ArticleID=11005
ArticleTitle=Partitioning in action: Consolidating data
publish-date=05012000
author1-email=drobbins@gentoo.org
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).