diff --git a/vmtools/.gitignore b/vmtools/.gitignore new file mode 100644 index 0000000..b2be92b --- /dev/null +++ b/vmtools/.gitignore @@ -0,0 +1 @@ +result diff --git a/vmtools/flake.lock b/vmtools/flake.lock new file mode 100644 index 0000000..df3f9f6 --- /dev/null +++ b/vmtools/flake.lock @@ -0,0 +1,43 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1654230545, + "narHash": "sha256-8Vlwf0x8ow6pPOK2a04bT+pxIeRnM1+O0Xv9/CuDzRs=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "236cc2971ac72acd90f0ae3a797f9f83098b17ec", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/vmtools/flake.nix b/vmtools/flake.nix new file mode 100644 index 0000000..58cdc10 --- /dev/null +++ b/vmtools/flake.nix @@ -0,0 +1,22 @@ +{ + description = "vmtools flake for nixos images on triton"; + + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + + outputs = { self, flake-utils, nixpkgs }: + flake-utils.lib.eachDefaultSystem (system: + let + triton-vmtools = import ./pkgs/triton-vmtools.nix { inherit nixpkgs system; }; + in + { + + # Internal utility package with shell function and env vars helper. + # These get source'd in devshell.bash.extra when starting tritonshell + packages = { + triton-vmtools = triton-vmtools; + }; + + defaultPackage = triton-vmtools; + }); +} diff --git a/vmtools/pkgs/0001-fix-paths-in-scripts.patch b/vmtools/pkgs/0001-fix-paths-in-scripts.patch new file mode 100644 index 0000000..b0c4b6a --- /dev/null +++ b/vmtools/pkgs/0001-fix-paths-in-scripts.patch @@ -0,0 +1,236 @@ +From 3e221bf3c440a956fefb2e0b11ab20cccc0d578e Mon Sep 17 00:00:00 2001 +From: teutat3s <10206665+teutat3s@users.noreply.github.com> +Date: Sun, 5 Jun 2022 23:40:31 +0200 +Subject: [PATCH] Prepare scripts for nix build integration + +--- + src/linux/lib/smartdc/firstboot | 11 ++++++----- + src/linux/lib/smartdc/format-secondary-disk | 2 +- + src/linux/lib/smartdc/get-user-data | 2 +- + src/linux/lib/smartdc/joyent_rc.local | 16 ++++++++-------- + src/linux/lib/smartdc/lib_smartdc_scripts.cfg | 6 +++--- + src/linux/lib/smartdc/run-operator-script | 2 +- + src/linux/lib/smartdc/run-user-script | 2 +- + src/linux/lib/smartdc/send-arp-updates | 2 +- + src/linux/lib/smartdc/set-hostid | 2 +- + src/linux/lib/smartdc/set-hostname | 2 +- + src/linux/lib/smartdc/set-root-authorized-keys | 2 +- + src/linux/lib/smartdc/set-rootpassword | 2 +- + 12 files changed, 26 insertions(+), 25 deletions(-) + +diff --git a/src/linux/lib/smartdc/firstboot b/src/linux/lib/smartdc/firstboot +index 044adca..6bd022f 100755 +--- a/src/linux/lib/smartdc/firstboot ++++ b/src/linux/lib/smartdc/firstboot +@@ -4,21 +4,22 @@ + # For scripts that should be run on first boot only + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + lib_smartdc_info "Start of $(basename $0) script" + + # Set root password if root_pw is set in the image manifest +-(/lib/smartdc/set-rootpassword) ++(/__replace_lib/smartdc/set-rootpassword) + + # Set hostid +-(/lib/smartdc/set-hostid) ++(/__replace_lib/smartdc/set-hostid) + + # Format disk +-(/lib/smartdc/format-secondary-disk) ++(/__replace_lib/smartdc/format-secondary-disk) + + # Disable firstboot once all scripts are run + lib_smartdc_info "Disabling firstboot" +-touch /lib/smartdc/.firstboot-complete-do-not-delete ++mkdir -p /var/lib/smartdc ++touch /var/lib/smartdc/.firstboot-complete-do-not-delete + + exit 0 +diff --git a/src/linux/lib/smartdc/format-secondary-disk b/src/linux/lib/smartdc/format-secondary-disk +index cfc6eb4..777b853 100755 +--- a/src/linux/lib/smartdc/format-secondary-disk ++++ b/src/linux/lib/smartdc/format-secondary-disk +@@ -8,7 +8,7 @@ + # by default we provision the whole disk as one partition + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + SFDISK=`which sfdisk 2> /dev/null` + PARTED=`which parted 2> /dev/null` +diff --git a/src/linux/lib/smartdc/get-user-data b/src/linux/lib/smartdc/get-user-data +index 38a1a04..e2499a1 100755 +--- a/src/linux/lib/smartdc/get-user-data ++++ b/src/linux/lib/smartdc/get-user-data +@@ -7,7 +7,7 @@ + # before script "run-user-script" + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + lib_smartdc_info "Retrieving metadata user-data" + +diff --git a/src/linux/lib/smartdc/joyent_rc.local b/src/linux/lib/smartdc/joyent_rc.local +index 1641417..75f7953 100755 +--- a/src/linux/lib/smartdc/joyent_rc.local ++++ b/src/linux/lib/smartdc/joyent_rc.local +@@ -6,7 +6,7 @@ + # this is called from /etc/rc.local + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + # DO NOT use lib_smartdc_fatal in here + # You want the rest of the init script to run +@@ -112,14 +112,14 @@ case `uname -s | tr '[:upper:]' '[:lower:]'` in + esac + + # scripts that can run on all systems +-(/lib/smartdc/set-root-authorized-keys) +-if [[ ! -f /lib/smartdc/.firstboot-complete-do-not-delete ]] ; then +- (/lib/smartdc/firstboot) ++(/__replace_lib/smartdc/set-root-authorized-keys) ++if [[ ! -f /var/lib/smartdc/.firstboot-complete-do-not-delete ]] ; then ++ (/__replace_lib/smartdc/firstboot) + fi +-(/lib/smartdc/set-hostname) +-(/lib/smartdc/run-operator-script) +-(/lib/smartdc/get-user-data) +-(/lib/smartdc/run-user-script) ++(/__replace_lib/smartdc/set-hostname) ++(/__replace_lib/smartdc/run-operator-script) ++(/__replace_lib/smartdc/get-user-data) ++(/__replace_lib/smartdc/run-user-script) + + if [[ ! -d /var/lock/subsys ]] ; then + mkdir -p /var/lock/subsys +diff --git a/src/linux/lib/smartdc/lib_smartdc_scripts.cfg b/src/linux/lib/smartdc/lib_smartdc_scripts.cfg +index 1d9a045..37618c7 100755 +--- a/src/linux/lib/smartdc/lib_smartdc_scripts.cfg ++++ b/src/linux/lib/smartdc/lib_smartdc_scripts.cfg +@@ -2,7 +2,7 @@ + + # Copyright (c) 2014, Joyent, Inc. All rights reserved. + # +-# Common variables used by scripts in /lib/smartdc ++# Common variables used by scripts in /__replace_lib/smartdc + + # location of binaries that functions use + LOGGER=`which logger 2> /dev/null` +@@ -10,13 +10,13 @@ UNAME_BIN=`which uname 2> /dev/null` + MOUNT_BIN=`which mount 2> /dev/null` + + MDATA_GET_BIN='/usr/sbin/mdata-get' +-SEND_ARP_UPDATES_BIN='/lib/smartdc/send-arp-updates' ++SEND_ARP_UPDATES_BIN='/__replace_lib/smartdc/send-arp-updates' + + # location of common files + USER_SCRIPT_FILE='/var/tmp/user-script' + USER_SCRIPT_LOCKFILE='/var/tmp/user-script.lock' + +-# Common functions used by scripts /lib/smartdc ++# Common functions used by scripts /__replace_lib/smartdc + + lib_smartdc_fatal() { + echo " " +diff --git a/src/linux/lib/smartdc/run-operator-script b/src/linux/lib/smartdc/run-operator-script +index e76c813..7a7b38e 100755 +--- a/src/linux/lib/smartdc/run-operator-script ++++ b/src/linux/lib/smartdc/run-operator-script +@@ -3,7 +3,7 @@ + # Copyright (c) 2013 Joyent Inc., All rights reserved. + # + +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + SCRIPT=/tmp/.operator-script + rm -f $SCRIPT +diff --git a/src/linux/lib/smartdc/run-user-script b/src/linux/lib/smartdc/run-user-script +index 5830ba8..434fd5a 100755 +--- a/src/linux/lib/smartdc/run-user-script ++++ b/src/linux/lib/smartdc/run-user-script +@@ -10,7 +10,7 @@ + # This script is executed on each system boot + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + # set run_userscript_flag to the string TRUE + # to remove $USER_SCRIPT_LOCKFILE so that userscript is ran +diff --git a/src/linux/lib/smartdc/send-arp-updates b/src/linux/lib/smartdc/send-arp-updates +index f686954..61c49be 100755 +--- a/src/linux/lib/smartdc/send-arp-updates ++++ b/src/linux/lib/smartdc/send-arp-updates +@@ -9,7 +9,7 @@ + # This script sends gratuitous arp packets to force an arp cache update + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + set -o errexit + set -o pipefail +diff --git a/src/linux/lib/smartdc/set-hostid b/src/linux/lib/smartdc/set-hostid +index 00914a9..075b467 100755 +--- a/src/linux/lib/smartdc/set-hostid ++++ b/src/linux/lib/smartdc/set-hostid +@@ -4,7 +4,7 @@ + # Explicitly set hostid. + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + lib_smartdc_info "Start of $(basename $0) script" + +diff --git a/src/linux/lib/smartdc/set-hostname b/src/linux/lib/smartdc/set-hostname +index 95f075d..ba3f4cc 100755 +--- a/src/linux/lib/smartdc/set-hostname ++++ b/src/linux/lib/smartdc/set-hostname +@@ -4,7 +4,7 @@ + # + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + # set system host name in this order + # 1) use /etc/hostname if it is not empty +diff --git a/src/linux/lib/smartdc/set-root-authorized-keys b/src/linux/lib/smartdc/set-root-authorized-keys +index b7a19aa..72466bf 100755 +--- a/src/linux/lib/smartdc/set-root-authorized-keys ++++ b/src/linux/lib/smartdc/set-root-authorized-keys +@@ -9,7 +9,7 @@ + # By default this script is called on each boot + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + # set overwrite_root_akeys to the string OVERWRITE + # to overwrite root authorized_keys on reboot +diff --git a/src/linux/lib/smartdc/set-rootpassword b/src/linux/lib/smartdc/set-rootpassword +index d4ff469..74d42d0 100755 +--- a/src/linux/lib/smartdc/set-rootpassword ++++ b/src/linux/lib/smartdc/set-rootpassword +@@ -12,7 +12,7 @@ + # /etc/shadow to ensure a root password is not being overwritten. + + # load common functions and vars +-. /lib/smartdc/lib_smartdc_scripts.cfg ++. /__replace_lib/smartdc/lib_smartdc_scripts.cfg + + lib_smartdc_info "Start of $(basename $0) script" + +-- +2.36.0 + diff --git a/vmtools/pkgs/triton-vmtools.nix b/vmtools/pkgs/triton-vmtools.nix new file mode 100644 index 0000000..1ed383e --- /dev/null +++ b/vmtools/pkgs/triton-vmtools.nix @@ -0,0 +1,62 @@ +{ nixpkgs, system, ... }: +let + pkgs = import nixpkgs { + inherit system; + }; +in with pkgs; +stdenv.mkDerivation rec { + pname = "triton-vmtools"; + version = "0.0.1"; + src = fetchFromGitHub { + owner = "TritonDataCenter"; + repo = "sdc-vmtools"; + rev = "664539f808786bc71a2ab566f663fd25b8c12140"; + sha256 = "sha256-RI1iqRvjSEPVJob0Y20/eJ8AmObhXP6Nn9b1Vv1cCO4="; + }; + + PATH = lib.makeBinPath nativeBuildInputs; + + nativeBuildInputs = [ + autoPatchelfHook + coreutils + e2fsprogs + inetutils + iputils + policycoreutils + util-linux + parted + ]; + + fixupPhase = '' + for file in $out/lib/smartdc/* + do + substituteInPlace ''$file \ + --replace "/__replace_lib/" "$out/lib/" + substituteInPlace ''$file \ + --replace "/__replace_lib/" "$out/lib/" + done + + substituteInPlace $out/lib/smartdc/lib_smartdc_scripts.cfg \ + --replace "/usr/sbin/" "$out/bin/" + ''; + + patches = [ + ./0001-fix-paths-in-scripts.patch + ]; + + installPhase = '' + echo "Installing SmartOS VM Guest Tools..." + sdcvmtools="src/linux" + mkdir -p $out/bin $out/lib $out/share/man/man1 + cp -r ''${sdcvmtools}/lib/smartdc $out/lib/ + cp -r ''${sdcvmtools}/usr/sbin/mdata-* $out/bin/ + cp -r ''${sdcvmtools}/usr/share/man/man1/mdata-* $out/share/man/man1/ + echo "SmartOS VM Guest Tools installed" + ''; + + meta = with lib; { + description = "The VM Guest tools contains scripts and drivers that are used to create virtualized machine images in SmartOS."; + homepage = "https://github.com/TritonDataCenter/sdc-vmtools"; + license = licenses.mit; + }; +}