/sbin/swapon /var/swap.1报错

bug问题 · bb2visit · 于 5年前 发布 · 4337 次阅读

在执行/sbin/swapon /var/swap.1命令时报 swapon: /var/swap.1: swapon failed: Operation not permitted 求解???如何解决?是设置的缓存大小超出了内存卡可用空间的大小吗?

共收到 4 条回复
Fecmall#15年前 0 个赞

搜索一下就能解决这个报错,这些报错和fecshop没有关系

https://www.njphper.com/detail/158.html

在安装phalcon的时候遇到内存不足的问题,需要swap增加内存,可是在docker里面遇到 swapon failed: Operation not permitted

Hi, As each running docker container uses the host Kernel, they also use the memory and swap of the host. If this is a one of requirement its better to increase the host swap space.

If you want to still add swap from the container you have two options.

Run container in privileged mode

In this case you will have to run the container with --privileged option.

Example

docker run -it --rm --privileged centos:6

Running container with privileged mode gives container full privilege on the host. If you read the manpage of swapon you can see that for swapon to run the process should have CAP_SYS_ADMIN capability. In Docker you can add a particular capability selectively to the container using the --cap-add argument.

Example

docker run -it --rm --cap-add SYS_ADMIN centos:6

If you run the container in either of the above two modes you can achieve what you are trying.

Now the problem with this approach is , when you create swap inside the container and start using that, its actually the Host Kernel that is using it, as a result when you exit the container without a swapoff the host kernel will be still using the file, and you wont get a clean exit of the container. You will see a dead status for the container.

翻译:

嗨, 由于每个运行的docker容器都使用主机内核,它们还使用主机的内存和交换。如果这是要求更好的增加主机交换空间的一个。 如果你想从容器中添加交换,你有两个选择。 以特权模式运行容器 在这种情况下,您将必须使用--privileged选项运行容器。

示例

docker run -it --rm --privileged centos:6

运行具有特权模式的容器可以为主机提供容器完整权限。如果您阅读swapon的联机帮助页面,您可以看到,对于swapon来运行该进程应该具有CAP_SYS_ADMIN功能。在Docker中,您可以使用-cap-add参数有选择地向容器添加特定的功能。

示例

docker run -it --rm --cap-add SYS_ADMIN centos:6

如果您以上述两种模式运行容器,您可以实现您正在尝试的功能。

现在,这种方法的问题是,当您在容器内创建交换并开始使用它时,实际上是使用它的主机内核,因此当您退出容器而不进行swapoff时,主机内核将仍然使用该文件,你不会得到一个干净的出口的容器。您将看到容器的死亡状态。

Fecmall#25年前 0 个赞

原因就是你的内存太小,需要swap搞虚拟内存,其他的参考资料:

https://shipengliang.com/software-exp/swapon-failed-operation-not-permitted-解决办法.html

http://blog.gt520.com/vps/91.html

资料大把。

bb2visit#35年前 1 个赞

解决办法:

1.创建一个文件,名为 fakeswap.sh
2.写入如下代码:
#!/bin/bash
SWAP="${1:-512}"
NEW="$[SWAP*1024]"; TEMP="${NEW//?/ }"; OLD="${TEMP:1}0"
umount /proc/meminfo 2> /dev/null
sed "/^Swap\(Total\|Free\):/s,$OLD,$NEW," /proc/meminfo > /etc/fake_meminfo
mount --bind /etc/fake_meminfo /proc/meminfo
free -m
3.顺序执行如下指令:
chmod +x fakeswap.sh
sh fakeswap.sh 256
free -m
注意,256代表你想创建的swap的大小。
Fecmall#45年前 0 个赞

@bb2visit #3楼 用markdown 格式改改格式,参看 : http://www.fecshop.com/topic/624

第5部分,用代码块括起来。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics