-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sealer alpha create-rootfs command, support check image file
- Loading branch information
1 parent
48baeed
commit b63060d
Showing
8 changed files
with
239 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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 ( | ||
"path/filepath" | ||
|
||
"github.com/sealerio/sealer/common" | ||
"github.com/sealerio/sealer/pkg/define/options" | ||
"github.com/sealerio/sealer/pkg/imageengine" | ||
"github.com/sealerio/sealer/pkg/imageengine/buildah" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
path string | ||
imageID string | ||
) | ||
|
||
var longMountCmdDescription = ` | ||
mount the cluster image to '/var/lib/sealer/data/mountdir' the directory and check whether the contents of the build image and rootfs are consistent in advance | ||
` | ||
|
||
var exampleForMountCmd = ` | ||
sealer alpha mount my-image | ||
` | ||
|
||
func NewMountCmd() *cobra.Command { | ||
mountCmd := &cobra.Command{ | ||
Use: "mount", | ||
Short: "mount cluster image", | ||
Long: longMountCmdDescription, | ||
Example: exampleForMountCmd, | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
engine, err := buildah.NewBuildahImageEngine(options.EngineGlobalConfigurations{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store := engine.ImageStore() | ||
images, err := store.Images() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, i := range images { | ||
for _, name := range i.Names { | ||
if name == args[0] { | ||
imageID = i.ID | ||
path = filepath.Join(common.DefaultLayerDir, imageID) | ||
} | ||
} | ||
} | ||
|
||
imageEngine, err := imageengine.NewImageEngine(options.EngineGlobalConfigurations{}) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := imageEngine.CreateWorkingContainer(&options.BuildRootfsOptions{ | ||
DestDir: path, | ||
ImageNameOrID: args[0], | ||
}); err != nil { | ||
return err | ||
} | ||
logrus.Infof("mount cluster image %s to %s successful", args[0], path) | ||
return nil | ||
}, | ||
} | ||
return mountCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// 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 ( | ||
"io/fs" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/containers/storage" | ||
"github.com/sealerio/sealer/common" | ||
imagecommon "github.com/sealerio/sealer/pkg/define/options" | ||
"github.com/sealerio/sealer/pkg/imageengine" | ||
"github.com/sealerio/sealer/pkg/imageengine/buildah" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var umountAll bool | ||
|
||
var longUmountCmdDescription = ` | ||
umount the cluster image and delete the mount directory | ||
` | ||
|
||
var exampleForUmountCmd = ` | ||
sealer alpha umount my-image | ||
` | ||
|
||
func NewUmountCmd() *cobra.Command { | ||
umountCmd := &cobra.Command{ | ||
Use: "umount", | ||
Short: "umount cluster image", | ||
Long: longUmountCmdDescription, | ||
Example: exampleForUmountCmd, | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var cid string | ||
|
||
imageEngine, err := imageengine.NewImageEngine(imagecommon.EngineGlobalConfigurations{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
engine, err := buildah.NewBuildahImageEngine(imagecommon.EngineGlobalConfigurations{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store := engine.ImageStore() | ||
containers, err := store.Containers() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
files, err := ioutil.ReadDir(common.DefaultLayerDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// umount all cluster image | ||
if umountAll { | ||
for _, c := range containers { | ||
if err := imageEngine.RemoveContainer(&imagecommon.RemoveContainerOptions{ | ||
ContainerNamesOrIDs: []string{c.ID}}, | ||
); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, file := range files { | ||
if err := os.RemoveAll(filepath.Join(common.DefaultLayerDir, file.Name())); err != nil { | ||
return err | ||
} | ||
} | ||
logrus.Infof("umount all cluster image successful") | ||
return nil | ||
} | ||
|
||
images, err := store.Images() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, c := range containers { | ||
id := getContainerID(images, args[0]) | ||
if c.ImageID == id { | ||
cid = c.ID | ||
} | ||
} | ||
|
||
if err := imageEngine.RemoveContainer(&imagecommon.RemoveContainerOptions{ | ||
ContainerNamesOrIDs: []string{cid}}, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range files { | ||
for _, image := range images { | ||
if err := removeContainerDir(image, file, args[0]); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
logrus.Infof("umount cluster image %s successful", args[0]) | ||
return nil | ||
}, | ||
} | ||
umountCmd.Flags().BoolVarP(&umountAll, "all", "a", false, "umount all cluster image directories") | ||
return umountCmd | ||
} | ||
|
||
func getContainerID(images []storage.Image, imageName string) string { | ||
var cid string | ||
for _, image := range images { | ||
for _, n := range image.Names { | ||
if n == imageName { | ||
cid = image.ID | ||
} | ||
} | ||
} | ||
return cid | ||
} | ||
|
||
func removeContainerDir(image storage.Image, file fs.FileInfo, imageName string) error { | ||
for _, n := range image.Names { | ||
if n == imageName { | ||
if file.Name() == image.ID { | ||
if err := os.RemoveAll(filepath.Join(common.DefaultLayerDir, file.Name())); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters