Merge pull request #533 from asteven/feature/install

Feature/install
This commit is contained in:
Nico Schottelius 2017-06-27 14:05:43 +02:00 committed by GitHub
commit 0ef62e1901
31 changed files with 399 additions and 129 deletions

View file

@ -1,6 +1,6 @@
#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2016 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
@ -18,8 +18,19 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
# set defaults
device="$(cat "$__object/parameter/device" 2>/dev/null \
|| echo "/$__object_id" | tee "$__object/parameter/device")"
chroot="$(cat "$__object/parameter/chroot" 2>/dev/null \
|| echo "/target" | tee "$__object/parameter/chroot")"
chroot="/$__object_id"
if [ -f "$__object/parameter/manage-resolv-conf" ]; then
suffix="$(cat "$__object/parameter/manage-resolv-conf")"
resolv_conf="${chroot}/etc/resolv.conf"
original_resolv_conf="${resolv_conf}.${suffix}"
cat << DONE
$__remote_exec $__target_host << EOSSH
if [ -f "${resolv_conf}" ]; then
mv "${resolv_conf}" "${original_resolv_conf}"
fi
# copy hosts resolv.conf into chroot
cp /etc/resolv.conf "${resolv_conf}"
EOSSH
DONE
fi

View file

@ -41,8 +41,4 @@ mountpoint -q "${chroot}/dev/pts" \
[ -d "${chroot}/tmp" ] || mkdir -m 1777 "${chroot}/tmp"
mountpoint -q "${chroot}/tmp" \
|| mount -t tmpfs -o mode=1777,strictatime,nodev,nosuid tmpfs "${chroot}/tmp"
if [ ! -f "${chroot}/etc/resolv.conf" ]; then
cp /etc/resolv.conf "${chroot}/etc/"
fi
DONE

View file

@ -1,5 +1,5 @@
cdist-type__chroot_mount(7)
===================================
===========================
NAME
----
@ -18,7 +18,17 @@ None
OPTIONAL PARAMETERS
-------------------
None
manage-resolv-conf
manage /etc/resolv.conf inside the chroot.
Use the value of this parameter as the suffix to save a copy
of the current /etc/resolv.conf to /etc/resolv.conf.$suffix.
This is used by the __chroot_umount type to restore the initial
file content when unmounting the chroot.
BOOLEAN PARAMETERS
------------------
None.
EXAMPLES
@ -28,6 +38,9 @@ EXAMPLES
__chroot_mount /path/to/chroot
__chroot_mount /path/to/chroot \
--manage-resolv-conf "some-known-string"
AUTHORS
-------
@ -36,7 +49,7 @@ Steven Armstrong <steven-cdist--@--armstrong.cc>
COPYING
-------
Copyright \(C) 2012 Steven Armstrong. You can redistribute it
Copyright \(C) 2012-2017 Steven Armstrong. You can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

View file

@ -0,0 +1 @@
manage-resolv-conf

View file

@ -0,0 +1,36 @@
#!/bin/sh
#
# 2016 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
chroot="/$__object_id"
if [ -f "$__object/parameter/manage-resolv-conf" ]; then
suffix="$(cat "$__object/parameter/manage-resolv-conf")"
resolv_conf="${chroot}/etc/resolv.conf"
original_resolv_conf="${resolv_conf}.${suffix}"
cat << DONE
$__remote_exec $__target_host << EOSSH
if [ -f "${original_resolv_conf}" ]; then
# restore original /etc/resolv.conf that we moved out of the way
# in __chroot_mount/gencode-local
mv -f "${original_resolv_conf}" "${resolv_conf}"
fi
EOSSH
DONE
fi

View file

@ -26,7 +26,6 @@ umount -l "${chroot}/dev/pts"
umount -l "${chroot}/dev"
umount -l "${chroot}/sys"
umount -l "${chroot}/proc"
rm -f "${chroot}/etc/resolv.conf"
if [ -d "${chroot}/etc/resolvconf/resolv.conf.d" ]; then
# ensure /etc/resolvconf/resolv.conf.d/tail is not linked to \
# e.g. /etc/resolvconf/resolv.conf.d/original

View file

