首先这个错误我发现并非是没有mkswap的缘故,而是btrfs根本不支持swapfile,解决的方法也很简单,就是把swapfile挂载到loopback就可以了,参见http://www.spinics.net/lists/linux-btrfs/msg28533.html
github上面有个脚本,参见:https://github.com/sebastian-philipp/btrfs-swapon
脚本内容如下
#!/bin/sh # # Copyright (C) Sebastian Philipp # set -e swapsize="$1" swapname="$2" if [ -z "$swapsize" -o -z "$swapname" ] then cat <<EOF Usage: $0 size: the size of the file, like "8G" file: path to the new swap file. This file should not exists. EOF exit 1 fi if [ -e $swapname ] then echo "error: File already exists. $swapname" exit 1 fi swapfile=$(losetup -f) #free loop device # set NOCOW touch $swapname chattr +C $swapname head -c $swapsize /dev/zero >> $swapname losetup $swapfile $swapname #mount file to loop mkswap $swapfile swapon $swapfile
使用方法是./btrfs-swapon <size> <file path>,文件地址需要一个不存在的文件,把这个添加到rc.local就可以开机自启动挂载swapfile了