Skip to content

Commit bb72490

Browse files
committed
Enhance DHCP functionality by adding lease time support across various components. Updated DhcpEntryCommand to include lease time, modified VmDhcpConfig to handle lease time, and adjusted related scripts and configurations to accommodate this new parameter. This allows for configurable DHCP lease durations, improving flexibility in network management.
1 parent b8813c7 commit bb72490

File tree

9 files changed

+76
-17
lines changed

9 files changed

+76
-17
lines changed

core/src/main/java/com/cloud/agent/api/routing/DhcpEntryCommand.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class DhcpEntryCommand extends NetworkElementCommand {
3636
private boolean isDefault;
3737
boolean executeInSequence = false;
3838
boolean remove;
39+
Long leaseTime;
3940

4041
public boolean isRemove() {
4142
return remove;
@@ -152,4 +153,12 @@ public boolean isDefault() {
152153
public void setDefault(boolean isDefault) {
153154
this.isDefault = isDefault;
154155
}
156+
157+
public Long getLeaseTime() {
158+
return leaseTime;
159+
}
160+
161+
public void setLeaseTime(Long leaseTime) {
162+
this.leaseTime = leaseTime;
163+
}
155164
}

core/src/main/java/com/cloud/agent/resource/virtualnetwork/facade/DhcpEntryConfigItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
3535
final DhcpEntryCommand command = (DhcpEntryCommand) cmd;
3636

3737
final VmDhcpConfig vmDhcpConfig = new VmDhcpConfig(command.getVmName(), command.getVmMac(), command.getVmIpAddress(), command.getVmIp6Address(), command.getDuid(), command.getDefaultDns(),
38-
command.getDefaultRouter(), command.getStaticRoutes(), command.isDefault(), command.isRemove());
38+
command.getDefaultRouter(), command.getStaticRoutes(), command.isDefault(), command.isRemove(), command.getLeaseTime());
3939

4040
return generateConfigItems(vmDhcpConfig);
4141
}

