Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013 Joyent, Inc. All rights reserved.
Copyright (c) 2017 Joyent, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
20 changes: 4 additions & 16 deletions includes/00-mdata.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
# Copyright 2013, Joyent. Inc. All rights reserved.
#!/usr/bin/env bash
# Copyright (c) 2017, Joyent, Inc.

if [ -x /usr/sbin/mdata-get ]; then
HAS_METADATA=yes

log "waiting for metadata to show up"

until [ -e /.zonecontrol/metadata.sock ] ||\
[ -e /var/run/smartdc/metadata.sock ] ||\
[ $((MCOUNT++)) -gt 30 ]; do
sleep 1
done

[ -e /.zonecontrol/metadata.sock ] ||\
[ -e /var/run/smartdc/metadata.sock ] ||\
log "metadata failed to show up"
fi
log 'waiting for metadata to show up'
/usr/vm/sbin/filewait /.zonecontrol/metadata.sock
17 changes: 14 additions & 3 deletions includes/01-reboot-file.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# Copyright 2013, Joyent. Inc. All rights reserved.
#!/usr/bin/env bash
# Copyright (c) 2017, Joyent, Inc.

# create a temporary file that disappears on the first reboot
touch /tmp/.FIRST_REBOOT_NOT_YET_COMPLETE
# A zone should reboot if features.reboot is true, or if the call to json
# fails. This way, we are backwards-compatible with versions of zoneinit that
# do not have zoneinit.json
shouldreboot=$(json -f "$ZONEINIT_DIR/zoneinit.json" features.reboot)
if (($? != 0)) || [[ $shouldreboot == true ]]; then
ZONE_SHOULD_REBOOT=true

# create a temporary file that disappears on the first reboot
touch /tmp/.FIRST_REBOOT_NOT_YET_COMPLETE
else
ZONE_SHOULD_REBOOT=false
fi
180 changes: 67 additions & 113 deletions includes/02-config.sh
Original file line number Diff line number Diff line change
@@ -1,114 +1,69 @@
# Copyright 2013, Joyent. Inc. All rights reserved.
#!/usr/bin/env bash
# Copyright (c) 2017, Joyent, Inc.

log "determine machine parameters and configuration"
log 'determine machine parameters and configuration'

# Little helper to overcome the problem that mdata-get doesn't use stderr
mdata() {
set -o pipefail
output=$(mdata-get $1 2>/dev/null) && echo -e "${output}" || return 1
local output
output=$(mdata-get "$1" 2>/dev/null)
if (($? == 0)); then
echo "$output"
else
return 1
fi
}

log "checking for duplicate IPs"
# List nics
mdata-nics() {
mdata sdc:nics \
| json -d '|' -e 'this.ips = this.ips && this.ips.join(",")' \
-a interface ip ips nic_tag
}

log 'checking for duplicate IPs'
if ifconfig -a | grep DUP >/dev/null ; then
log "provisioned with IP already in use, shutting down."
halt
fi

( [ ${HAS_METADATA} ] && mdata sdc:uuid >/dev/null ) || USE_ZONECONFIG=yes

if [ ! ${USE_ZONECONFIG} ]; then

# This is a recent enough platform to use metadata to retrieve all
# information we need for provisioning

ZONENAME=$(mdata sdc:zonename)
HOSTNAME=$(mdata sdc:hostname || echo "${ZONENAME}")
DOMAINNAME=$(mdata sdc:dns_domain || echo "local")

unset i
while : ${i:=-1}; ((i++)); SERVER=$(mdata sdc:resolvers.${i}); [ ${SERVER} ]; do
RESOLVERS=(${RESOLVERS[@]} ${SERVER})
done

RAM_IN_BYTES=$(echo "$(mdata sdc:max_physical_memory)*1024^2" | bc 2>/dev/null)
SWAP_IN_BYTES=$(echo "$(mdata sdc:max_swap)*1024^2" | bc 2>/dev/null)
TMPFS=$(mdata sdc:tmpfs || echo "$((RAM_IN_BYTES/1024/1024))")m

# We want to fail if anything in the pipe fails during this step
set -o pipefail
/usr/sbin/mdata-get sdc:nics \
| /usr/bin/json -d '|' -e 'this.ips = this.ips && this.ips.join(",")' \
-a interface ip ips nic_tag \
| while IFS='|' read IFACE IP IPS NIC_TAG; do
NET_INTERFACES=(${NET_INTERFACES[@]} ${IFACE})

[[ -z $IPS ]] && IPS=$IP

OLDIFS=$IFS
IFS=','
for THIS_IP in $IPS; do
# strip prefix length and only use valid IPv4 addresses
[[ "${THIS_IP%/*}." =~ ^(([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){4}$ ]] || continue
eval "${IFACE}_IP=${THIS_IP}"
case $NIC_TAG in
external)
PUBLIC_IPS=(${PUBLIC_IPS[@]} ${THIS_IP})
;;
*)
PRIVATE_IPS=(${PRIVATE_IPS[@]} ${THIS_IP})
;;
esac
done
IFS=$OLDIFS
done
set +o pipefail

