Solved: High Resource Usage With External Samba Storage on Nextcloud
For photos and videos I intend to share with friends, family, or freelancers, I run my own hosting and chose Nextcloud to serve as the platform that does the heavy lifting. I’m running it on Ubuntu 20 LTS, and I use the External Storage App from the App Library in order to reference content that lives on a separate NAS on the network.
The NAS is setup to share content using Samba (SMB) in order to be friendly to the Windows 10 devices on the network that need access. I decided that rather than mount the drive locally on the Ubuntu server I was just going to use the in-platform option to connect to the specific folders I needed. It worked, I was happy, I moved on.
So What Happened?
At first it seemed fine – everything was running smoothly and I was sharing links to content when I needed to. It wasn’t too long before I started finding that the interface was sluggish, but it wasn’t enough to act on. Then one night my Plex Media Server started buffering content – old content – content that should never need buffering to play back on the local network. When I took a look, I found that the NAS was pegged. It was trying to handle dozens of SMB sessions, many of which that were open for days. Nearly 90% of the Atom Processor’s efforts were dedicated to maintaining these sessions.
What Caused It?
The smbclient running on the server was not playing well with Nextcloud. When initiated, the client would remain open regardless of activity in the session, and a new session would be added on top as soon as a new browser interacted with an external resource.
Get To The Fix!
Hopefully this is where you can save some time vs. my trial and error journey through this. I dug through the following articles:
- Adding external storage to NC causes 100% CPU
- Extreme CPU load and slow operation with external storage SMB/CIFS
- External storage, high CPU load on NAS
- How to Fix Nextcloud 19, Nextcloud 20, Nextcloud 21, Nextcloud 22 SMB/SAMBA/Windows Share error on Ubuntu 20.04.1 LTS
The first articles agreed that replacing the smbclient with the php version was the way to go, but it was the fourth article that gave me what I needed to do that successfully. Once the updated client was in place I restarted Apache, but the former sessions persisted and rather than hunt I opted to just restart the entire server.
To solve the issue, we’re going to add a specific Personal Package Archive (PPA) to the Ubuntu VM running Nextcloud. Note: if you run other systems alongside Nextcloud this may not be the best solution for you. I personally like to keep my services segregated, so solving this does not inadvertently harm some other service provided by the same server.
We’re going to tack the PHP PPA by Ondřej Surý onto the acceptable php package list:
sudo echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/php.list
Next we’ll import the keys to consider this a signed package:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C
Then we’ll update the list of available software:
sudo apt update
Finally, we’ll install the php-smbclient we’re after:
sudo apt install php-smbclient
Restart Apache to have the change take effect for new sessions:
sudo systemctl restart apache2
Following this adjustment, I’ve seen basically no load caused either on the Nextcloud server or on the NAS by the ongoing SMB connections. Huzzah!