github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/create-image.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2016 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  # create-image.sh creates a minimal Debian Linux image suitable for syzkaller.
     6  
     7  set -eux
     8  
     9  # Create a minimal Debian distribution in a directory.
    10  PREINSTALL_PKGS=openssh-server,curl,tar,gcc,libc6-dev,time,strace,sudo,less,psmisc,selinux-utils,policycoreutils,checkpolicy,selinux-policy-default,firmware-atheros,debian-ports-archive-keyring
    11  
    12  # If ADD_PACKAGE is not defined as an external environment variable, use our default packages
    13  if [ -z ${ADD_PACKAGE+x} ]; then
    14      ADD_PACKAGE="make,sysbench,git,vim,tmux,usbutils,tcpdump"
    15  fi
    16  
    17  # Variables affected by options
    18  ARCH=$(uname -m)
    19  RELEASE=bullseye
    20  FEATURE=minimal
    21  SEEK=2047
    22  PERF=false
    23  
    24  # Display help function
    25  display_help() {
    26      echo "Usage: $0 [option...] " >&2
    27      echo
    28      echo "   -a, --arch                 Set architecture"
    29      echo "   -d, --distribution         Set on which debian distribution to create"
    30      echo "   -f, --feature              Check what packages to install in the image, options are minimal, full"
    31      echo "   -s, --seek                 Image size (MB), default 2048 (2G)"
    32      echo "   -h, --help                 Display help message"
    33      echo "   -p, --add-perf             Add perf support with this option enabled. Please set envrionment variable \$KERNEL at first"
    34      echo
    35  }
    36  
    37  while true; do
    38      if [ $# -eq 0 ];then
    39  	echo $#
    40  	break
    41      fi
    42      case "$1" in
    43          -h | --help)
    44              display_help
    45              exit 0
    46              ;;
    47          -a | --arch)
    48  	    ARCH=$2
    49              shift 2
    50              ;;
    51          -d | --distribution)
    52  	    RELEASE=$2
    53              shift 2
    54              ;;
    55          -f | --feature)
    56  	    FEATURE=$2
    57              shift 2
    58              ;;
    59          -s | --seek)
    60  	    SEEK=$(($2 - 1))
    61              shift 2
    62              ;;
    63          -p | --add-perf)
    64  	    PERF=true
    65              shift 1
    66              ;;
    67          -*)
    68              echo "Error: Unknown option: $1" >&2
    69              exit 1
    70              ;;
    71          *)  # No more options
    72              break
    73              ;;
    74      esac
    75  done
    76  
    77  # Handle cases where qemu and Debian use different arch names
    78  case "$ARCH" in
    79      ppc64le)
    80          DEBARCH=ppc64el
    81          ;;
    82      aarch64)
    83          DEBARCH=arm64
    84          ;;
    85      arm)
    86          DEBARCH=armel
    87          ;;
    88      x86_64)
    89          DEBARCH=amd64
    90          ;;
    91      *)
    92          DEBARCH=$ARCH
    93          ;;
    94  esac
    95  
    96  # Foreign architecture
    97  
    98  FOREIGN=false
    99  if [ $ARCH != $(uname -m) ]; then
   100      # i386 on an x86_64 host is exempted, as we can run i386 binaries natively
   101      if [ $ARCH != "i386" -o $(uname -m) != "x86_64" ]; then
   102          FOREIGN=true
   103      fi
   104  fi
   105  
   106  if [ $FOREIGN = "true" ]; then
   107      # Check for according qemu static binary
   108      if ! which qemu-$ARCH-static; then
   109          echo "Please install qemu static binary for architecture $ARCH (package 'qemu-user-static' on Debian/Ubuntu/Fedora)"
   110          exit 1
   111      fi
   112      # Check for according binfmt entry
   113      if [ ! -r /proc/sys/fs/binfmt_misc/qemu-$ARCH ]; then
   114          echo "binfmt entry /proc/sys/fs/binfmt_misc/qemu-$ARCH does not exist"
   115          exit 1
   116      fi
   117  fi
   118  
   119  # Double check KERNEL when PERF is enabled
   120  if [ $PERF = "true" ] && [ -z ${KERNEL+x} ]; then
   121      echo "Please set KERNEL environment variable when PERF is enabled"
   122      exit 1
   123  fi
   124  
   125  # If full feature is chosen, install more packages
   126  if [ $FEATURE = "full" ]; then
   127      PREINSTALL_PKGS=$PREINSTALL_PKGS","$ADD_PACKAGE
   128  fi
   129  
   130  DIR=$RELEASE
   131  sudo rm -rf $DIR
   132  sudo mkdir -p $DIR
   133  sudo chmod 0755 $DIR
   134  
   135  # 1. debootstrap stage
   136  
   137  DEBOOTSTRAP_PARAMS="--arch=$DEBARCH --include=$PREINSTALL_PKGS --components=main,contrib,non-free,non-free-firmware $RELEASE $DIR"
   138  if [ $FOREIGN = "true" ]; then
   139      DEBOOTSTRAP_PARAMS="--foreign $DEBOOTSTRAP_PARAMS"
   140  fi
   141  
   142  # riscv64 is hosted in the debian-ports repository
   143  # debian-ports doesn't include non-free, so we exclude firmware-atheros
   144  if [ $DEBARCH == "riscv64" ]; then
   145      DEBOOTSTRAP_PARAMS="--keyring /usr/share/keyrings/debian-ports-archive-keyring.gpg --exclude firmware-atheros $DEBOOTSTRAP_PARAMS http://deb.debian.org/debian-ports"
   146  fi
   147  
   148  # debootstrap may fail for EoL Debian releases
   149  RET=0
   150  sudo --preserve-env=http_proxy,https_proxy,ftp_proxy,no_proxy debootstrap $DEBOOTSTRAP_PARAMS || RET=$?
   151  
   152  if [ $RET != 0 ] && [ $DEBARCH != "riscv64" ]; then
   153      # Try running debootstrap again using the Debian archive
   154      DEBOOTSTRAP_PARAMS="--keyring /usr/share/keyrings/debian-archive-removed-keys.gpg $DEBOOTSTRAP_PARAMS https://archive.debian.org/debian-archive/debian/"
   155      sudo --preserve-env=http_proxy,https_proxy,ftp_proxy,no_proxy debootstrap $DEBOOTSTRAP_PARAMS
   156  fi
   157  
   158  # 2. debootstrap stage: only necessary if target != host architecture
   159  
   160  if [ $FOREIGN = "true" ]; then
   161      sudo cp $(which qemu-$ARCH-static) $DIR/$(which qemu-$ARCH-static)
   162      sudo chroot $DIR /bin/bash -c "/debootstrap/debootstrap --second-stage"
   163  fi
   164  
   165  # Set some defaults and enable promtless ssh to the machine for root.
   166  sudo sed -i '/^root/ { s/:x:/::/ }' $DIR/etc/passwd
   167  echo 'T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100' | sudo tee -a $DIR/etc/inittab
   168  printf '\nauto eth0\niface eth0 inet dhcp\n' | sudo tee -a $DIR/etc/network/interfaces
   169  echo '/dev/root / ext4 defaults 0 0' | sudo tee -a $DIR/etc/fstab
   170  echo 'debugfs /sys/kernel/debug debugfs defaults 0 0' | sudo tee -a $DIR/etc/fstab
   171  echo 'securityfs /sys/kernel/security securityfs defaults 0 0' | sudo tee -a $DIR/etc/fstab
   172  echo 'configfs /sys/kernel/config/ configfs defaults 0 0' | sudo tee -a $DIR/etc/fstab
   173  echo 'binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0' | sudo tee -a $DIR/etc/fstab
   174  echo -en "127.0.0.1\tlocalhost\n" | sudo tee $DIR/etc/hosts
   175  echo "nameserver 8.8.8.8" | sudo tee -a $DIR/etc/resolve.conf
   176  echo "syzkaller" | sudo tee $DIR/etc/hostname
   177  ssh-keygen -f $RELEASE.id_rsa -t rsa -N ''
   178  sudo mkdir -p $DIR/root/.ssh/
   179  cat $RELEASE.id_rsa.pub | sudo tee $DIR/root/.ssh/authorized_keys
   180  
   181  # Add perf support
   182  if [ $PERF = "true" ]; then
   183      cp -r $KERNEL $DIR/tmp/
   184      BASENAME=$(basename $KERNEL)
   185      sudo chroot $DIR /bin/bash -c "apt-get update; apt-get install -y flex bison python-dev libelf-dev libunwind8-dev libaudit-dev libslang2-dev libperl-dev binutils-dev liblzma-dev libnuma-dev"
   186      sudo chroot $DIR /bin/bash -c "cd /tmp/$BASENAME/tools/perf/; make"
   187      sudo chroot $DIR /bin/bash -c "cp /tmp/$BASENAME/tools/perf/perf /usr/bin/"
   188      rm -r $DIR/tmp/$BASENAME
   189  fi
   190  
   191  # Add udev rules for custom drivers.
   192  # Create a /dev/vim2m symlink for the device managed by the vim2m driver
   193  echo 'ATTR{name}=="vim2m", SYMLINK+="vim2m"' | sudo tee -a $DIR/etc/udev/rules.d/50-udev-default.rules
   194  
   195  # Build a disk image
   196  dd if=/dev/zero of=$RELEASE.img bs=1M seek=$SEEK count=1
   197  sudo mkfs.ext4 -F $RELEASE.img
   198  sudo mkdir -p /mnt/$DIR
   199  sudo mount -o loop $RELEASE.img /mnt/$DIR
   200  sudo cp -a $DIR/. /mnt/$DIR/.
   201  sudo umount /mnt/$DIR