Skip to content

Commit f918bdc

Browse files
authored
Merge pull request #854 from srl-labs/ceos-if-wait
added if-wait script for ceos nodes
2 parents 456fd47 + e1a525a commit f918bdc

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

nodes/ceos/ceos.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"github.com/srl-labs/containerlab/utils"
2424
)
2525

26+
const (
27+
ifWaitScriptContainerPath = "/mnt/flash/if-wait.sh"
28+
)
29+
2630
var (
2731
// defined env vars for the ceos
2832
ceosEnv = map[string]string{
@@ -72,11 +76,14 @@ func (s *ceos) Init(cfg *types.NodeConfig, opts ...nodes.NodeOption) error {
7276
s.cfg.Env = utils.MergeStringMaps(ceosEnv, s.cfg.Env)
7377

7478
// the node.Cmd should be aligned with the environment.
79+
// prepending original Cmd with if-wait.sh script to make sure that interfaces are available
80+
// before init process starts
7581
var envSb strings.Builder
76-
envSb.WriteString("/sbin/init ")
82+
envSb.WriteString("bash -c '" + ifWaitScriptContainerPath + " ; exec /sbin/init ")
7783
for k, v := range s.cfg.Env {
7884
envSb.WriteString("systemd.setenv=" + k + "=" + v + " ")
7985
}
86+
envSb.WriteString("'")
8087
s.cfg.Cmd = envSb.String()
8188
s.cfg.MacAddress = utils.GenMac("00:1c:73")
8289

@@ -182,6 +189,11 @@ func createCEOSFiles(node *types.NodeConfig) error {
182189
err = utils.CreateFile(sysMacPath, m.String())
183190
}
184191

192+
// adding if-wait.sh script to flash dir
193+
ifScriptP := path.Join(node.LabDir, "flash", "if-wait.sh")
194+
utils.CreateFile(ifScriptP, utils.IfWaitScript)
195+
os.Chmod(ifScriptP, 0777) // skipcq: GSC-G302
196+
185197
return err
186198
}
187199

utils/if-wait.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package utils
2+
3+
// IfWaitScript is used in ENTRYPOINT/CMD of the nodes that need to ensure that all
4+
// of the clab links/interfaces are available in the container before calling the main process
5+
var IfWaitScript string = `#!/bin/sh
6+
7+
INTFS=$(echo $CLAB_INTFS)
8+
SLEEP=0
9+
10+
int_calc ()
11+
{
12+
index=0
13+
for i in $(ls -1v /sys/class/net/ | grep 'eth\|ens\|eno\|^e[0-9]'); do
14+
let index=index+1
15+
done
16+
MYINT=$index
17+
}
18+
19+
int_calc
20+
21+
echo "Waiting for all $INTFS interfaces to be connected"
22+
while [ "$MYINT" -lt "$INTFS" ]; do
23+
echo "Connected $MYINT interfaces out of $INTFS"
24+
sleep 1
25+
int_calc
26+
done
27+
28+
echo "Sleeping $SLEEP seconds before boot"
29+
sleep $SLEEP
30+
`

0 commit comments

Comments
 (0)