#!/bin/sh # r2docker # ======== # # Requires ~140MB of free disk space # # Build docker image with: # $ ./sys/docker_build_alpine_image.sh # # Run the docker image: # # $ r2d() { # local rm # case "$1" in # "-r"|"--rm") rm="--rm" && shift ;; # esac # docker run --cap-drop=ALL --cap-add=SYS_PTRACE -i \ # `--name r2_$(date +%F_%H%M%S%N) $rm -tv $(pwd):/r2 \ # r2_alpine:latest $@ # } # $ r2d # Optional --rm # # Once you quit the session, get the container id with something like: # # $ containerid="$(docker ps -a | awk '/r2_alpine/ {print $NF}')" # # To get into that shell again just type: # # $ docker start -ai $containerid # # To share those images: # # $ docker export $containerid | xz >container.tar.xz # $ xz -d $tmp_docker_dir/Dockerfile <>/root/.bashrc && \ echo "alias ll=\"\\ls -Fhl\"" >>/root/.bashrc && \ echo "alias ls=\"\\ls -F\"" >>/root/.bashrc && \ echo "alias q=\"exit\"" >>/root/.bashrc \ ) \ ) && ( \ [ "$gname" = "root" ] || \ ( \ groupadd -f $gname && \ (groupmod -g $gid $gname 2>/dev/null || true) && \ useradd -g $gid -mou $uid $uname && \ echo "$uname ALL=(ALL) NOPASSWD: ALL" \ >/etc/sudoers.d/$uname && \ echo "alias la=\"\\ls -AF\"" >>/home/$uname/.bashrc && \ echo "alias ll=\"\\ls -Fhl\"" >>/home/$uname/.bashrc && \ echo "alias ls=\"\\ls -F\"" >>/home/$uname/.bashrc && \ echo "alias q=\"exit\"" >>/home/$uname/.bashrc \ ) \ ) && ( \ cd /mnt && \ git clone -b $r2branch --depth 1 $github && \ cd radare2 && \ git checkout $r2commit && \ ./sys/install.sh && \ make install \ ) && ( \ apk del --purge -r \ g++ \ gcc \ linux-headers \ make \ npm \ py2-pip && \ rm -rf /tmp/* /var/cache/apk/* /var/tmp/* \ ) # Initialize env USER $(id -nu) WORKDIR /r2 # Setup r2pm RUN set -o pipefail && \ r2pm init && \ r2pm update CMD ["/bin/bash"] EOF # Tag old images echo "[*] Tagging any old r2_alpine images" docker images | awk '{print $1":"$3}' | while read -r tag; do case "$tag" in "r2_alpine:"*) docker image tag "${tag#*:}" "$tag" ;; esac done unset tag findbase="$( docker images | grep -E "^(docker\.io\/)?alpine +latest " )" # Build image (may take a while) echo "[*] Building image..." echo "[*] This may take a long time..." # Pull newest base image and build r2_alpine image docker pull alpine:latest ( if [ ! -d "$tmp_docker_dir" ]; then echo "$tmp_docker_dir not found" fi cd $tmp_docker_dir || exit 3 # shellcheck disable=SC2154 docker build \ ${http_proxy:+--build-arg http_proxy=$http_proxy} \ ${https_proxy:+--build-arg https_proxy=$https_proxy} \ -t r2_alpine:latest . ) # Only remove base image if it didn't already exist [ -n "$findbase" ] || docker rmi alpine:latest echo "[*] done" old_base="^(docker\.io\/)?alpine +" old_r2="^r2_alpine +[^l ]" found="$( docker images | grep -E "($old_base)|($old_r2)" )" if [ -n "$found" ]; then # List old images echo echo "[*] Old images:" docker images | head -n 1 docker images | grep -E "($old_base)|($old_r2)" | \ while read -r line; do echo "$line" done unset line # Prompt to remove old images unset remove echo while :; do echo "Remove old images (y/N/q)?" read -r ans echo case "$ans" in ""|"n"|"N"|"q"|"Q") break ;; "y"|"Y") remove="true"; break ;; *) echo "Invalid choice" ;; esac done if [ -n "$remove" ]; then # Remove old images docker images | awk "/$old_r2/ {print \$1\":\"\$3}" | \ while read -r tag; do docker rmi "$tag" done unset tag docker images | awk "/$old_base/ {print \$3}" | \ while read -r id; do docker rmi "$id" done unset id fi fi unset found cleanup 0 cat <