For AI agents: the complete documentation index is available at https://www.tteam.icu/zh/llms.txt, the full documentation bundle is available at https://www.tteam.icu/zh/llms-full.txt, and this page is available as Markdown at https://www.tteam.icu/zh/ops/cloudservice/tencent-cosfs.md.

腾讯云 cosfs

AI 总结AI 生成

核心内容:介绍腾讯云轻量应用服务器官方云助手安装 COSFS 的方式,并提供从官方流程提取的可复用挂载脚本。

内容要点:

  • 官方云助手 COSFS 安装入口
  • 从官方流程提取的可复用挂载脚本
  • 脚本支持的系统版本和参数配置
  • 公开桶、私有桶及开机自动挂载注意事项

内容偏向可直接复用的运维操作,涵盖命令、配置顺序和部署注意事项。

cosfs 官方文档

轻量应用服务器自动化助手文档

支持范围

Info

腾讯云轻量应用服务器目前只在云助手(自动化助手 TAT)中提供 COSFS 安装入口。为方便复用,本文从该安装流程中提取并整理了挂载脚本;脚本也可以在系统满足下方版本限制时自行使用。

脚本会根据 /etc/os-release 判断系统版本,目前只覆盖以下 Linux 系统:

系统脚本支持的版本
Ubuntu18、20、22、24、26
CentOS7、8、9
Debian10、11、12、13
OpenCloudOS8、9

不在此范围内的系统会直接退出。官方 COSFS 虽然还支持 macOS 和 TencentOS,但本文脚本没有对应分支,不能直接套用。CentOS 9 分支使用的是 x86_64 的 fuse-devel 安装包,其他 CPU 架构也不应直接运行。

如果通过云助手执行,轻量应用服务器的 Linux 实例需要安装自动化助手客户端。2020 年 12 月 15 日之后使用应用模板、系统镜像或自定义镜像创建的实例通常已预装;如果没有,请先参考上方自动化助手文档安装客户端。

配置参数

可以打开轻量应用服务器控制台的自动化助手,创建命令并粘贴下方脚本;也可以在满足系统版本要求的 Linux 实例中自行复用。执行前修改脚本顶部的参数:

  • BucketName:COS 存储桶名称,格式为 BucketName-APPID
  • CosRegion:存储桶所在地域简称,例如 ap-shanghai
  • MountPoint:本地挂载目录,默认 /oss
  • CacheDir:COSFS 缓存目录,默认 /var/tmp/cosfs
  • PublicBucket:公开读存储桶保持为 1;私有桶改为 0

挂载目录必须为空,脚本不会覆盖其中已有的文件。CacheDir 所在磁盘需要保留足够空间,脚本通过 -oensure_diskfree=10240 预留约 10 GB。

挂载脚本

脚本固定使用 COSFS 1.0.21,会安装编译依赖、编译 COSFS、挂载到 /oss,并把挂载命令追加到 rc.local。

mount_cosfs.sh
#!/bin/bash

# 本脚本从腾讯云轻量应用服务器官方云助手安装流程中提取,可按需复用。
BucketName="BucketName-APPID"
CosRegion="ap-shanghai"
MountPoint="/oss"
CacheDir="/var/tmp/cosfs"
PublicBucket=1
CosfsVersion="1.0.21"
CosfsRoot="/usr/cosfs/cosfs-${CosfsVersion}"
CosEndpoint="http://cos.${CosRegion}.myqcloud.com"

if [[ $EUID -ne 0 ]]; then
    echo "请使用 root 权限执行"
    exit 1
fi

if [[ "$BucketName" == "BucketName-APPID" || -z "$CosRegion" ]]; then
    echo "请先修改 BucketName 和 CosRegion"
    exit 1
fi