# Pick a valid IP for either of the public/private vars, fall back to localhost
PUBLIC_IP="${PUBLIC_IPS[0]}"
PRIVATE_IP="${PRIVATE_IPS[0]}"
LOCAL_IP="$(ifconfig lo0 | awk '{if ($1=="inet") print $2}')"

else

# This seems to be an older release of SmartOS, or SDC 6.5.x
# We cannot source the information we need from metadata, so
# need the 'zoneconfig' file passed with some information.

if [ -f "${ZONECONFIG}" ]; then
source ${ZONECONFIG}
fi

: ${ZONENAME:=$(zonename)}
: ${HOSTNAME:=${ZONENAME}}
: ${DOMAINNAME:=local}

[ ${RAM_IN_BYTES} ] || RAM_IN_BYTES=$( kstat -p -c zone_memory_cap -s physcap | awk '{print $2}' )
[ ${RAM_IN_BYTES} -gt 0 2>/dev/null ] || RAM_IN_BYTES=134217728
log "zone physical memory cap determined as $((RAM_IN_BYTES/1024/1024)) MiB"

[ ${SWAP_IN_BYTES} ] || SWAP_IN_BYTES=$( kstat -p -c zone_memory_cap -s swapcap | awk '{print $2}' )
[ ${SWAP_IN_BYTES} -gt 0 2>/dev/null ] || SWAP_IN_BYTES=$((RAM_IN_BYTES*2))
log "zone virtual memory cap determined as $((SWAP_IN_BYTES/1024/1024)) MiB"

[ ${TMPFS} ] || TMPFS=$((RAM_IN_BYTES/1024/1024))m

unset i
while : ${i:=-1}; ((i++)); IFACE=NET${i}_INTERFACE; [ ${!IFACE} ]; do
NET_INTERFACES=(${NET_INTERFACES[@]} ${!IFACE})
eval "${!IFACE}_IP=\${NET${i}_IP}"
done

# We should already have PUBLIC_IP & PRIVATE_IP set via zoneconfig

PUBLIC_IPS=(${PUBLIC_IP})
PRIVATE_IPS=(${PRIVATE_IP})
RESOLVERS=(${RESOLVERS})

log 'provisioned with IP already in use, shutting down.'
halt
fi

# Make sure *some* resolvers are used
[ ${#RESOLVERS[@]} -gt 0 ] || RESOLVERS=(8.8.8.8 8.8.4.4)
declare -A INTERFACE_IPS
PUBLIC_IPS=()
PRIVATE_IPS=()

ZONENAME=$(mdata sdc:zonename || zonename)
HOSTNAME=$(mdata sdc:hostname || echo "$ZONENAME")
DOMAINNAME=$(mdata sdc:dns_domain || echo 'local')

RAM_IN_BYTES=$(($(mdata sdc:max_physical_memory) * 1024 * 1024))
SWAP_IN_BYTES=$(($(mdata sdc:max_swap) * 1024 * 1024))
TMPFS=$(mdata sdc:tmpfs || echo "$((RAM_IN_BYTES/1024/1024))")m

while IFS='|' read -r iface ip ips nic_tag; do
[[ -z $ips ]] && ips=$ip

OLDIFS=$IFS
IFS=','
for this_ip in $ips; do
# strip prefix length and only use valid IPv4 addresses
[[ "${this_ip%/*}." =~ ^(([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){4}$ ]] || continue
INTERFACE_IPS[$iface]=$this_ip
case "$nic_tag" in
external)
PUBLIC_IPS+=("$this_ip")
;;
*)
PRIVATE_IPS+=("$this_ip")
;;
esac
done
IFS=$OLDIFS
done < <(mdata-nics)

# Pick a valid IP for either of the public/private vars, fall back to localhost
PUBLIC_IP=${PUBLIC_IPS[0]}
PRIVATE_IP=${PRIVATE_IPS[0]}
LOCAL_IP=$(ifconfig lo0 | awk '{if ($1=="inet") print $2}')

# Use mdata-get to retrieve passwords for users needed by the image
# put them in respective variables (e.g. for 'admin' use $ADMIN_PW)
Expand All @@ -117,17 +72,16 @@ fi
: ${USERS=admin root}
USERS=(${USERS})

for USER in ${USERS[@]}; do
PASS_VAR_LOWER=${USER}_pw
PASS_VAR_UPPER=$(echo ${PASS_VAR_LOWER} | tr '[a-z]' '[A-Z]')

if [ ${HAS_METADATA} ]; then
USER_PW="$(mdata ${PASS_VAR_LOWER})" || unset USER_PW
if [ -n "${USER_PW}" ]; then
eval "${PASS_VAR_UPPER}='${USER_PW}'"
else
unset ${PASS_VAR_UPPER}
fi
fi
declare -A PASSWORDS

for user in "${USERS[@]}"; do
PASS_VAR_LOWER=${user}_pw
PASS_VAR_UPPER=$(echo "$PASS_VAR_LOWER" | tr '[[:lower:]]' '[[:upper:]]')

user_pw=$(mdata "$PASS_VAR_LOWER" || true)
if [[ -n $user_pw ]]; then
PASSWORDS[$PASS_VAR_UPPER]=$user_pw
fi
done

true
16 changes: 10 additions & 6 deletions includes/04-mdata.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Copyright 2013, Joyent. Inc. All rights reserved.
#!/usr/bin/env bash
# Copyright (c) 2017, Joyent, Inc.

if [ ${HAS_METADATA} ]; then

# Enable the mdata service that fetches the metadata user-script on each boot
log "enabling metadata agent"
svcadm enable mdata:fetch
# Enable the mdata service that fetches the metadata user-script on each boot
log 'enabling metadata agent'

if [[ $ZONE_SHOULD_REBOOT == 'true' ]]; then
# This service will not start if there is a reboot pending, so we
# enable the service and move on immediately.
svcadm enable mdata:fetch
else
svcadm enable -s mdata:fetch
fi
43 changes: 23 additions & 20 deletions includes/11-files.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
# Copyright 2013, Joyent. Inc. All rights reserved.
#!/usr/bin/env bash
# Copyright (c) 2017, Joyent, Inc.

log "cleaning files"
log 'cleaning files'

if [ -e /var/log/auth.log ]; then
cp /dev/null /var/log/auth.log >/dev/null
if [[ -e /var/log/auth.log ]]; then
cp /dev/null /var/log/auth.log >/dev/null
else
cp /dev/null /var/log/authlog >/dev/null
cp /dev/null /var/log/authlog >/dev/null
fi

log "substituting placeholders for real data in config files"
log 'substituting placeholders for real data in config files'

substitute_files=($(find /etc /opt/local/etc -type f | sort | xargs \
/usr/bin/egrep -l '@(PUBLIC_IP|PRIVATE_IP|LOCAL_IP|DOMAINNAME|HOSTNAME|ZONENAME)@' || true))
egrep -l '@(PUBLIC_IP|PRIVATE_IP|LOCAL_IP|DOMAINNAME|HOSTNAME|ZONENAME)@' || true))

for file in ${substitute_files[@]}; do
if sed -e "s/@PUBLIC_IP@/${PUBLIC_IP}/g" \
-e "s/@PRIVATE_IP@/${PRIVATE_IP}/g" \
-e "s/@LOCAL_IP@/${LOCAL_IP}/g" \
-e "s/@HOSTNAME@/${HOSTNAME}/g" \
-e "s/@ZONENAME@/${ZONENAME}/g" \
-e "s/@DOMAINNAME@/${DOMAINNAME}/g" \
${file} > ${file}.tmp; then
mv ${file}{.tmp,}
fi
rm -f ${file}.tmp
for file in "${substitute_files[@]}"; do
if sed -e "s/@PUBLIC_IP@/$PUBLIC_IP/g" \
-e "s/@PRIVATE_IP@/$PRIVATE_IP/g" \
-e "s/@LOCAL_IP@/$LOCAL_IP/g" \
-e "s/@HOSTNAME@/$HOSTNAME/g" \
-e "s/@ZONENAME@/$ZONENAME/g" \
-e "s/@DOMAINNAME@/$DOMAINNAME/g" \
"$file" > "$file.tmp"; then

log "rewritting $file"
mv "$file.tmp" "$file"
fi
rm -f "$file.tmp"
done

if sed -e "/tmpfs/s/-$/size=${TMPFS}/" /etc/vfstab > /etc/vfstab.tmp 2>/dev/null; then
mv /etc/vfstab{.tmp,}
if sed -e "/tmpfs/s/-$/size=$TMPFS/" /etc/vfstab > /etc/vfstab.tmp 2>/dev/null; then
mv /etc/vfstab{.tmp,}
fi
rm -f /etc/vfstab.tmp
17 changes: 5 additions & 12 deletions includes/12-network.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
# Copyright 2013, Joyent. Inc. All rights reserved.
#!/usr/bin/env bash
# Copyright (c) 2017, Joyent, Inc.

log "setting hostname, IPs and resolvers"
log 'setting hostname, IPs and resolvers'

echo "${HOSTNAME}" > /etc/nodename
/bin/hostname ${HOSTNAME}

(
/bin/sed '/nameserver/d' /etc/resolv.conf 2>/dev/null
for HOST in ${RESOLVERS[@]}; do
echo "nameserver ${HOST}"
done
) > /etc/resolv.conf.tmp
mv /etc/resolv.conf{.tmp,}
echo "$HOSTNAME" > /etc/nodename
/bin/hostname "$HOSTNAME"

sed '/^127\.0\.0\.1/s/$/ '${HOSTNAME}'/' /etc/inet/hosts > /etc/inet/hosts.tmp
mv /etc/inet/hosts{.tmp,}
Loading