Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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:
- Run the script once per VM, not again when the key or IP changes. With
cicustom user=set and nometa=, Proxmox derives the NoCloud instance-id fromsha1(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). Atext/x-shellscriptpart ran again after an instance-id change. Atext/x-shellscript-per-oncepart ran once and stayed at one.per-onceneeds cloud-init 22.1 or later, so either confirm that holds for our images or also write ameta=snippet with the stableinstance-id: lnvps-vm-{id}that libvirt already uses. Then the docs line about reruns on reconfiguration can go. - Migration must refuse, or carry the script, when the target host cannot deliver it.
supports_user_datais only checked at order time (provisioner/vm.rs:223,:367).plan_migrationand 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 inensure_user_snippetafter the source VM was stopped. Please check the target inplan_migration, with a test. - Revived VMs must build. The late-payment path (
mysql.rs:4662) setsdeleted = 0, but the empty marker fromdelete_vmstays, 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. - 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: trueand thechpasswdblock for scripted VMs only. Please add that to API_DOCUMENTATION.md, or putpackage_upgradein the cloud-config part so both kinds boot the same way. - Please file the same-cluster
qm migrategap and the transfer leftover as issues so they are not lost. - We keep comments out of new code. Please trim the new
///and//lines.UserDataHostcould wrap aDummyVmHostand override onlysupports_user_data, instead of delegating 20 methods by hand.
| --{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\ |
There was a problem hiding this comment.
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).
|
|
||
| if user_data.is_some() { | ||
| let client = self.host_client(&host.host).await?; | ||
| ensure!( |
There was a problem hiding this comment.
ensure! comes back as a 500. This is the customer's choice of region, so please return a 4xx with this message.
| if user_data.is_some() { | ||
| let client = self.host_client(&host.host).await?; | ||
| ensure!( | ||
| client.supports_user_data().await?, |
There was a problem hiding this comment.
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.
| 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 = ?") |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
|
180b0cf addresses the script-specific failures; revival still requires an SSH key to be reassigned, as detailed below.
Smaller:
Tests, run here:
|
Summary
A VM order can carry one first-boot script, as
user_dataonPOST /api/v1/vmandPOST /api/v1/vm/custom-template.It must be a
#!script of at most 16 KiB. cloud-init runs it once as root, inscripts-user, after the SSH key andthe 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
user_data.#!, no NUL. Otherwise the ordergets the usual 400.
deliver it.
VmHostClient::supports_user_data(defaultfalse).snippetscontent type, the same as the vendor and networksnippets need.
20261119045817_vm_user_data.sqladdsvm.user_data TEXT NULL, encrypted like SSH keys andincluded in
rotate_encryption_key.building without the script that was ordered.
cloud_init::user_dataemitsmultipart/mixed: the existing cloud-config, then thescript as
text/x-shellscript. Both parts are base64-encoded, so non-ASCII text arrives byte-exact. The boundary isderived from the content, and a collision is refused.
lnvps-user-<id>.yaml, created with mode 0600 and reconciled to 0600 when its contentalready matches.
cicustom user=points at it. This replaces the user-data Proxmox would generate, so the SSHkey comes from the cloud-config part, as on libvirt.
API_DOCUMENTATION.mdandAPI_CHANGELOG.mdcover the limit, the#!rule and the re-runs. Areinstall 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=1cargo test -p lnvps_api_common --features libvirt host::cloud_init -- --test-threads=1lnvps_api_commonunit tests--features libvirt,proxmoxAll of them pass.
New tests cover:
VmConfigcaptured from master;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, on20261201000000_audit_log_ip_truncate.sql;cargo fmt --all -- --check, on lines this PR doesn't touch;cargo clippy -- -D warnings, ontry-procedure/src/lib.rs:236and:558.Not in this PR
qm migratedoesn't write the user snippet on the target node. The network snippet has thesame gap today.
package_upgrade) no longerapply.