Skip to content

Commit

Permalink
modify mount and umount
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei committed Mar 3, 2023
1 parent bd2d1c4 commit 7cf2803
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
28 changes: 14 additions & 14 deletions cmd/sealer/cmd/alpha/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,21 @@ func (m MountService) Mount(imageNameOrID []string) error {
ok := utilsstrings.IsInSlice(imageID, imageIDList)
if ok {
logrus.Infof("this image has already been mounted, please do not repeat the operation")
} else {
cid, err := m.engine.CreateContainer(&options.FromOptions{
Image: imageID,
Quiet: false,
})
if err != nil {
return err
}
mounts, err := m.engine.Mount(&options.MountOptions{Containers: []string{cid}})
if err != nil {
return err
}
mountPoint := mounts[0].MountPoint
logrus.Infof("mount cluster image %s to %s successful", name, mountPoint)
continue
}
cid, err := m.engine.CreateContainer(&options.FromOptions{
Image: imageID,
Quiet: false,
})
if err != nil {
return err
}
mounts, err := m.engine.Mount(&options.MountOptions{Containers: []string{cid}})
if err != nil {
return err
}
mountPoint := mounts[0].MountPoint
logrus.Infof("mount cluster image %s to %s successful", name, mountPoint)
}
return nil
}
35 changes: 10 additions & 25 deletions cmd/sealer/cmd/alpha/umount.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package alpha
import (
"context"
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -27,7 +26,6 @@ import (

var (
umountAll bool
lastError error

longUmountCmdDescription = `
umount the cluster image and delete the mount directory
Expand Down Expand Up @@ -65,16 +63,17 @@ func NewUmountCmd() *cobra.Command {
return nil
}

umountInfo.Umount(args)
if err := umountInfo.Umount(args); err != nil {
return err
}
return nil
},
}
umountCmd.Flags().BoolVarP(&umountAll, "all", "a", false, "umount all cluster image directories")
return umountCmd
}

func (m MountService) Umount(imageNameOrID []string) {
var lastError error
func (m MountService) Umount(imageNameOrID []string) error {
for _, name := range imageNameOrID {
var containerID string
if ok := m.IsContainImgName(name); !ok {
Expand All @@ -85,25 +84,15 @@ func (m MountService) Umount(imageNameOrID []string) {

client, err := buildah.OpenBuilder(context.TODO(), m.store, containerID)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = fmt.Errorf("failed to unmount image %s: %w", name, err)
continue
}
if client.MountPoint == "" {
continue
return fmt.Errorf("failed to unmount image %s: %w", name, err)
}

if err = client.Unmount(); err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = fmt.Errorf("failed to unmount container %q: %w", client.Container, err)
continue
if err := client.Unmount(); err != nil {
return fmt.Errorf("failed to unmount container %q: %w", client.Container, err)
}
logrus.Infof("umount %s successful", name)
}
return nil
}

func (m MountService) getContainerID(imageName string) string {
Expand All @@ -126,12 +115,8 @@ func (m MountService) UmountAllContainers() error {
continue
}

if err = client.Unmount(); err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = fmt.Errorf("failed to unmount container %q: %w", client.Container, err)
continue
if err := client.Unmount(); err != nil {
return fmt.Errorf("failed to unmount container %q: %w", client.Container, err)
}
}
return nil
Expand Down

0 comments on commit 7cf2803

Please sign in to comment.