@ -18,7 +18,17 @@ None
OPTIONAL PARAMETERS
-------------------
None
manage-resolv-conf
manage /etc/resolv.conf inside the chroot.
Use the value of this parameter as the suffix to find the backup file
that was saved by the __chroot_mount.
This is used by the to restore the initial file content when unmounting
the chroot.
BOOLEAN PARAMETERS
------------------
None.
EXAMPLES
@ -28,6 +38,9 @@ EXAMPLES
__chroot_umount /path/to/chroot
__chroot_umount /path/to/chroot \
--manage-resolv-conf "some-known-string"
SEE ALSO
--------
@ -41,7 +54,7 @@ Steven Armstrong <steven-cdist--@--armstrong.cc>
COPYING
-------
Copyright \(C) 2012 Steven Armstrong. You can redistribute it
Copyright \(C) 2012-2017 Steven Armstrong. You can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

View file

@ -0,0 +1,36 @@
#!/bin/sh
#
# 2016 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
chroot="/$__object_id"
if [ -f "$__object/parameter/manage-resolv-conf" ]; then
suffix="$(cat "$__object/parameter/manage-resolv-conf")"
resolv_conf="${chroot}/etc/resolv.conf"
original_resolv_conf="${resolv_conf}.${suffix}"
cat << DONE
$__remote_exec $__target_host << EOSSH
if [ -f "${original_resolv_conf}" ]; then
# restore original /etc/resolv.conf that we moved out of the way
# in __chroot_mount/gencode-local
mv -f "${original_resolv_conf}" "${resolv_conf}"
fi
EOSSH
DONE
fi

View file

@ -0,0 +1 @@
manage-resolv-conf

View file

@ -0,0 +1,100 @@
#!/bin/sh
#
# 2010-2011 Nico Schottelius (nico-cdist at schottelius.org)
# 2014 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
# All os variables are lower case. Keep this file in alphabetical
# order by os variable except in cases where order otherwise matters,
# in which case keep the primary os and its derivatives together in
# a block (see Debian and Redhat examples below).
#
chroot="$(cat "$__object/parameter/chroot")"
if grep -q ^Amazon "$chroot/etc/system-release" 2>/dev/null; then
echo amazon
exit 0
fi
if [ -f "$chroot/etc/arch-release" ]; then
echo archlinux
exit 0
fi
if [ -f "$chroot/etc/cdist-preos" ]; then
echo cdist-preos
exit 0
fi
### Debian and derivatives
if grep -q ^DISTRIB_ID=Ubuntu "$chroot/etc/lsb-release" 2>/dev/null; then
echo ubuntu
exit 0
fi
if [ -f "$chroot/etc/debian_version" ]; then
echo debian
exit 0
fi
###
if [ -f "$chroot/etc/gentoo-release" ]; then
echo gentoo
exit 0
fi
if [ -f "$chroot/etc/openwrt_version" ]; then
echo openwrt
exit 0
fi
if [ -f "$chroot/etc/owl-release" ]; then
echo owl
exit 0
fi
### Redhat and derivatives
if grep -q ^CentOS "$chroot/etc/redhat-release" 2>/dev/null; then
echo centos
exit 0
fi
if grep -q ^Fedora "$chroot/etc/redhat-release" 2>/dev/null; then
echo fedora
exit 0
fi
if [ -f "$chroot/etc/redhat-release" ]; then
echo redhat
exit 0
fi
###
if [ -f "$chroot/etc/SuSE-release" ]; then
echo suse
exit 0
fi
if [ -f "$chroot/etc/slackware-version" ]; then
echo slackware
exit 0
fi
echo "Unknown OS" >&2
exit 1

View file

