Skip to content

Commit ea372cc

Browse files
authored
Add get_graph service to fetch minigraph automatically (#288)
- Add a functionality to get SNMP community from DHCP (option 224) - Add a functionality to get minigraph from http service instead of using default minigraph - The url for graph service is passed through DHCP option 225 - This feature is by default disabled. Modify rule/config to enable it on build time, or modify /etc/sonic/graph_service_url on run time. - Fix a bug that getting hostname from DHCP is not working correctly
1 parent 2d0b41a commit ea372cc

13 files changed

+162
-21
lines changed

build_debian.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
188188
usbutils \
189189
pciutils \
190190
iptables-persistent \
191-
logrotate
191+
logrotate \
192+
curl
192193

193194
## Remove sshd host keys, and will regenerate on first sshd start
194195
sudo rm -f $FILESYSTEM_ROOT/etc/ssh/ssh_host_*_key*
@@ -241,6 +242,9 @@ EOF
241242

242243
sudo cp files/dhcp/rfc3442-classless-routes $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d
243244
sudo cp files/dhcp/sethostname $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
245+
sudo cp files/dhcp/graphserviceurl $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
246+
sudo cp files/dhcp/snmpcommunity $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
247+
sudo cp files/dhcp/dhclient.conf $FILESYSTEM_ROOT/etc/dhcp/
244248

245249
if [ -f sonic_debian_extension.sh ]; then
246250
./sonic_debian_extension.sh $FILESYSTEM_ROOT

files/build_templates/sonic_debian_extension.j2

+15
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable interfaces-config.service
9494
sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.sh $FILESYSTEM_ROOT/usr/bin/
9595
sudo cp $IMAGE_CONFIGS/interfaces/*.j2 $FILESYSTEM_ROOT/etc/sonic/templates/
9696

97+
# Copy initial interfaces configuration file, will be overwritten on first boot
98+
sudo cp $IMAGE_CONFIGS/interfaces/init_interfaces $FILESYSTEM_ROOT/etc/network
99+
100+
# Copy updategraph script and service file
101+
sudo cp $IMAGE_CONFIGS/updategraph/updategraph.service $FILESYSTEM_ROOT/etc/systemd/system/
102+
sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable updategraph.service
103+
sudo cp $IMAGE_CONFIGS/updategraph/updategraph $FILESYSTEM_ROOT/usr/bin/
104+
{% if enable_dhcp_graph_service == "y" %}
105+
sudo bash -c "echo enabled=true > $FILESYSTEM_ROOT/etc/sonic/updategraph.conf"
106+
sudo bash -c "echo src=dhcp >> $FILESYSTEM_ROOT/etc/sonic/updategraph.conf"
107+
sudo bash -c "echo dhcp_as_static=true >> $FILESYSTEM_ROOT/etc/sonic/updategraph.conf"
108+
{% else %}
109+
sudo bash -c "echo enabled=false > $FILESYSTEM_ROOT/etc/sonic/updategraph.conf"
110+
{% endif %}
111+
97112
# Copy SNMP configuration files
98113
sudo cp $IMAGE_CONFIGS/snmp/snmp.yml $FILESYSTEM_ROOT/etc/sonic/
99114

files/dhcp/dhclient.conf

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Configuration file for /sbin/dhclient, which is included in Debian's
2+
# dhcp3-client package.
3+
#
4+
# This is a sample configuration file for dhclient. See dhclient.conf's
5+
# man page for more information about the syntax of this file
6+
# and a more comprehensive list of the parameters understood by
7+
# dhclient.
8+
#
9+
# Normally, if the DHCP server provides reasonable information and does
10+
# not leave anything out (like the domain name, for example), then
11+
# few changes must be made to this file, if any.
12+
#
13+
14+
option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
15+
option snmp-community code 224 = text;
16+
option minigraph-url code 225 = text;
17+
18+
send host-name = gethostname();
19+
request subnet-mask, broadcast-address, time-offset, routers,
20+
domain-name, domain-name-servers, domain-search, host-name,
21+
dhcp6.name-servers, dhcp6.domain-search,
22+
netbios-name-servers, netbios-scope, interface-mtu,
23+
rfc3442-classless-static-routes, ntp-servers, snmp-community, minigraph-url;
24+

files/dhcp/graphserviceurl

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
case $reason in
2+
BOUND|RENEW|REBIND|REBOOT)
3+
if [ -n "$new_minigraph_url" ]; then
4+
echo $new_minigraph_url > /tmp/dhcp_graph_url
5+
else
6+
echo "N/A" > /tmp/dhcp_graph_url
7+
fi
8+
;;
9+
esac

files/dhcp/sethostname

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
#!/bin/bash
2-
# This script is to update hostname of the system.
1+
case $reason in
2+
BOUND|RENEW|REBIND|REBOOT)
3+
current_host_name=`hostname -s`
4+
if [ "$current_host_name" != "$new_host_name" ] && [ -n "$new_host_name" ]
5+
then
6+
echo $new_host_name > /etc/hostname
7+
hostname -F /etc/hostname
8+
sed -i "/\s$current_host_name$/d" /etc/hosts
9+
echo "127.0.0.1 $new_host_name" >> /etc/hosts
10+
fi
11+
;;
12+
esac
313

4-
if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
5-
&& [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
6-
then
7-
exit 0
8-
fi
9-
10-
current_host_name=`hostname -s`
11-
12-
if [ "$current_host_name" != "$new_host_name" ]
13-
then
14-
echo $new_host_name > /etc/hostname
15-
line_to_replace=`grep 127.0.0.1.*$current_host_name /etc/hosts`
16-
new_line=`echo $line_to_replace | sed "s/$current_host_name/$new_host_name/"`
17-
sed -i "s/$line_to_replace/$new_line/" /etc/hosts
18-
19-
hostname -F /etc/hostname
20-
fi

files/dhcp/snmpcommunity

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
case $reason in
2+
BOUND|RENEW|REBIND|REBOOT)
3+
if [ -n "${new_snmp_community}" ]; then
4+
if [ -f /etc/sonic/snmp.yml ]; then
5+
sed -i "s/^snmp_rocommunity:.*/snmp_rocommunity: $new_snmp_community/g" /etc/sonic/snmp.yml
6+
else
7+
echo "snmp_rocommunity: "$new_snmp_community > /etc/sonic/snmp.yml
8+
fi
9+
fi
10+
;;
11+
esac
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Initial /etc/network/interface file for first boot
2+
# Will be overwritten based on minigraph information by interfaces-config service
3+
4+
# The loopback network interface
5+
auto lo
6+
iface lo inet loopback
7+
#
8+
# The management network interface
9+
auto eth0
10+
iface eth0 inet dhcp
11+

files/image_config/interfaces/interfaces-config.service

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Unit]
22
Description=Update interfaces configuration
3-
Before=network.target
3+
Before=database.service
44

