README.md 3.0 KB
Newer Older
1
# syscontainer-hooks
O
overweight 已提交
2

3
This is a simple custom syscontainer hook for our own need,
O
overweight 已提交
4 5 6
it interacts with isulad as a multifunctional hook.

 1. allow user to add your own devices or binds into the container and update device Qos for container(device hook in prestart state).
7
 2. allow user to remove udev rule which added by syscontainer-tools when container is exiting(device hook in post-stop state).
O
overweight 已提交
8 9 10 11
 3. allow user to add network interface and route rule to container(network hook in prestart state).
 4. allow user to remove network interface on host when container is exiting(network hook in post-stop state).
 5. allow user to do oci relabel for container in both prestart and post-stop state for container.

12 13
Actually, this hook only handles the container restart process, we use syscontainer-tools to
add device/binds/network interface/route rule to container. And syscontainer-tools will save the device/network config to disk.
O
overweight 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
And the hook will make sure the resources you added to container will be persistent after restart.

Rename it to your favourite name afterwards.

## build

To build the binary, you need to download it then run 

```
# make
# sudo make install
```

Note: make install will install the binary into your "/usr/bin",
it's not a mandatory step, make your own choice for your convenience :)


## customise hook service

33
We could use `syscontainer-hooks` to customise the hook service.
O
overweight 已提交
34
```
35
Usage of syscontainer-hooks:
O
overweight 已提交
36 37 38
  -log string
        set output log file
  -state string
39
        set syscontainer hook state mode: prestart or poststop
O
overweight 已提交
40
  -with-relabel
41
        syscontainer hook enable oci relabel hook function
O
overweight 已提交
42 43 44 45 46 47 48
```

As block device and network interface are both in our requirement, so these two function are mandantory.
We could use `--with-relabel=true` to add oci-relabel hook service for container.
We could use `--state` to specify which state the hook will be running in.

Full hook config:
49
[hook spec example of syscontainer-hooks](hooks/syscontainer-hooks/example/hookspec.json)
O
overweight 已提交
50 51 52

## Try it!

53
First you need an enhanced `isula` with newly added `--hook-spec` flag,
O
overweight 已提交
54 55 56 57 58
after that, you can run it like this:

1.run isulad container with hook spec in `example` directory

```
59
$ isula run -d --name test_device --hook-spec $PWD/example/hookspec.json busybox sleep 20000
O
overweight 已提交
60
```
61
2.use syscontainer-tools to add device or binds to container
O
overweight 已提交
62 63

```
64
syscontainer-tools add-device test_device /dev/zero:/dev/test_zero:rwm /dev/zero:/dev/test_zero2:rwm
O
overweight 已提交
65 66 67 68 69
```

3.restart the container. to check the device is still in container.

```
70
isula restart test_device
O
overweight 已提交
71 72 73 74 75 76 77 78
```

Let's check the [`hookspec.json`](example/hookspec.json) file:

```
{
        "prestart": [
            {
79
                "path": "/var/lib/isulad/hooks/device-hook",
O
overweight 已提交
80 81 82 83 84 85 86 87 88 89 90
                "args": ["device-hook"],
                "env": []
            }
        ],
        "poststart":[],
        "poststop":[]
}
```

# Contact me

Z
zhangsong234 已提交
91
If you have any question or suggestion, please contact: isulad@openeuler.org.
O
overweight 已提交
92
Also welcome for any issue or MR! Thanks!