github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/tools/create-ec2-rootfs.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2023 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  # Author: Kuniyuki Iwashima <kuniyu@amazon.com>
     6  #
     7  # create-ec2-rootfs.sh creates a rootfs from AL2023 container image.
     8  #
     9  # Usage:
    10  #
    11  #   1) Create a rootfs
    12  #
    13  #     ./create-ec2-rootfs.sh -f xfs -n rootfs.xfs -s 2G
    14  #
    15  #   2) Extend a rootfs
    16  #
    17  #     ./create-ec2-rootfs.sh -f xfs -n rootfs.xfs -s 4G
    18  #
    19  # The image can be tested locally with e.g.:
    20  #
    21  #   qemu-system-x86_64 -boot c -m 2G -kernel ${PATH_TO_bzImage} -hda ${PATH_TO_ROOTFS} \
    22  #                      -append "root=/dev/sda rw console=ttyS0,115200" \
    23  #                      -serial stdio -display none -nic user,hostfwd=tcp::10022-:22 \
    24  #                      -enable-kvm -cpu host
    25  #
    26  # once the kernel boots, you can ssh into it with:
    27  #
    28  #   ssh -o StrictHostKeyChecking=no -p 10022 root@localhost
    29  #
    30  
    31  set -eux
    32  
    33  NAME="rootfs.ext4"
    34  FORMAT="ext4"
    35  RESIZER="resize2fs"
    36  SIZE="1G"
    37  IMAGE="amazonlinux:2023"
    38  PLATFORM="linux/amd64"
    39  
    40  # Display help function
    41  display_help() {
    42      echo "Usage: $0 [option...] " >&2
    43      echo
    44      echo "   -f, --format               rootfs format (ext4 or xfs), default ext4"
    45      echo "   -h, --help                 Display help message"
    46      echo "   -n, --name                 rootfs name, default rootfs.ext4"
    47      echo "   -p, --platform             linux platform type, default linux/amd64"
    48      echo "   -s, --size                 rootfs size, default 1G"
    49      echo
    50  }
    51  
    52  while true; do
    53      if [ $# -eq 0 ]; then
    54          break
    55      fi
    56      case "$1" in
    57          -h | --help)
    58              display_help
    59              exit 0
    60              ;;
    61          -f | --format)
    62              FORMAT=$2
    63              shift 2
    64  
    65              case "${FORMAT}" in
    66                  ext4)
    67                      RESIZER="resize2fs"
    68                      ;;
    69                  xfs)
    70                      RESIZER="xfs_growfs"
    71                      ;;
    72                  -*)
    73                      echo "Error Unknown format: ${FORMAT}" >&2
    74                      exit 1
    75                      ;;
    76              esac
    77              ;;
    78          -n | --name)
    79              NAME=$2
    80              shift 2
    81              ;;
    82          -p | --platform)
    83              PLATFORM=$2
    84              shift 2
    85              ;;
    86          -s | --size)
    87              SIZE=$2
    88              shift 2
    89              ;;
    90          -*)
    91              echo "Error: Unknown option: $1" >&2
    92              exit 1
    93              ;;
    94          *)
    95              break
    96              ;;
    97      esac
    98  done
    99  
   100  MOUNT_DIR=$(mktemp -d)
   101  
   102  if [ -f "${NAME}" ]; then
   103      truncate -s ${SIZE} ${NAME}
   104      sudo mount -o loop ${NAME} ${MOUNT_DIR}
   105      sudo ${RESIZER} /dev/loop0
   106      sudo umount ${MOUNT_DIR}
   107      rm -r ${MOUNT_DIR}
   108      exit 0;
   109  fi
   110  
   111  truncate -s ${SIZE} ${NAME}
   112  yes | mkfs.${FORMAT} ${NAME}
   113  sudo mount -o loop ${NAME} ${MOUNT_DIR}
   114  
   115  REMOVE_IMAGE=false
   116  if [[ "$(sudo docker images --platform ${PLATFORM} -q ${IMAGE} 2>/dev/null)" == "" ]]; then
   117      REMOVE_IMAGE=true
   118  fi
   119  
   120  CONTAINER=$(sudo docker create --platform ${PLATFORM} ${IMAGE})
   121  sudo docker export ${CONTAINER} | sudo tar -xC ${MOUNT_DIR}
   122  sudo docker rm ${CONTAINER}
   123  
   124  if "${REMOVE_IMAGE}" ; then
   125      sudo docker rmi -f ${IMAGE}
   126  fi
   127  
   128  sudo cp /etc/resolv.conf ${MOUNT_DIR}/etc/resolv.conf
   129  
   130  sudo chroot ${MOUNT_DIR} sh -c "
   131  dnf install -y \
   132      systemd systemd-networkd systemd-resolved systemd-udev \
   133      openssh-server passwd strace openssh-clients
   134  
   135  systemctl enable systemd-networkd
   136  
   137  cat << EOF > /etc/systemd/network/ether.network
   138  [Match]
   139  Name=*
   140  
   141  [Network]
   142  DHCP=yes
   143  EOF
   144  
   145  rm /etc/resolv.conf
   146  chmod 644 /etc/systemd/network/ether.network
   147  
   148  cat << EOF > /etc/ssh/sshd_config
   149  PasswordAuthentication yes
   150  PermitRootLogin yes
   151  PermitEmptyPasswords yes
   152  Subsystem    sftp    /usr/libexec/openssh/sftp-server
   153  EOF
   154  
   155  passwd -d root
   156  "
   157  
   158  sudo umount ${MOUNT_DIR}
   159  rm -r ${MOUNT_DIR}