mirror of
https://github.com/containers/podman.git
synced 2025-09-25 07:44:24 +08:00
vendor in go-criu and dependencies
Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:

committed by
Adrian Reber

parent
58a26ac9dc
commit
20b5714f35
201
vendor/github.com/checkpoint-restore/go-criu/LICENSE
generated
vendored
Normal file
201
vendor/github.com/checkpoint-restore/go-criu/LICENSE
generated
vendored
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
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.
|
27
vendor/github.com/checkpoint-restore/go-criu/README.md
generated
vendored
Normal file
27
vendor/github.com/checkpoint-restore/go-criu/README.md
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
[](https://travis-ci.org/checkpoint-restore/go-criu)
|
||||
|
||||
## go-criu -- Go bindings for [CRIU](https://criu.org/)
|
||||
|
||||
This repository provides Go bindings for CRIU. The code is based on the Go based PHaul
|
||||
implementation from the CRIU repository. For easier inclusion into other Go projects the
|
||||
CRIU Go bindings have been moved to this repository.
|
||||
|
||||
The Go bindings provide an easy way to use the CRIU RPC calls from Go without the need
|
||||
to set up all the infrastructure to make the actual RPC connection to CRIU.
|
||||
|
||||
The following example would print the version of CRIU:
|
||||
```
|
||||
c := criu.MakeCriu()
|
||||
version, err := c.GetCriuVersion()
|
||||
fmt.Println(version)
|
||||
```
|
||||
or to just check if at least a certain CRIU version is installed:
|
||||
```
|
||||
c := criu.MakeCriu()
|
||||
result, err := c.IsCriuAtLeast(31100)
|
||||
```
|
||||
|
||||
### License
|
||||
|
||||
The license of go-criu is the Apache 2.0 license.
|
||||
|
250
vendor/github.com/checkpoint-restore/go-criu/main.go
generated
vendored
Normal file
250
vendor/github.com/checkpoint-restore/go-criu/main.go
generated
vendored
Normal file
@ -0,0 +1,250 @@
|
||||
package criu
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/checkpoint-restore/go-criu/rpc"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
// Criu struct
|
||||
type Criu struct {
|
||||
swrkCmd *exec.Cmd
|
||||
swrkSk *os.File
|
||||
}
|
||||
|
||||
// MakeCriu returns the Criu object required for most operations
|
||||
func MakeCriu() *Criu {
|
||||
return &Criu{}
|
||||
}
|
||||
|
||||
// Prepare sets up everything for the RPC communication to CRIU
|
||||
func (c *Criu) Prepare() error {
|
||||
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cln := os.NewFile(uintptr(fds[0]), "criu-xprt-cln")
|
||||
syscall.CloseOnExec(fds[0])
|
||||
srv := os.NewFile(uintptr(fds[1]), "criu-xprt-srv")
|
||||
defer srv.Close()
|
||||
|
||||
args := []string{"swrk", strconv.Itoa(fds[1])}
|
||||
cmd := exec.Command("criu", args...)
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
cln.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
c.swrkCmd = cmd
|
||||
c.swrkSk = cln
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cleanup cleans up
|
||||
func (c *Criu) Cleanup() {
|
||||
if c.swrkCmd != nil {
|
||||
c.swrkSk.Close()
|
||||
c.swrkSk = nil
|
||||
c.swrkCmd.Wait()
|
||||
c.swrkCmd = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Criu) sendAndRecv(reqB []byte) ([]byte, int, error) {
|
||||
cln := c.swrkSk
|
||||
_, err := cln.Write(reqB)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
respB := make([]byte, 2*4096)
|
||||
n, err := cln.Read(respB)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return respB, n, nil
|
||||
}
|
||||
|
||||
func (c *Criu) doSwrk(reqType rpc.CriuReqType, opts *rpc.CriuOpts, nfy Notify) error {
|
||||
resp, err := c.doSwrkWithResp(reqType, opts, nfy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respType := resp.GetType()
|
||||
if respType != reqType {
|
||||
return errors.New("unexpected responce")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Criu) doSwrkWithResp(reqType rpc.CriuReqType, opts *rpc.CriuOpts, nfy Notify) (*rpc.CriuResp, error) {
|
||||
var resp *rpc.CriuResp
|
||||
|
||||
req := rpc.CriuReq{
|
||||
Type: &reqType,
|
||||
Opts: opts,
|
||||
}
|
||||
|
||||
if nfy != nil {
|
||||
opts.NotifyScripts = proto.Bool(true)
|
||||
}
|
||||
|
||||
if c.swrkCmd == nil {
|
||||
err := c.Prepare()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer c.Cleanup()
|
||||
}
|
||||
|
||||
for {
|
||||
reqB, err := proto.Marshal(&req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respB, respS, err := c.sendAndRecv(reqB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = &rpc.CriuResp{}
|
||||
err = proto.Unmarshal(respB[:respS], resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !resp.GetSuccess() {
|
||||
return resp, fmt.Errorf("operation failed (msg:%s err:%d)",
|
||||
resp.GetCrErrmsg(), resp.GetCrErrno())
|
||||
}
|
||||
|
||||
respType := resp.GetType()
|
||||
if respType != rpc.CriuReqType_NOTIFY {
|
||||
break
|
||||
}
|
||||
if nfy == nil {
|
||||
return resp, errors.New("unexpected notify")
|
||||
}
|
||||
|
||||
notify := resp.GetNotify()
|
||||
switch notify.GetScript() {
|
||||
case "pre-dump":
|
||||
err = nfy.PreDump()
|
||||
case "post-dump":
|
||||
err = nfy.PostDump()
|
||||
case "pre-restore":
|
||||
err = nfy.PreRestore()
|
||||
case "post-restore":
|
||||
err = nfy.PostRestore(notify.GetPid())
|
||||
case "network-lock":
|
||||
err = nfy.NetworkLock()
|
||||
case "network-unlock":
|
||||
err = nfy.NetworkUnlock()
|
||||
case "setup-namespaces":
|
||||
err = nfy.SetupNamespaces(notify.GetPid())
|
||||
case "post-setup-namespaces":
|
||||
err = nfy.PostSetupNamespaces()
|
||||
case "post-resume":
|
||||
err = nfy.PostResume()
|
||||
default:
|
||||
err = nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
req = rpc.CriuReq{
|
||||
Type: &respType,
|
||||
NotifySuccess: proto.Bool(true),
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Dump dumps a process
|
||||
func (c *Criu) Dump(opts rpc.CriuOpts, nfy Notify) error {
|
||||
return c.doSwrk(rpc.CriuReqType_DUMP, &opts, nfy)
|
||||
}
|
||||
|
||||
// Restore restores a process
|
||||
func (c *Criu) Restore(opts rpc.CriuOpts, nfy Notify) error {
|
||||
return c.doSwrk(rpc.CriuReqType_RESTORE, &opts, nfy)
|
||||
}
|
||||
|
||||
// PreDump does a pre-dump
|
||||
func (c *Criu) PreDump(opts rpc.CriuOpts, nfy Notify) error {
|
||||
return c.doSwrk(rpc.CriuReqType_PRE_DUMP, &opts, nfy)
|
||||
}
|
||||
|
||||
// StartPageServer starts the page server
|
||||
func (c *Criu) StartPageServer(opts rpc.CriuOpts) error {
|
||||
return c.doSwrk(rpc.CriuReqType_PAGE_SERVER, &opts, nil)
|
||||
}
|
||||
|
||||
// StartPageServerChld starts the page server and returns PID and port
|
||||
func (c *Criu) StartPageServerChld(opts rpc.CriuOpts) (int, int, error) {
|
||||
resp, err := c.doSwrkWithResp(rpc.CriuReqType_PAGE_SERVER_CHLD, &opts, nil)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
return int(resp.Ps.GetPid()), int(resp.Ps.GetPort()), nil
|
||||
}
|
||||
|
||||
// GetCriuVersion executes the VERSION RPC call and returns the version
|
||||
// as an integer. Major * 10000 + Minor * 100 + SubLevel
|
||||
func (c *Criu) GetCriuVersion() (int, error) {
|
||||
resp, err := c.doSwrkWithResp(rpc.CriuReqType_VERSION, nil, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if resp.GetType() != rpc.CriuReqType_VERSION {
|
||||
return 0, fmt.Errorf("Unexpected CRIU RPC response")
|
||||
}
|
||||
|
||||
version := int(*resp.GetVersion().Major) * 10000
|
||||
version += int(*resp.GetVersion().Minor) * 100
|
||||
if resp.GetVersion().Sublevel != nil {
|
||||
version += int(*resp.GetVersion().Sublevel)
|
||||
}
|
||||
|
||||
if resp.GetVersion().Gitid != nil {
|
||||
// taken from runc: if it is a git release -> increase minor by 1
|
||||
version -= (version % 100)
|
||||
version += 100
|
||||
}
|
||||
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// IsCriuAtLeast checks if the version is at least the same
|
||||
// as the parameter version
|
||||
func (c *Criu) IsCriuAtLeast(version int) (bool, error) {
|
||||
criuVersion, err := c.GetCriuVersion()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if criuVersion >= version {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
63
vendor/github.com/checkpoint-restore/go-criu/notify.go
generated
vendored
Normal file
63
vendor/github.com/checkpoint-restore/go-criu/notify.go
generated
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
package criu
|
||||
|
||||
//Notify interface
|
||||
type Notify interface {
|
||||
PreDump() error
|
||||
PostDump() error
|
||||
PreRestore() error
|
||||
PostRestore(pid int32) error
|
||||
NetworkLock() error
|
||||
NetworkUnlock() error
|
||||
SetupNamespaces(pid int32) error
|
||||
PostSetupNamespaces() error
|
||||
PostResume() error
|
||||
}
|
||||
|
||||
// NoNotify struct
|
||||
type NoNotify struct {
|
||||
}
|
||||
|
||||
// PreDump NoNotify
|
||||
func (c NoNotify) PreDump() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostDump NoNotify
|
||||
func (c NoNotify) PostDump() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PreRestore NoNotify
|
||||
func (c NoNotify) PreRestore() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostRestore NoNotify
|
||||
func (c NoNotify) PostRestore(pid int32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NetworkLock NoNotify
|
||||
func (c NoNotify) NetworkLock() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NetworkUnlock NoNotify
|
||||
func (c NoNotify) NetworkUnlock() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetupNamespaces NoNotify
|
||||
func (c NoNotify) SetupNamespaces(pid int32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostSetupNamespaces NoNotify
|
||||
func (c NoNotify) PostSetupNamespaces() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostResume NoNotify
|
||||
func (c NoNotify) PostResume() error {
|
||||
return nil
|
||||
}
|
1203
vendor/github.com/checkpoint-restore/go-criu/rpc/rpc.pb.go
generated
vendored
Normal file
1203
vendor/github.com/checkpoint-restore/go-criu/rpc/rpc.pb.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
133
vendor/github.com/checkpoint-restore/go-criu/test/main.go
generated
vendored
Normal file
133
vendor/github.com/checkpoint-restore/go-criu/test/main.go
generated
vendored
Normal file
@ -0,0 +1,133 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/checkpoint-restore/go-criu"
|
||||
"github.com/checkpoint-restore/go-criu/rpc"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// TestNfy struct
|
||||
type TestNfy struct {
|
||||
criu.NoNotify
|
||||
}
|
||||
|
||||
// PreDump test function
|
||||
func (c TestNfy) PreDump() error {
|
||||
fmt.Printf("TEST PRE DUMP\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func doDump(c *criu.Criu, pidS string, imgDir string, pre bool, prevImg string) error {
|
||||
fmt.Printf("Dumping\n")
|
||||
pid, _ := strconv.Atoi(pidS)
|
||||
img, err := os.Open(imgDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't open image dir (%s)", err)
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
opts := rpc.CriuOpts{
|
||||
Pid: proto.Int32(int32(pid)),
|
||||
ImagesDirFd: proto.Int32(int32(img.Fd())),
|
||||
LogLevel: proto.Int32(4),
|
||||
LogFile: proto.String("dump.log"),
|
||||
}
|
||||
|
||||
if prevImg != "" {
|
||||
opts.ParentImg = proto.String(prevImg)
|
||||
opts.TrackMem = proto.Bool(true)
|
||||
}
|
||||
|
||||
if pre {
|
||||
err = c.PreDump(opts, TestNfy{})
|
||||
} else {
|
||||
err = c.Dump(opts, TestNfy{})
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("dump fail (%s)", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Usage: test $act $pid $images_dir
|
||||
func main() {
|
||||
c := criu.MakeCriu()
|
||||
// Read out CRIU version
|
||||
version, err := c.GetCriuVersion()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("CRIU version", version)
|
||||
// Check if version at least 3.2
|
||||
result, err := c.IsCriuAtLeast(30200)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !result {
|
||||
fmt.Println("CRIU too old")
|
||||
os.Exit(1)
|
||||
}
|
||||
act := os.Args[1]
|
||||
switch act {
|
||||
case "dump":
|
||||
err := doDump(c, os.Args[2], os.Args[3], false, "")
|
||||
if err != nil {
|
||||
fmt.Print(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
case "dump2":
|
||||
err := c.Prepare()
|
||||
if err != nil {
|
||||
fmt.Print(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = doDump(c, os.Args[2], os.Args[3]+"/pre", true, "")
|
||||
if err != nil {
|
||||
fmt.Printf("pre-dump failed")
|
||||
fmt.Print(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = doDump(c, os.Args[2], os.Args[3], false, "./pre")
|
||||
if err != nil {
|
||||
fmt.Printf("dump failed")
|
||||
fmt.Print(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
c.Cleanup()
|
||||
case "restore":
|
||||
fmt.Printf("Restoring\n")
|
||||
img, err := os.Open(os.Args[2])
|
||||
if err != nil {
|
||||
fmt.Printf("can't open image dir")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
opts := rpc.CriuOpts{
|
||||
ImagesDirFd: proto.Int32(int32(img.Fd())),
|
||||
LogLevel: proto.Int32(4),
|
||||
LogFile: proto.String("restore.log"),
|
||||
}
|
||||
|
||||
err = c.Restore(opts, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Error:")
|
||||
fmt.Print(err)
|
||||
fmt.Printf("\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
default:
|
||||
fmt.Printf("unknown action\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Success\n")
|
||||
}
|
192
vendor/github.com/checkpoint-restore/go-criu/test/phaul-main.go
generated
vendored
Normal file
192
vendor/github.com/checkpoint-restore/go-criu/test/phaul-main.go
generated
vendored
Normal file
@ -0,0 +1,192 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/checkpoint-restore/go-criu"
|
||||
"github.com/checkpoint-restore/go-criu/phaul"
|
||||
"github.com/checkpoint-restore/go-criu/rpc"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
type testLocal struct {
|
||||
criu.NoNotify
|
||||
r *testRemote
|
||||
}
|
||||
|
||||
type testRemote struct {
|
||||
srv *phaul.Server
|
||||
}
|
||||
|
||||
/* Dir where test will put dump images */
|
||||
const imagesDir = "image"
|
||||
|
||||
func prepareImages() error {
|
||||
err := os.Mkdir(imagesDir, 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
/* Work dir for PhaulClient */
|
||||
err = os.Mkdir(imagesDir+"/local", 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
/* Work dir for PhaulServer */
|
||||
err = os.Mkdir(imagesDir+"/remote", 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
/* Work dir for DumpCopyRestore */
|
||||
err = os.Mkdir(imagesDir+"/test", 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeImages(dumpDir, lastPreDumpDir string) error {
|
||||
idir, err := os.Open(dumpDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer idir.Close()
|
||||
|
||||
imgs, err := idir.Readdirnames(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, fname := range imgs {
|
||||
if !strings.HasSuffix(fname, ".img") {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("\t%s -> %s/\n", fname, lastPreDumpDir)
|
||||
err = syscall.Link(dumpDir+"/"+fname, lastPreDumpDir+"/"+fname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *testRemote) doRestore() error {
|
||||
lastSrvImagesDir := r.srv.LastImagesDir()
|
||||
/*
|
||||
* In imagesDir we have images from dump, in the
|
||||
* lastSrvImagesDir -- where server-side images
|
||||
* (from page server, with pages and pagemaps) are.
|
||||
* Need to put former into latter and restore from
|
||||
* them.
|
||||
*/
|
||||
err := mergeImages(imagesDir+"/test", lastSrvImagesDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imgDir, err := os.Open(lastSrvImagesDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer imgDir.Close()
|
||||
|
||||
opts := rpc.CriuOpts{
|
||||
LogLevel: proto.Int32(4),
|
||||
LogFile: proto.String("restore.log"),
|
||||
ImagesDirFd: proto.Int32(int32(imgDir.Fd())),
|
||||
}
|
||||
|
||||
cr := r.srv.GetCriu()
|
||||
fmt.Printf("Do restore\n")
|
||||
return cr.Restore(opts, nil)
|
||||
}
|
||||
|
||||
func (l *testLocal) PostDump() error {
|
||||
return l.r.doRestore()
|
||||
}
|
||||
|
||||
func (l *testLocal) DumpCopyRestore(cr *criu.Criu, cfg phaul.Config, lastClnImagesDir string) error {
|
||||
fmt.Printf("Final stage\n")
|
||||
|
||||
imgDir, err := os.Open(imagesDir + "/test")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer imgDir.Close()
|
||||
|
||||
psi := rpc.CriuPageServerInfo{
|
||||
Fd: proto.Int32(int32(cfg.Memfd)),
|
||||
}
|
||||
|
||||
opts := rpc.CriuOpts{
|
||||
Pid: proto.Int32(int32(cfg.Pid)),
|
||||
LogLevel: proto.Int32(4),
|
||||
LogFile: proto.String("dump.log"),
|
||||
ImagesDirFd: proto.Int32(int32(imgDir.Fd())),
|
||||
TrackMem: proto.Bool(true),
|
||||
ParentImg: proto.String(lastClnImagesDir),
|
||||
Ps: &psi,
|
||||
}
|
||||
|
||||
fmt.Printf("Do dump\n")
|
||||
return cr.Dump(opts, l)
|
||||
}
|
||||
|
||||
func main() {
|
||||
pid, _ := strconv.Atoi(os.Args[1])
|
||||
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
|
||||
if err != nil {
|
||||
fmt.Printf("Can't make socketpair: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = prepareImages()
|
||||
if err != nil {
|
||||
fmt.Printf("Can't prepare dirs for images: %v\n", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Make server part (socket %d)\n", fds[1])
|
||||
srv, err := phaul.MakePhaulServer(phaul.Config{
|
||||
Pid: pid,
|
||||
Memfd: fds[1],
|
||||
Wdir: imagesDir + "/remote"})
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to run a server: %v", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
r := &testRemote{srv}
|
||||
|
||||
fmt.Printf("Make client part (socket %d)\n", fds[0])
|
||||
cln, err := phaul.MakePhaulClient(&testLocal{r: r}, srv,
|
||||
phaul.Config{
|
||||
Pid: pid,
|
||||
Memfd: fds[0],
|
||||
Wdir: imagesDir + "/local"})
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to run a client: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Migrate\n")
|
||||
err = cln.Migrate()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("SUCCESS!\n")
|
||||
}
|
57
vendor/github.com/checkpoint-restore/go-criu/test/piggie.c
generated
vendored
Normal file
57
vendor/github.com/checkpoint-restore/go-criu/test/piggie.c
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <sched.h>
|
||||
|
||||
#define STKS (4*4096)
|
||||
|
||||
#ifndef CLONE_NEWPID
|
||||
#define CLONE_NEWPID 0x20000000
|
||||
#endif
|
||||
|
||||
static int do_test(void *logf)
|
||||
{
|
||||
int fd, i = 0;
|
||||
|
||||
setsid();
|
||||
|
||||
close(0);
|
||||
close(1);
|
||||
close(2);
|
||||
|
||||
fd = open("/dev/null", O_RDONLY);
|
||||
if (fd != 0) {
|
||||
dup2(fd, 0);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
fd = open(logf, O_WRONLY | O_TRUNC | O_CREAT, 0600);
|
||||
dup2(fd, 1);
|
||||
dup2(fd, 2);
|
||||
if (fd != 1 && fd != 2)
|
||||
close(fd);
|
||||
|
||||
while (1) {
|
||||
sleep(1);
|
||||
printf("%d\n", i++);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int pid;
|
||||
void *stk;
|
||||
|
||||
stk = mmap(NULL, STKS, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, 0, 0);
|
||||
pid = clone(do_test, stk + STKS, SIGCHLD | CLONE_NEWPID, argv[1]);
|
||||
printf("Child forked, pid %d\n", pid);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user