Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support ntfs mount and unmount #1792

Merged
merged 2 commits into from
Jun 12, 2024
Merged

feat(cli): support ntfs mount and unmount #1792

merged 2 commits into from
Jun 12, 2024

Conversation

paralta
Copy link
Contributor

@paralta paralta commented Jun 12, 2024

Description

First part of #356, add support to mount and unmount NTFS devices. No further changes are required in the mount/unmount logic and the mount option remain the same.

Steps to test NTFS Support
1. Launch ubuntu instance on AWS
2. Create EBS volume and attach it to the instance (device name /dev/xvdbn)
3. Check that the new attached volume is listed as a block device (lsblk)
4. Convert to ntfs (sudo mkfs.ntfs -f /dev/xvdbn)
5. Run the following script to mount and unmount the NTFS block device

// utils/fsutils/cmd/main.go
package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/google/uuid"
	"github.com/openclarity/vmclarity/utils/fsutils/blockdevice"
	"github.com/openclarity/vmclarity/utils/fsutils/filesystem"
	"github.com/openclarity/vmclarity/utils/fsutils/mount"
)

const (
	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.
}

func main() {
	blockDevices, err := blockdevice.List(context.Background())
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}

	fmt.Printf("%d Block devices: %+v\n", len(blockDevices), blockDevices)

	for _, device := range blockDevices {
		if device.MountPoint == "" && isSupportedFS(device.FSType) {
			mountPoint := fmt.Sprintf(MountPointTemplate, uuid.New())
			fmt.Printf("mountPoint %s\n", mountPoint)

			if err := os.MkdirAll(mountPoint, MountPointDirPerm); err != nil {
				fmt.Printf("failed to create mountpoint. Device=%s MountPoint=%s: %w\n",
					device.Path, mountPoint, err)
				continue
			}

			if err := mount.Mount(context.Background(), device.Path, mountPoint, device.FSType, DefaultMountOptions); err != nil {
				fmt.Errorf("failed to mount device. Device=%s MountPoint=%s: %w\n",
					device.Path, mountPoint, err)
				continue
			}

			fmt.Printf("Device is mounted. Device=%s MountPoint=%s\n", device.Path, mountPoint)
		} else if device.MountPoint != "" && isSupportedFS(device.FSType) && device.FSType == string(filesystem.Ntfs){
			if err := mount.Umount(context.Background(), device.MountPoint); err != nil {
				fmt.Printf("failed to unmount device. Device=%s MountPoint=%s: %s\n",
					device.Path, device.MountPoint, err)
				continue
			}
			fmt.Printf("Device is unmounted. Device=%s MountPoint=%s\n", device.Path, mountPoint)
		}
	}
}

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

Additionally, add list block devices support for darwin.

Type of Change

[ ] Bug Fix
[x] New Feature
[ ] Breaking Change
[ ] Refactor
[ ] Documentation
[ ] Other (please describe)

Checklist

  • I have read the contributing guidelines
  • Existing issues have been referenced (where applicable)
  • I have verified this change is not present in other open pull requests
  • Functionality is documented
  • All code style checks pass
  • New code contribution is covered by automated tests
  • All new and existing tests pass

@paralta paralta self-assigned this Jun 12, 2024
@paralta paralta requested a review from a team as a code owner June 12, 2024 11:13
ramizpolic
ramizpolic previously approved these changes Jun 12, 2024
Copy link
Member

@ramizpolic ramizpolic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well done! 🚀

Copy link

Hey!

Your images are ready:

  • ghcr.io/openclarity/vmclarity-apiserver-dev:pr1792-d850b613ef0a5386daba1c21e111bfabf41f8710
  • ghcr.io/openclarity/vmclarity-cli-dev:pr1792-d850b613ef0a5386daba1c21e111bfabf41f8710
  • ghcr.io/openclarity/vmclarity-cr-discovery-server-dev:pr1792-d850b613ef0a5386daba1c21e111bfabf41f8710
  • ghcr.io/openclarity/vmclarity-orchestrator-dev:pr1792-d850b613ef0a5386daba1c21e111bfabf41f8710
  • ghcr.io/openclarity/vmclarity-plugin-kics-dev:pr1792-d850b613ef0a5386daba1c21e111bfabf41f8710
  • ghcr.io/openclarity/vmclarity-ui-dev:pr1792-d850b613ef0a5386daba1c21e111bfabf41f8710
  • ghcr.io/openclarity/vmclarity-ui-backend-dev:pr1792-d850b613ef0a5386daba1c21e111bfabf41f8710

@paralta paralta requested a review from ramizpolic June 12, 2024 12:25
@paralta paralta added this pull request to the merge queue Jun 12, 2024
Merged via the queue into main with commit dec4c32 Jun 12, 2024
40 checks passed
@paralta paralta deleted the support-ntfs branch June 12, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants