Skip to content

Instantly share code, notes, and snippets.

@TimJones
Created November 24, 2018 16:52
Show Gist options
  • Save TimJones/b7a593de4d1b6f023e8f61a9d329f72f to your computer and use it in GitHub Desktop.
Save TimJones/b7a593de4d1b6f023e8f61a9d329f72f to your computer and use it in GitHub Desktop.
Proxmox helper script to load custom cloudinit user-data
#!/usr/bin/perl
use strict;
use warnings;
use PVE::Tools qw(file_get_contents);
use PVE::QemuConfig;
use PVE::QemuServer;
use PVE::QemuServer::Cloudinit;
my $vmid = shift or die("Missing VMID argument");
my $userdatafile = shift or die("Missing user-data file argument");
my $conf = PVE::QemuConfig->load_config($vmid);
# Monkey-patch the PVE::QemuServerCloudinit::cloudinit_userdata function
# to simply return the contents of the supplied file
*{PVE::QemuServer::Cloudinit::cloudinit_userdata} = sub {
return file_get_contents($userdatafile);
};
PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
# Reload the cloudinit drive
PVE::QemuServer::foreach_drive($conf, sub {
my ($ds, $drive) = @_;
my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
my $storecfg = PVE::Storage::config();
my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
PVE::QemuServer::vm_mon_cmd($vmid, "eject", force => JSON::true, device => "drive-$ds");
PVE::QemuServer::vm_mon_cmd($vmid, "change", device => "drive-$ds", target => "$path");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment