$content .= " gateway $net->{gw6}\n" if $net->{gw6};
}
}
}
return $content;
}
```
* New fonction before sub configdrive2_gen_metadata
Cloudbase-Init doesnt turn static network adapters back to DHCP configuration. This fonction will provide us the mac adresses of adapters to turn on dhcp from our config file and we will use it later with our script to enable dhcp on those network adapters.
```
sub get_mac_addresses {
my ($conf) = @_;
my $dhcpstring = undef;
my @dhcpmacs = ();
my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
foreach my $iface (sort @ifaces) {
(my $id = $iface) =~ s/^net//;
my $net = PVE::QemuServer::parse_net($conf->{$iface});
next if !$conf->{"ipconfig$id"};
my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
my $mac = lc $net->{macaddr};
if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){
push @dhcpmacs, $mac;
}
}
if (@dhcpmacs){
$dhcpstring .= ",\n \"dhcp\":[";
foreach my $mac (@dhcpmacs){
if ($mac != @dhcpmacs[-1]){
$dhcpstring .= "\"$mac\",";
}
else{
$dhcpstring .= "\"$mac\"]";
}
}
}
return ($dhcpstring);
}
```
* configdrive2_gen_metadata
We will generate our meta data variables from this fonction and call the give to another fonction which will format them.
We get DHCP macs from our previous fonction, UUID, Hostname, Username, Password and we generate a json list from ssh keys.
```
sub configdrive2_gen_metadata {
my ($conf, $vmid, $user, $network) = @_;
# Get DHCP nics by mac adresses
my $dhcpmacs = undef;
$dhcpmacs = get_mac_addresses($conf);
#Get UUID
my $uuid_str = Digest::SHA::sha1_hex($user.$network);
# Get hostname
my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
# Get username, default to Administrateur if none
my $username = "Administrateur";
if (defined($conf->{ciuser})){
$username = $conf->{ciuser};
}
# Get user password
my $password = $conf->{cipassword};
# Get ssh keys and make a list out of it in json format
$content .= " gateway $net->{gw6}\n" if $net->{gw6};
}
}
}
return $content;
}
```
* New fonction before sub configdrive2_gen_metadata
Cloudbase-Init doesnt turn static network adapters back to DHCP configuration. This fonction will provide us the mac adresses of adapters to turn on dhcp from our config file and we will use it later with our script to enable dhcp on those network adapters.
```
sub get_mac_addresses {
my ($conf) = @_;
my $dhcpstring = undef;
my @dhcpmacs = ();
my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
foreach my $iface (sort @ifaces) {
(my $id = $iface) =~ s/^net//;
my $net = PVE::QemuServer::parse_net($conf->{$iface});
next if !$conf->{"ipconfig$id"};
my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
my $mac = lc $net->{macaddr};
if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){
push @dhcpmacs, $mac;
}
}
if (@dhcpmacs){
$dhcpstring .= ",\n \"dhcp\":[";
foreach my $mac (@dhcpmacs){
if ($mac != @dhcpmacs[-1]){
$dhcpstring .= "\"$mac\",";
}
else{
$dhcpstring .= "\"$mac\"]";
}
}
}
return ($dhcpstring);
}
```
* configdrive2_gen_metadata
We will generate our meta data variables from this fonction and call the give to another fonction which will format them.
We get DHCP macs from our previous fonction, UUID, Hostname, Username, Password and we generate a json list from ssh keys.
```
sub configdrive2_gen_metadata {
my ($conf, $vmid, $user, $network) = @_;
# Get DHCP nics by mac adresses
my $dhcpmacs = undef;
$dhcpmacs = get_mac_addresses($conf);
#Get UUID
my $uuid_str = Digest::SHA::sha1_hex($user.$network);
# Get hostname
my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
# Get username, default to Administrateur if none
my $username = "Administrateur";
if (defined($conf->{ciuser})){
$username = $conf->{ciuser};
}
# Get user password
my $password = $conf->{cipassword};
# Get ssh keys and make a list out of it in json format