65

I am attempting to move some folders (such as /var and /home) to a separate partition after reading this guide:

3.2.1 Choose an intelligent partition scheme

I was able to move one folder successfully following thisguide.

However, it doesn't seem to work for multiple folders, and all my folders are dumped into the partition without proper folders.

I would like to mount /var, /home, and /tmp onto the separate partition; can someone guide me on this?

4
  • Did you make an extra partition for each of the directories or just one?
    – mreithub
    May 21, 2014 at 12:44
  • @mreithub i would like it to be ideally all stored within the same partition and only varies with folder. in that way it is much simpler for me, not having to worry about the space. May 21, 2014 at 13:16
  • 1
    I don't recommend putting /var on a separate partition. There's no benefit to it. Keep the whole OS on a single partition (/). Use separate partitions only for things that are not the OS — user data in /home, mail spool (if you have a big one) in /var/mail, databases in e.g. /var/lib/mysql or /srv, … May 21, 2014 at 23:32
  • 9
    @Gilles "There's no benefit to it"?????? (Maybe I need more question marks). Of course there's a benefit to it. / is mostly static. /var is almost entirely dynamic. If you mount /var on a separate partition, you can mount the root partition read-only. There are a ton of situations where that is a Good Thing™.
    – Auspex
    Jan 30, 2016 at 20:52

4 Answers 4

88

1. First you need some unallocated space to create the partitions for each mountpoint (/var, /home, /tmp). Use Gparted for this.

2. Then you need to create the filesystems for those partitions (can be done with Gparted too) or use:

mkfs.ext4 /dev/sdaX

for example to create a new ext4 filesystem on the /dev/sdaX device (replace /dev/sdaX with your own device)

3. Mount the new filesystem under /mnt

mkdir /mnt/var        
mount /dev/sdaX /mnt/var

4. Go to single-user mode so that there is no rw activity on the directory during the process

init 1

5. Enter your root password.

6. Backup data in var only (not the /var directory itself)

cd /var
cp -ax * /mnt/var

7. Rename the /var directory after your data has been transferred successfully.

cd /
mv var var.old

8. Make the new var directory

mkdir var

9. Unmount the new partition.

umount /dev/sdaX

10. Remount it as /var

mount /dev/sdaX /var

11. Edit /etc/fstab file to include the new partition, with /var being the mount point, so that it will be automatically mounted at boot.

/dev/sdaX       /var     ext4    defaults    0 0

12. Repeat steps 1-11 for /home and /tmp.

13. Finally return to multitasking mode.

init 5
11
  • yeah i was thinking of using gparted. thanks for the answer. i am planning to set aside 30gb space for these few folders actually /home, /tmp, /var/, /opt, /usr/, do you have a rough gauge of how much i should give accordingly (for a simple hosting server)? May 21, 2014 at 13:03
  • 1
    i was looking up on using lvm, would that be better as compared to gparted? May 21, 2014 at 13:03
  • LVM is a completely different topic. LVM is used as a layer to create logical volumes on top of physical harddisks. Gparted is only the tool to create the LVM physical volumes.
    – cioby23
    May 21, 2014 at 14:11
  • 2
    Actually, cp -ax * /mnt/var is a bad idea, because it'll ignore hidden files/dirs. Prefer cp -ax . /mnt/var. (@cioby23, I think you should update your answer)
    – berbt
    May 26, 2016 at 22:55
  • 7
    Please add a final init 5 to the answer. Aug 19, 2016 at 8:38
23

If you want to use them all on the same partition you can either use LVM or three simple bind mounts.

To create a bind based solution you

  1. create a partition with a filesystem
  2. go to rescue mode (single user still needs some of the folders)
  3. mount it as /mnt/data
  4. Move all folders you want to move. Using cp and mv dir dir.old might be safer, but since I did this after install it did not matter to me.

    mv /home /mnt/data/
    mv /var  /mnt/data/
    
  5. create appropriate bind mount entries in fstab, for example I have

    UUID=01234567-89abc-def0-1234-567890abcde /mnt/data         ext4          noatime,nodiratime,relatime                           0       0
    
    # Binds
    /mnt/data/opt                             /opt              none          bind                                                  0       0
    /mnt/data/home                            /home             none          bind                                                  0       0
    /mnt/data/usr                             /usr              none          bind                                                  0       0
    
  6. reboot

4
  • In the mount(8) man page under nodiratime it says: If noatime option is set, this option is not needed. Is there a redundancy advantage in including it anyway? Feb 27, 2017 at 12:09
  • @brannerchinese there’s no advantage whatsoever: lwn.net/Articles/244941
    – Gaston
    Jun 19, 2020 at 14:01
  • I'm having a boot failure: Failed to start Remount Root and Kernel File Systems. Not entirely unexpected. What am I supposed to do about it? Oct 27, 2021 at 10:01
  • As it turned out, I had to create empty directories /var, /opt, etc. on the root partition in place of the ones moved out to another partition. Oct 27, 2021 at 11:58
5

I'd personally either go with @Fabian's bind-mount solution or use symlinks:

  1. create /etc/fstab entry for the new partition (use blkid to get the right UUID for your new partition and don't forget to mkdir /mnt/data):

    UUID=01234567-89abc-def0-1234-567890abcde  /mnt/data  ext4  defaults  0 0
    
  2. Use mount -a or something similar to mount the new partition

  3. move the original folders to your new partition:

    mv /var /mnt/data/
    mv /home /mnt/data/
    # ... (you get the point)
    
  4. Create symlinks

    ln -s /mnt/data/var /var
    ln -s /mnt/data/home /home
    # ...
    

Although the bind-mount method is probably the best practice solution I kind of prefer this one because you don't end up with unnecessarily long df and mount partition lists.

3
  • 1
    What happens with the respective methods if something wants /var before it's mounted? I rather suspect that the bind method will be cleaner, but I really don't know.
    – Auspex
    Jan 30, 2016 at 22:05
  • @Auspex There shouldn't be a difference between the two methods in that regard (as afaik all local partitions in /etc/fstab are mounted at the same time during boot). Before /mnt/data gets mounted all your symlinks would be 'dead', so any read/write attempt on them (and on non-existent subdirs) would fail (with ENOENT if I remember correctly). With bind-mounts you get pretty much the same result but programs might actually write onto the root partition (if they try to be smart about non-existing directories)
    – mreithub
    Jan 31, 2016 at 0:15
  • 1
    Well, that's not strictly true. Root gets mounted first, and I specifically asked because Debian's "Read-only root" advice mentions the possibility of processes wanting to access /var before it's mounted. But in my case, where I have a Raspberry Pi and have set up a read-only filesystem on / (because the SD card keeps getting corrupted when the power goes out to the machine), you're encouraging me that either method will work. Nothing's going to write onto the root filesystem, because it's RO, so either case will result in an error.
    – Auspex
    Jan 31, 2016 at 19:55
0

In addition to the procedure given cioby23, I also had to do:

  restorecon -R /var

in order for sshd to start.

1
  • This should be a comment...
    – Jack_Hu
    Jun 6, 2020 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.