完全用RAM运行Ubuntu(2)

TurnToJPG -->


完全用ram工作的场景下,关机时需要回写到磁盘上,以下是用来将Ram中的数据回写到磁盘的方法。

# vim /bin/writeback.sh
    #!/bin/sh
    kkk=`mount | grep "none on / type tmpfs"`
    if [ ! -z "$kkk" ]
    then
    mkdir -p /writeback
    mount /dev/mapper/ubuntu--vg-root /writeback
    rsync -a --delete --exclude 'tmp'   --exclude 'proc' --exclude 'writeback' --exclude 'sys' / /writeback/
    fi

创建一个回写的服务:

# vim  /etc/systemd/system/run-before-shutdown.service 
[Unit]
Description=Run my custom task at shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/bin/writeback.sh
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target

使能服务:

# systemctl enable run-before-shutdown

则关机时系统会调用回写脚本将Ram中的数据写入到磁盘中。