Move /usr folder back to / partition

I have 2 partitions - /dev/raid/r0p2 and /dev/raid/r0p3 mounted as / and /usr

Code:
Filesystem        Size    Used   Avail Capacity  Mounted on
/dev/raid/r0p2     48G    1.2G     43G     3%    /
devfs             1.0K      0B    1.0K     0%    /dev
/dev/raid/r0p1    260M    1.3M    259M     1%    /boot/efi
/dev/raid/r0p3    174G     12G    148G     7%    /usr

I would like to move the /usr folder back to the / partition - everything but the /usr/opt/ folder.

After doing so, I would like to change the mount point on /dev/raid/r0p3 from /usr to /usr/opt so that folder will continue to be read and write on the /dev/raid/r0p3 partition.

Is this possible?
 
Should be possible.
Beware! You should backup the whole system first.
And if you have enough spare space in both raid partitions, all "move (mv)" in the examples below SHALL be altered by "copy (cp)" and rm after successful reboot.
And more, you'd better performing the operations on single user mode and use equivalent commands under /rescue to be safe.
Finally, I've not actually tested, so you need to confirm what should happen on each step well before actually doing the moves.

  1. If needed, temporarily add path of root for temporary directories.
  2. Copy /usr/ to, for example, /usr2. It should be in /dev/raid/r0p2.
  3. Edit fstab entry for /usr to /usr/opt2.
  4. Rename /usr to /opt2.
  5. Rename /usr2 to /usr.
  6. Create /usr/opt2 as temporary mount point.
  7. Reboot and confirm /usr is in /dev/raid/r0p2 and old /usr is in /usr/opt2/opt2.
  8. Remove all contents of /usr/opt2.
  9. Move /usr/opt to now-empty /usr/opt2.
  10. Remove now-empty /usr/opt.
  11. Rename /usr/opt2 to /usr/opt.
  12. Edit fstab entry for /usr/opt2 to /usr/opt.
  13. Reboot.
Of course, there is more efficient way. But it would be harder to confirm on each step. (Easily confused.)
 
Thank you for all your help. I do have a couple of questions though.

For Step 6 : Create /usr/opt2 as temporary mount point --- on which partition? I thought you could only mount one directory per partition.

For Step 7 : I'm assuming the last /usr/opt2/opt2 was a typo?
 
At step 6, /usr is a copy of the original /usr, but resident in the root. Since /usr is now in the root, the directory (mount point) to be created, /usr/opt2, will also be in the root.

At step 7, /usr/opt2/opt2 has the stutters. It should be /usr/opt2.

I always change mount point directories to mode 111 at the time the mount point is made. It has no operational impact, but makes unmounted fie systems much easier to spot when trouble-shooting.

Be very careful about how you copy (parts of) file systems, most especially when the root is involved. It's very easy to lose hard links and special files. You will generally need to use rsync(1), tar(1), cpio(1), or pax(1) (as root). To recursively copy the contents of directory /rooted/path/to/source to the directory /rooted/path/to/target (both must exist) with rsync:
Code:
mkdir -p /rooted/path/to/target
rsync -SHAXax /rooted/path/to/source/ /rooted/path/to/target/    # NOTE: trailing slash on source and target
The same thing, but using cpio:
Code:
mkdir -p /rooted/path/to/target
cd /rooted/path/to/source
find . -depth -print | cpio -pdmu /rooted/path/to/target
 
For Step 6 : Create /usr/opt2 as temporary mount point --- on which partition? I thought you could only mount one directory per partition.
On /dev/raid/r0p2 (root) partition. As partitions other than root partition (/) mounted to someware in root partition needs directory to be mounted to in root partition. You'd better drawing charts for each step. I usuall draw in papers, as most of apps for drawing cannot be used in single user mode.

I.e., initially,
Code:
+--------------------+
|         /+           |   (/dev/raid/r0p2)
|           +usr     |
|            .  +-----+
|            .  |  +----------------+
+------------+ |   /+             |  (/dev/raid/r0p3)
                   |     +bin       |
                   |     +opt      |
                   |     .             |
                   |     .             |
                  +-----------------+

this case,, root directory of /dev/raid/r0p3 partition is mounted to /usr in /dev/raid/r0p2 partition.

For Step 7 : I'm assuming the last /usr/opt2/opt2 was a typo?
Not 100% sure, as I've not tested. Temporarily unmounting the partition and mount it to, for example, /mnt would suggest correct result, but it's unsafe to blindly try on multiuser (usual) mode for /usr. Maybe I'd been too paranoid.

If you have an installer media (memstick, cdrom,....) that is at the same version as running environment, the safest is booting from it to manipulating partitions. My example assume that you don't have any other way (installer media, another [non-listed] rescue partitions,...).

