justlinux.com
Sun, 27-May-2012 11:36:09 GMT

Forum: Registered Users: 76222, Online: 301
nhfs Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Calendar Find other members Frequently Asked Questions Search Home Home

Help File Library: How to Create a Multiple-Partition System


Written By: Danny "Strike" DiPaolo

( IMPORTANT PREFACE: Doing this has a decent probability of completely destroying any and all data on your system. Back up all important data before attempting ANY of these measures, and do read the entire Help File before proceeding. Any time you mess with partitions, it is a risk of data loss and proper precautions MUST be taken. )

This Help File is aimed at those who wish to know what this business with multiple partitions is all about. Here's a quick breakdown of what I consider a few pros and cons of each to be:

Single Partition (just / and swap)
Pros:

  • Simple, and easy to manage
  • Non-restrictive to size of subdirectories
Cons:
  • Putting all your eggs in one basket - lose this partition and you've lost everything
  • Not share-able with other distributions

Multiple Partitions
Pros:i

  • Lots of control over size of directories
  • Easy maintenance
Cons:
  • Complex
  • Have to keep track of sizes of each subdirectory

So, it's not like one is necessarily BETTER than another (although people won't hesitate to tell you that their scheme is better). It is merely a preferential thing. I've used both systems but prefer the control of multiple partitions versus the simplicity of the single partition scheme.

And, because of this, I want people to be able to try the multiple partition scheme without having to wipe out their hard drive and reinstall. So, in order to do that, we have to do a lots of moving of data and creation of partitions and filesystems, etc. So, we'll go through this real slowly.

First, I'd recommend that you read 7DS's Help File on the basic filesystem stuff - knowing that will be a great help with this Help File, and I'm going to assume from this point on that you have read it.

Well, now that we know the way the filesystem in Linux is generally organized and the way partitions are set up on drives, you need to go read my Help File on using fdisk to create and delete partitions, and then my Help File on creating filesystems on existing partitions, and you should be done. The only other thing we need to cover is laying out your multi-partition plan. This is something you should do before messing with partitions in fdisk.

Now what? Well, if you are doing this from a drive that already has stuff on it, then that has a little bit of a different approach than if you were simply planning on building a multi- partition system when you install. Both are possible, of course, but installing on a clean system is easier.

