diff options
-rw-r--r-- | .SRCINFO | 20 | ||||
-rw-r--r-- | PKGBUILD | 21 | ||||
-rw-r--r-- | etc_docker-btrfs.sh | 2 | ||||
-rwxr-xr-x | hooks_docker-btrfs | 61 | ||||
-rwxr-xr-x | install_docker-btrfs | 38 |
5 files changed, 142 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 000000000000..cdc75a36056b --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,20 @@ +pkgbase = mkinitcpio-docker-hooks + pkgdesc = mkinitcpio hooks that provides support for using docker image as root + pkgver = 1.0 + pkgrel = 1 + url = https://github.com/zasdfgbnm/mkinitcpio-docker-hooks + arch = any + groups = base + license = GPL + depends = mkinitcpio + depends = jq + backup = etc/docker-btrfs.sh + source = install_docker-btrfs + source = hooks_docker-btrfs + source = etc_docker-btrfs.sh + md5sums = 0da33ef9ff132737a09173fdd45cddd6 + md5sums = e1d51dd467412b48361c648226e6b57e + md5sums = a1243d4a874f69ef696f5c6ac58424cb + +pkgname = mkinitcpio-docker-hooks + diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 000000000000..c400a964914a --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,21 @@ +# Maintainer: Xiang Gao <qasdfgtyuiop at gmail dot com> +pkgname=mkinitcpio-docker-hooks +pkgver=1.0 +pkgrel=1 +pkgdesc="mkinitcpio hooks that provides support for using docker image as root" +arch=(any) +url="https://github.com/zasdfgbnm/mkinitcpio-docker-hooks" +license=('GPL') +groups=('base') +depends=('mkinitcpio' 'jq') +backup=(etc/docker-btrfs.sh) +source=('install_docker-btrfs' 'hooks_docker-btrfs' 'etc_docker-btrfs.sh') +md5sums=('0da33ef9ff132737a09173fdd45cddd6' + 'e1d51dd467412b48361c648226e6b57e' + 'a1243d4a874f69ef696f5c6ac58424cb') + +package() { + install -Dm644 install_docker-btrfs "${pkgdir}/usr/lib/initcpio/install/docker-btrfs" + install -Dm644 hooks_docker-btrfs "${pkgdir}/usr/lib/initcpio/hooks/docker-btrfs" + install -Dm644 etc_docker-btrfs.sh "${pkgdir}/etc/docker-btrfs.sh" +} diff --git a/etc_docker-btrfs.sh b/etc_docker-btrfs.sh new file mode 100644 index 000000000000..1843e6c864aa --- /dev/null +++ b/etc_docker-btrfs.sh @@ -0,0 +1,2 @@ +docker_image=archlinux/base +docker_tag=latest diff --git a/hooks_docker-btrfs b/hooks_docker-btrfs new file mode 100755 index 000000000000..31efedf9ebc9 --- /dev/null +++ b/hooks_docker-btrfs @@ -0,0 +1,61 @@ +#!/usr/bin/bash + +run_hook() { + . /etc/docker-btrfs.sh + docker_rwlayer=$(echo $rootflags | grep -o "subvol=[^,]*" | grep -o "[^=]*\$") + + mkdir -p /docker_root + mkdir -p /var/lib/docker + mkdir -p /top_layer + + msg ":: mounting "$root" at /docker_root" + rootdev=$(resolve_device "$root") && root=$rootdev + unset rootdev + if ! mount -t btrfs "$root" "/docker_root"; then + echo "You are now being dropped into an emergency shell." + launch_interactive_shell + msg "Trying to continue (this will most likely fail) ..." + fi + + msg ":: bind mounting "/docker_root/$docker_path" at /var/lib/docker" + if ! mount -o bind "/docker_root/$docker_path" "/var/lib/docker"; then + echo "You are now being dropped into an emergency shell." + launch_interactive_shell + msg "Trying to continue (this will most likely fail) ..." + fi + + msg ":: getting docker cache subvolume" + cd /var/lib/docker/image/btrfs + image_id=$(jq ".Repositories.\"$docker_image\".\"$docker_image:$docker_tag\"" repositories.json | sed 's/:/\//g;s/"//g') + last_layer_id=$(jq '.rootfs.diff_ids[]' imagedb/content/$image_id | sed 's/"//g' | tail -n 1) + cache_id_file=$(grep $last_layer_id -rl layerdb | sed 's/diff/cache-id/g') + cache_id=$(cat $cache_id_file) + + msg ":: creating rwlayer" + [ -d /docker_root/"$docker_rwlayer" ] && btrfs subvolume delete /docker_root/"$docker_rwlayer" + if ! btrfs subvolume snapshot /var/lib/docker/btrfs/subvolumes/$cache_id /docker_root/"$docker_rwlayer"; then + echo "You are now being dropped into an emergency shell." + launch_interactive_shell + msg "Trying to continue (this will most likely fail) ..." + fi + + msg ":: mounting top layer drive" + toplayerdev=$(resolve_device "$toplayer") && root=$toplayerdev + unset toplayerdev + if ! mount ${toplayerfstype:+-t $toplayerfstype} ${toplayerflags:+-o $toplayerflags} "$toplayer" "/top_layer"; then + echo "You are now being dropped into an emergency shell." + launch_interactive_shell + msg "Trying to continue (this will most likely fail) ..." + fi + + msg ":: applying top layer" + cp -r /top_layer/"$toplayer_path"/* /docker_root/"$docker_rwlayer"/ + + # cleaning + cd / + umount /top_layer + umount /var/lib/docker + umount /docker_root +} + +# vim: set ft=sh ts=4 sw=4 et:
\ No newline at end of file diff --git a/install_docker-btrfs b/install_docker-btrfs new file mode 100755 index 000000000000..272329d55451 --- /dev/null +++ b/install_docker-btrfs @@ -0,0 +1,38 @@ +#!/bin/bash + +build() { + add_module btrfs + add_binary btrfs + add_binary btrfsck + add_binary jq + add_file /etc/docker-btrfs.sh + add_runscript +} + +help() { + cat <<HELPEOF +This hook provides support for using docker image as root. Only btrfs +is supported. This hook creates an rwlayer as a snapshot of the subvolume +in docker cache of the specified image and use this rwlayer as root. +The rwlayer is not automatically saved and will be emptied the next time +during boot. + +To setup this hook: + +In /etc/docker-btrfs.sh, the user specify the image and tag that would +be used as root. During installation, this file will be copied to the +initramfs generated and read by the runtime hook during booting. + +While booting, the kernel option "root" should be the drive that contains +the docker directory (the directory that will become /var/lib/docker after +boot). The "docker_path" is used to specify the relative path of docker +directory with respect to "root". The "rootflags" should at least have a +"subvol=******"; this subvolume will become the rwlayer, and all existing +data will be emptied. The "toplayer" is the drive that stores top layer +data which will be applied to rwlayer after rwlayer is mounted. The +"toplayerfstype", "toplayerflags" playes the same role to top layer as +"rootfstype", "rootflags" plays to root. The "toplayer_path" is the relative +path of the directory of the top layer with respect to "toplayer". + +HELPEOF +}
\ No newline at end of file |