And doing changes on singleuser mode would need remounting root partition as rw and mounting /usr manually, because in single user mode, root partition is mounted as ro and partitions other than root are not mounted automatically (these are usually done automatically on starting multiuser mode). Until mounting /usr, any commands under /usr (/usr/bin/*, /usr/sbin/* and so on) cannot be used, thus, /rescue exists.

Edit: Fixed the figure, but still not perfect as fixed width font cannot be chosen.
 
Should I move a copy of /usr/local/bin/rsync in to /rescue?

Am I missing anything?

In single-user mode, remount the root filesystem as read-write
/rescue/mount -uw /

Create the/usr2 dir
/rescue/mkdir /usr2

Copy the data from /usr to /usr2 except for the /opt directory (It's 20GB big - restore it later from nightly backup)
/usr/local/bin/rsync -SHAXax --exclude=/usr/opt /usr/ /usr2/

Edit fstab entry for /usr and change it to /usr/opt2
/rescue/vi /etc/fstab

Rename /usr to /usr-old
/rescue/mv /usr /usr-old

Rename /usr2 to /usr
/rescue/mv /usr2 /usr

reboot

After confirming everything is working, remove the /usr-old directory
/rescue rm -rf /usr-old

Empty the /usr/opt2 directory
/rescue rm -rf /usr/opt2

Create the /opt dir in /usr
/rescue mkdir /usr/opt

Edit fstab entry for /usr/opt2 and change it to /usr/opt
/rescue/vi /etc/fstab

reboot
 
Should I move a copy of /usr/local/bin/rsync in to /rescue?
No, the executables in /rescue are statically linked and do not dynamically link to runtime libraries. Rsync needs lots of dynamic libraries in specific locations (which won't be availabe if /usr is not mounted in the correct location:
Code:
[tulloch.141] $ ldd /usr/local/bin/rsync
/usr/local/bin/rsync:
    libz.so.6 => /lib/libz.so.6 (0x20f8a167b000)
    libiconv.so.2 => /usr/local/lib/libiconv.so.2 (0x20f8a202a000)
    liblz4.so.1 => /usr/local/lib/liblz4.so.1 (0x20f8a1067000)
    libzstd.so.1 => /usr/local/lib/libzstd.so.1 (0x20f8a3712000)
    libxxhash.so.0 => /usr/local/lib/libxxhash.so.0 (0x20f8a21fa000)
    libcrypto.so.30 => /lib/libcrypto.so.30 (0x20f8a2476000)
    libc.so.7 => /lib/libc.so.7 (0x20f8a4830000)
    libthr.so.3 => /lib/libthr.so.3 (0x20f8a4390000)
    [vdso] (0x20f8a05d4000)
Your best approach is probably to use tar(1) in /rescue. The man page suggests:
Code:
# To move file hierarchies, invoke tar as
tar -cf - -C srcdir . | tar -xpf - -C destdir
# or more traditionally
cd srcdir ; tar -cf - . | (cd destdir ; tar -xpf -)
Sorry, I don't have time right now to comment on other aspects of the plan.
 
Should I move a copy of /usr/local/bin/rsync in to /rescue?

Am I missing anything?

In single-user mode, remount the root filesystem as read-write
/rescue/mount -uw /

Create the/usr2 dir
/rescue/mkdir /usr2

Copy the data from /usr to /usr2 except for the /opt directory (It's 20GB big - restore it later from nightly backup)
/usr/local/bin/rsync -SHAXax --exclude=/usr/opt /usr/ /usr2/

Edit fstab entry for /usr and change it to /usr/opt2
/rescue/vi /etc/fstab

Rename /usr to /usr-old
/rescue/mv /usr /usr-old

Rename /usr2 to /usr
/rescue/mv /usr2 /usr

reboot

After confirming everything is working, remove the /usr-old directory
/rescue rm -rf /usr-old

Empty the /usr/opt2 directory
/rescue rm -rf /usr/opt2

Create the /opt dir in /usr
/rescue mkdir /usr/opt

Edit fstab entry for /usr/opt2 and change it to /usr/opt
/rescue/vi /etc/fstab

reboot
You would need to mount /usr after remounting root filesystem (/).
Or mount -a instead to mount all filesystems not yet mounted, if any and you want.
This is because single user mode mounts root partition only (do not handle /etc/fstab automatically).

If you prefer copy (cp) over move (mv), you can copy on multi user mode.
Because old /usr keeps on existing.
cp should be invoked with option -a to keep attributes/ownerships and directory structures.

If you don't hesitate to copy/move per files/directories directly under /usr (other than /usr/opt), you don't need excessive transfers of /usr/opt.
And this way, after confirming new /usr and deleted /usr/opt2/* other than /usr/opt2/opt per-files/directories, you can move the contents of /usr/opt2/opt to /usr/opt2.

And finally, on deleting contents of /usr/opt2/ other than /usr/opt2/opt, possibly you'll need
chflags -R noschg <target-directorys>
if any remnants remains after deletion.
This is because some files could have immutable flag, causing fail to delete.
If I recall correctly, if you have ports tree on /usr/ports and installed ports-mgmt/sccache-overlay, something remained uncleaned under /usr/ports/ports-mgmt/work. (Possibly fixed now, though.)
 
Back
Top