@ -1,6 +1,6 @@
#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2011-2015 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
@ -18,51 +18,74 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
device="$(cat "$__object/parameter/device")"
device="$(cat "$__object/parameter/device" 2>/dev/null || echo "/$__object_id")"
chroot="$(cat "$__object/parameter/chroot")"
target_os=$(cat "$__object/explorer/target_os")
mkdir "$__object/files"
install_script="$__object/files/install_script"
# Link file descriptor #6 with stdout
exec 6>&1
# Link stdout with $install_script
exec > $install_script
# Generate script to install bootloader on distro
printf '#!/bin/sh -l\n'
case "$target_os" in
ubuntu|debian)
if [ -s "$__global/explorer/efi" ]; then
# FIXME: untested. maybe also just run update-grub for EFI system?
printf 'grub-mkconfig --output=/boot/efi/EFI/%s/grub.cfg\n' "$target_os"
printf 'mkdir -p /boot/efi/EFI/BOOT\n'
printf 'cp /boot/efi/EFI/%s/grubx64.efi /boot/efi/EFI/BOOT/bootx64.efi' "$target_os"
else
printf 'grub-install "%s"\n' "$device"
printf 'update-grub\n'
fi
;;
archlinux)
if [ -s "$__global/explorer/efi" ]; then
echo "EFI boot loader installation is on your operating system ($target_os) is currently not supported by this type (${__type##*/})." >&2
echo "Please contribute an implementation for it if you can." >&2
exit 1
else
printf 'grub-install "%s"\n' "$device"
# bugfix/workarround: rebuild initramfs
# FIXME: doesn't belong here
printf 'mkinitcpio -p linux\n'
printf 'grub-mkconfig -o /boot/grub/grub.cfg\n'
fi
;;
centos)
if [ -s "$__global/explorer/efi" ]; then
printf 'grub2-mkconfig --output=/boot/efi/EFI/%s/grub.cfg\n' "$target_os"
printf 'mkdir -p /boot/efi/EFI/BOOT\n'
printf 'cp /boot/efi/EFI/%s/grubx64.efi /boot/efi/EFI/BOOT/bootx64.efi' "$target_os"
else
printf 'grub2-install "%s"\n' "$device"
printf 'grub2-mkconfig --output=/boot/grub2/grub.cfg\n'
fi
;;
*)
echo "Your operating system ($target_os) is currently not supported by this type (${__type##*/})." >&2
echo "If you can, please contribute an implementation for it." >&2
exit 1
;;
esac
# Restore stdout and close file descriptor #6.
exec 1>&6 6>&-
cat << DONE
os=\$(
if grep -q ^DISTRIB_ID=Ubuntu ${chroot}/etc/lsb-release 2>/dev/null; then
echo ubuntu
exit 0
fi
if [ -f ${chroot}/etc/arch-release ]; then
echo archlinux
exit 0
fi
if [ -f ${chroot}/etc/debian_version ]; then
echo debian
exit 0
fi
)
# Ensure /tmp exists
[ -d "${chroot}/tmp" ] || mkdir -m 1777 "${chroot}/tmp"
# Generate script to run in chroot
script=\$(mktemp "${chroot}/tmp/__install_bootloader_grub.XXXXXXXXXX")
# Link file descriptor #6 with stdout
exec 6>&1
# Link stdout with \$script
exec > \$script
echo "#!/bin/sh -l"
echo "grub-install $device"
case \$os in
archlinux)
# bugfix/workarround: rebuild initramfs
# FIXME: doesn't belong here
echo "mkinitcpio -p linux"
echo "grub-mkconfig -o /boot/grub/grub.cfg"
;;
ubuntu|debian) echo "update-grub" ;;
esac
# Restore stdout and close file descriptor #6.
exec 1>&6 6>&-
script=\$(mktemp "${chroot}/tmp/${__type##*/}.XXXXXXXXXX")
cat > \$script << script_DONE
$(cat "$install_script")
script_DONE
# Make script executable
chmod +x "\$script"
@ -70,4 +93,5 @@ chmod +x "\$script"
# Run script in chroot
relative_script="\${script#$chroot}"
chroot "$chroot" "\$relative_script"
rm -rf \$script
DONE

View file

@ -0,0 +1 @@
/target

View file

@ -0,0 +1 @@
../__chroot_mount/parameter

View file

@ -0,0 +1 @@
../__chroot_umount/parameter

View file

@ -25,7 +25,7 @@
#
log() {
echo "$@" | logger -t "__install_config copy"
#echo "$@" | logger -t "__install_config copy"
:
}

View file

@ -25,7 +25,7 @@
#
log() {
echo "$@" | logger -t "__install_config exec"
#echo "$@" | logger -t "__install_config exec"
:
}
@ -35,7 +35,10 @@ target_host="$__target_host"
shift
ssh="ssh -o User=root -q $target_host"
code="$ssh chroot $chroot sh -c '$@'"
# escape ' with '"'"'
code="$(echo "$@" | sed -e "s/'/'\"'\"'/g")"
code="$ssh chroot $chroot sh -c '$code'"
log "target_host: $target_host"
log "chroot: $chroot"

View file