install_cosfs() {
    echo -e "`date +%T` to download cosfs"
    curl -v "http://cos-sdk-archive-1253960454.cos-internal.accelerate.tencentcos.cn/cosfs/v${CosfsVersion}/cosfs-${CosfsVersion}.tar.gz" -e cloud.tencent.com -o "cosfs-${CosfsVersion}.tar.gz" > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` download cosfs failed"
        exit 10
    fi
    echo -e "`date +%T` download cosfs success"
    mkdir -p /usr/cosfs
    tar -zxf "cosfs-${CosfsVersion}.tar.gz" -C /usr/cosfs
    if [[ $? -ne 0 ]]; then
        rm -rf /usr/cosfs
        rm -rf "cosfs-${CosfsVersion}.tar.gz"
        echo -e "untar cosfs failed"
        exit 10
    fi
    echo -e "`date +%T` tar cosfs success"
    rm -rf "cosfs-${CosfsVersion}.tar.gz"

    cd "${CosfsRoot}"
    ./autogen.sh > /dev/null 2>&1
    ./configure > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` cosfs configure failed"
        cd /root
        rm -rf /usr/cosfs
        exit 3
    fi
    echo -e "configure cosfs success"
    make > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` make cosfs failed"
        cd /root
        rm -rf /usr/cosfs
        exit 3
    fi
    echo -e "`date +%T` make cosfs success"
    make install > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` make install cosfs failed"
        cd /root
        rm -rf /usr/cosfs
        exit 3
    fi
    echo -e "`date +%T` make install cosfs success"
    cd /root
    rm -rf /usr/cosfs
}

install_centos() {
    version=$1
    if [[ $version == "9" ]]; then
        yum install -y automake gcc-c++ git libcurl-devel libxml2-devel make openssl-devel fuse fuse-libs > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` install dependency failed"
            exit 3
        fi
        echo -e "`date +%T` install dependency success"
        curl -v "http://cos-sdk-archive-1253960454.cos-internal.accelerate.tencentcos.cn/cosfs/dependency/fuse-devel-2.9.9-16.el9.x86_64.rpm" -e cloud.tencent.com -o fuse-devel-2.9.9-16.el9.x86_64.rpm > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` download fuse-devel failed"
            exit 3
        fi
        echo -e "`date +%T` download fuse-devel success"
        rpm -ivh fuse-devel-2.9.9-16.el9.x86_64.rpm
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` install fuse-devel failed"
            exit 3
        fi
        echo -e "`date +%T` install fuse-devel success"
        rm -rf fuse-devel-2.9.9-16.el9.x86_64.rpm
    else
        yum install -y automake gcc-c++ git libcurl-devel libxml2-devel fuse-devel make openssl-devel fuse > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` install dependency failed"
            exit 3
        fi
        echo -e "`date +%T` install dependency success"
    fi
    install_cosfs
}

install_ubuntu() {
    version=$1
    set="22"
    if [[ "$set" == *"$version"* ]]; then
        apt-get update > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` apt-get update failed"
            exit 3
        fi
        apt-get install -y automake autotools-dev g++ git libfuse-dev libxml2-dev make pkg-config fuse > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` install dependency failed"
            exit 3
        fi
        DEBIAN_FRONTEND=noninteractive apt-get install -y libcurl4-gnutls-dev libssl-dev > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` DEBIAN_FRONTEND=noninteractive install dependency failed"
            exit 3
        fi
    else
        if [[ "20 24" == *"$version"* ]]; then
            apt-get update > /dev/null 2>&1
            if [[ $? -ne 0 ]]; then
                echo -e "`date +%T` apt-get update failed"
                exit 3
            fi
            echo -e "`date +%T` apt-get update success"
        fi
        apt-get install -y automake autotools-dev g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config fuse > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            echo -e "`date +%T` install dependency failed"
            exit 3
        fi
    fi
    echo -e "`date +%T` install dependency success"
    install_cosfs
}

install_debian() {
    apt-get update > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` apt-get update failed"
        exit 3
    fi
    apt-get install -y automake autotools-dev g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config fuse curl > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` install dependency failed"
        exit 3
    fi
    echo -e "`date +%T` install dependency success"
    install_cosfs
}

install_opencloudos() {
    yum install -y automake gcc-c++ git libcurl-devel libxml2-devel fuse-devel make openssl-devel fuse > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` install dependency failed"
        exit 3
    fi
    echo -e "`date +%T` install dependency success"
    install_cosfs
}

