Copy without Cache 四, 16/04/2020 - 17:17 的修訂版本





[方法1] dd 的 "direct" flag

# iflag=direct oflag=direct

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


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

apt-get install cgroup-tools

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

 cp -a vda.qcow2 tmp.qcow2


nocache utility


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



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

calling `posix_fadvise` with the `POSIX_FADV_DONTNEED` parameter


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.)


apt-get install nocache

nocache 測試

# 當 copy 後會 cleanup cache

nocache cp -a vda.qcow2 tmp.qcow2


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

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


# 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