cloud-init cannot create swap files on paths that it mounts

Bug #1869114 reported by Johnson Shi
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cloud-init
Expired
Wishlist
Unassigned

Bug Description

1. cloud-init's swap module runs before the mount module, which causes swapfile creation on runtime mounted disks to fail.
2. cloud-init's swap create does not ensure it has a path to the file (if specified with a directory that does not exist yet).

Bug exists in all clouds, but the bug was discovered through deploying an Azure VM, mounting the Azure resource disk, and then creating a swapfile within the mounted resource disk.

Relevant logs: https://paste.ubuntu.com/p/QdGybC2wyc/

Johnson Shi (johnsonshi)
Changed in cloud-init:
assignee: nobody → Johnson Shi (johnsonshi)
Revision history for this message
Dan Watkins (oddbloke) wrote :

Hi Johnson, thanks for the bug report!

Swap file creation and mounting are both handled by the same module, cc_mounts. This has two phases: /etc/fstab generation and /etc/fstab application. In order for the use of swap files to be persisted between boots, they have to be included in /etc/fstab, so the swap file is created during the generation phase. This, of course, means that the other generated mounts haven't yet been applied, which leads to the behaviour you've observed here.

As other mounts (in general) presumably aren't dependent on the swap file's creation, it may be possible to change the order of these operations to behave as you would like without affecting other use cases, but we would probably need the expected new behaviour to be specified well to satisfy ourselves of this.

Thanks!

Dan

summary: - swap module runs before mount module
+ cloud-init cannot create swap files on paths that it mounts
Changed in cloud-init:
status: New → Triaged
importance: Undecided → Wishlist
Revision history for this message
Johnson Shi (johnsonshi) wrote :

Hi Dan, thanks for getting back to me.

I discussed this with Ryan Harper through IRC and he confirmed that this is a bug.

Relevant links:

https://github.com/canonical/cloud-init/blob/e30599c103ff926e8cbf38f36d1ba071918fc386/cloudinit/config/cc_mounts.py#L478-L480

Swapfile setup (L478 where cloud-init attempts to create the swapfile) and writing of swap entry to /etc/fstab happens much earlier than "mount -a" (happens later below the code block).

https://github.com/canonical/cloud-init/blob/e30599c103ff926e8cbf38f36d1ba071918fc386/cloudinit/config/cc_mounts.py#L523-L535

Also, "swapon -a" is issued before "mount -a".

Suggested approach:

1. Write mount entries to /etc/fstab first. Then issue "mount -a" cmd.
2. After step 1 happens, cloud-init would then run handle_swapcfg where it attempts to create the swapfile https://github.com/canonical/cloud-init/blob/e30599c103ff926e8cbf38f36d1ba071918fc386/cloudinit/config/cc_mounts.py#L478
3. After that, cloud-init would then write the swap entry to /etc/fstab.
4. Cloud-init then issues "swapon -a" cmd.

This suggested change will enable cloud-init users to create swapfiles (not just swap partitions) on mounted disks on various clouds (not just Azure but also AWS and GCE).

Indeed, I think mounts in general won't presumably depend on swap files being created beforehand, while on the other hand, having swapfile creation happen after mounting would make more sense and unlock more user potentials, so our suggested changes won't disrupt users too much.

Thanks!
Johnson

Revision history for this message
Dan Watkins (oddbloke) wrote :

Hi Johnson, thanks for the update! I agree that this is something we can address. I see you've assigned yourself, are you interested in (or already) working on a fix?

Revision history for this message
Marco Wester (mwester) wrote :

Hi everybody,

today I ran into the same problem.
Johnsons approach should work.
Is still somebody working on the fix?

Thanks!
Marco

Johnson Shi (johnsonshi)
Changed in cloud-init:
assignee: Johnson Shi (johnsonshi) → nobody
Revision history for this message
James Falcon (falcojr) wrote :
Changed in cloud-init:
status: Triaged → Expired
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.