to_install() {
    if [[ -f /etc/os-release ]]; then
        os=`grep "^ID=" /etc/os-release | awk -F"ID=" '{print $2}'`
        version=`grep "^VERSION_ID=" /etc/os-release | awk -F"VERSION_ID=" '{print $2}'`
    elif type lsb_release > /dev/null 2>&1; then
        os=$(lsb_release -si)
        version=$(lsb_release -sr)
    elif [[ -f /etc/lsb-release ]]; then
        os=`grep "^DISTRIB_ID=" /etc/lsb-release | awk -F"DISTRIB_ID=" '{print $2}'`
        version=`grep "^DISTRIB_RELEASE=" /etc/lsb-release | awk -F"DISTRIB_RELEASE=" '{print $2}'`
    elif [[ -f /etc/debian_version ]]; then
        os=debian
        version=$(cat /etc/debian_version)
    else
        echo -e "`date +%T` Unsupported operating system."
        exit 1
    fi
    os=${os,,}
    version=${version//\"/}
    echo -e "`date +%T` Your os is $os."
    echo -e "`date +%T` Your distribute is $version."

    if [[ "$os" == *"ubuntu"* ]]; then
        set="18 20 22 24 26"
        version=`echo $version | awk -F"." '{print $1}'`
        if [[ "$set" == *"$version"* ]]; then
            install_ubuntu "$version"
        else
            echo -e "`date +%T` Unsupported os version"
            exit 7
        fi
    elif [[ "$os" == *"centos"* ]]; then
        set="7 8 9"
        if [[ "$set" == *"$version"* ]]; then
            install_centos "$version"
        else
            echo -e "`date +%T` Unsupported os version"
            exit 7
        fi
    elif [[ "$os" == *"debian"* ]]; then
        set="10 11 12 13"
        version=`echo $version | awk -F"." '{print $1}'`
        if [[ "$set" == *"$version"* ]]; then
            install_debian "$version"
        else
            echo -e "`date +%T` Unsupported os version"
            exit 7
        fi
    elif [[ "$os" == *"opencloudos"* ]]; then
        set="8 9"
        version=`echo $version | awk -F"." '{print $1}'`
        if [[ "$set" == *"$version"* ]]; then
            install_opencloudos "$version"
        else
            echo -e "`date +%T` Unsupported os version"
            exit 7
        fi
    else
        echo "`date +%T` Unsupported Linux distribution."
        exit 1
    fi
}

cosfs --version > /dev/null
if [[ $? -ne 0 ]]; then
    echo "`date +%T` cosfs does not installed, to install cosfs"
    to_install
fi

if [[ -f /etc/os-release ]]; then
    os=`grep "^ID=" /etc/os-release | awk -F"ID=" '{print $2}'`
    version=`grep "^VERSION_ID=" /etc/os-release | awk -F"VERSION_ID=" '{print $2}'`
elif type lsb_release > /dev/null 2>&1; then
    os=$(lsb_release -si)
    version=$(lsb_release -sr)
elif [[ -f /etc/lsb-release ]]; then
    os=`grep "^DISTRIB_ID=" /etc/lsb-release | awk -F"DISTRIB_ID=" '{print $2}'`
    version=`grep "^DISTRIB_RELEASE=" /etc/lsb-release | awk -F"DISTRIB_RELEASE=" '{print $2}'`
elif [[ -f /etc/debian_version ]]; then
    os=debian
    version=$(cat /etc/debian_version)
else
    echo -e "`date +%T` Unsupported operating system."
    exit 1
fi
os=${os,,}
version=${version//\"/}
echo -e "`date +%T` Your os is $os."
echo -e "`date +%T` Your distribute is $version."

rclocal="/etc/rc.local"
if [[ "$os" == *"ubuntu"* ]]; then
    rclocal="/etc/rc.local"
elif [[ "$os" == *"centos"* ]]; then
    rclocal="/etc/rc.d/rc.local"
elif [[ "$os" == *"debian"* ]]; then
    rclocal="/etc/rc.local"
elif [[ "$os" == *"opencloudos"* ]]; then
    rclocal="/etc/rc.d/rc.local"
else
    echo -e "`date +%T` Unsupported Linux distribution."
    exit 1
fi

echo -e "`date +%T` check the mount path"

mkdir -p "$CacheDir"
if [[ $? -ne 0 ]]; then
    echo -e "`date +%T` failed to create cosfs cache directory $CacheDir"
    exit 12
fi
chmod 700 "$CacheDir"

if df -h | awk -v target="$MountPoint" '$1 == "cosfs" && $6 == target { found=1 } END { exit !found }'; then
    echo -e "`date +%T` the mount path[$MountPoint] already mounted\n"
    exit 0
fi

if [[ -L "$MountPoint" ]]; then
    echo -e "`date +%T` mountpath[$MountPoint] is link file"
    exit 13
fi

if [[ ! -d "$MountPoint" ]]; then
    mkdir -p "$MountPoint"
fi

if [[ -n "$(ls -A "$MountPoint" | head -1)" ]]; then
    echo -e "`date +%T` mountpath[$MountPoint] is not empty"
    exit 5
fi

sleep 10

echo -e "`date +%T` ready to mount"

mount_options=(
    "-ourl=${CosEndpoint}"
    "-odbglevel=err"
    "-oallow_other"
    "-ouse_cache=${CacheDir}"
    "-otmpdir=${CacheDir}"
    "-oensure_diskfree=10240"
)

if [[ $PublicBucket -eq 1 ]]; then
    mount_options+=("-opublic_bucket=1")
else
    mount_options+=("-opasswd_file=/etc/passwd-cosfs")
fi

mount_command="cosfs '${BucketName}:/' '${MountPoint}'"
for option in "${mount_options[@]}"; do
    mount_command+=" '${option}'"
done

ret=0
for ((i=1; i<=12; i++)); do
    cosfs "${BucketName}:/" "$MountPoint" "${mount_options[@]}" > /dev/null 2>&1
    ret=$?
    if [[ $ret -ne 0 ]]; then
        echo -e "`date +%T` mount failed, continue"
        sleep 5
        continue
    fi
    break
done
if [[ $ret -ne 0 ]]; then
    sleep 5
    echo -e "`date +%T` final mount"
    cosfs "${BucketName}:/" "$MountPoint" "${mount_options[@]}"
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` final mount failed"
        exit 6
    fi
fi

echo -e "`date +%T` mount success"

if ! grep -Fqx "$mount_command" "$rclocal" 2> /dev/null; then
    echo "$mount_command" >> "$rclocal"
    if [[ $? -ne 0 ]]; then
        echo -e "`date +%T` echo rc.local failed"
        umount -l "$MountPoint"
        exit 11
    fi
fi

echo -e "`date +%T` echo to rc.local succ"
chmod a+x "$rclocal"

公开读存储桶无需额外配置密钥。私有桶需要先把密钥写入 /etc/passwd-cosfs,并将脚本中的 PublicBucket 改为 0:

echo 'BucketName-APPID:SecretId:SecretKey' > /etc/passwd-cosfs
chmod 640 /etc/passwd-cosfs

建议使用只拥有目标存储桶权限的子账号密钥,不要把密钥直接写进云助手命令。

检查挂载

执行完成后查看挂载结果:

df -h
mount | grep /oss
ls -la /oss

df -h 中应出现类型为 cosfs、挂载点为 /oss 的文件系统。卸载时先离开挂载目录,再执行:

cd /
fusermount -u /oss

脚本使用 rc.local 实现开机自动挂载。如果实例没有启用 rc-local.service,重启后不会自动执行其中的命令,需要单独确认该服务是否已启用。

Tip

COSFS 会把读写操作经过本地磁盘中转,适合文件备份和简单管理,不适合数据库、高并发读写或多客户端同时修改同一文件。