Creating a new partition on a hard drive with Linux

I have been having troubles since I started doing development for embedded software with the amount of available space in my /opt directory. You see, normally, you put all of your cross-compilers and software development kits (sdk's) underneath the /opt directory. Unfortunately, when I set up my laptop and partitioned the hard drive, I didn't set /opt into its own partition and so it was sitting in the root (/) partition. That has two problems: 1) it means you can accidentally mess up stuff in root just a little too easily (as I did on one of my early experiments) and 2) it means that I didn't have nearly enough room for multiple sdk's to live side by side.

So, what to do? I needed to create a special partition to handle the situation. Luckily, I had followed the advice of a friend when I first set this computer up and left lots of my large hard drive unformatted and unused. That means that I have plenty of space to create new partitions and move things around as needed.

I could have given /opt its own partition and just stuck all sdk's there, but I decided to go a different route. I left /opt in root but created new partitions for the sdk's I'm working with at /opt/[sdk name]. That means that each of my projects is essentially sitting in its own space on the hard drive and when I am done with a project, I can tar up its whole directory structure, save it to some back up medium, and then nuke the partition it sat on, giving me new, freshly formatted space the next time I start a project.

Notes of import:
For this description, I'm going to assume that your harddrive device is /dev/sda. If your harddrive is called something else, then translate that to what it should be.

In these descriptions I'm putting "sudo" before all of the commands that need to be done with root privileges. Of course, if you are root, you don't need to type sudo, but it's safer to use sudo and NOT be root.

Start off by taking a look at what you have right now.
sudo fdisk /dev/sda
It will ask you for a command and tell you what to use for help. Get help first. Your version of fdisk may be slightly different from mine, so there may be different key bindings for commands. It's always best to double check.

Assuming your fdisk is like mine, press p for print. that will show you what partitions you have on your harddrive now.

Next, press n for new partition. It will ask you where you want the starting point. Unless you have some grand plan, just take the default that you are offered. Next, it will ask you for an end point. The easiest thing to do there is give it a size in the format +size[K,M,G]. In my case I use 10G for each of these projects so that there is enough room for the sdk filesystems and my code, and plenty of leftover, too. So, I typed in +10G.

Press p to check your partition table and make sure it's as you wanted. If it's all good, type w for write. If it's not, just type q and the program will quit without saving your changes.

Once you have your partition table written, you need to format your new partition with the filesystem you want on it. The format for the command is mkfs -t <filesystem type> <partition name> Let's say that the filesystem you want is ext3 and the partition name is /dev/sda11, so you will type
sudo mkfs -t ext3 /dev/sda11

Now, create the directory that you want to map to that new partition. Let's say I want a directory called /opt/fone-linux. I need to type.
sudo mkdir /opt/fone-linux

Now you are ready to mount your directory to the partition.
sudo mount /dev/sda11 /opt/fone-linux

Check it to make sure it works.
sudo touch /opt/fone-linux/test
sudo ls /opt/fone-linux/test

(I put sudo before those two because if you haven't chmod'ed or chown'd anything yet, your /opt/fone-linux directory is owned by root and not readable by others at this point.)

Now, go to /etc/fstab and add your new partition's information into the file so that things mount automatically at boot. This is the easy part. If you've never edited your fstab before, don't be afraid. Just open it up and read what's there now. You'll be able to copy the line from one of your other partitions and just make changes to fit your new partition.