@ -1,6 +1,6 @@
#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2011-2017 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
@ -22,29 +22,15 @@ chroot="$(cat "$__object/parameter/chroot")"
remote_exec="$__type/files/remote/exec"
remote_copy="$__type/files/remote/copy"
cdist_args="-v"
[ "$__debug" = "yes" ] && cdist_args="$cdist_args -d"
cdist_args=""
[ "$__verbose" = "yes" ] && cdist_args="-vv"
[ "$__debug" = "yes" ] && cdist_args="-d"
cat << DONE
#echo "__apt_noautostart --state present" \
# | cdist $cdist_args \
# config \
# --initial-manifest - \
# --remote-exec="$remote_exec $chroot" \
# --remote-copy="$remote_copy $chroot" \
# $__target_host
cdist $cdist_args \
config \
cdist config \
$cdist_args \
--remote-exec="$remote_exec $chroot" \
--remote-copy="$remote_copy $chroot" \
$__target_host
#echo "__apt_noautostart --state absent" \
# | cdist $cdist_args \
# config \
# --initial-manifest - \
# --remote-exec="$remote_exec $chroot" \
# --remote-copy="$remote_copy $chroot" \
# $__target_host
DONE

View file

@ -0,0 +1 @@
../__directory/explorer

View file

@ -0,0 +1 @@
../__directory/gencode-remote

View file

@ -0,0 +1 @@
../__directory/man.rst

View file

@ -0,0 +1 @@
../__directory/parameter

View file

@ -25,7 +25,7 @@ mkdir "$__object/files"
# get current UUID's from target_host
$__remote_exec $__target_host blkid > "$__object/files/blkid"
for object in $(find "$__global/object/__install_mount" -path "*.cdist"); do
for object in $(find "$__global/object/__install_mount" -type d -name "$__cdist_object_marker"); do
device="$(cat "$object/parameter/device")"
dir="$(cat "$object/parameter/dir")"
prefix="$(cat "$object/parameter/prefix")"
@ -44,6 +44,11 @@ for object in $(find "$__global/object/__install_mount" -path "*.cdist"); do
tmpfs)
pass=0
;;
bind)
pass=0
type=none
options="bind,$options"
;;
*)
pass=1
;;

View file

@ -24,7 +24,7 @@ type="$(cat "$__object/parameter/type")"
case "$type" in
swap)
echo "mkswap $device"
echo "mkswap -f $device"
exit 0
;;
xfs)

View file

