replace automount script with a more rebust one, will later replace with alpine setup-disk. fix #378

Signed-off-by: Justin Cormack <justin.cormack@unikernel.com>
This commit is contained in:
Justin Cormack 2015-12-14 14:55:53 +00:00
parent 30722afd50
commit deb0043f9d
2 changed files with 56 additions and 126 deletions

View File

@ -4,7 +4,7 @@ MAINTAINER Justin Cormack <justin.cormack@unikernel.com>
COPY repositories /etc/apk/
RUN apk update && apk upgrade && apk add e2fsprogs docker chrony blkid
RUN apk update && apk upgrade && apk add e2fsprogs docker chrony hdparm
COPY etc /etc/

View File

@ -1,134 +1,64 @@
#!/sbin/openrc-run
# script taken from boot2docker TODO customise for Moby
depend()
{
after dev
before docker
}
start()
{
ebegin "Mounting host block device"
ebegin "Mounting external drives"
# XXX this may change on other ports
DEV="sda"
MAJOR="8"
DRIVE="/dev/${DEV}"
DATA="${DRIVE}1"
SWAP="${DRIVE}2"
MOUNTPOINT="/mnt/${DEV}"
PARTPROBE="hdparm -z ${DRIVE}"
LABEL=boot2docker-data
MAGIC="boot2docker, please format-me"
# If there is a partition with `boot2docker-data` as its label, use it and be
# very happy. Thus, you can come along if you feel like a room without a roof.
BOOT2DOCKER_DATA=`findfs LABEL=$LABEL`
echo $BOOT2DOCKER_DATA
if [ ! -n "$BOOT2DOCKER_DATA" ]; then
echo "Is the disk unpartitioned?, test for the 'boot2docker format-me' string"
# Is the disk unpartitioned?, test for the 'boot2docker format-me' string
UNPARTITIONED_HD=`fdisk -l | grep "doesn't contain a valid partition table" | head -n 1 | sed 's/Disk \(.*\) doesn.*/\1/'`
if [ -n "$UNPARTITIONED_HD" ]; then
# Test for our magic string (it means that the disk was made by ./boot2docker init)
HEADER=`dd if=$UNPARTITIONED_HD bs=1 count=${#MAGIC} 2>/dev/null`
if [ "$HEADER" = "$MAGIC" ]; then
# save the preload userdata.tar file
dd if=$UNPARTITIONED_HD of=/userdata.tar bs=1 count=4096 2>/dev/null
# Create the partition, format it and then mount it
echo "NEW boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use"
echo "NEW boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use" > /home/docker/log.log
# Add a swap partition (so Docker doesn't complain about it missing)
(echo n; echo p; echo 2; echo ; echo +1000M ; echo w) | fdisk $UNPARTITIONED_HD
# Let kernel re-read partition table
partprobe
(echo t; echo 82; echo w) | fdisk $UNPARTITIONED_HD
# Let kernel re-read partition table
partprobe
mkswap "${UNPARTITIONED_HD}2"
# Add the data partition
(echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk $UNPARTITIONED_HD
# Let kernel re-read partition table
partprobe
BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
mkfs.ext4 -L $LABEL $BOOT2DOCKER_DATA
swapon "${UNPARTITIONED_HD}2"
fi
DISK_VENDOR=$(cat /sys/class/block/$(basename $UNPARTITIONED_HD /dev/)/device/vendor /sys/class/block/$(basename $UNPARTITIONED_HD /dev/)/device/model | tr -d "\n")
# Test if disk is "VMware, VMware Virtual S" and empty.
if [ "$DISK_VENDOR" = "VMware, VMware Virtual S" ] || [ "$DISK_VENDOR" = "VMware Virtual disk " ]; then
# Check whether the disk has any known partitions on it
blkid -o device $UNPARTITIONED_HD
if [ $? == 2 ]; then
# As there are no partitions, let's make sure the disk is empty for real
dd if=$UNPARTITIONED_HD of=device_test_file bs=1k count=256 > /dev/null 2>&1
NON_NUL=$(<device_test_file tr -d '\0\n' | wc -c)
if [ $NON_NUL == 0 ]; then
# Create the partition, format it and then mount it
echo "NEW VMware boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use"
echo "NEW VMware boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use" > /home/docker/log.log
# Add a swap partition (so Docker doesn't complain about it missing)
(echo n; echo p; echo 2; echo ; echo +1000M ; echo w) | fdisk $UNPARTITIONED_HD
(echo t; echo 82) | fdisk $UNPARTITIONED_HD
mkswap "${UNPARTITIONED_HD}2"
# Add the data partition
(echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk $UNPARTITIONED_HD
BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
mkfs.ext4 -L $LABEL $BOOT2DOCKER_DATA
swapon "${UNPARTITIONED_HD}2"
else
echo "Disk unpartitioned but something is there... not doing anything"
fi
else
echo "Partition table found on disk, not doing anything"
fi
fi
else
# Pick the first ext4 as a fallback
# TODO: mount all Linux partitions and look for a /var/lib/docker...
BOOT2DOCKER_DATA=`blkid | grep -e 'TYPE="btrfs"' -e 'TYPE="ext4"' | head -n 1 | sed 's/:.*//'`
fi
fi
echo $BOOT2DOCKER_DATA
if [ -n "$BOOT2DOCKER_DATA" ]; then
PARTNAME=`echo "$BOOT2DOCKER_DATA" | sed 's/.*\///'`
echo "mount p:$PARTNAME ..."
mkdir -p /mnt/$PARTNAME
if ! mount $BOOT2DOCKER_DATA /mnt/$PARTNAME 2>/dev/null; then
# for some reason, mount doesn't like to modprobe btrfs
BOOT2DOCKER_FSTYPE=`blkid -o export $BOOT2DOCKER_DATA | grep TYPE= | cut -d= -f2`
modprobe $BOOT2DOCKER_FSTYPE || true
umount -f /mnt/$PARTNAME || true
mount $BOOT2DOCKER_DATA /mnt/$PARTNAME
fi
# Just in case, the links will fail if not
umount -f /var/lib/docker || true
rm -rf /var/lib/docker /var/lib/boot2docker
if [ -d /mnt/$PARTNAME/vm ]; then
# The old behavior - use the entire disk for boot2docker data
ln -s /mnt/$PARTNAME /var/lib/docker
# Give us a link to the new cusomisation location
ln -s /var/lib/docker/vm /var/lib/boot2docker
else
# Detected a disk with a normal linux install (/var/lib/docker + more))
mkdir -p /var/lib
mkdir -p /mnt/$PARTNAME/var/lib/docker
ln -s /mnt/$PARTNAME/var/lib/docker /var/lib/docker
mkdir -p /mnt/$PARTNAME/var/lib/boot2docker
ln -s /mnt/$PARTNAME/var/lib/boot2docker /var/lib/boot2docker
fi
# Make sure /tmp is on the disk too
rm -rf /mnt/$PARTNAME/tmp || true
mv /tmp /mnt/$PARTNAME/tmp
ln -fs /mnt/$PARTNAME/tmp /tmp
if [ -e "/userdata.tar" ]; then
mv /userdata.tar /var/lib/boot2docker/
fi
ls -l /mnt/$PARTNAME
fi
mkdir -p ${MOUNTPOINT}
umount ${DATA} || true
umount -f /var/lib/docker || true
rm -rf /var/lib/docker
if fdisk -l ${DRIVE} | grep -q ${DATA}
then
# drive partitioned, try mounting
umount -f ${MOUNTPOINT} || true
mount ${DATA} ${MOUNTPOINT}
if [ $? -eq 0 ]
then
mkdir -p ${MOUNTPOINT}/var/lib/docker
ln -s ${MOUNTPOINT}/var/lib/docker /var/lib/docker
rm -f /tmp
mkdir -p ${MOUNTPOINT}/tmp
ln -s ${MOUNTPOINT}/tmp /tmp
swapon ${SWAP} || mkswap ${SWAP} && swapon ${SWAP}
exit 0
fi
fi
# ok that did not work out, let us (re)format
swapoff ${SWAP} || true
dd if=/dev/zero of=${DRIVE} bs=512 count=1
${PARTPROBE}
(echo n; echo p; echo 2; echo ; echo +1000M ; echo w) | fdisk ${DRIVE}
(echo t; echo 82; echo w) | fdisk ${DRIVE}
(echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk ${DRIVE}
${PARTPROBE}
# XXX have to sleep as the device nodes disappear otherwise
sleep 1
mknod -m 600 ${DATA} b ${MAJOR} 1 || true
mknod -m 600 ${SWAP} b ${MAJOR} 2 || true
mkfs.ext4 -q -F ${DATA}
mount ${DATA} ${MOUNTPOINT}
mkdir -p ${MOUNTPOINT}/var/lib/docker
ln -s ${MOUNTPOINT}/var/lib/docker /var/lib/docker
rm -f /tmp
mkdir -p ${MOUNTPOINT}/tmp
ln -s ${MOUNTPOINT}/tmp /tmp
mkswap ${SWAP}
swapon ${SWAP}
}