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 59035bf commit a4cfea4
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 64 deletions.
20 changes: 4 additions & 16 deletions cmd/sealer/cmd/alpha/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func NewMountService() (MountService, error) {
return MountService{}, err
}

builders, err := imagebuildah.OpenBuilders(store)
builders, err := buildah.OpenAllBuilders(store)
if err != nil {
return MountService{}, err
}

table := tablewriter.NewWriter(common.StdOut)
table.SetHeader([]string{imageName, tableHeaderContainerID, tableHeaderMountPath})
table.SetHeader([]string{tableHeaderContainerID, tableHeaderMountPath})

return MountService{
table: table,
Expand All @@ -118,19 +118,8 @@ func NewMountService() (MountService, error) {
}, nil
}

func (m MountService) getMountedImageName(container storage.Container) string {
for _, image := range m.images {
for _, name := range image.Names {
if container.ImageID == image.ID {
return name
}
}
}
return ""
}

func (m MountService) Show() error {
clients, err := imagebuildah.OpenBuilders(m.store)
clients, err := buildah.OpenAllBuilders(m.store)
if err != nil {
return fmt.Errorf("reading build Containers: %w", err)
}
Expand All @@ -141,9 +130,8 @@ func (m MountService) Show() error {
}
for _, container := range m.containers {
if client.ContainerID == container.ID && mounted {
name := m.getMountedImageName(container)
containerID := imagebuildah.TruncateID(client.ContainerID, true)
m.table.Append([]string{name, containerID, client.MountPoint})
m.table.Append([]string{containerID, client.MountPoint})
}
}
}
Expand Down
42 changes: 7 additions & 35 deletions cmd/sealer/cmd/alpha/umount.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"context"
"fmt"

"github.com/containers/buildah"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/sealerio/sealer/pkg/imageengine/buildah"
imagebuildah "github.com/sealerio/sealer/pkg/imageengine/buildah"
)

var (
Expand All @@ -31,7 +32,6 @@ var (
umount the cluster image and delete the mount directory
`
exampleForUmountCmd = `
sealer alpha umount my-image
sealer alpha umount containerID
sealer alpha umount --all
`
Expand Down Expand Up @@ -72,38 +72,21 @@ func NewUmountCmd() *cobra.Command {
return umountCmd
}

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)
func (m MountService) Umount(containerID string) error {
client, err := imagebuildah.OpenBuilder(context.TODO(), m.store, containerID)
if err != nil {
return fmt.Errorf("failed to unmount image %s: %w", imageNameOrID, err)
return fmt.Errorf("failed to reading build container %s: %w", containerID, err)
}

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

func (m MountService) getContainerID(imageName string) string {
imageID := m.getImageID(imageName)
for _, container := range m.containers {
if container.ImageID == imageID {
return container.ID
}
}
return ""
}

func (m MountService) UmountAllContainers() error {
clients, err := buildah.OpenBuilders(m.store)
clients, err := buildah.OpenAllBuilders(m.store)
if err != nil {
return fmt.Errorf("reading build Containers: %w", err)
}
Expand All @@ -118,14 +101,3 @@ func (m MountService) UmountAllContainers() error {
}
return nil
}

func (m MountService) IsContainImgName(imageName string) bool {
for _, image := range m.images {
for _, name := range image.Names {
if name == imageName {
return true
}
}
}
return false
}
2 changes: 1 addition & 1 deletion pkg/imageengine/buildah/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (engine *Engine) Commit(opts *options.CommitOptions) (string, error) {

ctx := getContext()
store := engine.ImageStore()
builder, err := openBuilder(ctx, store, name)
builder, err := OpenBuilder(ctx, store, name)
if err != nil {
return "", errors.Wrapf(err, "error reading build container %q", name)
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/imageengine/buildah/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ func getStore(configurations *options.EngineGlobalConfigurations) (storage.Store
}

func OpenBuilder(ctx context.Context, store storage.Store, name string) (builder *buildah.Builder, err error) {
return openBuilder(ctx, store, name)
}

func openBuilder(ctx context.Context, store storage.Store, name string) (builder *buildah.Builder, err error) {
if name != "" {
builder, err = buildah.OpenBuilder(store, name)
if os.IsNotExist(errors.Cause(err)) {
Expand All @@ -102,10 +98,6 @@ func openBuilder(ctx context.Context, store storage.Store, name string) (builder
return builder, nil
}

func OpenBuilders(store storage.Store) (builders []*buildah.Builder, err error) {
return buildah.OpenAllBuilders(store)
}

func openImage(ctx context.Context, sc *types.SystemContext, store storage.Store, name string) (builder *buildah.Builder, err error) {
options := buildah.ImportFromImageOptions{
Image: name,
Expand Down
2 changes: 1 addition & 1 deletion pkg/imageengine/buildah/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (engine *Engine) Config(opts *options.ConfigOptions) error {

ctx := getContext()
store := engine.ImageStore()
builder, err := openBuilder(ctx, store, name)
builder, err := OpenBuilder(ctx, store, name)
if err != nil {
return errors.Wrapf(err, "error reading build container %q", name)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/imageengine/buildah/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (engine *Engine) Copy(opts *options.CopyOptions) error {
return errors.Errorf("--ignorefile option requires that you specify a context dir using --contextdir")
}

builder, err := openBuilder(getContext(), store, name)
builder, err := OpenBuilder(getContext(), store, name)
if err != nil {
return errors.Wrapf(err, "error reading build container %q", name)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/imageengine/buildah/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (engine *Engine) Mount(opts *options.MountOptions) ([]options.JSONMount, er
}

for _, name := range containers {
builder, err := openBuilder(getContext(), store, name)
builder, err := OpenBuilder(getContext(), store, name)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
Expand Down
2 changes: 1 addition & 1 deletion pkg/imageengine/buildah/remove_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (engine *Engine) RemoveContainer(opts *options.RemoveContainerOptions) erro
}
} else {
for _, name := range opts.ContainerNamesOrIDs {
builder, err := openBuilder(getContext(), store, name)
builder, err := OpenBuilder(getContext(), store, name)
if err != nil {
lastError = util.WriteError(os.Stderr, errors.Wrapf(err, "%s %q", delContainerErrStr, name), lastError)
continue
Expand Down

0 comments on commit a4cfea4

Please sign in to comment.