diff --git a/cmd/agent/server/containerd_image.go b/cmd/agent/server/containerd_image.go index 094985f635273c5d0a335e29b5d06b00b4029f75..0b614b57a3ec3a1530e79ea00974af6ad190fe66 100644 --- a/cmd/agent/server/containerd_image.go +++ b/cmd/agent/server/containerd_image.go @@ -76,7 +76,7 @@ func checkAndCleanMount(mountPath string) error { if err = runCommand("ctr", "-n="+defaultNamespace, "images", "unmount", mountPath); err != nil { return err } - if err = runCommand("ctr", "-n="+defaultNamespace, "snapshots", "delete", mountPath); err != nil { + if err = runCommand("ctr", "-n="+defaultNamespace, "snapshots", "remove", mountPath); err != nil { return err } } diff --git a/cmd/agent/server/server.go b/cmd/agent/server/server.go index 67e3f7cf55e83f9b73d52f68430d2b16d5318e07..f31b771ae8a91e4696e2ebb255164633ef29fcee 100644 --- a/cmd/agent/server/server.go +++ b/cmd/agent/server/server.go @@ -16,8 +16,6 @@ package server import ( "context" "fmt" - "os/exec" - "strings" "sync/atomic" "syscall" "time" @@ -35,11 +33,6 @@ const ( imgPermission = 0600 ) -var ( - partA string - partB string -) - // Lock is a custom Lock to implement a spin lock type Lock struct { state uint32 @@ -62,16 +55,6 @@ type Server struct { disableReboot bool } -func init() { - out, err := exec.Command("sh", "-c", "df / | awk 'NR==2{print}' | awk '{print $1}'").CombinedOutput() - if err != nil { - logrus.Errorln("init error " + err.Error()) - } - curRootfs := strings.TrimSpace(string(out)) - partA = curRootfs[:len(curRootfs)-1] + "2" - partB = curRootfs[:len(curRootfs)-1] + "3" -} - // Update implements the OSServer.Update func (s *Server) Update(_ context.Context, req *pb.UpdateRequest) (*pb.UpdateResponse, error) { if !s.mutex.TryLock() { @@ -119,6 +102,10 @@ func (s *Server) update(req *pb.UpdateRequest) error { if err != nil { return err } + partA, partB, err := getRootfsDisks() + if err != nil { + return err + } side, next, err := getNextPart(partA, partB) if err != nil { return err @@ -130,6 +117,10 @@ func (s *Server) update(req *pb.UpdateRequest) error { } func (s *Server) rollback() error { + partA, partB, err := getRootfsDisks() + if err != nil { + return err + } _, next, err := getNextPart(partA, partB) if err != nil { return err diff --git a/cmd/agent/server/utils.go b/cmd/agent/server/utils.go index a38dee0d050506d0a7a15043833059d1079a8683..111497cef25177c6bccdaa823a888956f77c7563 100644 --- a/cmd/agent/server/utils.go +++ b/cmd/agent/server/utils.go @@ -103,6 +103,38 @@ func getNextPart(partA string, partB string) (string, string, error) { return side, next, nil } +func getRootfsDisks() (string, string, error) { + out, err := runCommandWithOut("lsblk", "-lno", "NAME,MOUNTPOINTS") + if err != nil { + logrus.Errorln("get rootfs disks error " + err.Error()) + return "", "", err + } + var diskName string + const mountedDeviceOutLen = 2 + mounts := strings.Split(out, "\n") + for _, m := range mounts { + res := strings.Fields(m) + if len(res) != mountedDeviceOutLen { + continue + } + if res[1] == "/" { + diskName = filepath.Join("/dev", res[0]) + } + } + if len(diskName) == 0 { + logrus.Errorln("get rootfs disks error: not get diskName") + return "", "", fmt.Errorf("get rootfs disks error: not get diskName") + } + curDiskBytes := make([]byte, len(diskName)-1) + copy(curDiskBytes, diskName[:len(diskName)-1]) + curDisk := string(curDiskBytes) + const partAPartitionNum = "2" + const partBartitionNum = "3" + partA := curDisk + partAPartitionNum + partB := curDisk + partBartitionNum + return partA, partB, nil +} + func createOSImage(neededPath preparePath) (string, error) { imagePath := neededPath.imagePath updatePath := neededPath.updatePath