Technical

Proxmox snippets feature: enable storage, upload files, wire them into cloud-init and hookscripts

Proxmox snippets feature: enable storage, upload files, wire them into cloud-init and hookscripts

Proxmox snippets feature is just a way to store small text files—cloud-init configs, hookscripts—on your storage so VMs can use them. The docs list ‘snippets’ as a content type but don’t tell you how to actually set it up. Here’s the missing part.

Black computer cables splayed on a vibrant yellow surface, highlighting technology connection themes.

Enable snippet storage

First, pick a storage that supports snippets. Directory storage works fine. If you’re using the default local storage, it already has snippets enabled? Nope, not by default. You have to add it.

Go to Datacenter → Storage, select your storage, click Edit. In the Content dropdown, check ‘Snippets’. Save. That’s it. Now you can upload files.

Upload a snippet

Back in the storage view, you’ll see a ‘Snippets’ section. Click Upload, choose your file. It lands in the storage’s snippets directory. For local that’s /var/lib/vz/snippets/.

You can also just scp the file there directly. Same thing.

Detailed close-up of ethernet cables and network connections on a router, showcasing modern technology.

Use a snippet for cloud-init

When you create a VM with a cloud-init drive, you can point it at a snippet. In the VM’s Cloud-Init tab, there’s a ‘Cloud-Init’ dropdown. Select your snippet file. Proxmox merges it with the generated config. Handy for setting users, SSH keys, packages.

Example snippet:

#cloud-config
users:
  - name: admin
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
      - ssh-rsa AAAAB3...

No need to rebuild the template every time. Just edit the snippet.

Use a snippet as a hookscript

Hookscripts are shell scripts that run at VM lifecycle events. Upload the script as a snippet, then set it in the VM’s Options tab under Hookscript. Proxmox runs it with the VM ID and event phase as arguments.

Basic hookscript:

#!/bin/bash
if [ "$2" == "pre-start" ]; then
  echo "VM $1 starting"
fi

Make sure it’s executable (chmod +x) before uploading. If the hookscript doesn’t run, check this post on tracing the invocation path.

Why bother with snippets?

They keep configs versionable and shared across VMs. Instead of editing each VM’s cloud-init or copying hookscripts around, you update one file. It’s less to undo when things change.

If you’d rather not deal with manual uploads, you could use a configuration management tool, but for a homelab, snippets are fine. No subscription needed.

Leave a comment

Comments are reviewed before they appear. Your email is never published.