Bläddra i källkod

Main script works

Laurent HUBERT 6 år sedan
förälder
incheckning
8813d05b60
6 ändrade filer med 119 tillägg och 0 borttagningar
  1. 0 0
      Makefile
  2. 23 0
      deploy
  3. 10 0
      first-boot-init.service
  4. 28 0
      first-boot-script
  5. 29 0
      scripts/00-hostname
  6. 29 0
      scripts/skeleton

+ 0 - 0
Makefile


+ 23 - 0
deploy

@@ -0,0 +1,23 @@
+#!/bin/bash
+# Licence : GPL v3
+# Author: Laurent HUBERT
+#
+# Deploys the first boot initialization scripts on a system
+if [[ $EUID -ne 0 ]]; then
+	 echo "This script cannot work if not super-user. Please run as root" 1>&2
+	 exit 1
+fi
+SYSTEMD_DIR=/etc/systemd/system
+FIRST_BOOT_BASE_DIR=/etc/first-boot
+FIRST_BOOT_SCRIPTS_DIR=$FIRST_BOOT_BASE_DIR/scripts
+cp first-boot-init.service ${SYSTEMD_DIR}
+mkdir -p $FIRST_BOOT_SCRIPTS_DIR
+cp first-boot-script $FIRST_BOOT_BASE_DIR/
+cp scripts/* $FIRST_BOOT_SCRIPTS_DIR
+# Resets the flag file
+touch $FIRST_BOOT_BASE_DIR/.mustrun
+
+echo "You can now run rm -r on this directory"
+echo "e.g. :"
+echo "\$ cd .."
+echo "\$ rm -r $(pwd)"

+ 10 - 0
first-boot-init.service

@@ -0,0 +1,10 @@
+[Unit]
+Description=yourscript
+ConditionPathExists=/etc/first-boot/.mustrun
+
+[Service]
+Type=oneshot
+ExecStart=/etc/first-boot/scripts/first-boot-script
+
+[Install]
+WantedBy=multi-user.target

+ 28 - 0
first-boot-script

@@ -0,0 +1,28 @@
+#!/bin/bash
+# Licence : GPL v3
+# Author: Laurent HUBERT
+#
+# Executes each script from scripts dir if they are executable
+FIRST_BOOT_BASE_DIR=/etc/first-boot
+FIRST_BOOT_SCRIPTS_DIR=$FIRST_BOOT_BASE_DIR/scripts
+
+if [[ $EUID -ne 0 ]]; then
+	 echo "This script cannot work if not super-user. Please run as root" 1>&2
+	 exit 1
+fi
+
+for f in $(ls $FIRST_BOOT_SCRIPTS_DIR/*)
+do
+  if [ -x $f ] ; then
+    echo Executing $f $FIRST_BOOT_BASE_DIR
+    $f $FIRST_BOOT_BASE_DIR
+    status=$?
+    if [ 0 -ne "$status" ] ; then
+      echo "#ERROR# : script $f did not finish with success status !"
+      exit $status
+    fi
+  fi
+done
+
+rm $FIRST_BOOT_BASE_DIR/.mustrun
+exit 0

+ 29 - 0
scripts/00-hostname

@@ -0,0 +1,29 @@
+#!/bin/bash
+# Licence : GPL v3
+# Author: Laurent HUBERT
+#
+# Example file for first-boot-script
+# Copy this file to a new file and add permission execution to the new file
+# Example:
+# # cp skeleton 02-myscript
+# # chmod +x 02-myscript
+#
+cat <<EOF
+##############################
+# Changing hostname          #
+##############################
+EOF
+# Example Section : asking user to enter a value
+read -p "Please enter new hostname: " answer
+
+if [ -n "$answer" ]
+then
+  echo Changing hostname to $answer
+  current=$(hostname)
+  sed -i.firstboot.backup s/$current/$answer/g /etc/hostname
+  sed -i.firstboot.backup s/$current/$answer/g /etc/hosts
+  exit 0
+else
+  # Exit with non zero status
+  exit 1
+fi

+ 29 - 0
scripts/skeleton

@@ -0,0 +1,29 @@
+#!/bin/bash
+# Licence : GPL v3
+# Author: Laurent HUBERT
+#
+# Example file for first-boot-script
+# Copy this file to a new file and add permission execution to the new file
+# Example:
+# # cp skeleton 02-myscript
+# # chmod +x 02-myscript
+#
+# Example Section : Displaying a banner
+cat <<EOF
+##############################
+# My Banner                  #
+##############################
+EOF
+# Example Section : asking user to enter a value
+read -p "Please enter a value: " answer
+
+# Example Section: use the value only if not empty
+if [ -n "$answer" ]
+then
+  echo Using $answer as user data
+  # Do something here
+  exit 0
+else
+  # Exit with non zero status
+  exit 1
+fi