@ -20,7 +20,7 @@
get_type_from_mkfs() {
_device="$1"
for mkfs_object in $(find "$__global/object/__install_mkfs" -path "*.cdist"); do
for mkfs_object in $(find "$__global/object/__install_mkfs" -type d -name "$__cdist_object_marker"); do
mkfs_device="$(cat "$mkfs_object/parameter/device")"
if [ "$_device" = "$mkfs_device" ]; then
cat "$mkfs_object/parameter/type"
@ -42,18 +42,25 @@ else
# store for later use by others
echo "$type" > "$__object/parameter/type"
fi
[ -n "$type" ] || die "Can't determine type for $__object"
[ -n "$type" ] || {
echo "Can't determine type for $__object" >&2
exit 1
}
if [ "$type" = "swap" ]; then
echo "swapon \"$device\""
printf 'swapon "%s"\n' "$device"
else
if [ -f "$__object/parameter/options" ]; then
options="$(cat "$__object/parameter/options")"
else
options=""
fi
[ -n "$options" ] && options="-o $options"
mount_point="${prefix}${dir}"
echo "[ -d \"$mount_point\" ] || mkdir -p \"$mount_point\""
echo "mount -t \"$type\" $options \"$device\" \"$mount_point\""
printf '[ -d "%s" ] || mkdir -p "%s"\n' "$mount_point" "$mount_point"
printf 'mount'
if [ "$type" = "bind" ]; then
printf ' --bind'
device="${prefix}${device}"
else
printf ' -t "%s"' "$type"
fi
if [ -f "$__object/parameter/options" ]; then
printf ' -o %s' "$(cat "$__object/parameter/options")"
fi
printf ' "%s"' "$device"
printf ' "%s"\n' "$mount_point"
fi

View file

@ -19,6 +19,12 @@ type
OPTIONAL PARAMETERS
-------------------
device
the device we're working on. Defaults to the string prefix of --partition
minor
the partition number we're working on. Defaults to the numeric suffix of --partition
partition
defaults to object_id
@ -49,6 +55,8 @@ EXAMPLES
__install_partition_msdos /dev/sda6 --type 83 --size 50%
# rest of the extended partition, linux
__install_partition_msdos /dev/sda7 --type 83 --size +
# nvm device partition 2
__install_partition_msdos /dev/nvme0n1p2 --device /dev/nvme0n1 --minor 2 --type 83 --size 128M --bootable true
AUTHORS
@ -58,7 +66,7 @@ Steven Armstrong <steven-cdist--@--armstrong.cc>
COPYING
-------
Copyright \(C) 2011 Steven Armstrong. You can redistribute it
Copyright \(C) 2011-2017 Steven Armstrong. You can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

View file

@ -25,10 +25,15 @@ else
partition="/$__object_id"
echo "$partition" > "$__object/parameter/partition"
fi
device="$(echo "$partition" | sed 's/[0-9]//g')"
echo "$device" > "$__object/parameter/device"
minor="$(echo "$partition" | sed 's/[^0-9]//g')"
echo "$minor" > "$__object/parameter/minor"
if [ ! -f "$__object/parameter/device" ]; then
device="$(echo "$partition" | sed 's/[0-9]//g')"
echo "$device" > "$__object/parameter/device"
fi
if [ ! -f "$__object/parameter/minor" ]; then
minor="$(echo "$partition" | sed 's/[^0-9]//g')"
echo "$minor" > "$__object/parameter/minor"
fi
if [ ! -f "$__object/parameter/bootable" ]; then
echo "false" > "$__object/parameter/bootable"

View file

@ -1,3 +1,5 @@
device
minor
partition
bootable
size

View file

@ -18,6 +18,8 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#set -x
die() {
echo "[__install_partition_msdos_apply] $@" >&2
exit 1
@ -57,7 +59,7 @@ size_to_mb() {
get_objects() {
objects_file=$(mktemp)
for object in $(find "$__global/object/__install_partition_msdos" -path "*.cdist"); do
for object in $(find "$__global/object/__install_partition_msdos" -type d -name "$__cdist_object_marker"); do
object_device="$(cat "$object/parameter/device")"
object_minor="$(cat "$object/parameter/minor")"
echo "$object_device $object_minor $object" >> $objects_file

View file

@ -20,4 +20,13 @@
options="$(cat "$__object/parameter/options")"
echo "reboot $options"
#echo "reboot $options"
cat << DONE
echo 1 > /proc/sys/kernel/sysrq
echo s > /proc/sysrq-trigger
# close file descriptors to detach from ssh
sh -c 'sleep 3; echo b > /proc/sysrq-trigger' > /dev/null 2>&1 </dev/null &
exit
DONE

View file

@ -22,38 +22,44 @@ disk="/$__object_id"
disk_name="${disk##*/}"
cat << DONE
# stop lvm's if any
if find /sys/class/block/$disk_name*/holders/ -mindepth 1 | grep -q holders/dm; then
if command -v vgchange >/dev/null; then
vgchange -a n
else
echo "WARNING: vgchange command not found" >&2
debug() {
echo "[DEBUG] \$@" >&2
}
find_md_device_names() {
local disk_name="\$1"
for slave in \$(find /sys/devices/virtual/block/*/slaves/ -name "\${disk_name}*"); do
debug "slave: \$slave"
for holder in \$slave/holders/*; do
debug "holder: \$holder"
if [ -d "\$holder/md" ]; then
debug "mdadm found at \$holder"
holder_name="\${holder##*/}"
echo "\$holder_name"
fi
done
done
}
# disable any enabled volume group
if command -v vgchange >/dev/null; then
vgchange -a n
else
echo "WARNING: vgchange command not found" >&2
fi
# stop mdadm raids if any
if [ -r /proc/mdstat ]; then
md_name="\$(awk "/$disk_name/ {print \$1}" /proc/mdstat)"
if [ -n "\$md_name" ]; then
# disable any running mdadm arrays related to $disk
for md_name in \$(find_md_device_names "$disk_name" | sort | uniq); do
echo "md_name: \$md_name"
if command -v mdadm >/dev/null; then
mdadm --stop "/dev/\$md_name"
else
echo "WARNING: mdadm command not found" >&2
echo "WARNING: could not stop active mdadm raid for disk $disk" >&2
fi
fi
fi
done
if command -v pvremove >/dev/null; then
pvremove --force --force --yes "$disk" || true
else
echo "WARNING: pvremove command not found" >&2
fi
if command -v mdadm >/dev/null; then
mdadm --zero-superblock --force "$disk" || true
else
echo "WARNING: mdadm command not found" >&2
fi
# clean disks from any legacy signatures
if command -v wipefs >/dev/null; then
wipefs -a "$disk" || true