Saturday, May 21, 2022

Truenas Scale, NFS, and Nextcloud

After migrating from Truenas Core to Truenas Scale, I had been wanting to set up Nextcloud again. It was supposed to be as easy as installing the Nextcloud app from Truenas Scale's official app catalog... but that did not turn out so. More about my long journey at the end of the post. Meanwhile, here is what actually worked.

The basic concept is to set up a NFS share on Truenas Scale, mount that share on a VM (which hosts most of my services like PiHole and Portainer), and run Nextcloud on that VM as a snap. (Got most of the instructions from here.)

First, setting up the NFS share.
1a. Create user. In Truenas Scale, first create a new user under Credentials -> Local Users. Enter the full name, username, and password, and make sure "New Primary Group" is checked. For example, let's call this new user ncuser.
1b. Create dataset. Under Storage, create a new dataset under the mainpool. Edit permissions and make sure the owner and group for this new dataset (let's call it ncdata) is set to ncuser.
1c. Create share. Under Shares, add a new UNIX (NFS) share. Set the path to where ncdata is (in my case, /mnt/mainpool/ncdata). Under Advanced Options, set Mapall User to ncuser and Mapall Group to ncuser. Then save the share and enable NFS shares.
 
Second, set up the VM's requirements.
2a. Install nfs-common package.
sudo apt install nfs-common
2b. Prepare the mount point. Create a new directory to mount the share and make sure it is owned by root with the correct permissions.
mkdir -p /media/nc
sudo chown -R root:root /media/nc
sudo chmod 0770 /media/nc
2c. Enable automount. Edit /etc/fstab and add in the entry for the NFS to automount. In this example, I assume my Truenas server's IP address is 192.168.0.20.
sudo nano /etc/fstab
Then, add this single line at the end of the file
192.168.0.20:/mnt/mainpool/ncdata /media/nc nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
2d. Test mount. Make sure it works by mounting the share and creating a file in the share.
sudo mount /media/nc
touch /media/nc/testing.txt
ls /media/nc
(it should show testing.txt)
 
Third, set up Nextcloud using snap.
3a. Install Nextcloud as a snap.
sudo snap install nextcloud
3b. Connect the snap to removable-media so that it can access /media directory.
sudo snap connect nextcloud:removable-media
3c. Edit configuration.
sudo nano /var/snap/nextcloud/current/nextcloud/config/autoconfig.php
and updated the directory variable to
'/mnt/nextcloud' (including the single quotation marks).
3d. Set a different port. This is optional; the default is port 80. I usually set it to something else because I have nginx running as a reverse proxy with SSL; nginx will act as the SSL frontend with the outside world and direct everything to this port (which, in this example, is 8123). Setting up nginx is another monster which I will not cover here.
sudo snap set nextcloud ports.http=8123
3e. Restart the Nextcloud PHP Service.
sudo snap restart nextcloud.php-fpm
 
This should be it. Almost there. All that remains is to actually enter the web interface. For example, my VM's IP address is 192.168.0.30, so use a web browser and go to
http://192.168.0.30:8123/
This will start the setup process, when you create an admin user with a password. If you get an error about setting folder permission to 0770, just reload the page.
 
You may need to edit /var/snap/nextcloud/current/nextcloud/config/config.php and add your IP addresses to the trusted_domains if you see some error about this.

Okay, so what didn't work?
First, I tried to install the Nextcloud app from the official apps catalog of Truenas Scale. The container will deploy, but when I try to go to the web UI, it will prompt me for the login name and password (which is set as part of the deployment process). However, it would not log in, and I am dumped back at the login page again, and again, and again. So I deleted this container.

So I tried using the Truecharts catalog. After adding the Truecharts catalog following the instructions here, I waited until the catalog is downloaded. It took a while. Then, I tried to install the Truecharts catalog's Nextcloud app... but in the end, it has the same symptom as the official app catalog. So I deleted this container and the catalog, because Truecharts catalog is RESOURCE HUNGRY. Unless you like your CPU ramping to 100% every once in a while when it refreshes the catalog, don't both with Truecharts. They packed EVERYTHING into a single catalog, when it would have been more efficient to split into several smaller catalogs and let users choose which one they really need.

Next was to see if I can use Portainer with a SMB share... This experiment didn't work out well because trying to let a docker container access a SMB share gave me a bigger headache trying to figure out how to make the permissions (non-existent on SMB...) work. I tried using the normal nextcloud docker image and the nextcloud:all-in-one image, both did not work for me. So in the end, I went with the snap way.

It is also possible to use the NFS share with a dedicated VM running Nextcloud directly (which means setting up your own database and apache server and such) but the snap version is a lot less hustle.

No comments: