Skip to content

Per-order first-boot script (user_data) - #444

Draft
douglaz wants to merge 3 commits into
LNVPS:masterfrom
douglaz:feat/vm-user-data
Draft

douglaz wants to merge 3 commits into
LNVPS:masterfrom
douglaz:feat/vm-user-data

Conversation

@douglaz

@douglaz douglaz commented Nov 19, 2026

Copy link
Copy Markdown

Summary

A VM order can carry one first-boot script, as user_data on POST /api/v1/vm and POST /api/v1/vm/custom-template.
It must be a #! script of at most 16 KiB. cloud-init runs it once as root, in scripts-user, after the SSH key and
the host keys are in place. This lets a customer configure a machine at first boot without waiting for the first
login. One example is having the machine certify its own SSH host key, so the client can verify it at first contact.

Nothing changes for an order without a script. Its cloud-init document, its libvirt seed and its Proxmox config stay
byte-identical, and tests pin each one.

What changes

  • API.
    • Both order bodies take an optional user_data.
    • It is validated before anything is written: at most 16384 bytes, starts with #!, no NUL. Otherwise the order
      gets the usual 400.
    • An order with a script is refused before any VM, subscription or invoice exists when the chosen host can't
      deliver it.
  • Hosts.
    • VmHostClient::supports_user_data (default false).
    • Proxmox: needs SSH and a storage with the snippets content type, the same as the vendor and network
      snippets need.
    • libvirt: yes.
    • Marketplace nodes: ask their inner client.
    • The dummy host: refuses scripts.
  • Storage.
    • Migration: 20261119045817_vm_user_data.sql adds vm.user_data TEXT NULL, encrypted like SSH keys and
      included in rotate_encryption_key.
    • Never returned: no API response carries it, user or admin.
    • Admin transfer: clears it.
    • Deletion: replaces it with an empty marker. A VM revived by a late payment then fails to build, instead of
      building without the script that was ordered.
  • cloud-init. With a script, cloud_init::user_data emits multipart/mixed: the existing cloud-config, then the
    script as text/x-shellscript. Both parts are base64-encoded, so non-ASCII text arrives byte-exact. The boundary is
    derived from the content, and a collision is refused.
  • libvirt. The seed already embeds that document.
  • Proxmox.
    • The snippet: a per-VM lnvps-user-<id>.yaml, created with mode 0600 and reconciled to 0600 when its content
      already matches.
    • The config: cicustom user= points at it. This replaces the user-data Proxmox would generate, so the SSH
      key comes from the cloud-config part, as on libvirt.
    • On failure: if the snippet can't be written, the build fails.
    • Clean-up: the snippet is removed with the VM.
  • Docs. API_DOCUMENTATION.md and API_CHANGELOG.md cover the limit, the #! rule and the re-runs. A
    reinstall runs the script again, and on Proxmox so does a reconfiguration such as an IP or key change, so scripts
    should be safe to run more than once. They also cover retention and the host requirement.

Tests

Run as CI runs them:

  • cargo test --locked --package lnvps_api --package lnvps_agent --features lnvps_api/libvirt -- --test-threads=1
  • cargo test -p lnvps_api_common --features libvirt host::cloud_init -- --test-threads=1
  • the full lnvps_api_common unit tests
  • the Proxmox tests, with --features libvirt,proxmox

All of them pass.

New tests cover:

  • the validator, including the 16384-byte boundary;
  • refusal before any write on both order paths;
  • capability forwarding;
  • a golden no-script document, written before the generator changed;
  • byte-exact base64 parts for non-ASCII scripts and key comments;
  • boundary collisions;
  • the deletion marker, through the mock's late-payment path;
  • the transfer clear;
  • the seed carrying the script;
  • a frozen no-script Proxmox VmConfig captured from master;
  • status output never containing the script.

The changed generator's output was also booted on a NixOS cloud image with cloud-init 25.2. The script ran as root
after the SSH key was installed, non-ASCII text arrived byte-exact, and the key still logged in.

Three checks fail on master as well, unchanged by this PR:

  • cargo test -p lnvps_db --test migration_versions, on 20261201000000_audit_log_ip_truncate.sql;
  • cargo fmt --all -- --check, on lines this PR doesn't touch;
  • cargo clippy -- -D warnings, on try-procedure/src/lib.rs:236 and :558.

Not in this PR

  • A refusal because the host can't deliver a script returns 500, not a 4xx.
  • A same-cluster Proxmox qm migrate doesn't write the user snippet on the target node. The network snippet has the
    same gap today.
  • After an admin transfer, the old owner's snippet stays on the Proxmox host, mode 0600, until the next configure.
  • For VMs with a script on Proxmox, Proxmox's own generated user-data defaults (such as package_upgrade) no longer
    apply.
  • The SFTP mode handling has no automated test, because there is no host-free SFTP harness.

An order can carry one first-boot script: `user_data` on both order bodies, a
`#!` script of at most 16 KiB. cloud-init runs it once as root in scripts-user,
after the SSH key and host keys are in place.

- The order is refused before anything is created or invoiced when the script
  is invalid, or when the chosen host cannot deliver it (new
  `VmHostClient::supports_user_data`, default false; Proxmox needs SSH and
  snippet storage, libvirt yes, marketplace nodes ask their inner client).
- The script is stored encrypted in `vm.user_data`, included in key rotation,
  never returned by the API, cleared on admin transfer, and replaced by an
  empty marker at deletion so a revived VM fails to build instead of losing it.
- cloud_init::user_data is unchanged without a script. With one it emits a
  multipart/mixed document: the existing cloud-config, then the script, both
  base64-encoded.
- libvirt carries it in the existing seed. Proxmox writes a per-VM
  `lnvps-user-<id>.yaml` snippet, created 0600, and sets `cicustom user=`; a
  VM without a script keeps its config unchanged.
- A reinstall, and on Proxmox a reconfiguration, run the script again; this is
  documented in API_DOCUMENTATION.md and the changelog.

@v0l v0l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewing although this is a draft.

The overall shape works for us. The script is validated before any write, stored encrypted and covered by key rotation, never serialized, and cleared on admin transfer, and the no-script path is pinned by golden tests. The migration is nullable, so it is safe on the production vm table. The field is optional, so the existing web and admin clients are unaffected. Tests pass here: lnvps_api_common 919, lnvps_api 822. migration_versions fails on 20261201000000_audit_log_ip_truncate.sql, but master fails the same way.

Four changes are needed before merge:

  1. Run the script once per VM, not again when the key or IP changes. With cicustom user= set and no meta=, Proxmox derives the NoCloud instance-id from sha1(user_data . network_data). A key, IP or template change therefore gives a new instance-id, and cloud-init reruns the script as root. I checked two ways out on a Debian 12 guest (cloud-init 22.4). A text/x-shellscript part ran again after an instance-id change. A text/x-shellscript-per-once part ran once and stayed at one. per-once needs cloud-init 22.1 or later, so either confirm that holds for our images or also write a meta= snippet with the stable instance-id: lnvps-vm-{id} that libvirt already uses. Then the docs line about reruns on reconfiguration can go.
  2. Migration must refuse, or carry the script, when the target host cannot deliver it. supports_user_data is only checked at order time (provisioner/vm.rs:223, :367). plan_migration and the adopt path never check it, so a scripted VM can be moved to a Proxmox host without SSH or snippet storage. It then fails in ensure_user_snippet after the source VM was stopped. Please check the target in plan_migration, with a test.
  3. Revived VMs must build. The late-payment path (mysql.rs:4662) sets deleted = 0, but the empty marker from delete_vm stays, so every build fails with "script was cleared" on a VM the customer has paid for. A revived VM needs to build. Please cover it with a test through the payment path.
  4. An unsupported host must return a clear 4xx, not a 500. "This region's host cannot deliver user_data" comes from ensure!, which maps to an internal error.

Smaller:

  • Replacing Proxmox's generated user-data drops package_upgrade: true and the chpasswd block for scripted VMs only. Please add that to API_DOCUMENTATION.md, or put package_upgrade in the cloud-config part so both kinds boot the same way.
  • Please file the same-cluster qm migrate gap and the transfer leftover as issues so they are not lost.
  • We keep comments out of new code. Please trim the new /// and // lines. UserDataHost could wrap a DummyVmHost and override only supports_user_data, instead of delegating 20 methods by hand.

Comment thread lnvps_api_common/src/host/cloud_init.rs Outdated
--{boundary}\r\nContent-Type: text/cloud-config; charset=\"utf-8\"\r\n\
Content-Transfer-Encoding: base64\r\n\r\n\
{config}\r\n--{boundary}\r\n\
Content-Type: text/x-shellscript; charset=\"utf-8\"\r\n\

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

text/x-shellscript runs once per instance-id, and on Proxmox the instance-id changes with any key or IP change. text/x-shellscript-per-once ran exactly once across an instance-id change on Debian 12 (cloud-init 22.4).

Comment thread lnvps_api/src/provisioner/vm.rs Outdated

if user_data.is_some() {
let client = self.host_client(&host.host).await?;
ensure!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ensure! comes back as a 500. This is the customer's choice of region, so please return a 4xx with this message.

Comment thread lnvps_api/src/provisioner/vm.rs Outdated
if user_data.is_some() {
let client = self.host_client(&host.host).await?;
ensure!(
client.supports_user_data().await?,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This only runs at order time. Please run the same check on the target host in plan_migration, so a migration is refused before the source VM is stopped.

Comment thread lnvps_db/src/mysql.rs
sqlx::query("update vm set deleted = 1, ssh_key_id = null where id = ?")
// An encrypted empty script marks deletion so late payment cannot silently
// build the VM without the script it was ordered with.
sqlx::query("update vm set deleted = 1, ssh_key_id = null, user_data = case when user_data is null then null else ? end where id = ?")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The revival at :4662 sets deleted = 0 but keeps this marker, so a paid revival can never build. Revived VMs need to build.

};

let cicustom = CiCustom {
user: if value.vm.user_data.is_some() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With user= set and no meta=, Proxmox hashes user and network data into the instance-id. A meta= snippet with instance-id: lnvps-vm-{id} would keep it stable, as on libvirt.

… revived VMs

Deliver the script as text/x-shellscript-per-once, so a key, address or template change no longer runs it again; this needs cloud-init 22.1 or later in the image. Migration, planning and copy-and-adopt refuse a scripted VM before any source-side change when the target host cannot deliver the script, while drift adoption still records where a moved VM really is. A late payment that revives a deleted VM clears its deletion marker, so the VM builds without the erased script once it has an SSH key again. An order whose host cannot deliver the script is refused with 400 instead of 500. Scripted Proxmox VMs keep package_upgrade, the omitted password block is documented, new comments are removed and the test host wraps DummyVmHost.
@douglaz

douglaz commented Nov 20, 2026

Copy link
Copy Markdown
Author

180b0cf addresses the script-specific failures; revival still requires an SSH key to be reassigned, as detailed below.

  1. Once per VM. The script part is now text/x-shellscript-per-once.

    • The images in this repository ship cloud-init 22.1 or later: Debian 13 in CI, the NixOS image, Debian 12, and
      Ubuntu 22.04 and 24.04. The demo data's CentOS Stream 9, Arch and Ubuntu 24.10 qualify too. The exception is
      Debian 11 in dev_setup.sql, which is end of life.
    • I can't see production's image list. Please check it for anything older than 22.1.
    • I didn't add a meta= snippet. A stable instance-id would also stop Proxmox from re-applying key and address
      changes, for scripted VMs only. Happy to add it if you'd rather.
    • The docs now say: once per VM disk (a reinstall runs it again), cloud-init 22.1 or later, and older images
      ignore the part. The rerun line is gone.
  2. Migration. For a scripted VM, the target's supports_user_data() is now asked before anything on the source
    is touched:

    • in migrate_vm, just before plan_migration;
    • in plan_vm_migration, as a refusal;
    • in migrate_vm_with, the worker entry;
    • in copy_and_adopt.

    Each has a test. Drift adoption still records where an externally moved VM really is, and only logs a warning:
    refusing there would leave host_id on the old node, and a later delete would miss the real VM.

  3. Revival. subscription_payment_paid now clears the deletion marker on the rows it revives, so the erased
    script no longer blocks the build. The test goes through complete_payment.

    • Still open: upstream's delete_vm already clears ssh_key_id, before this PR. So a revived VM, with or
      without a script, can't build until a key is reassigned. The test shows both steps.
    • Is that intended, or do you want it fixed here?
  4. 4xx. A typed UserDataUnsupported maps to 400 with the message on both order paths, the same way
    DiscountError does.

Smaller:

Tests, run here:

  • lnvps_api: 827 passed (822 plus 5 new).
  • lnvps_api_common: 849 passed with libvirt, and the 69 host::proxmox tests passed. With both features there are
    921 non-ignored tests (919 plus 2 new). Three SSH tests outside those filters weren't run.
  • migration_versions fails the same way as on master.

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