Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace concatenation with writing full bootlocal.sh to /etc/rc.d/automount #58

Closed
gbraad opened this issue Aug 28, 2017 · 2 comments
Closed
Assignees
Milestone

Comments

@gbraad
Copy link
Member

gbraad commented Aug 28, 2017

At the moment we write by concatenating to the automount file.

RUN cat $ROOTFS/tmp/bootlocal.sh >> $ROOTFS/etc/rc.d/automount

it might be better to replace this with echoing a full file, as what is done in our CentOS ISO. This allows for a better insight to what happens and control over how links/mounts are done. See #57, #46, etc

I would also suggest to move the bootlocal.sh script to scripts/handle-user-data to make the two versions of images more consistent from a developer's viewpoint.

@gbraad
Copy link
Member Author

gbraad commented Aug 28, 2017

Currently, the file will look as follows:

root@minishift:~# cat /etc/rc.d/automount 
#!/bin/sh
echo "automount ...";
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=`blkid -o device -l -t 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
            # wait for the partition to actually exist, timeout after about 5 seconds
            local timer=0
            while [ "$timer" -lt 10 -a ! -b "${UNPARTITIONED_HD}2" ]; do
                timer=$((timer + 1))
                sleep 0.5
            done
            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
            # wait for the partition to actually exist, timeout after about 5 seconds
            timer=0
            while [ "$timer" -lt 10 -a ! -b "${UNPARTITIONED_HD}1" ]; do
                timer=$((timer + 1))
                sleep 0.5
            done
            BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
            mkfs.ext4 -i 8192 -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 -i 8192 -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 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
# /etc dirs are initialised from /usr/local, to allow the user/admin to customise
mkdir -p /var/lib/boot2docker/etc/

echo "automount over."
#!/bin/bash

# Copyright (C) 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Setup a persistent directory for /var/lib/openshift
BOOT2DOCKER_DATA=`blkid -o device -l -t LABEL=boot2docker-data`
PARTNAME=`echo "$BOOT2DOCKER_DATA" | sed 's/.*\///'`

mkdir -p /mnt/$PARTNAME/var/lib/minishift
ln -s /mnt/$PARTNAME/var/lib/minishift /var/lib/minishift
mkdir -p /mnt/$PARTNAME/data
ln -s /mnt/$PARTNAME/data /data

@gbraad gbraad changed the title Replace concatenation with writing full bootlocal.sh Replace concatenation with writing full bootlocal.sh to /etc/rc.d/automount Aug 28, 2017
@gbraad gbraad self-assigned this Sep 11, 2017
gbraad added a commit to gbraad-redhat/minishift-b2d-iso that referenced this issue Sep 11, 2017
gbraad added a commit to gbraad-redhat/minishift-b2d-iso that referenced this issue Sep 11, 2017
praveenkumar pushed a commit that referenced this issue Sep 12, 2017
@LalatenduMohanty LalatenduMohanty added this to the v1.2.0 milestone Sep 20, 2017
@LalatenduMohanty
Copy link
Member

Resolved by #63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants