storage.go 4.8 KB
Newer Older
W
wangkang101 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/*
 * Copyright (c) 2020 Huawei Technologies Co., Ltd.
 * isula-transform is licensed under the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 * Create: 2020-04-24
 */

package transform

import (
	"context"
	"fmt"
	"net"
	"strings"
	"time"

	"github.com/sirupsen/logrus"
	"google.golang.org/grpc"
	"isula.org/isula-transform/api/isula"
	"isula.org/isula-transform/types"
)

// StorageType define graph storage driver
type StorageType string

// support storage driver
const (
	Overlay2         StorageType = "overlay2"
	DeviceMapper     StorageType = "devicemapper"
	defaultAddress               = "unix:///var/run/isuald/isula_image.sock"
	isuladImgTimeout             = 10 * time.Second
)

var (
	gBaseStorageDriver BaseStorageDriver
)

// StorageDriver defines methods for creating and rolling storage resources
type StorageDriver interface {
	// GenerateRootFs returns a new rootfs path used by container
	GenerateRootFs(id, image string) (string, error)
	// TransformRWLayer migrates container read-write layer data
	TransformRWLayer(ctr *types.IsuladV2Config, oldRootFs string) error
	// Cleanup olls back the image operation when the transformation fails
	Cleanup(id string)
}

// BaseStorageDriver contains the common functions used by StorageDriver
type BaseStorageDriver interface {
	GenerateRootFs(id, image string) (string, error)
	CleanupRootFs(id string)
	MountRootFs(id string) error
	UmountRootFs(id string) error
}

type baseStorageDriver struct {
	imgClient isula.ImageServiceClient
}

func initBaseStorageDriver(addr string) error {
	client, err := newIsuladImgClient(addr)
	if err != nil {
		return err
	}
	gBaseStorageDriver = &baseStorageDriver{imgClient: client}
	return nil
}

// GenerateRootFs returns a new rootfs path of container
func (sd *baseStorageDriver) GenerateRootFs(id, image string) (string, error) {
	req := &isula.ContainerPrepareRequest{
		Image: image,
		Id:    id,
		Name:  id,
	}
	resp, err := sd.imgClient.ContainerPrepare(context.Background(), req)
	if err != nil {
		return "", err
	}
	if msg := resp.GetErrmsg(); msg != "" {
		removeReq := &isula.ContainerRemoveRequest{
			NameId: id,
		}
		rResp, rErr := sd.imgClient.ContainerRemove(context.Background(), removeReq)
		logrus.Infof("isulad-img remove container: %v, err: %v", rResp, rErr)
		return "", fmt.Errorf("isulad-img prepare failed: %s", msg)
	}
	return resp.MountPoint, nil
}

// CleanupRootFs cleans up container data storaged in the isulad
func (sd *baseStorageDriver) CleanupRootFs(id string) {
	req := &isula.ContainerRemoveRequest{
		NameId: id,
	}
	// During the rollback, only information is collected
	_, err := sd.imgClient.ContainerRemove(context.Background(), req)
	if err != nil {
		logrus.Warnf("isulad-img remove container %s: %v", id, err)
	} else {
		logrus.Infof("isulad-img remove container %s successful", id)
	}
}

// MountRootFs mounts the rw layer of container
func (sd *baseStorageDriver) MountRootFs(id string) error {
	req := &isula.ContainerMountRequest{
		NameId: id,
	}
	resp, err := sd.imgClient.ContainerMount(context.Background(), req)
	if err != nil {
		return err
	}
	if msg := resp.GetErrmsg(); msg != "" {
		return fmt.Errorf("isulad-img mount failed: %s", msg)
	}
	return nil
}

// UmountRootFs umounts the rw layer of container
func (sd *baseStorageDriver) UmountRootFs(id string) error {
	req := &isula.ContainerUmountRequest{
		NameId: id,
	}
	resp, err := sd.imgClient.ContainerUmount(context.Background(), req)
	if err != nil {
		return err
	}
	if msg := resp.GetErrmsg(); msg != "" {
		req.Force = true
		fResp, fErr := sd.imgClient.ContainerUmount(context.Background(), req)
		logrus.Infof("isulad-img force umount container: %v, err: %v", fResp, fErr)
		if fErr == nil && fResp.GetErrmsg() == "" {
			return nil
		}
		return fmt.Errorf("isulad-img umount failed: %s", msg)
	}
	return nil
}

func dialOpt(ctx context.Context, addr string) (net.Conn, error) {
	// dialer to support unix dial
	dialer := func(addr string, timeout time.Duration) (net.Conn, error) {
		proto, address := "unix", strings.TrimPrefix(addr, "unix://")
		return net.DialTimeout(proto, address, timeout)
	}
	if deadline, ok := ctx.Deadline(); ok {
		return dialer(addr, time.Until(deadline))
	}
	return dialer(addr, isuladImgTimeout)
}

func newIsuladImgClient(addr string, opts ...grpc.DialOption) (isula.ImageServiceClient, error) {
	if addr == "" {
		addr = defaultAddress
	}
	opts = append(opts, grpc.WithInsecure(), grpc.WithContextDialer(dialOpt))
	conn, err := grpc.Dial(addr, opts...)
	if err != nil {
		return nil, err
	}

	return isula.NewImageServiceClient(conn), nil
}