cr: handle criu notifications in a separate function

Signed-off-by: Andrey Vagin <avagin@openvz.org>
This commit is contained in:
Andrey Vagin 2015-04-10 18:03:23 +03:00 committed by Michael Crosby
parent 522f7b36ff
commit 65f9b1bd84
1 changed files with 33 additions and 33 deletions

View File

@ -5,7 +5,6 @@ package libcontainer
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -447,8 +446,6 @@ func (c *linuxContainer) Restore(process *Process, imagePath string) error {
return err return err
} }
var pid int32 = math.MinInt32
buf := make([]byte, 10*4096) buf := make([]byte, 10*4096)
for true { for true {
n, err := criuClient.Read(buf) n, err := criuClient.Read(buf)
@ -476,31 +473,8 @@ func (c *linuxContainer) Restore(process *Process, imagePath string) error {
t = resp.GetType() t = resp.GetType()
switch { switch {
case t == criurpc.CriuReqType_NOTIFY: case t == criurpc.CriuReqType_NOTIFY:
notify := resp.GetNotify() if err := c.criuNotifications(resp, process, imagePath); err != nil {
if notify == nil { return err
return fmt.Errorf("invalid response: %s", resp.String())
}
if notify.GetScript() == "setup-namespaces" {
pid = notify.GetPid()
}
if notify.GetScript() == "post-restore" {
// In many case, restore from the images can be done only once.
// If we want to create snapshots, we need to snapshot the file system.
os.RemoveAll(imagePath)
r, err := newRestoredProcess(int(pid))
if err != nil {
return err
}
// TODO: crosbymichael restore previous process information by saving the init process information in
// the container's state file or separate process state files.
if err := c.updateState(r); err != nil {
return err
}
process.ops = r
} }
t = criurpc.CriuReqType_NOTIFY t = criurpc.CriuReqType_NOTIFY
@ -518,11 +492,7 @@ func (c *linuxContainer) Restore(process *Process, imagePath string) error {
} }
continue continue
case t == criurpc.CriuReqType_RESTORE: case t == criurpc.CriuReqType_RESTORE:
restore := resp.GetRestore() break;
if restore != nil {
pid = restore.GetPid()
break
}
default: default:
return fmt.Errorf("unable to parse the response %s", resp.String()) return fmt.Errorf("unable to parse the response %s", resp.String())
} }
@ -543,6 +513,36 @@ func (c *linuxContainer) Restore(process *Process, imagePath string) error {
return nil return nil
} }
func (c *linuxContainer) criuNotifications(resp *criurpc.CriuResp, process *Process, imagePath string) error {
notify := resp.GetNotify()
if notify == nil {
return fmt.Errorf("invalid response: %s", resp.String())
}
switch {
case notify.GetScript() == "post-restore":
// In many case, restore from the images can be done only once.
// If we want to create snapshots, we need to snapshot the file system.
os.RemoveAll(imagePath)
pid := notify.GetPid()
r, err := newRestoredProcess(int(pid))
if err != nil {
return err
}
// TODO: crosbymichael restore previous process information by saving the init process information in
// the container's state file or separate process state files.
if err := c.updateState(r); err != nil {
return err
}
process.ops = r
break
}
return nil
}
func (c *linuxContainer) updateState(process parentProcess) error { func (c *linuxContainer) updateState(process parentProcess) error {
c.initProcess = process c.initProcess = process
state, err := c.currentState() state, err := c.currentState()