07bbec6293b3a8a60e4755608fcd219361f5af36
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / sbin / cirros-apply
1 #!/bin/sh
2
3 . ${CIRROS_LIB:=/lib/cirros/shlib_cirros} ||
4         { echo "failed to read ${CIRROS_LIB}" 1>&2; exit 1; }
5
6 SSH_ROOT_PREFIX="command=\"echo Please login as \\'cirros\\' user, not as root; echo; sleep 10\""
7
8 Usage() {
9         cat <<EOF
10 Usage: ${0##*/} [ options ] mode
11
12    Call appropriate datasource for its 'apply-<mode>' stage, 
13    and then apply generic 'apply-<mode>' hooks.
14
15    If there is no data source, or datasource configured is
16    not of type 'mode', then it will exit silently with success.
17
18    mode is one of 'net' or 'local'
19
20    options:
21    -v | --verbose  : be more verbose
22 EOF
23 }
24
25 cirros_apply() {
26         local short_opts="hv"
27         local long_opts="help,verbose"
28         local getopt_out=""
29         getopt_out=$(getopt --name "${0##*/}" \
30                 --options "${short_opts}" --long "${long_opts}" -- "$@") &&
31                 eval set -- "${getopt_out}" ||
32                 { bad_Usage; return; }
33
34         local cur="" next="" mode="" VERBOSITY
35
36         while [ $# -ne 0 ]; do
37                 cur=${1}; next=${2};
38                 case "$cur" in
39                         -h|--help) Usage ; exit 0;;
40                         -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
41                         --) shift; break;;
42                 esac
43                 shift;
44         done
45
46         [ $# -ne 0 ] || { bad_Usage "must provide mode"; return; }
47         mode="$1"
48         shift
49
50         [ "$mode" = "net" -o "$mode" = "local" ] ||
51                 { bad_Usage "mode must be 'net' or 'local'"; return; }
52
53         local dsname dsmode root
54
55         assert_datasource ||
56                 { debug 1 "no datasource present"; return 0; }
57
58         dsname="${_DATASOURCE_NAME}"
59         dsmode="${_DATASOURCE_MODE}"
60
61         [ "${dsmode}" = "$mode" ] || {
62                 debug 1 "datasource found is mode '${dsmode}', skipping";
63                 return 0;
64         }
65
66         PATH="${DS_D}:$PATH"
67         local root="${TARGET_ROOT}"
68         if [ -n "$root" ]; then
69                 local sd="$PWD"
70                 cd "$root" && root="$PWD" && cd "$sd" ||
71                         return 1;
72         else
73                 root="/"
74         fi
75         TARGET_ROOT=$root "$dsname" "apply-${dsmode}" "$RESULTS_D" ||
76                 { error "$dsname failed in apply-$dsmode"; return 1; }
77
78         #
79         # here do any common things for $mode
80         # 
81         local hostname=""
82         if ds_get_item hostname || ds_get_item local-hostname; then
83                 hostname="${_RET%%.*}"
84                 echo "$hostname" > /etc/hostname
85                 hostname -F /etc/hostname
86         fi
87
88     
89         ## if there is a 'ephemeral0' block device, mount it
90         if find_devs_with LABEL=ephemeral0 && [ -n "${_RET}" ]; then
91                 # get the first one only
92                 local dev="${_RET% *}"
93                 echo "${dev}     /mnt      auto     rw,defaults 0 0" >> /etc/fstab
94                 mount /mnt ||
95                         error "failed to mount ${dev}"
96         fi
97
98         # if we were provided with a network/interfaces file copy it
99         if [ -f "$RESULTS_D/network-interfaces" ]; then
100                 local nif="$TARGET_ROOT/etc/network/interfaces"
101                 [ -d "$TARGET_ROOT/etc/network" ] ||
102                         mkdir -p "$TARGET_ROOT/etc/network"
103                 [ ! -f "$nif" ] || cp "$nif" "$nif.dist" ||
104                         error "failed to copy network interfaces"
105                 debug 1 "copied network/interfaces"
106         fi
107
108         # if we have public-keys, put them in cirros user                                                    
109         if ds_get_item_path public-keys; then
110                 local keyfile="$_RET"
111                 ds_get_item public-keys ||
112                         error "failed to get public-keys"
113                 mkdir -p "${TARGET_ROOT}/home/cirros" "${TARGET_ROOT}/root"
114                 HOME=$TARGET_ROOT/home/cirros su -c "ssh-add-key --replace -" cirros < "$keyfile"
115                 HOME=$TARGET_ROOT/root ssh-add-key --replace \
116                         --prefix "$SSH_ROOT_PREFIX" - < "$keyfile"
117         fi 
118
119         local name=""
120         if [ -c /dev/urandom ]; then
121                 for name in random_seed random-seed; do
122                         if ds_has_item "$name"; then
123                                 ds_cat_item "$name" > /dev/urandom && break
124                         fi
125                 done
126         fi
127
128         return 0
129 }
130
131 cirros_apply "$@"
132
133 # vi: ts=4 noexpandtab