github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/tools/syz-env (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2020 syzkaller project authors. All rights reserved.
     3  # Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     4  
     5  # syz-env is a wrapper around gcr.io/syzkaller/env container,
     6  # which includes all tools necessary to develop/test syzkaller.
     7  # It's recommended to create an alias for this script:
     8  #
     9  #	alias syz-env="$(go env GOPATH)/src/github.com/google/syzkaller/tools/syz-env"
    10  #
    11  # Then it can be used to wrap almost any make invocation as:
    12  #
    13  #	syz-env make format
    14  #	syz-env make presubmit
    15  #	syz-env make extract SOURCEDIR=~/linux
    16  #
    17  # Or you may run the shell inside of the container with just syz-env.
    18  #
    19  # Note: this way everything runs inside of the container
    20  # and uses all tools bundled in the container rather than host tools.
    21  #
    22  # Note: syz-env assumes a sudo-less Docker is installed, see:
    23  # https://docs.docker.com/engine/install
    24  # https://docs.docker.com/engine/install/linux-postinstall
    25  # (Googlers see go/docker).
    26  
    27  COMMAND=""
    28  BUILDARGS=()
    29  DOCKERARGS=()
    30  if [ -n $http_proxy ]; then
    31  	BUILDARGS+=" --build-arg http_proxy=$http_proxy"
    32  	DOCKERARGS+=" --env http_proxy=$http_proxy"
    33  fi
    34  if [ -n $https_proxy ]; then
    35  	BUILDARGS+=" --build-arg https_proxy=$https_proxy"
    36  	DOCKERARGS+=" --env https_proxy=$https_proxy"
    37  fi
    38  if [ -n $no_proxy ]; then
    39  	BUILDARGS+=" --build-arg no_proxy=$no_proxy"
    40  	DOCKERARGS+=" --env no_proxy=$no_proxy"
    41  fi
    42  
    43  for ARG in "$@"; do
    44  	while IFS='=' read KEY VAL; do
    45  		# If we have a kernel path passed in, we mount it in the container
    46  		# at /syzkaller/kernel and fix up SOURCEDIR argument.
    47  		if [ "$KEY" == "SOURCEDIR" ]; then
    48  			DOCKERARGS+=" --volume $VAL:/syzkaller/kernel:z"
    49  			COMMAND+=" SOURCEDIR=/syzkaller/kernel"
    50  		else
    51  			COMMAND+=" $ARG"
    52  		fi
    53  	done <<< "$ARG"
    54  done
    55  if [ "$CI" == "" ]; then
    56  	# This gives interactive shell and allows to abort commands with Ctrl+C.
    57  	DOCKERARGS+=" -it"
    58  fi
    59  if [ "$COMMAND" == "" ]; then
    60  	COMMAND="bash"
    61  fi
    62  
    63  SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
    64  IMAGE="env"
    65  if [ "$(basename -- "$0")" == "syz-old-env" ]; then
    66          IMAGE="old-env"
    67  fi
    68  
    69  # If we're running rootless docker, files owned by the host user appear within the
    70  # container as being owned by root.
    71  #
    72  # If we're running regular rootful docker, we need to specify --user, as otherwise
    73  # processes within the container will create files with the wrong ownership.
    74  if [ ! "$(docker info -f "{{println .SecurityOptions}}" | grep rootless)" ]; then
    75  	DOCKERARGS+=" --user $(id -u ${USER}):$(id -g ${USER})"
    76  fi
    77  
    78  
    79  # Build or update docker image
    80  if [ ! -z "$SYZ_ENV_BUILD" ]; then
    81  	IMAGE_NAME="syz-$IMAGE"
    82  	docker build "$SCRIPT_DIR/docker/$IMAGE" --tag "$IMAGE_NAME" ${BUILDARGS[@]}
    83  else
    84  	IMAGE_NAME="gcr.io/syzkaller/$IMAGE"
    85  	docker pull -q "$IMAGE_NAME"
    86  fi
    87  
    88  # Run everything as the host user, this is important for created/modified files.
    89  docker run \
    90  	--rm \
    91  	--volume "$SCRIPT_DIR/..:/syzkaller/gopath/src/github.com/google/syzkaller:z" \
    92  	--volume "$HOME/.cache:/syzkaller/.cache:z" \
    93  	--volume "/var/run/docker.sock":"/var/run/docker.sock" \
    94  	--workdir /syzkaller/gopath/src/github.com/google/syzkaller \
    95  	--env HOME=/syzkaller \
    96  	--env GOPATH=/syzkaller/gopath:/gopath \
    97  	--env GOPROXY \
    98  	--env FUZZIT_API_KEY \
    99  	--env GITHUB_REF \
   100  	--env GITHUB_SHA \
   101  	--env GITHUB_PR_HEAD_SHA \
   102  	--env GITHUB_PR_BASE_SHA \
   103  	--env GITHUB_PR_COMMITS \
   104  	--env CI \
   105  	${DOCKERARGS[@]} \
   106  	"$IMAGE_NAME" -c "$COMMAND"