Skip to content

Commit

Permalink
add sealer alpha create-rootfs command, support check image file
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei committed Nov 16, 2022
1 parent 45802b9 commit 5191d56
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cmd/sealer/cmd/alpha/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ func NewCmdAlpha() *cobra.Command {
cmd.AddCommand(NewUpgradeCmd())
cmd.AddCommand(NewGenCmd())
cmd.AddCommand(NewCheckCmd())
cmd.AddCommand(NewMountCmd())
cmd.AddCommand(NewUmountCmd())
return cmd
}
74 changes: 74 additions & 0 deletions cmd/sealer/cmd/alpha/mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package alpha

import (
"fmt"
"path/filepath"

imagecommon "github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sealerio/sealer/utils/os"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var longMountCmdDescription = `
mount the image file to the specified virtual directory and check whether the contents of the build image and rootfs are consistent in advance
You need to specify a virtual path, for example:
'sealer alpha mount my-image /root/rootfs'
rootfs are virtual path
`

var exampleForMountCmd = `
sealer alpha mount my-image /tmp/rootfs
sealer alpha mount e5b4997adba6 /tmp/rootfs
`

func NewMountCmd() *cobra.Command {
mountCmd := &cobra.Command{
Use: "mount",
Short: "mount image file",
Long: longMountCmdDescription,
Example: exampleForMountCmd,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("please enter full parameters")
}
path, err := filepath.Abs(args[1])
if err != nil {
return err
}
if os.IsFileExist(path) {
return fmt.Errorf("directory name already exists")
}

imageEngine, err := imageengine.NewImageEngine(imagecommon.EngineGlobalConfigurations{})
if err != nil {
return err
}
if _, err := imageEngine.CreateWorkingContainer(&imagecommon.BuildRootfsOptions{
DestDir: path,
ImageNameOrID: args[0],
}); err != nil {
return err
}
logrus.Infof("specific path is %s, mount image file success", path)
return nil
},
}
return mountCmd
}
66 changes: 66 additions & 0 deletions cmd/sealer/cmd/alpha/umount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package alpha

import (
"fmt"

imagecommon "github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imagedistributor"
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var longUmountCmdDescription = `
umount image file virtual path
You need to delete specify virtual path, for example:
'sealer alpha umount /root/rootfs'
rootfs are virtual path
`

var exampleForUmountCmd = `
sealer alpha umount /tmp/rootfs
`

func NewUmountCmd() *cobra.Command {
umountCmd := &cobra.Command{
Use: "umount",
Short: "umount image file",
Long: longUmountCmdDescription,
Example: exampleForUmountCmd,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
imageEngine, err := imageengine.NewImageEngine(imagecommon.EngineGlobalConfigurations{})
if err != nil {
return err
}
if err := imageEngine.RemoveContainer(&imagecommon.RemoveContainerOptions{
ContainerNamesOrIDs: nil,
All: true,
}); err != nil {
return fmt.Errorf("failed to remove mounted dir %s: %v", args[0], err)
}
mounter := imagedistributor.NewBuildAhMounter(imageEngine)
if err = mounter.Umount(args[0]); err != nil {
return err
}
logrus.Infof("umount image file success")
return nil
},
}

return umountCmd
}
6 changes: 2 additions & 4 deletions cmd/sealer/cmd/utils/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import (
"strconv"

"github.com/sealerio/sealer/cmd/sealer/cmd/types"

netutils "github.com/sealerio/sealer/utils/net"
strUtils "github.com/sealerio/sealer/utils/strings"

"github.com/sealerio/sealer/common"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
netutils "github.com/sealerio/sealer/utils/net"
strUtils "github.com/sealerio/sealer/utils/strings"
)

func ConstructClusterForRun(imageName string, runFlags *types.Flags) (*v2.Cluster, error) {
Expand Down
1 change: 0 additions & 1 deletion cmd/sealer/cmd/utils/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"

"github.com/sealerio/sealer/common"

v2 "github.com/sealerio/sealer/types/api/v2"
)

Expand Down
3 changes: 1 addition & 2 deletions cmd/sealer/cmd/utils/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (
"net"
"testing"

"github.com/stretchr/testify/assert"

"github.com/sealerio/sealer/pkg/clusterfile"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/stretchr/testify/assert"
)

func Test_TransferIPStrToHosts(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion cmd/sealer/cmd/utils/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"

"github.com/sealerio/sealer/cmd/sealer/cmd/types"

netutils "github.com/sealerio/sealer/utils/net"
)

Expand Down

0 comments on commit 5191d56

Please sign in to comment.