0%

NanoPi-R2s-SD卡系统设置Swap内存

NanoPi R2s SD卡系统设置Swap内存

一直报错:

ubuntu swapon failed: Invalid argument

问题溯源:

该问题是因为 sd卡系统中的存储空间在 根目录下,并且没有单独分区(是否还有其他原因,未曾证实)

解决方案:

1:压缩 根目录 / 下的 2G 空间出来,供虚拟内存使用

此处你需要安装 ubuntu desktop 系统,使用 其中的 磁盘工具 来操作

假设你已经设置后了一个单独的分区出来,接下来可以正式的开始设置 swap

2:查看系统是否存在虚拟内存

free -h

img

3:检查可用空间

df -h

挂载在 / 上的 /dev/sda 文件系统将是系统的硬盘驱动器。

4: 创建交换文件

在这里,我使用 fallocate 命令为 1GB RAM 服务器创建一个 1GB 的交换空间作为示例

sudo dd if=/dev/mmcblk0p10 of=/swap/swapfile bs=1M count=1024   # 这里如果存在/dev/mmcblk0p10设备, 则设置/swap/swapfile为交换空间 大小为1G

5:在Ubuntu上启用交换文件

sudo chmod 600 /swap/swapfile      # 设置权限
sudo mkswap /swap/swapfile # 标记交换空间
sudo swapon /swap/swapfile # 使用交换空间

6:创建永久交换空间

编辑 /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Generate the SSH keys if non-existent
if [ ! -f /etc/ssh/ssh_host_rsa_key ]
then
# else ssh service start in dpkg-reconfigure will fail
systemctl stop ssh.socket||true
dpkg-reconfigure openssh-server
fi

if [ -f /usr/bin/gpio ]; then
setcap cap_sys_rawio+ep /usr/bin/gpio
fi

if [ -e /dev/mem ]; then
chmod g+w /dev/mem
fi

mount /dev/mmcblk0p10 /swap
sleep 5
sudo swapon /swap/swapfile

exit 0