Snapcraft configuration of multipass VM arguments?

I’m building a snap that has rather intense memory requirements (JVM application, gradle build). It’s a core18 snap so the build is running in a multipass VM. It occasionally fails due to running out of memory in the VM.

Is there a way to pass parameters through to multipass so I can give the build more memory?

You can launch it by hand, as long as it has the right name snapcraft runs with it.
The project directory is bind-mounted (use multipass mount), and it needs snapcraft and multipass installed (use multipass shell). I think that’s about it.

1 Like

You can also use the SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY variable. It’s undocumented, but _CPU and _DISK are also available.

7 Likes

That’s exactly what I needed, thanks!

SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=8G snapcraft
Launching a VM.
'SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY' was set to '8G' in the environment. Changing the default allocation upon user request
1 Like

I tried using this SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=8G env var, but I don’t see the message @cmars indicated, just Launching a VM - and it doesn’t appear to have any effect. Is this still [un]supported? Is there an alternative?

1 Like

Hey @jungd-yii, are you sure you’re setting it right? Nothing changed to that code in 5 months. Can you show us a bit of your console log where you’re setting the variable and then how snapcraft reacts to it?

@Saviq I figured it out. Seems it is only applied when a new VM is created. Just running snapcraft doesn’t change it. As a new snapcraft user (given Snaps are supposed to be “easy”), I had to go and learn about what multipass is, how to use that command to stop the VM. However, that didn’t work either. So, I had to delete the VM - but that just causes snapcraft to complain the VM is delete and suggest purging it (not sure why it couldn’t have done that itself). After purging the deleted multipass VM and re-running, it does indeed take the parameter. Bit of a minefield for new users (- especially since initially I’d just killed the qemu process and then was faced with figuring out how to re-start the multipass daemon, which I could never figure out and just had to reboot my host machine). Thanks for your response though.

2 Likes

@jungd-yii there’s some rough edges for sure. Know that snapcraft clean would kill the instance for you, also snap restart multipass is how you restart snap services. Glad you got it going.

2 Likes

Also set the SNAPCRAFT_BUILD_ENVIRONMENT=managed-host environment variable.

We probably should delete and recreate the VM if the parameters don’t match…?

I believe --destructive-mode is the supported equivalent of that.

Yeah that makes sense. Care to file a bug for snapcraft?

1 Like

Isn’t that equivalent to SNAPCRAFT_BUILD_ENVIRONMENT=host instead?

Sure: Bug #1822265 “SNAPCRAFT_BUILD_ENVIRONMENT_* change doesn’t apply to existing VM” : Bugs : Snapcraft

I created this bug https://bugs.launchpad.net/snapcraft/+bug/1842376 because some software needs more than 2GiB of memory to be built and there is no way to specify that via some kind of metadata.

With --use-lxd it might be less of a problem if a container is not limited in terms of RAM allocation, however, OOM killer might decide to kill host OS processes.

Hello,

I think we ran into the same behavior problem, is it possible to have some instructions about how we can increase the RAM of the VM or at least check it ? By checking htop it seems we don’t have it and we need more for our current compilation.

SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=8G

Was not enough and “snapcraft clean” or “snap restart multipass” did not do the trick apparently.

since @jungd-yii mentionned complex steps, Is there a specific instruction to follow to increase the amount of RAM?


Edit :

Ok, that’s fixed, just not used to give environment variable and use sudo on Ubuntu.

For next users reading this, check how to correctly provide an environment variable to your shell while using sudo on Ubuntu.

Snapcraft behaves normally, at least with a snapcraft clean, I indeed get the message “user changed to 8Go…”

All the best,

Setting SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY fixed getting a MemoryError when pip install-ing a large package for me. I did have to run snapcraft clean first though or the memory wouldn’t be increased:

snapcraft clean && SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=4G snapcraft