github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/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  
    39  # Display help function
    40  display_help() {
    41      echo "Usage: $0 [option...] " >&2
    42      echo
    43      echo "   -f, --format               rootfs format (ext4 or xfs), default ext4"
    44      echo "   -h, --help                 Display help message"
    45      echo "   -n, --name                 rootfs name, default rootfs.ext4"
    46      echo "   -s, --size                 rootfs size, default 1G"
    47      echo
    48  }
    49  
    50  while true; do
    51      if [ $# -eq 0 ]; then
    52          break
    53      fi
    54      case "$1" in
    55          -h | --help)
    56              display_help
    57              exit 0
    58              ;;
    59          -f | --format)
    60              FORMAT=$2
    61              shift 2
    62  
    63              case "${FORMAT}" in
    64                  ext4)
    65                      RESIZER="resize2fs"
    66                      ;;
    67                  xfs)
    68                      RESIZER="xfs_growfs"
    69                      ;;
    70                  -*)
    71                      echo "Error Unknown format: ${FORMAT}" >&2
    72                      exit 1
    73                      ;;
    74              esac
    75              ;;
    76          -n | --name)
    77              NAME=$2
    78              shift 2
    79              ;;
    80          -s | --size)
    81              SIZE=$2
    82              shift 2
    83              ;;
    84          -*)
    85              echo "Error: Unknown option: $1" >&2
    86              exit 1
    87              ;;
    88          *)
    89              break
    90              ;;
    91      esac
    92  done
    93  
    94  MOUNT_DIR=$(mktemp -d)
    95  
    96  if [ -f "${NAME}" ]; then
    97      truncate -s ${SIZE} ${NAME}
    98      sudo mount -o loop ${NAME} ${MOUNT_DIR}
    99      sudo ${RESIZER} /dev/loop0
   100      sudo umount ${MOUNT_DIR}
   101      rm -r ${MOUNT_DIR}
   102      exit 0;
   103  fi
   104  
   105  truncate -s ${SIZE} ${NAME}
   106  mkfs.${FORMAT} ${NAME}
   107  sudo mount -o loop ${NAME} ${MOUNT_DIR}
   108  
   109  REMOVE_IMAGE=false
   110  if [[ "$(sudo docker images -q ${IMAGE} 2>/dev/null)" == "" ]]; then
   111      REMOVE_IMAGE=true
   112  fi
   113  
   114  CONTAINER=$(sudo docker create ${IMAGE})
   115  sudo docker export ${CONTAINER} | sudo tar -xC ${MOUNT_DIR}
   116  sudo docker rm ${CONTAINER}
   117  
   118  if "${REMOVE_IMAGE}" ; then
   119      sudo docker rmi ${IMAGE}
   120  fi
   121  
   122  sudo cp /etc/resolv.conf ${MOUNT_DIR}/etc/resolv.conf
   123  
   124  sudo chroot ${MOUNT_DIR} sh -c "
   125  dnf install -y \
   126      systemd systemd-networkd systemd-resolved systemd-udev \
   127      openssh-server passwd strace
   128  
   129  systemctl enable systemd-networkd
   130  
   131  cat << EOF > /etc/systemd/network/ether.network
   132  [Match]
   133  Driver=e1000
   134  
   135  [Network]
   136  DHCP=yes
   137  EOF
   138  
   139  rm /etc/resolv.conf
   140  
   141  sed -i -e 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' \
   142       -e 's/#PermitEmptyPasswords no/PermitEmptyPasswords yes/' \
   143       /etc/ssh/sshd_config
   144  
   145  passwd -d root
   146  "
   147  
   148  sudo umount ${MOUNT_DIR}
   149  rm -r ${MOUNT_DIR}