Either way, the first thing you will want to do is create a plan for how you want your partitions laid out. This is not something I can really give you a formula for. You have to play around with it (and, after this Help File, you shouldn't be too scared to play around with partitions) in order to get it to your liking. As an example, though. Here's what all those Linux partitions represent to me - my partition scheme is:

/dev/hdb1  = 1.49GB  - (FreeBSD sits here)
/dev/hdb2  = 133MB   - my swap partition
/dev/hdb3  = (extended partition)
/dev/hdb5  = 2GB     - /home
/dev/hdb6  = 251MB   - /var
/dev/hdb7  = 204MB   - /tmp
/dev/hdb8  = 1GB     - /
/dev/hdb9  = <unused>
/dev/hdb10 = <unused>
/dev/hdb11 = 2GB     - /usr
/dev/hdb12 = 2GB     - /usr/local
(plus 1141 unused cylinders at the end of the drive)

I also have partitions containing Windows stuff on /dev/hda, but that's unimportant to this Help File. You may be asking why I picked such weird numbers like 1.49GB and 133MB. Well, actually I didn't. A partition has to begin and end on a cylinder, and since my cylinders are locked in at a certain size, any and all of the partitions I create have to be a multiple of that size, so this was close as I could get to the sizes I requested. I actually wanted 1.5GB, 128MB, 250MB, and 200MB for the BSD, swap, /var, and /tmp partitions respectively. In fact, the 1GB and 2GB partitions aren't exactly 1GB and 2GB either.

For those of you who have an existing installation, you actually have an advantage in this area. You can simply take a look at how much space your directories take and then plan your strategy for partitioning accordingly. To see how much space a specific directory takes on your system, simply issue this command:


du -s -H 

For example, I want to see how much space all of the stuff I have in /usr takes up, so I do this:


du -s -H /usr

and I get back this:


1.6G    /usr

That's the size of all the files in that directory. If you do this as a normal user, you may get a bunch of permissions errors, and you might get a wrong answer, so su to root to do this first.

For those of you who don't have an existing installation, you can either use mine above as an example or ask around (or just wing it, I suppose).

Now, go plan out your partition strategy and then come back and we'll continue. Before going, though, I'd suggest that you leave space at the end of the drive (like I did) for further shifting around. For now I'd only make partitions a little bigger than you think you NEED.

You're back, huh? You better be ready, because here we go... First of all, I'm now going to diverge this Help File into two parts. One is for the people with nothing on their hard drive (or at least nothing they don't mind getting rid of). The other is for those of you who are tweaking an existing installation to accomodate a new Multi-Partition style setup.

Creating a Multi-Partition System with a clean (or soon to be clean) hard drive

You guys have it relatively easy. Basically all you have to do is:

  • remove all of the partitions on the hard drive (somehow - if you can get it to mount under Linux, then I have a using fdisk Help File that you will need later anyway)
  • create new partitions of the proper size according to your newly created strategy (I have the using fdisk Help File to help you on this)
-- now, at this point, if you are going to install a distro --
-- onto your hard drive, go ahead. It will do the rest for --
-- you, pretty much. Just make sure you specify the mount --
-- points according to your strategy and all should go fine --
  • create filesystems on those partitions (again, I have an Help File on this as well)
  • mess around with /etc/fstab however necessary

And then you are done! You will then be running a multi- partition Linux system. Enjoy it!

Creating a Multi-Partition System with an existing Linux installation already

This is actually not as hard as it sounds. You simply have to learn one new skill and you'll be well on your way - archiving. We use archiving to shrink the data down so that we can create new partitions accordingly, and then we expand the archive onto the new partition and delete it off of the old one.

It's hard to explain in words, and it's easier to explain in an example, so let's do an example.

Well, I already use a multi-partition setup and am happy with it. But, for the sake of this Help File, i'll make it a little more multi-partitioned.

Let's say that one of my partitions was supposed to be mounted at /usr/src (this is an awfully weird and excessively control-freakish choice, so it shouldn't be one of your choices). Let's also say that I wanted to give this a 1GB partition (also a bit high, unless you are a kernel fan).

The first thing I want to do is create a 1GB partition so that I can move all my stuff from /usr/src over there. Let's explore two scenarios:


I have enough space free for a 1GB partition

This actually isn't that likely unless you intentionally left space for that 1GB, because most people just fill up their hard drives. That's understandable, but I tend to leave that space unused now in case something like this happens, because having that free space is a LOT easier to deal with than not.

So, assuming you do have 1GB of unpartitioned space on your drive, all you have to do is this:

1. Make a 1GB partition (my using fdisk Help File will show you how to do this)
2. Create a filesystem on that partition (my filesystem creation Help File will show you how to do that)
3.
Mount the drive (I'll explain how)
4. Copy the data over
5. Edit /etc/fstab to reflect the changes

The very VERY last step in this is to delete all of the old data to free up that space. This should only be done after at LEAST one reboot. I've lost partitions as a result of this, so I recommend doing this stuff one partition at a time, not deleting data until it's certain that the new partition "takes".

So, if you don't know how to do 1 and 2, go read the Help Files I mentioned and come back here, because I'm going to cover step 3 assuming 1 and 2 are done.

3 - Mounting the drive
By now you should have a 1GB ext2fs partition ready for use. The next thing to do is to mount it. In order to do so, we need a mount point. Just for consistencies sake, I like to name the mount point /mnt/ where is the last subdirectory of the partition we plan on creating. In this case, that's just /src. So, as root, I do:


mkdir /mnt/src

Now I'm ready to mount our new partition. The one for my example is /dev/hdb14, though yours may be different. The mount command is:


mount <partition> /mnt/

so, for me that would be:


mount /dev/hdb14 /mnt/src 

There are no real messages indicating success here, so I like to check and make sure that the partition mounted right by doing a df:


[root@half-life ddipaolo]# df
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hdb8              1052256    165140    834504  17% /
/dev/hda1              1023312    790016    233296  77% /dos/C
/dev/hda5              4511512   3381464   1130048  75% /dos/D
/dev/hda6              4408896   3674860    734036  83% /dos/E
/dev/hdb5              2038504   1364400    568880  71% /home
/dev/hdb7               202225       247    191536   0% /tmp
/dev/hdb11             2071384   1504896    461264  77% /usr
/dev/hdb12             2071384    125472   1840688   6% /usr/local
/dev/hdb6               248894     12295    223748   5% /var
/dev/hdb14             1035660         4    983048   0% /mnt/src

That last line shows me what I want to see. A (virtually) empty filesystem mounted right where I told it to go. So, I'm convinced the mounting went fine.

4 - Copying data to new partition
Next, it's time for step 4 - copying the data over. Here's where you'll see why I named the mount point the way I did. To do this copying, you need to do a recursive copy so that you get all of the subdirectories and their data (since we want all of that). The format for this (with my scheme) is:


cp -R  /mnt

Where is the full path (and not just the deepeest subdirectory) of the partition you wanted to create. In my example, I would issue:


cp -R /usr/src /mnt

I do this because it automatically copies it into /mnt/src. I'm not exactly sure why it does, but as long as I'm consistent then this works wonderfully.

This copy can take a long time, depending upon how much data you are transferring and how fast your hard drive and system are.

Try not to do too much while the copy is happening, otherwise it might just end up taking longer. But once that is done, you can check that the copy went okay by using the du command again:


[root@half-life src]# cp -Rf /usr/src /mnt
[root@half-life src]# du -s -H /usr/src
316M    /usr/src
[root@half-life src]# du -s -H /mnt/src
316M    /mnt/src

Pretty convincing for me, at least. Let's move on to /etc/fstab so that we can mount this correctly at startup - here we go to on to step 5.

5 - Editing /etc/fstab
If you've never edited /etc/fstab before, there's not much to it. Let's take a look at it first. Use cat:


[ddipaolo@half-life ddipaolo]$ cat /etc/fstab
/dev/hdb8              /                       ext2    defaults        1 1
/dev/hda1               /dos/C                  auto    defaults        0
0
/dev/hda5               /dos/D                  auto    defaults        0
0
/dev/hda6               /dos/E                  auto    defaults        0
0
/dev/hdb5               /home                   ext2    defaults        1
2
/dev/cdrom              /mnt/cdrom              iso9660 noauto,owner,ro 0
0
/dev/hdb7               /tmp                    ext2    defaults        1
2
/dev/hdb11              /usr                    ext2    defaults        1
2
/dev/hdb12              /usr/local              ext2    defaults        1
2
/dev/hdb6               /var                    ext2    defaults        1
2
/dev/hdb2               swap                    swap    defaults        0
0
/dev/fd0                /mnt/floppy             ext2    noauto,owner    0
0
none                    /proc                   proc    defaults        0
0
none                    /dev/pts                devpts  gid=5,mode=620  0
0

Looks kinda hairy, huh? Well, it's not too bad, really. Just notice that a lot of the things are the same from column to column. Basically, you've got these five (well, six) columns:

Partition - this first one describes what partition is being mounted (or is to be mounted at boot, more precisely)

Mount point - this shows where on the Linux filesystem this partition will be mounted

Filesystem type - specifies what filesystem has been created on that partition or 'auto' to use auto-detection

Options - I've had no use to use anything but defaults, though I've heard of people having issues writing to their mounted Windows partitions as normal users when using "defaults" for their options. I simply never write to my Windows partition except as root, and defaults works fine.

Dumps/Fscks (last two columns) - This has to do with filesystem dumps, fscks, and priorities. For the most part, you can just use the values "1 2" and you'll be fine.

So, like I said, nothing to it. It's basically a cut and paste job. So, to put our new partition with filesystem at /usr/src, let's run down what should go in each column:

Partition - /dev/hdb14 (in my case)
Mount point - /usr/src (again, my case)
Filesystem - ext2
Options - defaults
Dumps - 1 2

So, if you string them all together you get:


/dev/hdb14         /usr/src        ext2    defaults        1 2 

Heck you don't even have to use a text editor to add this one. You can use echo:


echo "/dev/hdb14 /usr/src ext2 defaults 1 2" >> /etc/fstab

Make sure you use >> and not > though. Otherwise you are in serious trouble unless you made a backup.

Now, you know how to do this when you have space to make the partition. Let's look at what's different for when you DON'T have the space to make the partition you want.


I don't have enough space free for a 1GB partition

Well, if your hard drive is just completely full, with no more room for partitions and you really want to try out multiple partitions, then I think it's actually easiest to backup the most important data that you can somehow and then reinstall with the multi-partition strategy. But, there are other ways of doing this:

The bottom line is, you need somewhere to store an archive with all your stuff in it while you delete your big partitions, so that you can make room for more. You will most likely want to have a full-featured bootdisk to do this sort of stuff, though. Especially if you plan on using the Internet for storage, because you will need to have PPP or Ethernet or whatever working with that bootdisk because we are temporarily cleaning off the entire hard drive of all data.

The bottom line is this. You have to compress all your data into big files and put it somewhere else. Then, using the bootdisk, we will go into fdisk, and delete all the partitions. From there, we then access the files that we put "somewhere else", and uncompress them into their appropriate places. This is actually a common way of changing partition configurations around when you already have a multiple partition setup. For example, I recently moved all my /usr stuff from a 1GB partition to a 2GB partition that way (well, not by deleting all my partitions of course, but you get the idea).

Probably the easiest and most convenient way to do this would be to use an existing Windows partition to do this. It has the biggest and quickest storage capacity (typically) of all the choices mentioned above. You just need to make sure you get a bootdisk that will allow you to mount Windows partitions so you can access the data once you are done.

The second easiest would probably be using recordable media like CD-R's, simply because CD-ROM drives are supported by just about every bootdisk I've ever seen.

The most painful, though potentially the largest file store, would be using the Internet to store your files while we mess with the partitions. But, with your bootdisk, you need to make sure that you get a bootdisk that will support your connection to the Internet as well as having some basic binaries such as ftp (or ncftp, as I prefer). This is also generally a much slower method because of the slow connection speeds to the 'net compared to the fast access times of disks and CD's (relatively speaking).

So, what you need to do before even thinking about this journey is to:

a - pick your method of storage
b - get a bootdisk that will accomodate that method
c - make sure you have everything important backed up!

Once that is done, things get much simpler.

The next step in this process is to create the archives that you will uncompress later. You have a few choices when it comes to compression, but I always use tar and gzip because I have never seen a bootdisk without those either. You may choose to use a compressor like bzip2 however because of a better compression algorithm, especially if you are using the Internet as your method of storage. However, I'm going to show you how to use tar and gzip in my examples from here on out.

The archives you need to make correspond to the partition scheme you should have already drawn up. Basically, you tar and gzip up those directories and then you simply inflate them into the partitions we create with the bootdisk once the data is off the drive. For example (continuing with my one above), say /usr/src was one of the partitions in my scheme. I'd want to tar up the /usr/src directory and then inflate it when I got my partitions set up. Here's how to tar and gzip it up:


tar cvfz _usr_src.tar.gz /usr/src

I like using underscores where the /'s normally go just to make sure that there is no confusion which archive that is when I get them all made. You may also want to omit the "v" because this will print out a lot of information. Also, obviously, substitute the filename (mine was _usr_src.tar.gz, yours should end in .tar.gz as well, though it's not compulsory), and directory (mine was /usr/src) that is appropriate to your archiving.

So, once I do this for ALL the partitions on my scheme, I move them to my storage area (for me, it's my Windows partition).

Then, I pop in my bootdisk and reboot.

Once everything comes up on the bootdisk, I make ABSOLUTELY SURE that I:

a - can access the files I just moved
b - that I have BACKED UP all my important stuff!

Once I have verifired both a and b, as well as verifying that I really really really want to go to this multi-partition setup, then I go ahead and fire up fdisk and remove all my Linux partitions and add new ones according to my strategy (see my fdisk Help File if necessary for a refresher). Then, if I have room for them all on one of my partitions, I copy all those archives down onto a Linux partition (notice I said copy and not move).

Once they are copied down to the Linux partition, I feel much much better. The next thing to do is to mount the partitions according to plan. That was discussed in section 3 above, so refer to that section and then come back here. You need to do this for all the partitions (or, at least you might as well do it for all of them right now). Then, you need to inflate the archives. If you did them the way I told you, this should be very easy. First, make sure the archive is in the root (/) directory. If it's not on that partition right now, move it there. Then, simply issue practically the same tar command:


tar xvfz <filename>.tar.gz

or, in this case it'd be:


tar xvfz _usr_src.tar.gz

At this point, a list of files should go flying by - these are the files being inflated. They should also all have a leading path of some sort - like mine is /usr/src - unless, of course, this is your root (/) partition archive (which I normally name _.tar.gz, even though it's odd... it's consistent). Once you have successfully inflated each of the archives, you may delete them all from your Linux partitions (and from those partitions only), but do NOT delete them on your storage media just yet, we may need them later.

The next thing you need to do is configure your /etc/fstab so that things work right on startup. See section 5 above on that. Once that is done, remove the bootdisk and reboot, and (if you believe in doing so) pray. If things DO go wrong and it won't boot correctly, there's not much lost in doing a reinstall. You still have your partition scheme and your archives somewhere, right? Well, reinstall, and simply inflate the archives on top of the freshly installed files and see if that works. If this is done correctly, it should not hose your system, though I won't say that I have never hosed my system by doing things like this.

Well, with any luck, you have just set up your system for its new multi-partition setup. If things didn't go perfectly, I apologize. Otherwise, have fun!