Skip to content
This repository was archived by the owner on May 4, 2021. It is now read-only.

Commit ae25914

Browse files
committed
Started port to go
1 parent 56cfbc1 commit ae25914

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

Dockerfile.chroot

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ ADD . /workspace
55
WORKDIR /workspace
66

77
ENV SSL_CERT_DIR=/etc/ssl/certs
8-
RUN makisu pull --extract /testdir --registry gcr.io --tag v0.1.10 makisu-project/makisu
98
RUN ./tools/scripts/makisu_chroot.sh build -t test:121 --modifyfs=true testdata/build-context/symlink
109
RUN ./tools/scripts/makisu_chroot.sh build -t test:121 --modifyfs=true testdata/build-context/user-change
1110
RUN ./tools/scripts/makisu_chroot.sh build -t test:121 --modifyfs=true testdata/build-context/chroot-simple

bin/makisu/cmd/build.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"path/filepath"
2222
"runtime"
23+
"syscall"
2324
"time"
2425

2526
"github.com/uber/makisu/lib/builder"
@@ -66,14 +67,15 @@ type buildCmd struct {
6667
compressionLevel string
6768

6869
preserveRoot bool
70+
chroot string
6971
}
7072

7173
func getBuildCmd() *buildCmd {
7274
buildCmd := &buildCmd{
7375
Command: &cobra.Command{
74-
Use: "build -t=<image_tag> [flags] <context_path>",
76+
Use: "build -t=<image_tag> [flags] <context_path>",
7577
DisableFlagsInUseLine: true,
76-
Short: "Build docker image, optionally push to registries and/or load into docker daemon",
78+
Short: "Build docker image, optionally push to registries and/or load into docker daemon",
7779
},
7880
}
7981
buildCmd.Args = func(cmd *cobra.Command, args []string) error {
@@ -87,7 +89,6 @@ func getBuildCmd() *buildCmd {
8789
log.Errorf("failed to process flags: %s", err)
8890
os.Exit(1)
8991
}
90-
9192
if err := buildCmd.Build(args[0]); err != nil {
9293
log.Error(err)
9394
os.Exit(1)
@@ -122,6 +123,7 @@ func getBuildCmd() *buildCmd {
122123
buildCmd.PersistentFlags().StringVar(&buildCmd.compressionLevel, "compression", "default", "Image compression level, could be 'no', 'speed', 'size', 'default'")
123124

124125
buildCmd.PersistentFlags().BoolVar(&buildCmd.preserveRoot, "preserve-root", false, "Copy / in the storage dir and copy it back after build.")
126+
rootCmd.PersistentFlags().StringVar(&buildCmd.chroot, "chroot", "", "Executes the command in a chrooted environment.")
125127

126128
buildCmd.MarkFlagRequired("tag")
127129
buildCmd.Flags().SortFlags = false
@@ -214,6 +216,12 @@ func (cmd *buildCmd) newBuildPlan(
214216
func (cmd *buildCmd) Build(contextDir string) error {
215217
log.Infof("Starting Makisu build (version=%s)", utils.BuildHash)
216218

219+
if cmd.chroot != "" {
220+
if err := cmd.prepareChroot(contextDir); err != nil {
221+
return fmt.Errorf("failed to prepare chroot environment: %s", err)
222+
}
223+
}
224+
217225
// Create BuildContext.
218226
contextDirAbs, err := filepath.Abs(contextDir)
219227
if err != nil {
@@ -297,3 +305,29 @@ func (cmd *buildCmd) Build(contextDir string) error {
297305
log.Infof("Finished building %s", imageName.ShortName())
298306
return nil
299307
}
308+
309+
func (cmd *buildCmd) prepareChroot(context string) error {
310+
if runtime.GOOS != "linux" {
311+
return fmt.Errorf("cannot prepare chroot on %s", runtime.GOOS)
312+
}
313+
314+
dest, err := filepath.Abs(cmd.chroot)
315+
if err != nil {
316+
return fmt.Errorf("failed to convert chroot path to absolute: %s", err)
317+
} else if _, err := os.Lstat(dest); err == nil || !os.IsNotExist(err) {
318+
return fmt.Errorf("chroot target must not exist: %s", err)
319+
}
320+
321+
certs := filepath.Join(cmd.chroot, pathutils.DefaultInternalDir, "certs")
322+
if err := os.MkdirAll(certs, 0644); err != nil {
323+
return fmt.Errorf("failed to create chroot cert dir: %s", err)
324+
}
325+
// TODO: copy all certs into the chroot environment.
326+
327+
dev := filepath.Join(cmd.chroot, "dev")
328+
if err := os.MkdirAll(dev, 0644); err != nil {
329+
return fmt.Errorf("failed to create chroot /dev dir: %s", err)
330+
}
331+
// TODO: Create all nods
332+
return syscall.Chroot(dest)
333+
}

tools/scripts/makisu_chroot.sh

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@ function makisu::prepare_internals () {
1616
}
1717

1818
function makisu::prepare_dev () {
19-
mkdir -p $CHROOT/dev
19+
mkdir -p $CHROOT/dev $CHROOT/shm
2020
mknod -m 622 $CHROOT/dev/console c 5 1
21+
mknod -m 622 $CHROOT/dev/initctl p
22+
mknod -m 666 $CHROOT/dev/full c 1 7
2123
mknod -m 666 $CHROOT/dev/null c 1 3
22-
mknod -m 666 $CHROOT/dev/zero c 1 5
2324
mknod -m 666 $CHROOT/dev/ptmx c 5 2
25+
mknod -m 666 $CHROOT/dev/random c 1 8
2426
mknod -m 666 $CHROOT/dev/tty c 5 0
25-
mknod -m 444 $CHROOT/dev/random c 1 8
26-
mknod -m 444 $CHROOT/dev/urandom c 1 9
27+
mknod -m 666 $CHROOT/dev/tty0 c 4 0
28+
mknod -m 666 $CHROOT/dev/urandom c 1 9
29+
mknod -m 666 $CHROOT/dev/zero c 1 5
2730
chown root:tty $CHROOT/dev/{console,ptmx,tty}
31+
32+
# https://github.com/moby/moby/blob/8e610b2b55bfd1bfa9436ab110d311f5e8a74dcb/contrib/mkimage-crux.sh
33+
# https://github.com/moby/moby/blob/8e610b2b55bfd1bfa9436ab110d311f5e8a74dcb/contrib/mkimage-arch.sh
34+
# https://github.com/moby/moby/blob/d7ab8ad145fad4c63112f34721663021e5b02707/contrib/mkimage-yum.sh
2835
}
2936

3037
function makisu::prepare_etc () {

0 commit comments

Comments
 (0)