[pve-devel] [PATCH #1752 pve-manager 1/1] [PATCH #1752 pve-manager] Impl basic wake on LAN

Christian Ebner c.ebner at proxmox.com
Mon Jan 14 15:05:03 CET 2019


Provides the basic functionality to provide a wake on LAN feature implementation to start nodes in a cluster from other nodes.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 PVE/API2/Nodes.pm | 41 +++++++++++++++++++++++++++++++++++++++++
 PVE/NodeConfig.pm |  6 ++++++
 2 files changed, 47 insertions(+)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index dd5471f8..f59133dc 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -168,6 +168,7 @@ __PACKAGE__->register_method ({
 	    { name => 'version' },
 	    { name => 'syslog' },
 	    { name => 'status' },
+	    { name => 'wol' },
 	    { name => 'subscription' },
 	    { name => 'report' },
 	    { name => 'tasks' },
@@ -466,6 +467,46 @@ __PACKAGE__->register_method({
 	return undef;
     }});
 
+__PACKAGE__->register_method({
+    name => 'wol',
+    path => 'wol',
+    method => 'POST',
+    permissions => {
+	check => ['perm', '/nodes/{node}', [ 'Sys.PowerMgmt' ]],
+    },
+    protected => 1,
+    description => "Boot node via 'wake on LAN'.",
+    proxyto => 'node',
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	},
+    },
+    returns => { type => "null" },
+    code => sub {
+	my ($param) = @_;
+
+	my $config = PVE::NodeConfig::load_config($param->{node});
+	my $mac_addr = $config->{mac};
+	die "No wake on LAN mac address defined for node '$param->{node}'!\n" unless ($mac_addr);
+
+	my $prefix = chr(0xff) x 6;
+	my @mac = split(':', $mac_addr);
+	my $mac = pack('H*', join('', @mac));
+	my $packet = $prefix . $mac x 16;
+
+	my $proto = getprotobyname('udp');
+	my $addr = gethostbyname('255.255.255.255');
+	my $port = getservbyname('discard', 'udp');
+	my $to = Socket::pack_sockaddr_in($port, $addr);
+	socket(my $sock, Socket::AF_INET, Socket::SOCK_DGRAM, $proto) or die "Unable to open socket for sending wake on LAN magic packet!\n";
+	setsockopt($sock, Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1) or die "Unable to set socket options for sending wake on LAN magic packet!\n";
+	send($sock, $packet, 0, $to) or die "Unable to send wake on LAN magic packet!\n";
+	close($sock);
+
+	return undef;
+    }});
 
 __PACKAGE__->register_method({
     name => 'rrd', 
diff --git a/PVE/NodeConfig.pm b/PVE/NodeConfig.pm
index af1c782b..3cc8ba48 100644
--- a/PVE/NodeConfig.pm
+++ b/PVE/NodeConfig.pm
@@ -61,6 +61,12 @@ my $confdesc = {
 	description => 'Node description/comment.',
 	optional => 1,
     },
+    mac => {
+	type => 'string',
+	description => 'MAC address for wake on LAN',
+	pattern => '^([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})$',
+	optional => 1,
+    },
 };
 
 my $acmedesc = {
-- 
2.11.0




More information about the pve-devel mailing list