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?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityIt would be safest to use a Live CD, but you could do:
/usr
do not change while you are doing this./etc/fstab
so that /usr
will be mounted on the next rebootSee 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.
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
You need to know the UUID of your new partition. You can see the mapping by doing:
$ ls -l /dev/disk/by-uuid/
or
$ sudo blkid
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.
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.
rsync
instead?
rsync -avz
. Rsync allows you to restart copies and should generally be used when moving between partitions, block devices, or machines.
Dec 31, 2014 at 21:17
cp
. It doesn't preserve HARD LINKS. This will break package upgrades in future. For example /usr/bin/s2p
and /usr/bin/psed
is the same file. Moving with cp
will create 2 independent versions of that file. Do rsync -aH
instead.
rsync -avH a/ b
differently from rsync -avH a b
, copying the contents of folder a
(that is, a/*
) in the first case and the folder itself in the second. Make sure you use the slash here.
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.
This is how I've done it (following the Hamish's answer and the comments):
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
Edit /etc/fstab
(same as in Hamish's answer, of course, replace xxxxxx
with your UUID)
UUID=xxxxxx/usr ext4 defaults 0 2
Reboot the system
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.
After checking that everything is alright, you can delete your old /usr
partition. I will keep mine just in case something goes wrong.
sudo mount -a
allow it to keep on running?
Aug 6, 2016 at 9:01
mount --bind
is the answer.
/usrBACKUP
while the system was still running, and then it crashed. So don't do that. Instead, just reboot the system normally. Don't worry about having two /usr
folders, Linux will happily hide the old one and mount the new one in its place. (You can delete the hidden folder, but I'm not explaining that here.)
Aug 22, 2020 at 9:59
I performed a migration of the /usr
partition but I had some issues which I will be detailing in the following lines:
/etc/fstab
at the moment to mount by UUID (specially when u restart the OS), getting the error UUID specified does not exist.Linux Distribution: Ubuntu 18 (x86_64)
The steps I performed were the following (/dev/vda
will be my new disk):
Recovery Mode
or single user./usr
are being used:lsof /usr
mkdir -p /usr_new
pvcreate /dev/vda
vgcreate data_vg /dev/vda
lvcreate -l 100%FREE -n usr data_vg
lvdisplay
) and mount the partition to the new directory /usr_new
:mkfs.ext4 /dev/data_vg/usr
mount -t ext4 /dev/data_vg/usr /usr_new
usr
partition to the new one:cd /usr
find . -depth -print0 | cpio --null -pvdm /usr_new/
mv -v /usr /usr_old
umount /usr_new
mkdir /usr
mount -t ext4 /dev/data_vg/usr /usr
/etc/fstab
file ( add the following line to the end of the file):/dev/mapper/data_vg-usr /usr ext4 defaults 0 0
mount -a
init 6
In case you face incidents during the boot for mount incidents, take into consideration the following steps to fix / debug the issue:
e
and add the following text after the vmlinuz arguments:Press control + x, and boot from the edited option (the first one most probably)
If you are addressed to the initramfs
terminal, type the following:
initramfs > chroot /root
/etc/fstab
and made the required changes, or return the system like it was before.$ exit
initramfs > reboot