Creating images

In this post I will be posting the current script I use to make images for panda boards and smarttops. It is just a quick bash script I wrote to automate the process.

Process:
1. Partition sdcard
2. Mount partitions
3. Copy rootfs and boot files
4. Copy custom files
5. Unmount

The reason we do this instead of a dd or xzcat, is so we can copy over a boot script and hosts files. This will allow us to set an ip address on boot and custom services turned on or off. Then we can just plug it in and use a configuration management system(ansible or bcfg2) to configure the rest.

#!/bin/bash
# Andrew Oatley-Willis
# This script is used to modify a panda image before imaging.
# This is done so that the panda will come online with an ip address
# and immediately allow ssh access to it.

armboard="smarttop"

# Place to store temporary data
tmpdir="/tmp/${armboard}-$RANDOM"

# Script location
scriptdir="/data/f17v6/f18-${armboard}"

panda="panda"
device="device"
partition="False"
copyroot="False"
copyfiles="False"

function helpme {
cat <<EOF

Usage:  ./make-${armboard}.sh [option]

Partition device, copy rootfs & boot, and copy custom files
Example: ./make-${armboard}.sh --install

Don't partition device, copy rootfs & boot, and copy custom files
Example: ./make-${armboard}.sh --nopartition

Copy custom files - currently removed
Example: ./make-${armboard}.sh --copy

Partition device
Example: ./make-${armboard}.sh --partition

EOF
}

function cleanup {
        echo ""
        echo "[Cleaning up]"
        sync
        umount $tmpdir/p1/
        umount $tmpdir/p2/
        sleep 1
        rmdir $tmpdir/p1
        rmdir $tmpdir/p2
        rmdir $tmpdir/
        read -p "[ Press Enter to Continue ]" > /dev/null
        exit
}

function copy_root {
        echo "[Extracting boot partition]"
        rsync -avHAXE $scriptdir/boot/* $tmpdir/p1/
echo "[Extracting rootfs partition]"
        rsync -avHAXE $scriptdir/rootfs/* $tmpdir/p2/
}

function copy_files {
cat > $tmpdir/p2/etc/rc.d/rc.local <<EOF
#!/bin/bash
systemctl stop firewalld.service
systemctl stop NetworkManager.service
systemctl start sshd.service
ifconfig eth0 $panda netmask 255.255.255.0 up
route add default gw 192.168.1.254
iptables -F
chmod 700 /root/.ssh/
chmod 600 /root/.ssh/authorized_keys
ln -sf /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

EOF
}

function unmount_all {
        for i in 1 2 3 4 5 6 7; do
                umount ${device}${i}
        done
}

function partition_dev {
        echo "[Partitioning $device]"
        unset LANG
        if [ -b "$device" ] ; then
                dd if=/dev/zero of=$device bs=1024 count=1024
                SIZE=`fdisk -l $device | grep Disk | awk '{print $5}'`
                echo DISK SIZE - $SIZE bytes
                CYLINDERS=`echo $SIZE/255/63/512 | bc`
                echo CYLINDERS - $CYLINDERS
                {
                echo ,9,0x0C,*
                echo ,,,-
                } | sfdisk -D -H 255 -S 63 -C $CYLINDERS $device
                mkfs.vfat -F 32 -n "boot" ${device}1
                mke2fs -j -L "rootfs" ${device}2
        fi
}

function check_errors {
        checkfiles="$(ls -n ${scriptdir}/rootfs/ ${scriptdir}/boot/ | awk 'NR>1{print $3}' | grep -v '^$')"
        for i in $checkfiles;do
                if [ "$i" != "0" ];then
                        echo "rootfs/boot files must be owned by root"
                        exit
                fi
        done
        if [ "$USER" != "root" ]; then
                echo "Must run as root"
                exit
        fi

}

trap cleanup SIGINT

check_errors

if [ "$1" == "--install" ];then
        partition="True"
        copyroot="True"
        copyfiles="True"
elif [ "$1" == "--nopartition" ];then
        partition="False"
        copyroot="True"
        copyfiles="True"
elif [ "$1" == "--copy" ];then
        partition="False"
        copyroot="False"
        copyfiles="True"
elif [ "$1" == "--partition" ];then
        partition="True"
        copyroot="False"
        copyfiles="False"
else
        helpme
        exit
fi

read -p "Enter hostname of ${armboard}(ex. pa-5-1): " panda
echo "WARNING: ALL DATA ON DEVICE WILL BE ERASED"
read -p "Enter device to image(ex. /dev/sde): " device
if [ "$device" == "/dev/sda" -o "$device" == "/dev/sdb" ];then
        echo "You cannot use $device as it may contain system information"
        exit
fi

unmount_all

if [ "$partition" == "True" ]; then
        partition_dev
fi

echo "[Making temp directories]"
mkdir $tmpdir
mkdir $tmpdir/p1/
mkdir $tmpdir/p2/
echo "[Mounting $device device]"
mount ${device}1 $tmpdir/p1/
mount ${device}2 $tmpdir/p2/
sleep 1

if [ "$copyroot" == "True" ]; then
        copy_root
fi

if [ "$copyfiles" == "True" ];then
        copy_files
fi

echo "[Completed successfully]"
cleanup
Advertisements

About oatleywillisa

Computer Networking Student
This entry was posted in SBR600 and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s