55
[Service]
66
Type=oneshot

files/image_config/interfaces/interfaces-config.sh

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
sonic-cfggen -m /etc/sonic/minigraph.xml -t /etc/sonic/templates/interfaces.j2 >/etc/network/interfaces
44
sonic-cfggen -m /etc/sonic/minigraph.xml -t /etc/sonic/templates/vlan_interfaces.j2 >/etc/network/interfaces.d/vlan_interfaces
55
sonic-cfggen -m /etc/sonic/minigraph.xml -t /etc/sonic/templates/lag_interfaces.j2 >/etc/network/interfaces.d/lag_interfaces
6+
ifdown eth0 && ifup eth0
7+
ifdown lo && ifup lo
8+
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
if [ ! -f /etc/sonic/updategraph.conf ]; then
4+
echo "No updategraph.conf found, generating a default one."
5+
echo "enabled=false" >/etc/sonic/updategraph.conf
6+
fi
7+
8+
. /etc/sonic/updategraph.conf
9+
10+
if [ "$enabled" != "true" ]; then
11+
echo "Disabled in updategraph.conf. Skipping graph update."
12+
exit 0
13+
fi
14+
15+
if [ "$src" = "dhcp" ]; then
16+
while [ ! -f /tmp/dhcp_graph_url ]; do
17+
echo "Waiting for DHCP response..."
18+
sleep 1
19+
done
20+
21+
if [ "`cat /tmp/dhcp_graph_url`" = "N/A" ]; then
22+
echo "No graph_url option in DHCP response. Skipping graph update."
23+
if [ "$dhcp_as_static" = "true" ]; then
24+
sed -i "/enabled=/d" /etc/sonic/updategraph.conf
25+
echo "enabled=false" >> /etc/sonic/updategraph.conf
26+
fi
27+
exit 0
28+
fi
29+
30+
HOSTNAME=`hostname -s`
31+
GRAPH_URL=`sonic-cfggen -t /tmp/dhcp_graph_url -a "{\"hostname\": \"$HOSTNAME\"}"`
32+
if [ "$dhcp_as_static" = "true" ]; then
33+
sed -i "/src=d/d" /etc/sonic/updategraph.conf
34+
echo "src=$GRAPH_URL" >> /etc/sonic/updategraph.conf
35+
fi
36+
else
37+
GRAPH_URL=$src
38+
fi
39+
40+
if [ -f /etc/sonic/minigraph.xml ]; then
41+
echo "Renaming minigraph.xml to minigraph.old"
42+
mv /etc/sonic/minigraph.xml /etc/sonic/minigraph.old
43+
fi
44+
45+
echo "Getting minigraph from $GRAPH_URL"
46+
47+
while true; do
48+
curl -f $GRAPH_URL -o /etc/sonic/minigraph.xml --connect-timeout 15 && break
49+
sleep 5
50+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Unit]
2+
Description=download minigraph from graph service
3+
Before=ntp-config.service
4+
Before=rsyslog-config.service
5+
Before=interfaces-config.service
6+
7+
[Service]
8+
Type=oneshot
9+
ExecStart=/usr/bin/updategraph
10+
11+
[Install]
12+
WantedBy=multi-user.target

rules/config

+7
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ USERNAME = admin
3535

3636
# PASSWORD - password for installer build
3737
PASSWORD = YourPaSsWoRd
38+
39+
# ENABLE_DHCP_GRAPH_SERVICE - specify the source of minigraph to generate configuration file.
40+
# If set to y SONiC will get the minigraph from graph service. Graph service URL need to be
41+
# passed through DHCP option 225.
42+
# If not set (default behavior) the default minigraph built into the image will be used.
43+
# ENABLE_DHCP_GRAPH_SERVICE = y
44+

slave.mk

+2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : .platform
298298
export sonicadmin_user="$(USERNAME)"
299299
export sonic_hwsku="$(CONFIGURED_SKU)"
300300
export sonic_asic_platform="$(CONFIGURED_PLATFORM)"
301+
export enable_dhcp_graph_service="$(ENABLE_DHCP_GRAPH_SERVICE)"
302+
301303
$(foreach docker, $($*_DOCKERS),\
302304
export docker_image="$(docker)"
303305
export docker_image_name="$(basename $(docker))"

0 commit comments

Comments
 (0)