Move Home Directory to another dedicated Drive/Partition in Linux
By default the home directory will be installed within the OS hard drive and root partition. Below you will see how to outsource the home directory to a dedicated separate mounted disk.
This topic is well documented in the following article and I will refer to it.
Part 1 will show how to create a new partition on a further added hard drive. You can skip these steps in case you still have an existing partition you want to use dedicated for the home directories and go directly to Part 2.
- Part 1: Create a new Partition
- Part 2: Moving the Home Directory Folder to a dedicated Drive/Partition
Part 1: Create a new Partition
In my case I am using a Hyper-V virtual Ubuntu VM, therefore I will first add a virtual hard drive in Hyper-V.
After that I will using GNU parted to create a new partition on this second hard drive. Parted is pre-installed in Ubuntu. If not you can use the following command.
# # apt-get install parted
GNU Parted User Manual
In order to check if the second hard drive is detected by Ubuntu we can use the following command to list all connected disks.
# # parted -l
In my case the second added IDE virtual hard disk drive with a size of 1 TB becomes /dev/sda. The first virtual hard disk drive from the Ubuntu installation is listed as /dev/sdb.
The reason for why now the second hard drive becomes sda is described below under Overview: UUIDs, Labels and fstab.
msdos partition is Master Boot Record (MBR)
Overview: UUIDs, Labels and fstab
Persistent device naming for block devices has been made possible by the introduction of udev and has some advantages over the use of traditional bus-based names such as /dev/hda1 or /dev/sda2.
If you have more than one disk controller (IDE or especially SCSI/SATA), or even if you just have variable numbers of removable USB/firewire storage devices attached from day to day, the order in which they are detected may not be deterministic.
The result is that device names like /dev/sda1 and /dev/sdb1 may switch around randomly on each boot. Persistent naming allows you not to worry about this at all.
Create a new partition by using parted
As determined above, I want to partition my second added virtual hard disk drive which becomes /dev/sda. Normally you should start parted with the device you want to edit as follows.
# # parted /dev/sda GNU Parted 3.3 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
But you can also only type parted and after that tell parted which disk we want to edit in the second place using the select command.
# # parted GNU Parted 3.3 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) select /dev/sda Using /dev/sda (parted) #
In order to create a new partition, we need to to set the partition table type by using the mklabel command. To see a list of supported table types you can type in help mklabel.
# # parted /dev/sda GNU Parted 3.3 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help mklabel mklabel,mktable LABEL-TYPE create a new disklabel (partition table) LABEL-TYPE is one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, atari, loop (parted)
Therefore we can choose between the following table types:
I will use the gpt table type. So I will execute the following command.
(parted) mklabel gpt
# parted /dev/sda GNU Parted 3.3 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel gpt Information: You may need to update /etc/fstab.
In order to update/reload the /etc/fstab we need to execute the follwing command
# mount -a
Now we can check the partition by using the command
(parted) print free
# parted /dev/sda GNU Parted 3.3 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print free Model: Msft Virtual Disk (scsi) Disk /dev/sda: 1074GB Sector size (logical/physical): 512B/4096B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 17.4kB 1074GB 1074GB Free Space (parted)
So far we doesn’t have any partition on the disk, we only defined the partition table type so far.
To create a new primary partition prepared for the ext4 file system and using all space on the disk we can run
# mkpart ext4 0% 100%
In case you use the msdos (MBR) partition table you must specify the part-type (primary, extended or logical) as follows:
# mkpart primary ext4 0% 100%
or for a logical partition
# mkpart logical
I will create a partition prepared for the ext4 file system and the whole disk space.
Keep in mind that the above mkpart command will not create the ext4 filesystem itself on the partition, it will only prepare the partition for it.
Command: mkpart [part-type name fs-type] start end
Creates a new partition, without creating a new file system on that partition. This is useful for creating partitions for file systems (or LVM, etc.) that Parted doesn’t support. You may specify a file system type, to set the appropriate partition code in the partition table for the new partition. fs-type is required for data partitions (i.e., non-extended partitions). start and end are the offset from the beginning of the disk, that is, the “distance” from the start of the disk.
part-type is one of primary, extended or logical, and may be specified only with msdos or dvh partition tables. A name must be specified for a gpt partition table. Neither part-type nor name may be used with a sun partition table.
# parted /dev/sda GNU Parted 3.3 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mkpart ext4 0% 100% Information: You may need to update /etc/fstab.
We also need to update and reload another time the /etc/fstab file by using the following command
# mount -a
In case you want to remove the partition from your hard drive to create a new from the scratch you can use the following command
parted /dev/sda GNU Parted 3.3 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print Model: Msft Virtual Disk (scsi) Disk /dev/sda: 1074GB Sector size (logical/physical): 512B/4096B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 1074GB 1074GB (parted) rm Partition number? 1
Create the Filesystem with mkfs on the new Partition
As mentioned above we now had a new partition prepared for the ext4 filesystem. The next step is to create the ext4 filesystem for our new partition by using the mkfs command.
After a reboot from the virtual machine, the new disk now be listed as sdb and the new partition is sdb1. The initial OS partition is now listed as sda.
Keep in mind!
Names like /dev/sda1 and /dev/sdb1 may switch around randomly on each boot. Persistent naming allows you not to worry about this at all.
# mkfs.ext4 /dev/sdb1
Finally we need to mount the new partition and drive. Therefore I will first create a new folder /media/home for which we mount the new partition and drive.
# mkdir /media/home
In order to mount the new drive and partition we need to edit the /etc/fstab file as follows. I will use here the as mentioned above persistent device naming by using the Universally Unique Identifier (UUID) from the filesystem of my new hard drive and partition to avoid the randomly switch of the bus-based names such as /dev/sda1, …
UUID=00ba6a9d-c345-444a-85be-398b1aeaa189 /media/home ext4 defaults 0 0
The UUID you can determine by using the blkid command
To avoid a reboot we execute the following command after editing the /etc/fstab file.
# mount -a
That command will mount all filesystems mentioned in fstab.
We can also check if the new partition is mounted to /media/home by using several commands like lsblk, pydf, findmnt or df.
# lsblk (list block devices)
# lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL
Block devices are characterized by random access to data organized in fixed-size blocks. Examples of such devices are hard drives, CD-ROM drives, RAM disks, etc. The speed of block devices is generally much higher than the speed of character devices, and their performance is also important. This is why the Linux kernel handles differently these 2 types of devices (it uses a specialized API).
findmnt will list all mounted filesytems or search for a filesystem. The findmnt command is able to search in /etc/fstab, /etc/fstab.d, /etc/mtab or /proc/self/mountinfo. If device or mountpoint is not given, all filesystems are shown. The command prints all mounted filesystems in the tree-like format by default.
You can also check the mounted devices by using the process information pseudo-filesystem (proc).
Detailed information about the disk and partition you can also see by using the following command
Part 2: Moving the Home Directory Folder to a dedicated Drive/Partition
Copy the existing Home Directory Folder to the new Folder
As my new partition is mounted in /media/home I can now copy the existing home folder to this new mount point as follows.
# # sudo rsync -aXS --progress /home/. /media/home/.
- -a archive mode
- -X preserve extended attributes
- -S handle sparse files efficiently
- –progress show progress during transfer
Backup old Home Directory Folder and create a new one
To keep a copy of the origin home folder in case something went wrong, I will rename the old existing home folder and then create a new one.
# mv /home /home_old # mkdir /home
Mount new Disk/Partition with Home Directory Folder to the newly created Root Home Folder
Finally we need to mount the new home directory folder /media/home to the newly created /home folder in the root partition.
Therefore we need to modify the /etc/fstab file again. At the moment my fstab looks like this. You can see the last entry is my previously created new partition and hard drive which I mounted to the folder /media/home in the root partition.
Now I only need to change the mount point from /media/home in /home as follows.
Finally as usual I need to execute
# mount -a
to mount the filesystems listed in fstab.
Primary Partition, Logical Partition, Extended Partitions and Active Partition …
The difference comes in play by using the Master Boot Record (MBR) partition table, because the GUID Partition Table (GPT) only contains primary partitions.
A disk drive using the MBR partition table can contain a maximum of 4 primary partitions or 3 primary partitions and one extended partition, which then can be include several logical partitions to bypass the limit in MBR.
By having multiple primary partitions you can mark one of them as active partition, which then will be recognized by the BIOS and used to boot the OS from. The active partition also contains the boot loader.
The number of logical partitions wich can be used with MBR are not limited.
Further the MBR bootloader can only boot from a primary partition.
The newer partition scheme GUID Partition Table (GPT) doesn’t have such limits and does not need to use extended and logical partitions, all partitions are primary partitions as mentioned.
Master boot record (MBR)
GUID Partition Table (GPT)
Logical vs Primary | What’s the Difference and Which Is Better
What are the differences between primary and logical partition?
What are the differences between the various partition tables?
GNU Parted User Manual
Partitioning Disks with parted
How To Create a Partition Using “parted” Command
9 Linux Parted Command Examples – mkpart, mkpartfs, resize partitions
Overview: UUIDs, Labels and fstab
udev – Linux dynamic device management