Move /opt to a different, already existing drive

5,054

Solution 1

You can simply link to it:

  1. Move the /opt directory:

    sudo mv /opt /mnt/otherDisk/
    
  2. Create a symlink to the new location:

    sudo ln -s /mnt/OtherDisk/opt /
    

You will now have:

$ ls -ld /opt
lrwxrwxrwx 1 root root 5 Apr  6 14:23 /opt -> /mnt/OtherDisk/opt

As Rinzwind correctly pointed out in the comments, this can break your system if you move a directory containing files needed during boot. For example, you certainly don't want to do this for /bin. /opt should be fine though.

Solution 2

I second moving /home, but it's possible to do what you want.

If I understand correctly, you have two drives, one with ubuntu (and possibly some windows stuff) and one with the windows user files. What you would need to do is shrink the windows partition (while I have done this successfully many times, resizing partitions may lead to data loss, so back up your data), and that will free up room on that drive. You can use a program like GParted to resize partitions. Once done, you can take the leftover space and make a new partition (also done with GParted). Then you'll need to copy the contents of /usr/local and /opt over to the new partition (I usually do this part by inserting a live cd and mounting everything and copying). Finally, you'll need to edit /etc/fstab and tell it where to mount /usr/local and /opt.

The link given above on the steps to move /home are the same, so you can follow that guide for moving /usr/local and /opt. What you will need to do is resize the windows partition first.

Share:
5,054

Related videos on Youtube

Steve D
Author by

Steve D

Professional book-reader.

Updated on September 18, 2022

Comments

  • Steve D
    Steve D over 1 year

    I dual boot Ubuntu 16.04 and Windows 8.1. All of Ubuntu lives on an SSD, and the core Windows stuff lives there too, in a separate partition. The main User folders for Windows (Documents, Downloads, etc.) live on a separate hard drive.

    I'm running out of space in Ubuntu. I'd like to move /opt and /usr/local to the hard drive. All the Q&A I've found about this, however, starts with the assumption that these folders are already mounted on a different partition, or that I can format the destination drive. Neither of these is true for me.

    I don't remember exactly what I did when I set this computer up, but I do know the hard drive is accessible from Ubuntu (and is at /dev/sdb1, mounted at /media/steve/storage). Is it possible to do what I'm asking?

  • Rinzwind
    Rinzwind about 7 years
    Not /home/ but the user content (the directories) should be moved. So altering ./config/user_dirs.dirs
  • Forty-Two
    Forty-Two about 5 years
    This is not a complete answer. You can't just sym link opt and expect everything to be happy. If the drive or partition that you sym link it to does not automatically mount then it causes huge frustrations. The actual answer is to change the destination of OPT in FSTAB.
  • terdon
    terdon about 5 years
    @JoshuaRobison there shouldn't be anything in/opt that's required during boot. If there is, then yes indeed not having it mounted might be an issue. On the other hand, it might also be a problem to mount the drive automatically since that would stop the boot when the drive isn't attached. Moving and linking something like /usr would be a horrible idea, but /opt should be fine in the vast majority of cases. In any case, there is another answer that suggests the (perfectly valid, as you say) fstab approach.
  • NZ Dev
    NZ Dev about 4 years
    This answer is not good. Lots of issues:, Where did foo come from, sym linking to root???. As @jushuaRobison suggested - Move the data from opt to new drive, and mount this new drive on boot (fstab). once I do this I will post the process below. Often I put the current hdd into another machine to get unchanged data off, and this way pretty much anything can be moved (using dd). That siad be very careful!
  • terdon
    terdon about 4 years
    @NZDev whoops, thanks. I must have been using foo to test when I wrote it. That foo should have been opt. Apart from that, please see my answer to Joshua above. Mounting on boot can have a different set of problems. You just need to use the right approach for you. However, I struggle to think of a case where moving /opt would be a problem. There may well be such cases, I just can't think of one. I am not saying you can do this for any directory in general, but /opt should be fine.
  • NZ Dev
    NZ Dev about 4 years
    Somewhat agree, unfortunately the question did not have enough info to determine the exact setup. He said existing HDD this could mean it already has data on it, which in that case your solution is fine for opt. My preference would be to move out home, opt, srv, var to disk space that works for the solution. My preference is if creating brand new space for something that is getting larger is to stand up a new drive/partition and then mount it with fstab, mounting can have problems, but it is a good skill and usually the right practice
  • NZ Dev
    NZ Dev about 4 years
    I don't know enough about sym linking, but I would have assumed the parameters are the source file/directory and then the destination file/directory, You have provided just root as the second parameter, I would have though the command should be: sudo ln -s /mnt/OtherDisk/opt /opt
  • terdon
    terdon about 4 years
    @NZDev no, that would be very different. That would create a symlink at /opt/opt, not at /opt. Frankly, I'm surprised you don't know this after your pretty assertive "This answer is not good. Lots of issues:".
  • NZ Dev
    NZ Dev about 4 years
    OK so you are part right I have never used ln that way, and yes it does work as you have described, I have always been explicit with my naming of my symlinks, and hence /opt. But you are wrong, my way would not create /opt/opt, my way is how the docs and majority of guides explain how ln should be used. If I had put a / at the end then maybe it would have tried to add opt into an opt folder, but it would have failed "No such file or directory" due to /opt/ not existing.
  • terdon
    terdon about 4 years
    @NZDev no, of course it won't create a directory, how could it? If /opt exists and is a directory, then ln -s foo /opt will create /opt/foo. If /opt doesn't exist, then it will create /opt as a symlink to foo. And if it exists but isn't a directory it will fail unless you use ln -f in which case it would replace the existing file with a link.