How to put /opt and /home on the same partition but different from /?

Solution 1:

This is what I would do. Make a home partition, tell the installer to mount it to /home. After install and on first boot create a directory called /home/opt. Then mount bind it to /opt.

Make sure /opt is empty before you do this. I would think so if it's just after a fresh install.

mount -o bind /home/opt /opt

for a manual mount.

For mounting on boot, edit /etc/fstab it would look something like this. Make sure you add below your home mount, which the installer should have added.

/home/opt /opt none bind 0 0

Solution 2:

It's pretty easy with bind mounts. That way, you can mount a subdirectory from one location on another location.

Just make "opt" a subdirectory in your "home" partition, then mount the partition to /home and bind mount /home/opt to /opt:

sudo mount /dev/sda4 /home
sudo mount --bind /home/opt /opt

Or it can be made permanent by adding them to /etc/fstab like this:

/dev/sda4   /home   ext4   defaults   0 2
/home/opt   /opt    none   bind       0 0

And if you prefer using UUIDs, replace /dev/sda4 with UUID=<your UUID>

Solution 3:

The LVM way

If you don't mind it being two different volumes, on one partition.

Create an Physical Volume on the drive you want to put the /home and /opt on. Create a Volume Group with just this partition as the single member of it. Give it a name, e.g. myvg. Then create two Logical Volumes, e.g. homes and opt, both will have a filesystem and the obvious mount points.

Advantages:

  • You can do snapshotting.
  • Resizing Logical Volumes is a piece of cake compared to resizing partitions.
  • Create more volumes without even touching the partition table on a running system.

In order to profit from this, you'll have to keep some free space in the Volume Group.

How?

The installer will allow you to do this quite easily in the GUI. For 12.04 you'll need the alternative image, for 12.10 and onwards, this is included in the regular images.