Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show how to move mkosi WorkspaceDirectory to /tmp(fs) #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marc-hb
Copy link
Collaborator

@marc-hb marc-hb commented Feb 13, 2025

I've been selfishly enjoying this major optimization for a while; time to share it. Very small hence very easy to comment out or even revert if needed.

mkosi-workspace is located on /var/tmp/ by default, which is usually persistent and slow storage which wears out. On success, mkosi-workspace is deleted at the end of the build so it really does not need to be located on persistent storage. So, move it to /tmp which is usually tmpfs.

On one of my systems, moving to tmpfs lowers the build time from 4 min to 1 min! If you ever wondered how I could git bisect mkosi faster than reading its release notes: that's how.

@marc-hb
Copy link
Collaborator Author

marc-hb commented Feb 13, 2025

@marc-hb marc-hb marked this pull request as ready for review February 13, 2025 02:45
# remount,size=newsize after buying a decent amount of RAM), or comment this
# out.
#
# Warning 2: mkosi version v15 and v16 cannot use /tmp/ for this. (cpio
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://repology.org/project/mkosi/versions, only Fedora 39 shipped version 16. Fedora 39 is EOL now.

# mkosi-workspace is located on /var/tmp/ by default (why?), which is usually
# persistent and slow storage which wears out. On success, mkosi-workspace is
# deleted at the end of the build so it really does not need to be located on
# persistent storage. So, move it to /tmp which is usually tmpfs. Moving to
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This configuration option was first added in mkosi v10 commit https://github.com/systemd/mkosi/commits/59d8d913a5f67d9

At the time, mkosi.md documented it like this: mkosi.workspace/ else $TMPDIR else /var/tmp. It relied on tmp_dir().

@@ -0,0 +1,31 @@
# mkosi-workspace is located on /var/tmp/ by default (why?), which is usually
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a simplifcation. Since v19, mkosi is trying to default to somewhere in /home in order to use reflinks systemd/mkosi@94c1e78a496713e0

In my experience across multiple versions, tmpfs is just faster - especially when qbuild is symlinked to /tmp/qbuild.real too.

@marc-hb marc-hb marked this pull request as draft February 13, 2025 16:55
@marc-hb
Copy link
Collaborator Author

marc-hb commented Feb 13, 2025

@davejiang found a failure with v24.3, looking into it.

@stellarhopper
Copy link
Member

In case there are instances where mkosi leaves behind a /tmp/mkosi-workspace*, it might be worth adding a sudo rm -rf to remove it in the -r wipe case.

@marc-hb
Copy link
Collaborator Author

marc-hb commented Feb 13, 2025

@davejiang found a failure with v24.3, looking into it.

So this setting is incompatible with v24 commit systemd/mkosi@f4eba02dd3ca08

:: Synchronizing package databases...
 core is up to date
 extra        
chmod: changing permissions of '/tmp': Operation not permitted

Bubblewrap does a lot of weird things with bind mounts and what not.

v25 replaced Bubblewrap and works again.

I think I found a workaround. Will that workaround be compatible with other, random mkosi versions? Maybe not. Maybe the best is to make this opt-in. Easy enough to just copy this file to a different place.

@marc-hb
Copy link
Collaborator Author

marc-hb commented Feb 15, 2025

So this setting is incompatible with v24 commit systemd/mkosi@f4eba02dd3ca08
...
v25 dropped and replaced Bubblewrap and works again.

So this is what seems to happen. I looked into this just to make sure the workaround does not work by chance/accident.

  • mkosi/__init__.py#setup_workspace() starts with rmtree($WorkspaceDir/mkosi-workspace-12345)

  • tree.py#rmtree(p) runs rm -rf $p in a bubblewrap sandbox where mounts=[(p.parent, p.parent),...]. $WorkspaceDir is the workspace parent p! This means bubblewrap is invoked with --bind /tmp /tmp.

  • From v24 commit systemd/mkosi@f4eba02dd3ca08, every bubblewrap invocation starts with (among others) chmod 1777 /tmp where /tmp is harcoded; totally unrelated to $WorkspaceDir.

  • That chmod 1777 seems incompatible with a bubblewrap bind mount: Operation not permitted.

So the workaround is very simple; simply add one directory level: WorkspaceDir=/tmp/rq_mkosi_wspaces/. As a bonus this keeps /tmp/ tidier.

Maybe the best is to make this opt-in.

I don't want to debug issues like this again so the next force push will make this opt-in. Everyone can use it or not without polluting their git status.

I've been selfishly enjoying this major optimization for a while; time
to share it.

mkosi-workspace is located on /var/tmp/ by default, which usually does
not run out of RAM but is persistent and slow storage which wears out.
On success, mkosi-workspace is deleted at the end of the build so it
really does not need to be located on persistent storage. So, show how
to move it to /tmp which is usually tmpfs. Also document some of the
pitfalls discovered so far.

On one of my systems, moving to tmpfs lowers the build time from 4 min
to 1 min! If you ever wondered how I could git bisect mkosi faster than
reading its release notes: that's how.

Signed-off-by: Marc Herbert <[email protected]>
@marc-hb marc-hb changed the title run_qemu.sh: move mkosi WorkspaceDirectory to /tmp(fs) Show how to move mkosi WorkspaceDirectory to /tmp(fs) Feb 15, 2025
@marc-hb marc-hb marked this pull request as ready for review February 15, 2025 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants