Score:0

Debian - How to prevent file corruption on power loss?

bs flag

I have a Debian based OS, and I am creating a program that have support for upgrading itself. The program is ran on system-level as a systemd service.

I have a problem when I update the software, which in practice means replacing some files on /opt/myApp/ folder (home of all my application code) and replacing systemd service unit file /etc/systemd/system/my_service.service. After this I run systemctl daemon-reload, systemctl enable my_service and systemctl restart which works as intended. How ever, if a sudden power loss happens right after this, the new files copied to /opt/myApp/ and the new service file goes to size 0 in the filesystem, which of course then causes my program to fail and the service to go into masked state. If I reboot my system after the installation, the "danger" goes away and the system can handle sudden power losses without effect on the files I care about.

Is this some kind of file system corruption problem? How could I prevent this problem without rebooting the system? What could be the root of this issue?

vidarlo avatar
ar flag
Call `sync` to write buffers to disk?
ml_kalju avatar
bs flag
@vidarlo As a pretty new linux developer I didn't even know that files created doesn't immediately go to persistent storage. Thanks, that worked. I think there is no way to make comment a solve to a question?
vidarlo avatar
ar flag
All modern operating systems have write buffers.
Score:3
jp flag

Consider .deb packaging your software. This way, dpkg will handle this issue as well as the entire update process including the application files & the SystemD service unit. If something goes wrong, e.g., a sudden power loss or other interruption, dpkg will be aware of the situation and suggest the necessary action, which might be a re-installation of the package or dpkg --configure -a.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.