2

Background

I am creating several VirtualBox instances to use as Web development environments (Arch host/Debian guests), and I want them to share most files but have an additional drive for each machine, mounted at /mnt. This is more memory-efficient than simply cloning the entire disk, and allows me to install tools across systems without the need to reinstall on other projects that use the same programs, files, etc. I am aware of the limitation that two machines won't be able to work at the same time, but I don't switch contexts very often so it's a non-issue for me.

On each machine, for example, the /etc/hostname and /etc/hosts files are replaced with symlinks pointing to respective files on the individual drive, mounted at /mnt with some other useful directories that are specific to each project. This allows me to swap out drives for different projects while using the same, basic filesystem.

localhost ~ > ls -la /mnt/etc/host*
-rw-r--r-- 1 root root  16 Dec 10 08:04 /mnt/etc/hostname
-rw-r--r-- 1 root root 128 Jan  6 07:20 /mnt/etc/hosts
localhost ~ > ls -la /etc/host*
-rw-r--r-- 1 root root   9 Aug  7  2006 /etc/host.conf
lrwxrwxrwx 1 root root  17 Jan  5 22:36 /etc/hostname -> /mnt/etc/hostname
lrwxrwxrwx 1 root root  14 Jan  5 22:34 /etc/hosts -> /mnt/etc/hosts
-rw-r--r-- 1 root root 411 Dec  9 21:25 /etc/hosts.allow
-rw-r--r-- 1 root root 711 Dec  9 21:25 /etc/hosts.deny

Issue and Code

However, I see that my shell prompt doesn't respect the values in hostname or hosts, instead defaulting to localhost. These files need to contain different hostname values for each VM, or I'll easily lose track of which machine I'm working in. Here are the files:

/mnt/etc/hosts

# Static table lookup for hostnames.
# See hosts(5) for details.

127.0.1.1 mc-development.localdomain mc-development
127.0.0.1 localhost

/mnt/etc/hostname

mc-development

Edit 1

Here's the output of hostnamectl:

localhost ~ > hostnamectl
   Static hostname: mc-development
Transient hostname: localhost
         Icon name: computer-vm
           Chassis: vm
        Machine ID: …
           Boot ID: …
    Virtualization: oracle
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-13-amd64
      Architecture: x86-64

I thought that maybe the way I was symlinking to a different drive was the cause of the hostname definition issue, but according to this output, it is reading the files just fine. So it would appear that Fish is honoring the transient hostname when I want it to display the static hostname.

Any ideas of how to do this? What are the key differences between the two hostname types?

By the way, this shows up in Bash, too.

localhost ~ > bash
matt@localhost:~$ 

What I've Tried

I thought that maybe the order in which the drives were being mounted might make a difference because if the OS reads a link to a file that hasn't been loaded yet, it could cause an issue like this. However, changing the order of fstab entries did not help.

Edit 2

As mentioned in a comment on Ljm Dullaart's answer, copying the /usr directory didn't seem to work. My Debian box complains at boot that it is missing programs from /bin and /sbin, including /bin/init.

Here are the steps I took:

1. Boot to a live image and mount the drives, shared inside guest.

# mount /dev/sda1 /mnt
# mount /dev/sdb1 /mnt/mnt

2. Move the entire usr directory to the shared drive.

# mv /mnt/usr /mnt/mnt

3. Create the symlink.

# ln -s /mnt/mnt/usr /mnt/usr

4. Boot, and…

error message

1 Answer 1

2

No, it is not recommended.

In general, you would have a number of filesystems that can be shared (some even read-only), but the root filesystem is not one of them. The system boot process uses this filesystem.

In your case, I would share /usr an possibly /opt. You would keep separate per machine on the rootfs (sizes are on my machine; just to give you an impression of the memory use):

15M     bin
94M     boot
34M     etc
778M    lib
28M     lib64

You will probably keep separate per machine

4.9G    var

That is because logs are kept here. You should not want to mix the logging of your instances that way.

You will still be sharing

22G     usr
4.7G    opt
426G    home

As per EDIT 2_

This linking that you do will not work. You let /usr on /dev/sda1 point to /mnt/mnt/usr. Not to /usr on sdb1, but to /mnt/mnt/usr.

However, you did something more; /sbin must still be on the rootfs. The fact that it isn't there is just wrong.

What you will need to do is to re-install both machines (sorry), with exactly the same installation media and the same options.

Machine 1:

When the partition-editor appears, select a partition as follows:

  • /dev/sda1 : root filesystem (/)
  • /dev/sdb1 : /usr
  • /dev/sdb2 : /home
  • /dev/sdb3 : /opt

Then, install the lot.

When machine 1 is up and running, shut it down.

Next, for machine 2: exactly the same. Make sure that sda is it's own disk and that sdb is the shared disk.

Note that upgrade and updates will be a PITA, but as this is a virtual environment, you may choose to re-install from time to time.

____ Yet another edit _____

Because debian uses systemd, the described method is no longer possible. See https://fedoraproject.org/wiki/Features/UsrMove for an explanation why. (Fedora, but applies to all systemd-distro's)

13
  • Could I share /home as well? Jan 6, 2021 at 15:37
  • 1
    Yes. I added this to the answer. Jan 6, 2021 at 16:22
  • Hmm, copying the /usr directory didn't seem to work. My Debian box complains at boo that it is missing programs from /bin and /sbin, including /bin/init. That's a bit strange considering that I haven't touched those directories, but it makes some sense because there is quite a bit of symlinking going on already. Have you tried this before? What were your steps? I will update my question shortly to include my process. Jan 6, 2021 at 17:05
  • 1
    Yes i've done this; this is what is done when /usr is on NFS for example. Did you cp -L? Jan 6, 2021 at 17:49
  • 1
    I would suggest that you already have a number of dead links. cp -L will try to resolve the links and copy the real file instead of just the link. If it complains No such file or directory, that normally means the link may exist, but the target file of the link is gone. Jan 7, 2021 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .