Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My /usr folder needs to get moved to a new partition. How can I do this without erasing the contents?

Can this be done while Ubuntu is running, or do I need to use the LiveCD for this?

share|improve this question
up vote 21 down vote accepted

It would be safest to use a Live CD, but you could do:

  • copy all the files to a new partition, making sure that the contents of /usr do not change while you are doing this.
  • edit /etc/fstab so that /usr will be mounted on the next reboot
  • reboot
  • delete the old files

See below for details on each step.

Note that you can't mount the new partition on /usr while running as there will be lots of files in /usr that will be open.

Copying the files

I would use cp -a. -a is the archive option. From the man page:

-a, --archive
          same as -dR --preserve=all
...
-d     same as --no-dereference --preserve=links
...
-P, --no-dereference
          never follow symbolic links in SOURCE
...
--preserve[=ATTR_LIST]
          preserve      the      specified      attributes       (default:
          mode,ownership,timestamps),  if  possible additional attributes:
          context, links, xattr, all
...
-R, -r, --recursive
          copy directories recursively

Editing /etc/fstab

You need to know the UUID of your new partition. You can see the mapping by doing:

$ ls -l /dev/disk/by-uuid/

And then add this line to /etc/fstab:

UUID=634c31a5-e27c-4e33-ac67-2e22491a30c2 /usr           ext4    defaults        0       2

Change the UUID to your UUID, and change ext4 to be the file system type you are using - you should know this if you have set up the partition.

Delete the old files

After the reboot, the old files in /usr on the root partition will be hidden by the new partition mounted on /usr. But we can use some mount bind trickery to get to the old files and then delete them.

$ sudo mount --bind / /mnt
$ sudo rm -rf /mnt/usr
$ sudo umount /mnt

But some slight mistyping (say, hitting Enter when you'd only typed sudo rm -rf /mnt ) could cause disaster, so I would only use this method if you were very confident in what you were doing, really couldn't deal with any downtime, or had no physical access to the machine and hence were unable to boot off a live CD or live USB stick.

share|improve this answer
3  
rsync is so much better suited for this than cp – Marco Ceppi Aug 2 '10 at 20:17
    
Could I use this guide to do the opposite. I mean move the /usr folder to root partition? – AHugoH May 1 '14 at 12:12
1  
@MarcoCeppi Could you expand on that? Maybe give a variant answer that uses rsync instead? – Kazark Dec 31 '14 at 20:33
4  
Replace cp command with rsync -avz. Rsync allows you to restart copies and should generally be used when moving between partitions, block devices, or machines. – Marco Ceppi Dec 31 '14 at 21:17
    
Also I left off the -f when I did sudo rm -r /mnt/usr as an extra safeguard against bad things. – Kazark Dec 31 '14 at 23:39

Since most libraries that are used are in /usr, I would not recommend to move this directory while running Ubuntu. In fact, you probably get error messages when you try to do this. Hence, the best is to use the LiveCD.

You can use several possibilities to move/copy the files cp, rsync etc. you want to make sure that any symlinks are created and not just copied. cp and rsync both have options for this.

After moving the files to the other partition you need to add another mount in /etc/fstab to mount the new partition to /usr.

share|improve this answer

Attention: I have no idea what I'm doing, I'm just copying commands and doing as suggested by the others. This may be the incorrect way to do it, but it works for me (for now, at least).

This is how I've done it (following the Hamish's answer and the comments):

  1. Copy all the files the newly created partition (replace with the location of your partition, it should look similar to mine):

    rsync -avz /usr /media/aleksandar/750b84e2-e65f-4309-ade5-5af0033a937c 
    
  2. At this point I've realized that it copied everything into the /usr folder on the partition, which isn't going to work, so I copied everything (with file manager) from that folder to the root of the partition, and the deleted that empty folder afterwards.

  3. Edit /etc/fstab (same as in Hamish's answer, of course, replace with your UUID)

    UUID=634c31a5-e27c-4e33-ac67-2e22491a30c2 /usr           ext4    defaults        0       2
    
  4. A step that turned out to be not very wise: I changed the name of the currently mounted /usr partition to /usrBACKUP, just so that I know that, when I reboot the system, it in fact reboots into the newly created partition, and not the old one. After that, everything glitched on the screen, and I had to power it off by long-pressing the power key (genius as I am). Skip this step if you don't want to glitch your system.

  5. Reboot the system

  6. After rebooting, open System Monitor or similar application to see whether your new /usr partition is mounted, and to safety-check whether everything went as planned.

  7. After checking that everything is alright, you can delete your old /usr partition. I will keep mine just in case something goes wrong.

share|improve this answer
    
Why answering a question 5 years old if it already has an accepted answer and you dont know what you are talking about? – cmks Apr 7 at 22:54
    
@cmks because I felt like other answers were incomplete - I didn't get complete solution from one answer, and none of the answers didn't have enough detail to them. I felt like users who would come to this question would need help. It doesn't matter if it's five years old if someone still needs the information from this question. – Aleksandar Stefanović Apr 8 at 13:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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