Is it safe to do symlinks for the entire machine?
I have a virtual machine with limited amount of storage which has become a problem. I have my home directory linked to NFS storage on a server and it has been functioning without a problem. But I now need to link my entire machine to the NFS as well as I am constantly running low on storage!
I am only wondering whether it is safe to link the entire machine (that is to say, the entire: / ) to the NFS?
I want to use the following commands to do that:
/storage/nfs
is basically my mounted NFS.
$ mkdir /storage/nfs/myvmroot
$ sudo cp -rp /* /storage/nfs/myvmroot
$ sudo mv /* /old-root
$ sudo ln -s /storage/nfs/myvmroot /
You cannot make /
a symbolic link. It just isn't possible. /
has to be a directory. If /
was a symbolic link, it would point to some path — and to resolve that path, the system would go through /
, which is a symlink so needs to be resolved... A symlink that points under itself will always cause an error when it's dereferenced, but in any case, you won't be able to make /
a symlink since it's already an existing directory.
What you can do, and will achieve the effect you wanted, is to mount an NFS filesystem as the root filesystem, instead of mounting a local filesystem as is usually done. This is possible and supported by Ubuntu. There's a tutorial on the Ubuntu community wiki (it covers a complete diskless installation, you can skip the parts about TFTP if you keep /boot
locally). Moving an existing system is a bit different from doing the initial installation, so don't expect to follow the tutorial to the letter.
You need to ensure that the initramfs contains the necessary drivers. The initramfs is generated by the command update-initramfs
based on the current system configuration and on the configuration files under /etc/initramfs-tools
. You'll need to explicitly add NFS settings to /etc/initramfs-tools/initramfs.conf
, at least add a line BOOT=nfs
(I think the other parameters can be specified at boot time, but BOOT=nfs
is necessary in order to include the NFS drivers, and DEVICE=…
may be necessary as well to include the Ethernet driver). Once you've modified /etc/initramfs-tools/initramfs.conf
, run update-initramfs
.
I recommend keeping /boot
as a local partition, separate from /
, if it isn't already.
Do not use cp -rp /* /storage/nfs/myvmroot
to copy files, that would not work. You need to exclude mounted filesystems from the copy. Pass the -x
option. Also, use -a
rather than -rp
, there are attributes that -rp
doesn't preserve.
cp -ax /* /storage/nfs/myvmroot
or, in case the copy gets interrupted
rsync -axAX / /storage/nfs/myvmroot
Another, probably easier option is to keep the root filesystem local, and only move part of it. You'll generally get significantly better performance and reliability from a local filesystem than from NFS, even with a fast server and a fast network.
You can't just cut anywhere: what you put remotely must not be needed too early in the boot process. I think cutting at /usr
is not officially supported by Ubuntu these days, but it is by Ubuntu's parent distribution Debian, so it has a chance of working. Another place that has a good chance of working is /usr/share
. I don't recommend /var
, it's likely to be a hassle if anything breaks since that's where logs are kept. (If the NFS client is unhappy about something and it tries to write logs to /var/log
but /var
isn't reponding, good luck diagnosing the failure.)
No. That would render your virtual machine unbootable. The NFS system is loaded at a certain point in the booting process. Before this point, no files on the NFS will be accessible. This means that the machine won't be able to access the files it needs in order to boot. These include (but are not limited to) the kernel itself, the default shell /bin/sh
and everything under /bin
which includes the commands needed in order to mount the NFS share.
So no, this is not a good idea. More importantly, it isn't necessary. Most of the directories in /
are not very big. Just identify the one that is taking up the space and put that one there. You should be able to move either of the following with no major issues:
/var/cache
/usr/local
-
/usr
itself can also usually be moved but make sure nothing essential is in/usr/local/bin
. It probably won't be, but check.
Those are probably the ones taking up the space anyway.