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

  1. An Alibaba Cloud OSS bucket (already created).
  2. AccessKey with OSS access permissions (AccessKey ID and AccessKey Secret).
  3. 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 --version

For 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 --version

4. Configuring Access Credentials

  1. Create the credential configuration file:
echo <your-bucket-name>:<your-access-key-id>:<your-access-key-secret> | sudo tee /etc/passwd-ossfs

Example:

echo my-bucket:LTAI5t1234567890:abcdefghijklmnopqrstuvwxyz1234567890 | sudo tee /etc/passwd-ossfs
  1. Set file permissions:
sudo chmod 640 /etc/passwd-ossfs

5. Mounting the OSS Bucket

  1. Create a local mount point directory:
sudo mkdir /mnt/ossfs
  1. Execute the mount command:
sudo ossfs <your-bucket-name> <mount-point> -ourl=<your-oss-endpoint> -o allow_other

Parameter 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
  1. Verify the mount:
df -h

You should see output similar to:

ossfs      256T     0  256T   0% /mnt/ossfs

6. Advanced Configuration Options

  1. Use -oumask to set file permissions:
sudo ossfs my-bucket /mnt/ossfs -ourl=http://oss-cn-hangzhou.aliyuncs.com -o allow_other -oumask=007
  1. 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
  1. 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=1024

7. Setting Up Automatic Mount at Boot

  1. Edit the /etc/fstab file:
sudo vim /etc/fstab
  1. Add the following line:
<your-bucket-name> <mount-point> fuse.ossfs _netdev,url=<your-oss-endpoint>,allow_other 0 0

Example:

my-bucket /mnt/ossfs fuse.ossfs _netdev,url=http://oss-cn-hangzhou.aliyuncs.com,allow_other 0 0
  1. Test the fstab configuration:
sudo mount -a
  1. Remount (if needed):
sudo mount -a
sudo systemctl daemon-reload
sudo systemctl restart local-fs.target

8. Unmounting the OSS Bucket

sudo umount /mnt/ossfs

If encountering a “device is busy” error, force unmount:

sudo fusermount -u /mnt/ossfs

9. Common Issues and Solutions

  1. Mount fails: permission denied

    • Check that /etc/passwd-ossfs file permissions are 640.
    • Ensure the AccessKey is correct.
  2. File write fails

    • Check if the bucket has the correct permission policies.
    • Try using the -o allow_other option.
  3. Performance issues

    • Consider enabling caching.
    • For frequently accessed files, set a larger cache.
  4. Connection timeout

    • Check network connectivity.
    • Try using the internal endpoint (e.g., oss-cn-hangzhou-internal.aliyuncs.com).

10. Important Notes

  1. Certain operations (e.g., random writes, append writes) may have performance issues.
  2. Regularly check for and upgrade to the latest version of ossfs.
  3. For critical data, maintain local backups.