Using ossfs to Mount Aliyun OSS to Linux Server
2 min
Running Out of Cloud Server Disk Space? Try OSSFS
As a cloud server user, do you often face the headache of insufficient disk space? Every expansion requires a server reboot—both troublesome and disruptive to business continuity. Actually, Alibaba Cloud OSS combined with the OSSFS tool can solve this problem perfectly!
Why Choose OSSFS?
- No Downtime for Expansion: Gain massive storage instantly by mounting.
- Lower Cost: Storage fees are only 1/5 of those for cloud disks.
- Simple to Use: Use OSS storage just like a local directory.
- Multi-Server Sharing: Multiple servers can mount the same bucket simultaneously.
- Internal Network Mounting: Mount via internal network from Alibaba Cloud servers with zero traffic fees.
1. Introduction to ossfs
ossfs is a FUSE-based filesystem tool that allows users to mount an Alibaba Cloud OSS bucket to the local Linux filesystem, enabling object manipulation in OSS as if they were local files. It is suitable for scenarios requiring POSIX filesystem access.
2. Prerequisites
- An Alibaba Cloud OSS bucket (already created).
- AccessKey with OSS access permissions (AccessKey ID and AccessKey Secret).
- A Linux server (CentOS 7+ or Ubuntu 16.04+ recommended).
3. Installing ossfs
For CentOS/RHEL Systems
# Download the ossfs installation package
sudo wget http://gosspublic.alicdn.com/ossfs/ossfs_1.80.6_centos7.0_x86_64.rpm
# Install dependencies
sudo yum install -y fuse
# Install ossfs
sudo rpm -ivh ossfs_1.80.6_centos7.0_x86_64.rpm
# Verify installation
ossfs --versionFor Ubuntu/Debian Systems
# Download the ossfs installation package
sudo wget http://gosspublic.alicdn.com/ossfs/ossfs_1.80.6_ubuntu16.04_amd64.deb
# Install
sudo apt-get update
sudo apt-get install -y fuse
sudo dpkg -i ossfs_1.80.6_ubuntu16.04_amd64.deb
# Verify installation
ossfs --version4. Configuring Access Credentials
- Create the credential configuration file:
echo <your-bucket-name>:<your-access-key-id>:<your-access-key-secret> | sudo tee /etc/passwd-ossfsExample:
echo my-bucket:LTAI5t1234567890:abcdefghijklmnopqrstuvwxyz1234567890 | sudo tee /etc/passwd-ossfs- Set file permissions:
sudo chmod 640 /etc/passwd-ossfs5. Mounting the OSS Bucket
- Create a local mount point directory:
sudo mkdir /mnt/ossfs- Execute the mount command:
sudo ossfs <your-bucket-name> <mount-point> -ourl=<your-oss-endpoint> -o allow_otherParameter explanation:
<your-bucket-name>: Your OSS bucket name.<mount-point>: Local mount directory (e.g.,/mnt/ossfs).<your-oss-endpoint>: OSS access endpoint (e.g.,http://oss-cn-hangzhou.aliyuncs.com).
Example:
sudo ossfs my-bucket /mnt/ossfs -ourl=http://oss-cn-hangzhou.aliyuncs.com -o allow_other- Verify the mount:
df -hYou should see output similar to:
ossfs 256T 0 256T 0% /mnt/ossfs6. Advanced Configuration Options
- Use
-oumaskto set file permissions:
sudo ossfs my-bucket /mnt/ossfs -ourl=http://oss-cn-hangzhou.aliyuncs.com -o allow_other -oumask=007- Enable caching to improve performance:
sudo mkdir /tmp/ossfs_cache
sudo ossfs my-bucket /mnt/ossfs -ourl=http://oss-cn-hangzhou.aliyuncs.com -o allow_other -ouse_cache=/tmp/ossfs_cache- Set memory cache size (in MB):
sudo ossfs my-bucket /mnt/ossfs -ourl=http://oss-cn-hangzhou.aliyuncs.com -o allow_other -ouse_cache=/tmp/ossfs_cache -omax_stat_cache_size=1000 -oensure_diskfree=10247. Setting Up Automatic Mount at Boot
- Edit the
/etc/fstabfile:
sudo vim /etc/fstab- Add the following line:
<your-bucket-name> <mount-point> fuse.ossfs _netdev,url=<your-oss-endpoint>,allow_other 0 0Example:
my-bucket /mnt/ossfs fuse.ossfs _netdev,url=http://oss-cn-hangzhou.aliyuncs.com,allow_other 0 0- Test the fstab configuration:
sudo mount -a- Remount (if needed):
sudo mount -a
sudo systemctl daemon-reload
sudo systemctl restart local-fs.target8. Unmounting the OSS Bucket
sudo umount /mnt/ossfsIf encountering a “device is busy” error, force unmount:
sudo fusermount -u /mnt/ossfs9. Common Issues and Solutions
Mount fails: permission denied
- Check that
/etc/passwd-ossfsfile permissions are 640. - Ensure the AccessKey is correct.
- Check that
File write fails
- Check if the bucket has the correct permission policies.
- Try using the
-o allow_otheroption.
Performance issues
- Consider enabling caching.
- For frequently accessed files, set a larger cache.
Connection timeout
- Check network connectivity.
- Try using the internal endpoint (e.g.,
oss-cn-hangzhou-internal.aliyuncs.com).
10. Important Notes
- Certain operations (e.g., random writes, append writes) may have performance issues.
- Regularly check for and upgrade to the latest version of ossfs.
- For critical data, maintain local backups.