Copy Without Cache

 

 


[方法1] dd 的 "direct" flag

# iflag=direct oflag=direct

dd if=vda.qcow2 of=tmp.qcow2 bs=8M iflag=direct oflag=direct

# 不能加 "iflag=direct"

dd oflag=direct if=/dev/zero of=/dev/sdX bs=16M

[方法2] 建立 cgroup 去限制 memory usage

apt-get install cgroup-tools

env ppid=$$ sh -c '
    cgcreate -g memory:backuptask;
    echo 256M > /sys/fs/cgroup/memory/backuptask/memory.memsw.limit_in_bytes;
    echo $ppid > /sys/fs/cgroup/memory/backuptask/tasks;'

 cp -a vda.qcow2 tmp.qcow2

[方法3]

 

cgcreate -g memory:/CopyShell

cgset -r memory.limit_in_bytes=128m CopyShell

echo $$ > /sys/fs/cgroup/memory/CopyShell/tasks

 

[方法4]

CopyShell

#!/bin/bash
# tim@20210219
set -e

MyPath=/sys/fs/cgroup/memory/CopyShell

if [ ! -d $MyPath ]; then
        mkdir $MyPath
fi

# limit memory usage
echo 256M > $MyPath/memory.limit_in_bytes;
echo 256M > $MyPath/memory.memsw.limit_in_bytes;

# new shell in CopyShell cgroup
/bin/bash --init-file <(echo "export PS1='\u@CopyShell: '; echo 0 > $MyPath/tasks") -i

rmdir $MyPath

 


nocache utility

 

nocache utility ( 獲得工具: nocache, cachestats, cachedel )

HomePage: https://github.com/Feh/nocache

原理

This is done by intercepting the `open` and `close` system calls and

calling `posix_fadvise` with the `POSIX_FADV_DONTNEED` parameter

Limitations

The pre-loaded library tries really hard to catch all system calls that open or close a file.

This happens by "hijacking" the libc functions that wrap the actual system calls.

In some cases, this may fail, for example because the application does some clever wrapping.

(That is the reason why __openat_2 is defined: GNU tar uses this instead of a regular openat.)

Install

apt-get install nocache

nocache 測試

# 當 copy 後會 cleanup cache

nocache cp -a vda.qcow2 tmp.qcow2

cachestats

cachestats vda.qcow2                                        # cachestats tmp.qcow2 都有相同 result

pages in cache: 271168/3555104 (7.6%)  [filesize=14220416.0K, pagesize=4K]

cachedel

# cachedel calls posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) on the file argument.

# Thus, if the file is not accessed by any other application, the pages will be eradicated from the fs cache.

i.e. clean file cache

cachedel vda.qcow2