Skip to content

Commit

Permalink
Add common symlinks in init
Browse files Browse the repository at this point in the history
This commits adds creation of common symlinks in the /dev directory for
conveniently accessing the usual file descriptors of a process.

Closes #12
  • Loading branch information
aibor committed Feb 8, 2024
1 parent bbbaa15 commit 52d6e3b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions integrationtesting/guest/sysinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,20 @@ func TestEnv(t *testing.T) {
assert.Equal(t, "/data", envPath, "PATH env var should be correct")
}
}

func TestCommonSymlinks(t *testing.T) {
symlinks := map[string]string{
"/dev/fd": "/proc/self/fd/",
"/dev/stdin": "/proc/self/fd/0",
"/dev/stdout": "/proc/self/fd/1",
"/dev/stderr": "/proc/self/fd/2",
}

for link, expectedTarget := range symlinks {
t.Run(link, func(t *testing.T) {
target, err := os.Readlink(link)
require.NoError(t, err, "link must be readable")
assert.Equal(t, expectedTarget, target, "link target should be as expected")
})
}
}
Binary file modified internal/initramfs/inits/amd64
Binary file not shown.
Binary file modified internal/initramfs/inits/arm64
Binary file not shown.
21 changes: 21 additions & 0 deletions sysinit/mount.go → sysinit/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ var MountPointPresets = map[string]MountPoints{
},
}

// CommonSymlinks defines symbolic links usually set by init systems.
var CommonSymlinks = map[string]string{
"/dev/fd": "/proc/self/fd/",
"/dev/stdin": "/proc/self/fd/0",
"/dev/stdout": "/proc/self/fd/1",
"/dev/stderr": "/proc/self/fd/2",
}

// MountFs mounts the special file system with type FsType at the given path.
//
// If path does not exist, it is created. An error is returned if this or the
Expand Down Expand Up @@ -85,3 +93,16 @@ func MountAll() error {

return nil
}

// CreateCommonSymlinks creates common symbolic links in the file system.
//
// This must be run after all file systems have been mounted.
func CreateCommonSymlinks() error {
for link, target := range CommonSymlinks {
if err := os.Symlink(target, link); err != nil {
return fmt.Errorf("create common symlink %s: %v", link, err)
}
}

return nil
}
4 changes: 4 additions & 0 deletions sysinit/sysinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func Run(fn func() (int, error)) error {
return err
}

if err = CreateCommonSymlinks(); err != nil {
return err
}

rc, err := fn()
var eerr *exec.ExitError
if err != nil {
Expand Down

0 comments on commit 52d6e3b

Please sign in to comment.