逛github发现个有趣的项目:100行代码实现docker

    就这一个脚本

    1. #!/usr/bin/env bash
    2. set -o errexit -o nounset -o pipefail; shopt -s nullglob
    3. btrfs_path='/var/bocker' && cgroups='cpu,cpuacct,memory';
    4. [[ $# -gt 0 ]] && while [ "${1:0:2}" == '--' ]; do OPTION=${1:2}; [[ $OPTION =~ = ]] && declare "BOCKER_${OPTION/=*/}=${OPTION/*=/}" || declare "BOCKER_${OPTION}=x"; shift; done
    5. function bocker_check() {
    6. btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1
    7. }
    8. function bocker_init() { #HELP Create an image from a directory:\nBOCKER init <directory>
    9. uuid="img_$(shuf -i 42002-42254 -n 1)"
    10. if [[ -d "$1" ]]; then
    11. [[ "$(bocker_check "$uuid")" == 0 ]] && bocker_run "$@"
    12. btrfs subvolume create "$btrfs_path/$uuid" > /dev/null
    13. cp -rf --reflink=auto "$1"/* "$btrfs_path/$uuid" > /dev/null
    14. [[ ! -f "$btrfs_path/$uuid"/img.source ]] && echo "$1" > "$btrfs_path/$uuid"/img.source
    15. echo "Created: $uuid"
    16. else
    17. echo "No directory named '$1' exists"
    18. fi
    19. }
    20. function bocker_pull() { #HELP Pull an image from Docker Hub:\nBOCKER pull <name> <tag>
    21. token="$(curl -sL -o /dev/null -D- -H 'X-Docker-Token: true' "https://index.docker.io/v1/repositories/$1/images" | tr -d '\r' | awk -F ': *' '$1 == "X-Docker-Token" { print $2 }')"
    22. registry='https://registry-1.docker.io/v1'
    23. id="$(curl -sL -H "Authorization: Token $token" "$registry/repositories/$1/tags/$2" | sed 's/"//g')"
    24. [[ "${#id}" -ne 64 ]] && echo "No image named '$1:$2' exists" && exit 1
    25. ancestry="$(curl -sL -H "Authorization: Token $token" "$registry/images/$id/ancestry")"
    26. IFS=',' && ancestry=(${ancestry//[\[\] \"]/}) && IFS=' \n\t'; tmp_uuid="$(uuidgen)" && mkdir /tmp/"$tmp_uuid"
    27. for id in "${ancestry[@]}"; do
    28. curl -#L -H "Authorization: Token $token" "$registry/images/$id/layer" -o /tmp/"$tmp_uuid"/layer.tar
    29. tar xf /tmp/"$tmp_uuid"/layer.tar -C /tmp/"$tmp_uuid" && rm /tmp/"$tmp_uuid"/layer.tar
    30. done
    31. echo "$1:$2" > /tmp/"$tmp_uuid"/img.source
    32. bocker_init /tmp/"$tmp_uuid" && rm -rf /tmp/"$tmp_uuid"
    33. }
    34. function bocker_rm() { #HELP Delete an image or container:\nBOCKER rm <image_id or container_id>
    35. [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
    36. btrfs subvolume delete "$btrfs_path/$1" > /dev/null
    37. cgdelete -g "$cgroups:/$1" &> /dev/null || true
    38. echo "Removed: $1"
    39. }
    40. function bocker_images() { #HELP List images:\nBOCKER images
    41. echo -e "IMAGE_ID\t\tSOURCE"
    42. for img in "$btrfs_path"/img_*; do
    43. img=$(basename "$img")
    44. echo -e "$img\t\t$(cat "$btrfs_path/$img/img.source")"
    45. done
    46. }
    47. function bocker_ps() { #HELP List containers:\nBOCKER ps
    48. echo -e "CONTAINER_ID\t\tCOMMAND"
    49. for ps in "$btrfs_path"/ps_*; do
    50. ps=$(basename "$ps")
    51. echo -e "$ps\t\t$(cat "$btrfs_path/$ps/$ps.cmd")"
    52. done
    53. }
    54. function bocker_run() { #HELP Create a container:\nBOCKER run <image_id> <command>
    55. uuid="ps_$(shuf -i 42002-42254 -n 1)"
    56. [[ "$(bocker_check "$1")" == 1 ]] && echo "No image named '$1' exists" && exit 1
    57. [[ "$(bocker_check "$uuid")" == 0 ]] && echo "UUID conflict, retrying..." && bocker_run "$@" && return
    58. cmd="${@:2}" && ip="$(echo "${uuid: -3}" | sed 's/0//g')" && mac="${uuid: -3:1}:${uuid: -2}"
    59. ip link add dev veth0_"$uuid" type veth peer name veth1_"$uuid"
    60. ip link set dev veth0_"$uuid" up
    61. ip link set veth0_"$uuid" master bridge0
    62. ip netns add netns_"$uuid"
    63. ip link set veth1_"$uuid" netns netns_"$uuid"
    64. ip netns exec netns_"$uuid" ip link set dev lo up
    65. ip netns exec netns_"$uuid" ip link set veth1_"$uuid" address 02:42:ac:11:00"$mac"
    66. ip netns exec netns_"$uuid" ip addr add 10.0.0."$ip"/24 dev veth1_"$uuid"
    67. ip netns exec netns_"$uuid" ip link set dev veth1_"$uuid" up
    68. ip netns exec netns_"$uuid" ip route add default via 10.0.0.1
    69. btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$uuid" > /dev/null
    70. echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf
    71. echo "$cmd" > "$btrfs_path/$uuid/$uuid.cmd"
    72. cgcreate -g "$cgroups:/$uuid"
    73. : "${BOCKER_CPU_SHARE:=512}" && cgset -r cpu.shares="$BOCKER_CPU_SHARE" "$uuid"
    74. : "${BOCKER_MEM_LIMIT:=512}" && cgset -r memory.limit_in_bytes="$((BOCKER_MEM_LIMIT * 1000000))" "$uuid"
    75. cgexec -g "$cgroups:$uuid" \
    76. ip netns exec netns_"$uuid" \
    77. unshare -fmuip --mount-proc \
    78. chroot "$btrfs_path/$uuid" \
    79. /bin/sh -c "/bin/mount -t proc proc /proc && $cmd" \
    80. 2>&1 | tee "$btrfs_path/$uuid/$uuid.log" || true
    81. ip link del dev veth0_"$uuid"
    82. ip netns del netns_"$uuid"
    83. }
    84. function bocker_exec() { #HELP Execute a command in a running container:\nBOCKER exec <container_id> <command>
    85. [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
    86. cid="$(ps o ppid,pid | grep "^$(ps o pid,cmd | grep -E "^\ *[0-9]+ unshare.*$1" | awk '{print $1}')" | awk '{print $2}')"
    87. [[ ! "$cid" =~ ^\ *[0-9]+$ ]] && echo "Container '$1' exists but is not running" && exit 1
    88. nsenter -t "$cid" -m -u -i -n -p chroot "$btrfs_path/$1" "${@:2}"
    89. }
    90. function bocker_logs() { #HELP View logs from a container:\nBOCKER logs <container_id>
    91. [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
    92. cat "$btrfs_path/$1/$1.log"
    93. }
    94. function bocker_commit() { #HELP Commit a container to an image:\nBOCKER commit <container_id> <image_id>
    95. [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
    96. [[ "$(bocker_check "$2")" == 1 ]] && echo "No image named '$2' exists" && exit 1
    97. bocker_rm "$2" && btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$2" > /dev/null
    98. echo "Created: $2"
    99. }
    100. function bocker_help() { #HELP Display this message:\nBOCKER help
    101. sed -n "s/^.*#HELP\\s//p;" < "$1" | sed "s/\\\\n/\n\t/g;s/$/\n/;s!BOCKER!${1/!/\\!}!g"
    102. }
    103. [[ -z "${1-}" ]] && bocker_help "$0"
    104. case $1 in
    105. pull|init|rm|images|ps|run|exec|logs|commit) bocker_"$1" "${@:2}" ;;
    106. *) bocker_help "$0" ;;
    107. esac

    项目是7年前的,很多东西都过时了,还依赖btrfs。不过我们可以从这里面简单一窥docker到底在干嘛。