Beginner Linux users often make mistakes, sometimes mistakes that they cannot recover from. In this situation, often the first instinct is to reinstall the operating system from DVD/CD. Or perhaps you’d like to use a different Linux distribution. Either way, the thought of reinstalling all your user data from backups (you do take backups right?) can be unwelcome. One of the first things I recommend to new users of Linux is to move your /home directory to a different partition which ideally should be on a different physical disk. When mentioning this though, most users have already installed the operating system. By default, most Linux distributions install everything onto one partition on one disk. This is how Windows usually works so does not seem too unusual for new users. It makes installing just a couple of mouse clicks. While this is an easy option, it’s not really ideal – but while pretty much all Linux distros allow you to manually configure the partitions for each section of the operating system via a graphical system at install time, you probably won’t have access to this after the fact. So you’ll have to get your hands dirty with the command line and learning about hard disk partitioning.
For this scenario, I’ll assume you’ve already installed your Linux system on one disk on one partition which is usually the default partition layout on today’s Linux systems. Now though, you want to install your home directory on a different partition and for added data autonomy, on a different disk. I’ll start at installing the new hard disk and take you through the whole process.
First, though, we’ll need to talk a little bit about drives and partitions in Linux and the difference between primary, extended and logical partitions.
All devices on your Linux system are listed (naturally enough) under /dev. Hard disk drives attached to your system are labelled /dev/sda for the first hard disk, /dev/sdb for the second and so on. For each of these disks the partitions are also listed. So for partition 1 on your system, this would be /dev/sda1. The second partition on that drive would be /dev/sda2 and so on. Historically, Linux used to make a distinction between IDE hard drives and SCSI hard drives. IDE hard drives were listed with a ‘h’ instead of an ‘s’, as in /dev/hda. SCSI hard drives were always ‘s’, as in /dev/sda. Now, however, all hard drives under Linux just use ‘s’ unless you’re using a very old system.
While it’s not critical knowledge for this exercise, it’s also worth mentioning something about the limitations of partitions. On x86 systems (your PC or server basically), Linux can only accommodate four partitions per hard disk. This is something that was decided way back when the original PC was designed and you can google why if you’re interested :-) These distinct partitions are what’s known as primary partitions and you need at least one primary partition on a hard disk that holds the operating system as the Master Boot Record (MBR) ignores all partitions except the first primary partition. On the default partition layout written to the hard disk during a basic Linux installation, this, along with Logical Volume Management (LVM), is the only partition that is used and your home directory will be sharing the same partition as the operating system.
On some Linux systems, you’ll see partitions listed for each disk which have more than four partitions, such as /dev/sda5 or /dev/sda7. How could this be?
Well, to have more partitions than four to take advantage of Linux’s various partitioning schemes (especially on servers), you cheat a little bit by using extended and logical partitions.
You can devote one of these four primary partitions as a container of sorts to hold smaller partitions. This container partition is called an extended partition. Since the partitions it hold are by definition not one of the four primary partitions, they are known as logical partitions.
So, under /dev if you had a secondary hard disk with three primary partitions and two logical partitions, this would be: -
- /dev/sdb1 – Primary partition 1
- /dev/sdb2 – Primary partition 2
- /dev/sdb3 – Primary partition 3
- /dev/sdb4 – The extended partition
- /dev/sdb5 – Logical partition 1
- /dev/sdb6 – Logical partition 2
As you can see from the above, the extended partition /dev/sdb4 is listed as a partition itself but you won’t be able to access it. It holds the next two logical partitions.
Okay, back to our secondary disk. Based on what you now know about Linux disks and partitions, this should be fairly easy.
Power off your system and install the new drive. After you’ve powered up again and logged in, you should see the new drive listed under /dev as /dev/sdb. If you’re not sure if the system has detected the new hardware you can use a command such as ‘hwinfo’ or ‘dmesg’. But as I said, if you’re using a one disk, one partition system, the next drive after /dev/sda should be /dev/sdb :-)
If you run: -
…this should show a new disk devoid of partitions. We’ll now use fdisk to create the partition table.
The commands for fdisk are somewhat cryptic but you can press ‘h’ to see a list of commands. The ones we’ll be using are: -
p = Print current partition table
n = Add new partition
w = Write new partition data to disk
q = Quit without saving modified partition data
If you press ‘p’ now, you should see no partitions on your new drive. If you do, or you’re using an old drive, press ‘d’ to delete the partitions followed by their number. Once the drive has no partition information, press ‘n’ to create a new partition then press ‘p’ to make it a primary partition as we’re going to use the entire disk for this exercise – but you should know enough about partitioning now to be able to modify the partition table to your liking.
Anyway, accept the default value for the size – this always defaults to the most allowable space available which is the entire disk in this case.
Press ‘w’ to write this new partition information to the disk and return to the command-line. If you now examine the /dev directory with: -
ls -l | grep sdb*
…you should see a device called /dev/sdb1 – this is our new first primary partition of the second disk. Now it needs to be formatted. Do this by using the command: -
If you’re using a slightly older Linux distribution, the default file-system might well be ext3 instead of ext4. In which case, use the mkfs.ext3 command instead.
Now you have a new secondary disk ready for use. For this exercise, make sure you’ve backed up the contents of /home – backups, remember? :-)
Also, before you do anything else, you need to make sure nobody is using /home – no other users aside from you. You can check to see if any processes are using /home using: -
lsof | grep /home
Now you need to mount the drive to copy the contents of /home to the new drive. I’ll use an empty directory called /mnt/sdb1 but you can create whatever directory you want.
mount -t auto /dev/sdb1 /mnt/sdb1
Then you’ll need to recursively copy everything under /home to this location: -
cp -rp /home /mnt/sdb1
The ‘r’ flag means copy recursively and the ‘p’ flag means preserve file permissions – else everything would reset to root ownership and none of your users will be able to access their files :-)
Once it’s all copied, you can rename /home to something else – this is for safety’s sake, if something goes wrong you can always restore everything.
mv /home /home_old
Now you need to create a new mount point for /home.
Now, unmount the drive.
You’ll want to have this new drive automatically mount as /home every time the system boots, so you’ll need to edit your /etc/fstab configuration file which is responsible for mounting drives during the boot process.
Add the following line:
/dev/sdb1 /home ext4 defaults 1 2
Verify this works by simply typing: -
While still logged in as root, try logging in as a user and can access their home directory. If all goes well, you can delete the old home directory with: -
rm -rf /home_old
Now you have home on a separate partition so if you make a mistake, your user files are safe. If you want to try a different Linux distribution you can and keep your files. You’ll need to either modify the /etc/fstab to point to your second drive or use any advanced graphical partitioning tools during the installation process to point the home mount point for the new installation to your second drive. Phew!