~dpigott/lava-master-image-scripts/fix-stty-bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
# Copyright (C) 2012 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This script is invoked each boot of a LAVA master image.
# The first boot is special and involves additional actions
set -e

# Load common definitions
. /lib/lava/common

# First boot log file
LAVA_FIRST_BOOT_LOG=/var/log/lava/first-boot.log

# Set of packages to install on first boot
LAVA_PKGS="wget dosfstools u-boot-tools bzip2 python ntpdate parted"

DEVICE_TYPE=`lava-device-info --device-type`

# First boot startup function
# 1) Initializes APT
# 2) Installs the required packages
# 3) Synchronizes time with time server
# 4) Generates first-boot UUID and stores it in device.conf
# 5) Creates a copy of device.conf in /run
first_boot_startup() {
    echo "***************************************************************"
    echo "*         [Linaro Automated Validation Architecture]          *"
    echo "*                                                             *"
    echo "* LAVA now is running first-boot setup process...             *"
    echo "*                                                             *"
    echo "* The serial console will activate once this task is finished *"
    echo "* The full log file of this operation is stored in            *"
    echo "* /var/log/lava/first-boot.log                                *"
    echo "***************************************************************"
    # Announce first-boot start
    echo "First boot script started: $(date --utc --rfc-3339=seconds)" >> "$LAVA_FIRST_BOOT_LOG"
    # Update APT cache
    echo "Updating APT cache:" >> "$LAVA_FIRST_BOOT_LOG"
    # Work around the initramfs problem
    export FLASH_KERNEL_SKIP=1
    if ! apt-get update >> "$LAVA_FIRST_BOOT_LOG" 2>&1; then
        echo "Unable to update apt cache" >&2
        return 1
    fi
    # Install the packages we need on the master image
    echo "Installing packages: $LAVA_PKGS" >> "$LAVA_FIRST_BOOT_LOG"
    if ! apt-get install --yes --force-yes $LAVA_PKGS >> "$LAVA_FIRST_BOOT_LOG" 2>&1; then
        echo "Unable to install packages required by LAVA" >&2
        return 1
    fi
    # Update time so that our UUIDs are not crappy
    echo "Synchronizing time with $LAVA_NTP_SERVER" >> "$LAVA_FIRST_BOOT_LOG"
    if ! ntpdate $LAVA_NTP_SERVER >> "$LAVA_FIRST_BOOT_LOG" 2>&1; then
        echo "Unable to synchronize time" >&2
        return 1
    fi
    echo "stty columns 10000" >> /etc/profile
    # Do disk partitioning
    echo "Partitioning disk to LAVA format" >> "$LAVA_FIRST_BOOT_LOG"
    if ! lava-partition-disk -r $LAVA_ROOTFS_SIZE -u $LAVA_USERDATA_SIZE>> "$LAVA_FIRST_BOOT_LOG" 2>&1; then
        echo "Unable to partition disk" >&2
        return 1
    fi
    if [ "$DEVICE_TYPE" = "lava:panda" ]; then
        echo "Applying sysctl fix for streaming errors on Panda" >> "$LAVA_FIRST_BOOT_LOG"
        echo vm.min_free_kbytes = 32768 >> /etc/sysctl.conf
        sysctl -p
    fi
    # Generate LAVA first-boot UUID
    echo "Generating first-boot UUID" >> "$LAVA_FIRST_BOOT_LOG"
    LAVA_FIRST_BOOT_UUID="$(python -c 'import uuid; print uuid.uuid1()')"
    # Store it back in device conf
    echo "LAVA_FIRST_BOOT_UUID=$LAVA_FIRST_BOOT_UUID" >> $LAVA_DEVICE_CONF
    # Copy device.conf to /run
    mkdir -p "$(dirname $LAVA_RUN_DEVICE_CONF)"
    # Copy device.conf to /run/lava
    cp "$LAVA_DEVICE_CONF" "$LAVA_RUN_DEVICE_CONF"
    # Set boot UUID to first-boot UUID 
    LAVA_BOOT_UUID="$LAVA_FIRST_BOOT_UUID"
    # Store it back in /run/lava/device.conf
    echo "LAVA_BOOT_UUID=$LAVA_BOOT_UUID" >> "$LAVA_RUN_DEVICE_CONF"
    # Announce first-boot end 
    echo "First boot script finished: $(date --utc --rfc-3339=seconds)" >> "$LAVA_FIRST_BOOT_LOG"
}


# Normal boot startup function
# 1) Synchronizes time with time server 
# 4) Creates a copy of device.conf in /run
# 3) Generates boot UUID and stores it in /run copy of device.conf
normal_boot_startup() {
    # Update time so that our UUIDs are not crappy
    if ! ntpdate $LAVA_NTP_SERVER >> "$LAVA_FIRST_BOOT_LOG" 2>&1; then
        echo "Unable to synchronize time" >&2
        return 1
    fi
    # Create /run/lava if needed
    mkdir -p "$(dirname $LAVA_RUN_DEVICE_CONF)"
    # Copy device.conf to /run/lava
    cp "$LAVA_DEVICE_CONF" "$LAVA_RUN_DEVICE_CONF"
    # Generate boot UUID
    LAVA_BOOT_UUID="$(python -c 'import uuid; print uuid.uuid1()')"
    # Store it back in /run/lava/device.conf
    echo "LAVA_BOOT_UUID=$LAVA_BOOT_UUID" >> "$LAVA_RUN_DEVICE_CONF"
}


case "$1" in
    --version)
        echo "lava-master-init $LAVA_VERSION"
        exit 0
        ;;
    --help)
        echo "Usage: lava-master-init [OPTION]"
        echo
        echo "available options:"
        echo "  --run-from-upstart"
        echo "                Run first-boot or normal-boot actions."
        echo "                Normally this is started by upstart."
        echo
        echo "miscellaneous options"
        echo "  --version     Show version string"
        echo "  --help        Show help screen"
        echo
        echo "See $LAVA_URL for details"
        exit 0
        ;;
    --run-from-upstart)
        load_device_conf
        if [ -z "$LAVA_FIRST_BOOT_UUID" ]; then
            first_boot_startup
        else
            normal_boot_startup
        fi
        ;;
    *)
        echo "Error: no such option: $1" 2>&1
        $0 --help
        exit 1
        ;;
esac