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 7, 2023
1 parent 831d878 commit e779dee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 50 deletions.
55 changes: 24 additions & 31 deletions cmd/sealer/cmd/alpha/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ mount the cluster image to '/var/lib/containers/storage/overlay' the directory a
var exampleForMountCmd = `
sealer alpha mount(show mount list)
sealer alpha mount my-image
sealer alpha mount my-image my-image1 my-image2
sealer alpha mount imageID
sealer alpha mount imageID1 imageID2 imageID3
`

const (
Expand All @@ -62,6 +60,7 @@ func NewMountCmd() *cobra.Command {
Short: "mount cluster image",
Long: longMountCmdDescription,
Example: exampleForMountCmd,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
mountInfo, err := NewMountService()
if err != nil {
Expand All @@ -76,7 +75,7 @@ func NewMountCmd() *cobra.Command {
return nil
}

_, err = mountInfo.Mount(args)
_, err = mountInfo.Mount(args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -176,16 +175,13 @@ func (m MountService) getImageID(name string) string {
return ""
}

func (m MountService) Mount(imageNameOrID []string) ([]string, error) {
var (
mountPoints []string
imageIDList []string
)
func (m MountService) Mount(imageNameOrID string) (string, error) {
var imageIDList []string

for _, builder := range m.builders {
mounted, err := builder.Mounted()
if err != nil {
return nil, err
return "", err
}
for _, container := range m.containers {
if builder.ContainerID == container.ID && mounted {
Expand All @@ -195,28 +191,25 @@ func (m MountService) Mount(imageNameOrID []string) ([]string, error) {
}
}

for _, name := range imageNameOrID {
imageID := m.getImageID(name)
ok := utilsstrings.IsInSlice(imageID, imageIDList)
if ok {
logrus.Infof("this image has already been mounted, please do not repeat the operation")
continue
}
cid, err := m.engine.CreateContainer(&options.FromOptions{
Image: imageID,
Quiet: false,
})
if err != nil {
return nil, err
}
mounts, err := m.engine.Mount(&options.MountOptions{Containers: []string{cid}})
if err != nil {
return nil, err
}
mountPoint := mounts[0].MountPoint
mountPoints = append(mountPoints, mountPoint)
logrus.Infof("mount cluster image %s to %s successful", name, mountPoint)
imageID := m.getImageID(imageNameOrID)
ok := utilsstrings.IsInSlice(imageID, imageIDList)
if ok {
logrus.Warnf("this image has already been mounted, please do not repeat the operation")
return "", nil
}
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", imageNameOrID, mountPoint)

return mountPoints, nil
return mountPoint, nil
}
35 changes: 16 additions & 19 deletions cmd/sealer/cmd/alpha/umount.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ umount the cluster image and delete the mount directory
`
exampleForUmountCmd = `
sealer alpha umount my-image
sealer alpha umount my-image my-image1 my-image2
sealer alpha umount containerID
sealer alpha umount containerID1 containerID2 containerID3
sealer alpha umount --all
`
)
Expand All @@ -45,6 +43,7 @@ func NewUmountCmd() *cobra.Command {
Short: "umount cluster image",
Long: longUmountCmdDescription,
Example: exampleForUmountCmd,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 && !umountAll {
return fmt.Errorf("you must input imageName Or containerID")
Expand All @@ -63,7 +62,7 @@ func NewUmountCmd() *cobra.Command {
return nil
}

if err := umountInfo.Umount(args); err != nil {
if err := umountInfo.Umount(args[0]); err != nil {
return err
}
return nil
Expand All @@ -73,25 +72,23 @@ func NewUmountCmd() *cobra.Command {
return umountCmd
}

func (m MountService) Umount(imageNameOrID []string) error {
for _, name := range imageNameOrID {
var containerID string
if ok := m.IsContainImgName(name); !ok {
containerID = name
} else {
containerID = m.getContainerID(name)
}
func (m MountService) Umount(imageNameOrID string) error {
var containerID string
if ok := m.IsContainImgName(imageNameOrID); !ok {
containerID = imageNameOrID
} else {
containerID = m.getContainerID(imageNameOrID)
}

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

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

Expand Down

0 comments on commit e779dee

Please sign in to comment.