Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/distrib/utils/embedded ec2_init: Add support for Oracle Cloud



details:   https://anonhg.NetBSD.org/src/rev/e6e081611d78
branches:  trunk
changeset: 371879:e6e081611d78
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sat Oct 15 18:32:30 2022 +0000

description:
ec2_init: Add support for Oracle Cloud

diffstat:

 distrib/utils/embedded/conf/arm64.conf  |   4 +-
 distrib/utils/embedded/conf/evbarm.conf |  20 +++++++++++++++-
 distrib/utils/embedded/files/ec2_init   |  40 +++++++++++++++++++++++---------
 3 files changed, 50 insertions(+), 14 deletions(-)

diffs (143 lines):

diff -r 95b6d2416777 -r e6e081611d78 distrib/utils/embedded/conf/arm64.conf
--- a/distrib/utils/embedded/conf/arm64.conf    Sat Oct 15 16:58:16 2022 +0000
+++ b/distrib/utils/embedded/conf/arm64.conf    Sat Oct 15 18:32:30 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: arm64.conf,v 1.14 2021/10/16 14:05:48 jmcneill Exp $
+# $NetBSD: arm64.conf,v 1.15 2022/10/15 18:32:30 jmcneill Exp $
 # ARM64 customization script used by mkimage
 #
 board=arm64
@@ -28,7 +28,7 @@
 mdnsd=YES
 devpubd=YES
 wscons=\$(dev_exists wsdisplay0)
-ec2_init=\$(dev_exists ena0)
+ec2_init=\$(is_cloud)
 if checkyesno ec2_init ; then
        dhcpcd_flags="\$dhcpcd_flags -w"
 else
diff -r 95b6d2416777 -r e6e081611d78 distrib/utils/embedded/conf/evbarm.conf
--- a/distrib/utils/embedded/conf/evbarm.conf   Sat Oct 15 16:58:16 2022 +0000
+++ b/distrib/utils/embedded/conf/evbarm.conf   Sat Oct 15 18:32:30 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: evbarm.conf,v 1.39 2021/07/06 11:49:36 jmcneill Exp $
+# $NetBSD: evbarm.conf,v 1.40 2022/10/15 18:32:30 jmcneill Exp $
 # evbarm shared config
 #
 image=$HOME/${board}.img
@@ -145,6 +145,24 @@
        fi
 }
 
+is_cloud() {
+       ret=NO
+
+       # AWS EC2
+       if [ "\$(dev_exists ena0)" = "YES" ]; then
+               ret=YES
+       fi
+
+       # Oracle OCI
+       case "\$(/sbin/sysctl -n machdep.dmi.chassis-asset-tag)" in
+       OracleCloud*)
+               ret=YES
+               ;;
+       esac
+
+       printf \$ret
+}
+
 rc_configured=YES
 hostname=${hostname:-${board}}
 no_swap=YES
diff -r 95b6d2416777 -r e6e081611d78 distrib/utils/embedded/files/ec2_init
--- a/distrib/utils/embedded/files/ec2_init     Sat Oct 15 16:58:16 2022 +0000
+++ b/distrib/utils/embedded/files/ec2_init     Sat Oct 15 18:32:30 2022 +0000
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $NetBSD: ec2_init,v 1.4 2021/07/20 19:31:23 rhialto Exp $
+# $NetBSD: ec2_init,v 1.5 2022/10/15 18:32:30 jmcneill Exp $
 #
 # PROVIDE: ec2_init
 # REQUIRE: NETWORKING
@@ -13,9 +13,27 @@
 start_cmd="ec2_init"
 stop_cmd=":"
 
-EC2_USER="ec2-user"
-METADATA_URL="http://169.254.169.254/latest/meta-data/";
-SSH_KEY_URL="public-keys/0/openssh-key"
+CLOUD_TYPE=EC2 # default
+
+case "$(/sbin/sysctl -n machdep.dmi.chassis-asset-tag)" in
+OracleCloud*)
+       CLOUD_TYPE=OCI
+       ;;
+esac
+
+case ${CLOUD_TYPE} in
+EC2)
+       EC2_USER="ec2-user"
+       METADATA_URL="http://169.254.169.254/latest/meta-data/";
+       SSH_KEY_URL="public-keys/0/openssh-key"
+       ;;
+OCI)
+       EC2_USER="opc"
+       METADATA_URL="http://169.254.169.254/opc/v1/instance/";
+       SSH_KEY_URL="metadata/ssh_authorized_keys"
+       ;;
+esac
+
 HOSTNAME_URL="hostname"
 
 SSH_KEY_FILE="/home/${EC2_USER}/.ssh/authorized_keys"
@@ -24,7 +42,7 @@
 
 ec2_newuser()
 {
-       echo "Creating EC2 user account ${EC2_USER}"
+       echo "Creating ${CLOUD_TYPE} user account ${EC2_USER}"
        useradd -g users -G wheel,operator -m "${EC2_USER}"
 }
 
@@ -39,25 +57,25 @@
        umask 022
 
        # set hostname; it may be 5-10 seconds for the metadata service
-       # to  become reachable.
+       # to become reachable.
        try=0
        while [ $((try++)) -lt 20 ]
        do
                HOSTNAME=$(ftp -o - -q 2 "${METADATA_URL}${HOSTNAME_URL}")
                if [ -n "$HOSTNAME" ]; then
-                       echo "Setting EC2 hostname: ${HOSTNAME}"
+                       echo "Setting ${CLOUD_TYPE} hostname: ${HOSTNAME}"
                        echo "$HOSTNAME" > /etc/myname
                        hostname "$HOSTNAME"
                        break
                fi
-               echo "EC2 hostname not available yet (try $try)"
+               echo "${CLOUD_TYPE} hostname not available yet (try $try)"
                sleep 1
        done
 
-       # create EC2 user
+       # create cloud user
        id "${EC2_USER}" >/dev/null 2>&1 || ec2_newuser
 
-       # fetch the public key from Amazon Web Services
+       # fetch the public key from the metadata service
        EC2_SSH_KEY=$(ftp -o - -q 2 "${METADATA_URL}${SSH_KEY_URL}")
 
        if [ -n "$EC2_SSH_KEY" ]; then
@@ -71,7 +89,7 @@
 
                grep -q "$EC2_SSH_KEY" "$SSH_KEY_FILE"
                if [ $? -ne 0 ]; then
-                       echo "Setting EC2 SSH public key for user ${EC2_USER}: ${EC2_SSH_KEY##* }"
+                       echo "Setting ${CLOUD_TYPE} SSH public key for user ${EC2_USER}: ${EC2_SSH_KEY##* }"
                        echo "$EC2_SSH_KEY" >> "$SSH_KEY_FILE"
                fi
        fi



Home | Main Index | Thread Index | Old Index