Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
feat: add fsutils
Browse files Browse the repository at this point in the history
Add new `fsutils` package which includes helpers for listing and
mounting blockdevices on Linux.
  • Loading branch information
chrisgacsal committed Jun 21, 2023
1 parent 5de9fdb commit 49aa007
Show file tree
Hide file tree
Showing 26 changed files with 2,608 additions and 177 deletions.
7 changes: 6 additions & 1 deletion cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (

const (
DefaultWatcherInterval = 2 * time.Minute
DefaultMountTimeout = 10 * time.Minute
)

var (
Expand Down Expand Up @@ -94,7 +95,11 @@ var rootCmd = &cobra.Command{
}

if mountVolume {
mountPoints, err := cli.MountVolumes(abortCtx)
// Set timeout for mounting volumes
mountCtx, mountCancel := context.WithTimeout(abortCtx, DefaultMountTimeout)
defer mountCancel()

mountPoints, err := cli.MountVolumes(mountCtx)
if err != nil {
err = fmt.Errorf("failed to mount attached volume: %w", err)
if e := cli.MarkDone(ctx, []error{err}); e != nil {
Expand Down
66 changes: 43 additions & 23 deletions cli/pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,37 @@ package cli
import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/google/uuid"

"github.com/openclarity/vmclarity/cli/pkg/mount"
"github.com/openclarity/vmclarity/cli/pkg/presenter"
"github.com/openclarity/vmclarity/cli/pkg/state"
"github.com/openclarity/vmclarity/shared/pkg/families"
"github.com/openclarity/vmclarity/shared/pkg/families/types"
"github.com/openclarity/vmclarity/shared/pkg/fsutils/blockdevice"
"github.com/openclarity/vmclarity/shared/pkg/fsutils/filesystem"
"github.com/openclarity/vmclarity/shared/pkg/fsutils/mount"
"github.com/openclarity/vmclarity/shared/pkg/log"
)

const (
fsTypeExt4 = "ext4"
fsTypeXFS = "xfs"
MountPointTemplate = "/mnt/snapshots/%s"
MountPointDirPerm = 0o770
)

// DefaultMountOptions is a set of filesystem independent mount options.
var DefaultMountOptions = []string{
"noatime", // Do not update inode access times on this filesystem (e.g. for faster access on the news spool to speed up news servers).
"noauto", // Can only be mounted explicitly (i.e., the -a option will not cause the filesystem to be mounted).
"noexec", // Do not permit direct execution of any binaries on the mounted filesystem.
"norelatime", // Do not use the relatime feature: Update inode access times relative to modify or change time. Access time is only updated if the previous access time was earlier than the current modify or change time.
"nosuid", // Do not honor set-user-ID and set-group-ID bits or file capabilities when executing programs from this filesystem.
"ro", // Mount the filesystem read-only.
}

type CLI struct {
state.Manager
presenter.Presenter
Expand All @@ -53,31 +66,36 @@ func (c *CLI) FamilyFinished(ctx context.Context, res families.FamilyResult) err
}

func (c *CLI) MountVolumes(ctx context.Context) ([]string, error) {
var mountPoints []string

logger := log.GetLoggerFromContextOrDiscard(ctx)

devices, err := mount.ListBlockDevices()
blockDevices, err := blockdevice.List(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list block devices: %w", err)
}
logger.Infof("Found block devices: %v", devices)
for _, device := range devices {
// if the device is not mounted and of a supported filesystem type,
// we assume it belongs to the attached volume, so we mount it.
if device.MountPoint == "" && isSupportedFS(device.FilesystemType) {
mountDir := "/mnt/snapshot" + uuid.New().String()

if err := device.Mount(mountDir); err != nil {
return nil, fmt.Errorf("failed to mount device: %w", err)
logger.Debugf("Found block devices: %s", blockDevices)

var mountPoints []string
for _, device := range blockDevices {
// It is assumed that the device is part of the attached volume if it is not mounted
// and it has a supported filesystem.
if device.MountPoint == "" && isSupportedFS(device.FSType) {
mountPoint := fmt.Sprintf(MountPointTemplate, uuid.New())

if err := os.MkdirAll(mountPoint, MountPointDirPerm); err != nil {
return nil, fmt.Errorf("failed to create mountpoint. Device=%s MountPoint=%s: %w",
device.Path, mountPoint, err)
}
logger.Infof("Device %v on %v is mounted", device.DeviceName, mountDir)
mountPoints = append(mountPoints, mountDir)
}
if ctx.Err() != nil {
return mountPoints, fmt.Errorf("failed to mount block devices: %w", ctx.Err())

if err := mount.Mount(ctx, device.Path, mountPoint, device.FSType, DefaultMountOptions); err != nil {
return nil, fmt.Errorf("failed to mount device. Device=%s MountPoint=%s: %w",
device.Path, mountPoint, err)
}
logger.Infof("Device is mounted. Device=%s MountPoint=%s", device.Path, mountPoint)

mountPoints = append(mountPoints, mountPoint)
}
}

return mountPoints, nil
}

Expand Down Expand Up @@ -111,9 +129,11 @@ func (c *CLI) WatchForAbort(ctx context.Context, cancel context.CancelFunc, inte

func isSupportedFS(fs string) bool {
switch strings.ToLower(fs) {
case fsTypeExt4, fsTypeXFS:
case string(filesystem.Ext2), string(filesystem.Ext3), string(filesystem.Ext4):
return true
case string(filesystem.Xfs):
return true
default:
return false
}

return false
}
30 changes: 23 additions & 7 deletions cli/pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,49 @@ package cli

import (
"testing"

"github.com/openclarity/vmclarity/shared/pkg/fsutils/filesystem"
)

func Test_isSupportedFS(t *testing.T) {
type args struct {
fs string
fs filesystem.FilesystemType
}
tests := []struct {
name string
args args
want bool
}{
{
name: "supported ext4",
name: "ext2 is supported",
args: args{
fs: filesystem.Ext2,
},
want: true,
},
{
name: "ext3 is supported",
args: args{
fs: filesystem.Ext3,
},
want: true,
},
{
name: "ext4 is supported",
args: args{
fs: fsTypeExt4,
fs: filesystem.Ext4,
},
want: true,
},
{
name: "supported xfs",
name: "xfs is supported",
args: args{
fs: fsTypeXFS,
fs: filesystem.Xfs,
},
want: true,
},
{
name: "not supported btrfs",
name: "btrfs is not supported",
args: args{
fs: "btrfs",
},
Expand All @@ -52,7 +68,7 @@ func Test_isSupportedFS(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isSupportedFS(tt.args.fs); got != tt.want {
if got := isSupportedFS(string(tt.args.fs)); got != tt.want {
t.Errorf("isSupportedFS() = %v, want %v", got, tt.want)
}
})
Expand Down
140 changes: 0 additions & 140 deletions cli/pkg/mount/mount.go

This file was deleted.

6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/google/uuid v1.3.0
github.com/labstack/echo/v4 v4.10.2
github.com/mitchellh/mapstructure v1.5.0
github.com/moby/sys/mountinfo v0.6.2
github.com/onsi/gomega v1.27.8
github.com/openclarity/kubeclarity/cli v0.0.0-00010101000000-000000000000
github.com/openclarity/kubeclarity/shared v0.0.0
Expand All @@ -47,7 +48,7 @@ require (
gorm.io/gorm v1.25.0
gotest.tools/v3 v3.4.0
k8s.io/apimachinery v0.27.3
k8s.io/mount-utils v0.27.3

)

require (
Expand Down Expand Up @@ -300,7 +301,6 @@ require (
github.com/moby/buildkit v0.11.5 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/signal v0.7.0 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
Expand Down Expand Up @@ -409,7 +409,7 @@ require (
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2438,8 +2438,9 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down Expand Up @@ -2941,8 +2942,6 @@ k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f/go.mod h1:byini6yhqGC14c3
k8s.io/kubectl v0.26.3 h1:bZ5SgFyeEXw6XTc1Qji0iNdtqAC76lmeIIQULg2wNXM=
k8s.io/kubectl v0.26.3/go.mod h1:02+gv7Qn4dupzN3fi/9OvqqdW+uG/4Zi56vc4Zmsp1g=
k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
k8s.io/mount-utils v0.27.3 h1:oubkDKLTZUneW27wgyOmp8a1AAZj04vGmtq+YW8wdvY=
k8s.io/mount-utils v0.27.3/go.mod h1:vmcjYdi2Vg1VTWY7KkhvwJVY6WDHxb/QQhiQKkR8iNs=
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20211116205334-6203023598ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
Expand Down
Loading

0 comments on commit 49aa007

Please sign in to comment.