Make diskimage-builder work in Docker.

When running inside a Docker container, we cannot rely on devices in
/dev/mapper to be automagically created by udev, because we probably
don't have a udev at all.  To work around this, run dmsetup mknodes
after every kpartx run.

Change-Id: If7e30579224ce54c5ed26d08974d8293c144719a
This commit is contained in:
Victor Lowther 2014-04-24 19:46:11 -05:00
parent 60fdfb26dc
commit 38b14df72c
4 changed files with 17 additions and 3 deletions

View File

@ -3,7 +3,6 @@
# Intended to be called from the root.d cloud-image script as follows:
# $TMP_HOOKS_PATH/bin/extract-image $BASE_IMAGE_FILE $BASE_IMAGE_TAR $IMAGE_LOCATION $CACHED_IMAGE
set -eu
set -o pipefail
@ -64,6 +63,10 @@ function extract_image() {
# the device used for the image.
ROOT_LOOPDEV=$(sudo kpartx -av $WORKING/$RAW_FILE | \
awk "/loop[0-9]+$ROOT_PARTITON/ {print \$3}")
# If running inside Docker, make our nodes manually, because udev will not be working.
if [ -f /.dockerenv ]; then
sudo dmsetup --noudevsync mknodes
fi
if ! timeout 5 sh -c "while ! [ -e /dev/mapper/$ROOT_LOOPDEV ]; do sleep 1; done"; then
echo "Error: Could not find /dev/mapper/$ROOT_LOOPDEV"
exit 1

View File

@ -35,6 +35,10 @@ else
# NOTE: On RHEL, partprobe of /dev/loop0 does not create /dev/loop0p2,
# while kpartx at least creates /dev/mapper/loop0p2.
LOOPDEV=$(sudo kpartx -av $WORKING/$RAW_FILE | awk "/loop[0-9]+$MAGIC_BIT/ {print \$3}")
# If running inside Docker, make our nodes manually, because udev will not be working.
if [ -f /.dockerenv ]; then
sudo dmsetup --noudevsync mknodes
fi
export LOOPDEV=$LOOPDEV
echo "Loop device is set to: $LOOPDEV"
if ! timeout 5 sh -c "while ! [ -e /dev/mapper/$LOOPDEV ]; do sleep 1; done"; then

View File

@ -22,8 +22,15 @@ sudo udevadm settle
# If the partition isn't under /dev/loop*p1, create it with kpartx
DM=
if [ ! -e "${IMAGE_BLOCK_DEVICE}p1" ]; then
sudo kpartx -asv $TMP_IMAGE_PATH
DM=${IMAGE_BLOCK_DEVICE/#\/dev/\/dev\/mapper}
# If running inside Docker, make our nodes manually, because udev will not be working.
if [ -f /.dockerenv ]; then
# kpartx cannot run in sync mode in docker.
sudo kpartx -av $TMP_IMAGE_PATH
sudo dmsetup --noudevsync mknodes
else
sudo kpartx -asv $TMP_IMAGE_PATH
fi
fi
if [ -n "$DM" ]; then

View File

@ -211,7 +211,7 @@ function detach_loopback() {
loopdev_name=$(echo $loopdev | sed 's/\/dev\///g')
mapper_name=$(sudo dmsetup ls | grep $loopdev_name | awk '{ print $1 }')
if [ "$mapper_name" ]; then
sudo dmsetup remove $mapper_name
sudo dmsetup --noudevsync remove $mapper_name
fi
# loopback dev may be tied up a bit by udev events triggered by partition events