core/src/main/java/com/cloud/agent/resource/virtualnetwork/model/VmDhcpConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class VmDhcpConfig extends ConfigBase {
2929
private String defaultGateway;
3030
private String staticRoutes;
3131
private boolean defaultEntry;
32+
private Long leaseTime;
3233

3334
// Indicate if the entry should be removed when set to true
3435
private boolean remove;
@@ -39,6 +40,11 @@ public VmDhcpConfig() {
3940

4041
public VmDhcpConfig(String hostName, String macAddress, String ipv4Address, String ipv6Address, String ipv6Duid, String dnsAddresses, String defaultGateway,
4142
String staticRoutes, boolean defaultEntry, boolean remove) {
43+
this(hostName, macAddress, ipv4Address, ipv6Address, ipv6Duid, dnsAddresses, defaultGateway, staticRoutes, defaultEntry, remove, null);
44+
}
45+
46+
public VmDhcpConfig(String hostName, String macAddress, String ipv4Address, String ipv6Address, String ipv6Duid, String dnsAddresses, String defaultGateway,
47+
String staticRoutes, boolean defaultEntry, boolean remove, Long leaseTime) {
4248
super(VM_DHCP);
4349
this.hostName = hostName;
4450
this.macAddress = macAddress;
@@ -50,6 +56,7 @@ public VmDhcpConfig(String hostName, String macAddress, String ipv4Address, Stri
5056
this.staticRoutes = staticRoutes;
5157
this.defaultEntry = defaultEntry;
5258
this.remove = remove;
59+
this.leaseTime = leaseTime;
5360
}
5461

5562
public String getHostName() {
@@ -132,4 +139,12 @@ public void setDefaultEntry(boolean defaultEntry) {
132139
this.defaultEntry = defaultEntry;
133140
}
134141

142+
public Long getLeaseTime() {
143+
return leaseTime;
144+
}
145+
146+
public void setLeaseTime(Long leaseTime) {
147+
this.leaseTime = leaseTime;
148+
}
149+
135150
}

scripts/network/exdhcp/dhcpd_edithosts.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
# under the License.
1818

1919

20-
# Usage: dhcpd_edithosts.py mac ip hostname dns gateway nextserver
20+
# Usage: dhcpd_edithosts.py mac ip hostname dns gateway nextserver [leasetime]
2121
import sys, os
2222
from os.path import exists
2323
from time import sleep
2424
from os import remove
2525

26-
usage = '''dhcpd_edithosts.py mac ip hostname dns gateway nextserver'''
26+
usage = '''dhcpd_edithosts.py mac ip hostname dns gateway nextserver [leasetime]'''
2727
conf_path = "/etc/dhcpd.conf"
2828
file_lock = "/etc/dhcpd.conf_locked"
2929
sleep_max = 20
30-
host_entry = 'host %s { hardware ethernet %s; fixed-address %s; option domain-name-servers %s; option domain-name "%s"; option routers %s; default-lease-time infinite; max-lease-time infinite; min-lease-time infinite; filename "pxelinux.0";}'
31-
host_entry1 = 'host %s { hardware ethernet %s; fixed-address %s; option domain-name-servers %s; option domain-name "%s"; option routers %s; default-lease-time infinite; max-lease-time infinite; min-lease-time infinite; next-server %s; filename "pxelinux.0";}'
30+
host_entry = 'host %s { hardware ethernet %s; fixed-address %s; option domain-name-servers %s; option domain-name "%s"; option routers %s; default-lease-time %s; max-lease-time %s; min-lease-time %s; filename "pxelinux.0";}'
31+
host_entry1 = 'host %s { hardware ethernet %s; fixed-address %s; option domain-name-servers %s; option domain-name "%s"; option routers %s; default-lease-time %s; max-lease-time %s; min-lease-time %s; next-server %s; filename "pxelinux.0";}'
3232
def lock():
3333
if exists(file_lock):
3434
count = 0
@@ -59,10 +59,14 @@ def unlock():
5959
print "Cannot remove file lock at %s" % file_lock
6060
return False
6161

62-
def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
62+
def insert_host_entry(mac, ip, hostname, dns, gateway, next_server, lease_time="infinite"):
6363
if lock() == False:
6464
return 1
6565

66+
# Convert 0 to 'infinite' for lease time
67+
if lease_time == "0":
68+
lease_time = "infinite"
69+
6670
cmd = 'sed -i /"fixed-address %s"/d %s' % (ip, conf_path)
6771
ret = os.system(cmd)
6872
if ret != 0:
@@ -78,9 +82,9 @@ def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
7882
return 1
7983

8084
if next_server != "null":
81-
entry = host_entry1 % (hostname, mac, ip, dns, "cloudnine.internal", gateway, next_server)
85+
entry = host_entry1 % (hostname, mac, ip, dns, "cloudnine.internal", gateway, lease_time, lease_time, lease_time, next_server)
8286
else:
83-
entry = host_entry % (hostname, mac, ip, dns, "cloudnine.internal", gateway)
87+
entry = host_entry % (hostname, mac, ip, dns, "cloudnine.internal", gateway, lease_time, lease_time, lease_time)
8488
cmd = '''echo '%s' >> %s''' % (entry, conf_path)
8589
ret = os.system(cmd)
8690
if ret != 0:
@@ -111,12 +115,13 @@ def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
111115
dns = sys.argv[4]
112116
gateway = sys.argv[5]
113117
next_server = sys.argv[6]
118+
lease_time = sys.argv[7] if len(sys.argv) > 7 else "infinite"
114119

115120
if exists(conf_path) == False:
116121
conf_path = "/etc/dhcp/dhcpd.conf"
117122
if exists(conf_path) == False:
118123
print "Cannot find dhcpd.conf"
119124
sys.exit(1)
120125

121-
ret = insert_host_entry(mac, ip, hostname, dns, gateway, next_server)
126+
ret = insert_host_entry(mac, ip, hostname, dns, gateway, next_server, lease_time)
122127
sys.exit(ret)

scripts/network/exdhcp/dnsmasq_edithosts.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# $1 : the mac address
2222
# $2 : the associated ip address
2323
# $3 : the hostname
24+
# $4 : the lease time (optional, defaults to 'infinite')
2425

2526
wait_for_dnsmasq () {
2627
local _pid=$(pidof dnsmasq)
@@ -41,11 +42,17 @@ no_dhcp_release=$?
4142
[ ! -f /etc/dhcphosts.txt ] && touch /etc/dhcphosts.txt
4243
[ ! -f /var/lib/misc/dnsmasq.leases ] && touch /var/lib/misc/dnsmasq.leases
4344

45+
# Set lease time, default to 'infinite', convert 0 to 'infinite'
46+
lease_time=${4:-infinite}
47+
if [ "$lease_time" = "0" ]; then
48+
lease_time=infinite
49+
fi
50+
4451
sed -i /$1/d /etc/dhcphosts.txt
4552
sed -i /$2,/d /etc/dhcphosts.txt
4653
sed -i /$3,/d /etc/dhcphosts.txt
4754

48-
echo "$1,$2,$3,infinite" >>/etc/dhcphosts.txt
55+
echo "$1,$2,$3,$lease_time" >>/etc/dhcphosts.txt
4956

5057
#release previous dhcp lease if present
5158
if [ $no_dhcp_release -eq 0 ]

server/src/main/java/com/cloud/configuration/Config.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ public enum Config {
351351
ConfigKey.Kind.CSV,
352352
null),
353353

354+
DhcpLeaseTimeout(
355+
"Network",
356+
ManagementServer.class,
357+
Integer.class,
358+
"dhcp.lease.timeout",
359+
"0",
360+
"DHCP lease time in seconds for VMs. Use 0 for infinite lease time (default). A non-zero value sets the lease duration in seconds.",
361+
null),
362+
354363
//VPN
355364
RemoteAccessVpnPskLength(
356365
"Network",

server/src/main/java/com/cloud/network/router/CommandSetupHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ public void createDhcpEntryCommand(final VirtualRouter router, final UserVm vm,
296296
dhcpCommand.setDefault(nic.isDefaultNic());
297297
dhcpCommand.setRemove(remove);
298298

299+
// Set DHCP lease timeout from global config (0 = infinite)
300+
String leaseTimeStr = _configDao.getValue(Config.DhcpLeaseTimeout.key());
301+
Long leaseTime = (leaseTimeStr != null) ? Long.parseLong(leaseTimeStr) : 0L;
302+
dhcpCommand.setLeaseTime(leaseTime);
303+
299304
dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
300305
dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
301306
dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, _routerControlHelper.getRouterIpInNetwork(nic.getNetworkId(), router.getId()));

systemvm/debian/opt/cloud/bin/cs/CsDhcp.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,14 @@ def write_hosts(self):
199199

200200
def add(self, entry):
201201
self.add_host(entry['ipv4_address'], entry['host_name'])
202-
# Lease time set to "infinite" since we properly control all DHCP/DNS config via CloudStack.
202+
# Lease time is configurable via CloudStack global config dhcp.lease.timeout
203+
# 0 = infinite (default), otherwise the value represents seconds
203204
# Infinite time helps avoid some edge cases which could cause DHCPNAK being sent to VMs since
204205
# (RHEL) system lose routes when they receive DHCPNAK.
205206
# When VM is expunged, its active lease and DHCP/DNS config is properly removed from related files in VR,
206207
# so the infinite duration of lease does not cause any issues or garbage.
207-
lease = 'infinite'
208+
lease_time = entry.get('lease_time', 0)
209+
lease = 'infinite' if lease_time == 0 else str(lease_time)
208210

209211
if entry['default_entry']:
210212
self.dhcp_hosts.add("%s,%s,%s,%s" % (entry['mac_address'],

systemvm/debian/opt/cloud/bin/edithosts.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# edithosts.sh -- edit the dhcphosts file on the routing domain
2222

2323
usage() {
24-
printf "Usage: %s: -m <MAC address> -4 <IPv4 address> -6 <IPv6 address> -h <hostname> -d <default router> -n <name server address> -s <Routes> -u <DUID> [-N]\n" $(basename $0) >&2
24+
printf "Usage: %s: -m <MAC address> -4 <IPv4 address> -6 <IPv6 address> -h <hostname> -d <default router> -n <name server address> -s <Routes> -u <DUID> -l <lease time> [-N]\n" $(basename $0) >&2
2525
}
2626

2727
mac=
@@ -33,8 +33,9 @@ dns=
3333
routes=
3434
duid=
3535
nondefault=
36+
lease_time=infinite
3637

37-
while getopts 'm:4:h:d:n:s:6:u:N' OPTION
38+
while getopts 'm:4:h:d:n:s:6:u:l:N' OPTION
3839
do
3940
case $OPTION in
4041
m) mac="$OPTARG"
@@ -53,6 +54,8 @@ do
5354
;;
5455
s) routes="$OPTARG"
5556
;;
57+
l) lease_time="$OPTARG"
58+
;;
5659
N) nondefault=1
5760
;;
5861
?) usage
@@ -124,17 +127,21 @@ fi
124127
sed -i /$host,/d $DHCP_HOSTS
125128

126129
#put in the new entry
130+
# If lease_time is 0, use 'infinite', otherwise use the value
131+
if [ "$lease_time" = "0" ]; then
132+
lease_time=infinite
133+
fi
127134
if [ $ipv4 ]
128135
then
129-
echo "$mac,$ipv4,$host,infinite" >>$DHCP_HOSTS
136+
echo "$mac,$ipv4,$host,$lease_time" >>$DHCP_HOSTS
130137
fi
131138
if [ $ipv6 ]
132139
then
133140
if [ $nondefault ]
134141
then
135-
echo "id:$duid,set:nondefault6,[$ipv6],$host,infinite" >>$DHCP_HOSTS
142+
echo "id:$duid,set:nondefault6,[$ipv6],$host,$lease_time" >>$DHCP_HOSTS
136143
else
137-
echo "id:$duid,[$ipv6],$host,infinite" >>$DHCP_HOSTS
144+
echo "id:$duid,[$ipv6],$host,$lease_time" >>$DHCP_HOSTS
138145
fi
139146
fi
140147

0 commit comments

Comments
 (0)