2013-10-10 04:53:38 -04:00
|
|
|
package sysinit
|
2013-02-13 17:01:44 -05:00
|
|
|
|
|
|
|
import (
|
2013-08-14 20:14:30 -04:00
|
|
|
"encoding/json"
|
2013-02-13 20:24:35 -05:00
|
|
|
"flag"
|
2013-02-13 17:01:44 -05:00
|
|
|
"fmt"
|
2013-12-23 18:39:39 -05:00
|
|
|
"github.com/dotcloud/docker/pkg/netlink"
|
2013-07-01 19:48:59 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-10-30 23:24:47 -04:00
|
|
|
"github.com/syndtr/gocapability/capability"
|
2013-08-13 18:40:23 -04:00
|
|
|
"io/ioutil"
|
2013-02-13 17:01:44 -05:00
|
|
|
"log"
|
2013-09-17 09:02:12 -04:00
|
|
|
"net"
|
2013-02-13 17:01:44 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2013-02-13 20:24:35 -05:00
|
|
|
"strconv"
|
2013-03-23 22:11:00 -04:00
|
|
|
"strings"
|
2013-02-13 17:01:44 -05:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
type DockerInitArgs struct {
|
2013-10-30 23:24:47 -04:00
|
|
|
user string
|
|
|
|
gateway string
|
2013-10-29 14:37:00 -04:00
|
|
|
ip string
|
2013-10-30 23:24:47 -04:00
|
|
|
workDir string
|
|
|
|
privileged bool
|
|
|
|
env []string
|
|
|
|
args []string
|
2013-12-18 23:41:34 -05:00
|
|
|
mtu int
|
2013-12-02 13:58:45 -05:00
|
|
|
}
|
|
|
|
|
2013-10-29 14:31:00 -04:00
|
|
|
func setupHostname(args *DockerInitArgs) error {
|
|
|
|
hostname := getEnv(args, "HOSTNAME")
|
|
|
|
if hostname == "" {
|
2013-12-02 17:21:29 -05:00
|
|
|
return nil
|
2013-02-20 20:45:46 -05:00
|
|
|
}
|
2013-12-18 13:16:26 -05:00
|
|
|
return setHostname(hostname)
|
2013-10-29 14:31:00 -04:00
|
|
|
}
|
|
|
|
|
2013-02-20 20:45:46 -05:00
|
|
|
// Setup networking
|
2013-12-02 17:21:29 -05:00
|
|
|
func setupNetworking(args *DockerInitArgs) error {
|
2013-10-29 14:37:00 -04:00
|
|
|
if args.ip != "" {
|
|
|
|
// eth0
|
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking: %v", err)
|
|
|
|
}
|
|
|
|
ip, ipNet, err := net.ParseCIDR(args.ip)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking: %v", err)
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking: %v", err)
|
|
|
|
}
|
2013-12-18 23:41:34 -05:00
|
|
|
if err := netlink.NetworkSetMTU(iface, args.mtu); err != nil {
|
|
|
|
return fmt.Errorf("Unable to set MTU: %v", err)
|
|
|
|
}
|
2013-10-29 14:37:00 -04:00
|
|
|
if err := netlink.NetworkLinkUp(iface); err != nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking: %v", err)
|
|
|
|
}
|
2013-09-17 09:02:12 -04:00
|
|
|
|
2013-10-29 14:37:00 -04:00
|
|
|
// loopback
|
|
|
|
iface, err = net.InterfaceByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking: %v", err)
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkLinkUp(iface); err != nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking: %v", err)
|
|
|
|
}
|
2013-09-17 09:02:12 -04:00
|
|
|
}
|
2013-10-29 14:37:00 -04:00
|
|
|
if args.gateway != "" {
|
|
|
|
gw := net.ParseIP(args.gateway)
|
|
|
|
if gw == nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking, %s is not a valid gateway IP", args.gateway)
|
|
|
|
}
|
2013-09-17 09:02:12 -04:00
|
|
|
|
2013-10-29 14:37:00 -04:00
|
|
|
if err := netlink.AddDefaultGw(gw); err != nil {
|
|
|
|
return fmt.Errorf("Unable to set up networking: %v", err)
|
|
|
|
}
|
2013-02-20 20:45:46 -05:00
|
|
|
}
|
2013-12-02 17:21:29 -05:00
|
|
|
|
|
|
|
return nil
|
2013-02-20 20:45:46 -05:00
|
|
|
}
|
|
|
|
|
2013-08-15 14:38:17 -04:00
|
|
|
// Setup working directory
|
2013-12-02 17:21:29 -05:00
|
|
|
func setupWorkingDirectory(args *DockerInitArgs) error {
|
2013-12-02 13:58:45 -05:00
|
|
|
if args.workDir == "" {
|
2013-12-02 17:21:29 -05:00
|
|
|
return nil
|
2013-08-15 14:38:17 -04:00
|
|
|
}
|
2013-12-02 13:58:45 -05:00
|
|
|
if err := syscall.Chdir(args.workDir); err != nil {
|
2013-12-02 17:21:29 -05:00
|
|
|
return fmt.Errorf("Unable to change dir to %v: %v", args.workDir, err)
|
2013-10-14 03:27:38 -04:00
|
|
|
}
|
2013-12-02 17:21:29 -05:00
|
|
|
return nil
|
2013-08-15 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2013-02-13 20:24:35 -05:00
|
|
|
// Takes care of dropping privileges to the desired user
|
2013-12-02 17:21:29 -05:00
|
|
|
func changeUser(args *DockerInitArgs) error {
|
2013-12-02 13:58:45 -05:00
|
|
|
if args.user == "" {
|
2013-12-02 17:21:29 -05:00
|
|
|
return nil
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
2013-12-02 13:58:45 -05:00
|
|
|
userent, err := utils.UserLookup(args.user)
|
2013-02-13 20:24:35 -05:00
|
|
|
if err != nil {
|
2013-12-02 17:21:29 -05:00
|
|
|
return fmt.Errorf("Unable to find user %v: %v", args.user, err)
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uid, err := strconv.Atoi(userent.Uid)
|
|
|
|
if err != nil {
|
2013-12-02 17:21:29 -05:00
|
|
|
return fmt.Errorf("Invalid uid: %v", userent.Uid)
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
gid, err := strconv.Atoi(userent.Gid)
|
|
|
|
if err != nil {
|
2013-12-02 17:21:29 -05:00
|
|
|
return fmt.Errorf("Invalid gid: %v", userent.Gid)
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Setgid(gid); err != nil {
|
2013-12-02 17:21:29 -05:00
|
|
|
return fmt.Errorf("setgid failed: %v", err)
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
if err := syscall.Setuid(uid); err != nil {
|
2013-12-02 17:21:29 -05:00
|
|
|
return fmt.Errorf("setuid failed: %v", err)
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
2013-12-02 17:21:29 -05:00
|
|
|
|
|
|
|
return nil
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
|
2013-10-30 23:24:47 -04:00
|
|
|
func setupCapabilities(args *DockerInitArgs) error {
|
|
|
|
|
|
|
|
if args.privileged {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
drop := []capability.Cap{
|
|
|
|
capability.CAP_SETPCAP,
|
|
|
|
capability.CAP_SYS_MODULE,
|
|
|
|
capability.CAP_SYS_RAWIO,
|
|
|
|
capability.CAP_SYS_PACCT,
|
|
|
|
capability.CAP_SYS_ADMIN,
|
|
|
|
capability.CAP_SYS_NICE,
|
|
|
|
capability.CAP_SYS_RESOURCE,
|
|
|
|
capability.CAP_SYS_TIME,
|
|
|
|
capability.CAP_SYS_TTY_CONFIG,
|
|
|
|
capability.CAP_MKNOD,
|
|
|
|
capability.CAP_AUDIT_WRITE,
|
|
|
|
capability.CAP_AUDIT_CONTROL,
|
|
|
|
capability.CAP_MAC_OVERRIDE,
|
|
|
|
capability.CAP_MAC_ADMIN,
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := capability.NewPid(os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Unset(capability.CAPS|capability.BOUNDS, drop...)
|
|
|
|
|
|
|
|
if err := c.Apply(capability.CAPS | capability.BOUNDS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-23 22:11:00 -04:00
|
|
|
// Clear environment pollution introduced by lxc-start
|
2013-12-02 13:58:45 -05:00
|
|
|
func setupEnv(args *DockerInitArgs) {
|
2013-03-23 22:11:00 -04:00
|
|
|
os.Clearenv()
|
2013-12-02 13:58:45 -05:00
|
|
|
for _, kv := range args.env {
|
2013-03-23 22:11:00 -04:00
|
|
|
parts := strings.SplitN(kv, "=", 2)
|
|
|
|
if len(parts) == 1 {
|
|
|
|
parts = append(parts, "")
|
|
|
|
}
|
|
|
|
os.Setenv(parts[0], parts[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 14:31:00 -04:00
|
|
|
func getEnv(args *DockerInitArgs, key string) string {
|
|
|
|
for _, kv := range args.env {
|
|
|
|
parts := strings.SplitN(kv, "=", 2)
|
|
|
|
if parts[0] == key && len(parts) == 2 {
|
|
|
|
return parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2013-12-02 17:21:29 -05:00
|
|
|
func executeProgram(args *DockerInitArgs) error {
|
|
|
|
setupEnv(args)
|
|
|
|
|
2013-10-29 14:31:00 -04:00
|
|
|
if err := setupHostname(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-02 17:21:29 -05:00
|
|
|
if err := setupNetworking(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-30 23:24:47 -04:00
|
|
|
if err := setupCapabilities(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-02 17:21:29 -05:00
|
|
|
if err := setupWorkingDirectory(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := changeUser(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
path, err := exec.LookPath(args.args[0])
|
2013-02-13 20:24:35 -05:00
|
|
|
if err != nil {
|
2013-12-02 13:58:45 -05:00
|
|
|
log.Printf("Unable to locate %v", args.args[0])
|
2013-02-13 20:24:35 -05:00
|
|
|
os.Exit(127)
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
if err := syscall.Exec(path, args.args, os.Environ()); err != nil {
|
2013-02-13 20:24:35 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
2013-12-02 17:21:29 -05:00
|
|
|
|
|
|
|
// Will never reach here
|
|
|
|
return nil
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
|
2013-02-13 17:01:44 -05:00
|
|
|
// Sys Init code
|
|
|
|
// This code is run INSIDE the container and is responsible for setting
|
|
|
|
// up the environment before running the actual process
|
|
|
|
func SysInit() {
|
|
|
|
if len(os.Args) <= 1 {
|
2013-10-18 01:39:57 -04:00
|
|
|
fmt.Println("You should not invoke dockerinit manually")
|
2013-02-13 17:01:44 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
// Get cmdline arguments
|
|
|
|
user := flag.String("u", "", "username or uid")
|
|
|
|
gateway := flag.String("g", "", "gateway address")
|
2013-10-29 14:37:00 -04:00
|
|
|
ip := flag.String("i", "", "ip address")
|
2013-12-02 13:58:45 -05:00
|
|
|
workDir := flag.String("w", "", "workdir")
|
2013-10-30 23:24:47 -04:00
|
|
|
privileged := flag.Bool("privileged", false, "privileged mode")
|
2013-12-18 23:41:34 -05:00
|
|
|
mtu := flag.Int("mtu", 1500, "interface mtu")
|
2013-02-13 20:24:35 -05:00
|
|
|
flag.Parse()
|
2013-02-20 20:45:46 -05:00
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
// Get env
|
|
|
|
var env []string
|
|
|
|
content, err := ioutil.ReadFile("/.dockerenv")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to load environment variables: %v", err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(content, &env); err != nil {
|
|
|
|
log.Fatalf("Unable to unmarshal environment variables: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-11-08 17:15:11 -05:00
|
|
|
// Propagate the plugin-specific container env variable
|
|
|
|
env = append(env, "container="+os.Getenv("container"))
|
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
args := &DockerInitArgs{
|
2013-10-30 23:24:47 -04:00
|
|
|
user: *user,
|
|
|
|
gateway: *gateway,
|
2013-10-29 14:37:00 -04:00
|
|
|
ip: *ip,
|
2013-10-30 23:24:47 -04:00
|
|
|
workDir: *workDir,
|
|
|
|
privileged: *privileged,
|
|
|
|
env: env,
|
|
|
|
args: flag.Args(),
|
2013-12-18 23:41:34 -05:00
|
|
|
mtu: *mtu,
|
2013-12-02 13:58:45 -05:00
|
|
|
}
|
|
|
|
|
2013-12-02 17:21:29 -05:00
|
|
|
if err := executeProgram(args); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-02-13 17:01:44 -05:00
|
|
|
}
|