Skip to content

Commit b7e3cc4

Browse files
committed
minor changes
1 parent 5437f01 commit b7e3cc4

7 files changed

Lines changed: 47 additions & 12 deletions

File tree

config/cni.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import (
66
"github.com/projecteru2/cocoon/utils"
77
)
88

9-
const NetnsPath = "/var/run/netns"
9+
const (
10+
NetnsPath = "/var/run/netns"
11+
// NetnsPrefix prevents GC from deleting netns created by other tools
12+
// (docker, containerd, etc.). Only netns matching this prefix are managed.
13+
NetnsPrefix = "cocoon-"
14+
)
1015

1116
// EnsureCNIDirs creates all static directories required by the CNI network provider.
1217
func (c *Config) EnsureCNIDirs() error {
@@ -26,6 +31,12 @@ func (c *Config) CNIIndexLock() string { return filepath.Join(c.cniDBDir(), "net
2631
func (c *Config) CNICacheDir() string { return filepath.Join(c.cniDir(), "cache") }
2732

2833
// CNINetnsPath returns the named netns path for a VM.
34+
// Uses NetnsPrefix to namespace cocoon netns away from other tools.
2935
func (c *Config) CNINetnsPath(vmID string) string {
30-
return filepath.Join(NetnsPath, vmID)
36+
return filepath.Join(NetnsPath, NetnsPrefix+vmID)
37+
}
38+
39+
// CNINetnsName returns the named netns name (without path) for a VM.
40+
func (c *Config) CNINetnsName(vmID string) string {
41+
return NetnsPrefix + vmID
3142
}

gc/orchestrator.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ func (o *Orchestrator) Run(ctx context.Context) error {
4444
var skipped []string
4545
for _, m := range o.modules {
4646
ok, err := m.getLocker().TryLock(ctx)
47-
if err != nil || !ok {
48-
logger.Warnf(ctx, "skip %s: lock busy", m.getName())
47+
if err != nil {
48+
logger.Warnf(ctx, "skip %s: TryLock error: %v", m.getName(), err)
49+
skipped = append(skipped, m.getName())
50+
continue
51+
}
52+
if !ok {
53+
logger.Warnf(ctx, "skip %s: lock held by another operation", m.getName())
4954
skipped = append(skipped, m.getName())
5055
continue
5156
}

hypervisor/cloudhypervisor/create.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ const CowSerial = "cocoon-cow"
2525
// the DB), we write a placeholder record first, then create directories and
2626
// prepare disks, and finally update the record to Created state.
2727
func (ch *CloudHypervisor) Create(ctx context.Context, vmCfg *types.VMConfig, storageConfigs []*types.StorageConfig, networkConfigs []*types.NetworkConfig, bootCfg *types.BootConfig) (*types.VM, error) {
28-
id := hypervisor.GenerateID()
28+
id, err := hypervisor.GenerateID()
29+
if err != nil {
30+
return nil, fmt.Errorf("generate VM ID: %w", err)
31+
}
2932
now := time.Now()
3033

3134
blobIDs := extractBlobIDs(storageConfigs, bootCfg)
@@ -60,7 +63,6 @@ func (ch *CloudHypervisor) Create(ctx context.Context, vmCfg *types.VMConfig, st
6063
var (
6164
preparedStorage []*types.StorageConfig
6265
bootCopy *types.BootConfig
63-
err error
6466
)
6567
if bootCfg != nil {
6668
b := *bootCfg
@@ -136,6 +138,8 @@ func (ch *CloudHypervisor) prepareOCI(ctx context.Context, vmID string, vmCfg *t
136138

137139
// Append static IP configuration for each network interface.
138140
// Format: ip=<client-IP>:<server>:<gw-IP>:<netmask>:<hostname>:<device>:<autoconf>
141+
// The index i matches the CH --net ordering, which maps 1:1 to guest eth{i}.
142+
// NICs with Network==nil (DHCP) still occupy their slot but get no ip= param.
139143
if len(networkConfigs) > 0 {
140144
cmdline.WriteString(" net.ifnames=0")
141145
for i, n := range networkConfigs {
@@ -184,6 +188,7 @@ func (ch *CloudHypervisor) prepareCloudimg(ctx context.Context, vmID string, vmC
184188
Hostname: vmCfg.Name,
185189
RootPassword: ch.conf.DefaultRootPassword,
186190
}
191+
// Index i matches CH --net order → guest eth{i}. See prepareOCI comment.
187192
for i, n := range networkConfigs {
188193
if n.Network != nil {
189194
ones, _ := n.Network.Netmask.Size()

hypervisor/cloudhypervisor/gc.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"os"
8+
"strings"
89
"time"
910

1011
"github.com/projecteru2/cocoon/config"
@@ -61,11 +62,13 @@ func (ch *CloudHypervisor) GCModule() gc.Module[chSnapshot] {
6162
if snap.logDirs, err = utils.ScanSubdirs(ch.conf.CHLogDir()); err != nil {
6263
return snap, err
6364
}
64-
// Scan named netns: entries in /var/run/netns/ that match VM IDs.
65-
// These are bind-mount files (not dirs), so use ScanEntries.
65+
// Scan named netns with the cocoon- prefix only.
66+
// Other tools (docker, containerd) may have their own entries.
6667
if entries, readErr := os.ReadDir(config.NetnsPath); readErr == nil {
6768
for _, e := range entries {
68-
snap.netnsNames = append(snap.netnsNames, e.Name())
69+
if name, ok := strings.CutPrefix(e.Name(), config.NetnsPrefix); ok {
70+
snap.netnsNames = append(snap.netnsNames, name)
71+
}
6972
}
7073
}
7174
return snap, nil

hypervisor/db.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ func (idx *VMIndex) Init() {
3636
}
3737

3838
// GenerateID returns a random 16-character hex string (8 bytes of entropy).
39-
func GenerateID() string {
39+
func GenerateID() (string, error) {
4040
var b [8]byte
4141
if _, err := rand.Read(b[:]); err != nil {
42-
panic("crypto/rand failed: " + err.Error())
42+
return "", err
4343
}
44-
return hex.EncodeToString(b[:])
44+
return hex.EncodeToString(b[:]), nil
4545
}
4646

4747
// ResolveVMRef resolves a ref (exact ID, name, or ID prefix ≥3 chars) to a full VM ID.

network/cni/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package cni
22

33
import (
44
"context"
5+
"fmt"
56

7+
"github.com/projecteru2/cocoon/network"
68
"github.com/projecteru2/cocoon/types"
79
)
810

911
func (c *CNI) Config(_ context.Context, _ []*types.VMConfig) ([][]*types.NetworkConfig, error) {
12+
if c.networkConfList == nil || c.cniConf == nil {
13+
return nil, fmt.Errorf("%w: no conflist found in %s", network.ErrNotConfigured, c.conf.CNIConfDir)
14+
}
1015
panic("not implemented")
1116
}

network/network.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package network
22

33
import (
44
"context"
5+
"errors"
56

67
"github.com/projecteru2/cocoon/types"
78
)
89

10+
var (
11+
ErrNotFound = errors.New("network not found")
12+
ErrNotConfigured = errors.New("network provider not configured")
13+
)
14+
915
type Network interface {
1016
Type() string
1117

0 commit comments

Comments
 (0)