3

How to temporarily stop synchronizing of dirty data with disk, instead just keeping it in memory.

Clarification: I want to sync it later.

  • 1
    Do you need to sync the data later, or just discard it? – Murphy Apr 11 '16 at 14:30
11

If you want to prevent disk writes as much as possible, you can do this with Laptop Mode. One of the features of laptop mode is to allow a disk to spin down and to prevent the kernel from writing to it until memory gets full or until a timeout occurs (or until the disk needs to spin up in order to read data from it). See also the Arch Wiki. You'll presumably want to enable only disk spindown and not other features, and set LM_SECONDS_BEFORE_SYNC to a large value in the configuration.

There's also noflushd, which does this specific job (and is a lot easier to configure than Laptop Mode since it doesn't do anything else). I've used it happily in the past, but it's been unmaintained for a while so I don't know if it still works on modern systems.

Note that preventing disk writes isn't a defense against writing, it's a power saving (and noise reduction) measure. It's difficult to control exactly what might cause the disk to spin up, for example some data that needs to be read because it got wiped from the cache, and at that point the disk will be written to.

If the reason you want to prevent disk writes is that you don't want the disk content to be modified under any circumstances, you need different tools. You can do that at the filesystem level with a union mount. Mount the disk read-only, create a directory on a tmpfs filesystem, and create a union mount of the two where the tmpfs directory is the read-write branch. See Mount a filesystem read-only, and redirect writes to RAM? for several examples of union mount software on Linux. This is how live installations of Linux with a persistence option work: the live installation is read-only, but the persistent data partition is union-mounted on top of it.

You can also achieve a similar effect at the block device level, though I can't think of a compelling reason to prefer this solution. See GNU/Linux: overlay block device / stackable block device Alternatively, if the disk data is on an LVM volume or on a ZFS filesystem, you can make a snapshot to keep a copy of the data at a point in time while continuing to write to a logically separate device.

2

sounds like a job for eatmydata ( http://www.makelinux.net/man/1/E/eatmydata )

in debian, try something like

apt-get install eatmydata;

printf "\nLD_PRELOAD=libeatmydata.so" >> /etc/ld.so.preload

reboot

  • 4
    That only turns explicit syncs into no-ops. It doesn't prevent the kernel writing to the disk when it feels like it. – Gilles 'SO- stop being evil' Apr 11 '16 at 22:39
  • yeah, good point. i guess you need to hook into the kernel disable the disk write functions, doesn't sound easy – hanshenrik Apr 11 '16 at 23:52
  • I think this is what I want, combined with vm.dirty* sysctl options. – user2373145 Apr 12 '16 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.