From 7525bfde75ba709437d9c19e03eb489e9260cfa9 Mon Sep 17 00:00:00 2001
From: Itxaka <itxaka@kairos.io>
Date: Mon, 14 Apr 2025 13:33:22 +0200
Subject: [PATCH] Enable the use of common sysexts

This enables adding the common sysext to the entries to enable

Signed-off-by: Itxaka <itxaka@kairos.io>
---
 pkg/state/steps_shared.go | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/pkg/state/steps_shared.go b/pkg/state/steps_shared.go
index 16844f7..03e6eec 100644
--- a/pkg/state/steps_shared.go
+++ b/pkg/state/steps_shared.go
@@ -448,9 +448,9 @@ func (s *State) EnableSysExtensions(g *herd.Graph, opts ...herd.OpOption) error
 			return nil
 		}
 
-		// If we wanted to use a common dir for extensions used for both entries, here we would do something like:
-		// commonEntries, _ := os.ReadDir(s.path(fmt.Sprintf("%s/%s", cnst.SourceSysExtDir, "common")))
-		// entries = append(entries, commonEntries...)
+		// Common dir is always there for all states no matter what
+		commonEntries, _ := os.ReadDir(s.path(fmt.Sprintf("%s/%s", cnst.SourceSysExtDir, "common")))
+		entries = append(entries, commonEntries...)
 
 		for _, entry := range entries {
 			if !entry.IsDir() && filepath.Ext(entry.Name()) == ".raw" {
@@ -466,6 +466,14 @@ func (s *State) EnableSysExtensions(g *herd.Graph, opts ...herd.OpOption) error
 					}
 				}
 				// it has to link to the final dir after initramfs, so we avoid setting s.path here for the target
+				// Check if it already exists with the same name
+				// This is because as we have the common dir, there could be a point in which the common dir and the
+				// specific boot state dir have the same file, and we dont want to fail at this point, just warn and continue
+				if _, err := os.Stat(filepath.Join(cnst.DestSysExtDir, entry.Name())); !os.IsNotExist(err) {
+					// If it exists, we can just skip it
+					internalUtils.Log.Warn().Str("file", filepath.Join(cnst.DestSysExtDir, entry.Name())).Msg("Skipping sysextension as its already enabled")
+					continue
+				}
 				err = os.Symlink(filepath.Join(dir, entry.Name()), filepath.Join(cnst.DestSysExtDir, entry.Name()))
 				if err != nil {
 					internalUtils.Log.Err(err).Msg("Creating symlink")