#!/bin/ash # Copyright (c) 2013 - OpenSLX GmbH # # This program is free software distributed under the GPL version 2. # See http://openslx.org/COPYING # # If you have any feedback please consult http://openslx.org/feedback and # send your feedback to feedback@openslx.org # # General information about OpenSLX can be found under http://openslx.org # # Local hard disk autodetection script for OpenSLX linux stateless clients, # detecting swap and special partitions ############################################################################# # Mount point for persistent scratch partition (type 45) MOUNT_POINT_45="/opt/openslx/persistent" PARTITION_FILE="/run/openslx/partitions" readonly MOUNT_POINT_45 PARTITION_FILE mkdir -p "/run/openslx" # General formatter for the /tmp partition on a local harddisk format_disk () { MOUNT_OPTIONS_SET_BY_FORMAT_DISK="" # Global var! local target="$1" local fslist="xfs jfs ext3 ext2 ext4" local fs local path [ $# -ge 2 ] && fslist="$2" for fs in $fslist ; do if grep -q "\\b${fs}\\b" "/proc/filesystems"; then # Filesystem already supported by running kernel : elif modprobe "${fs}"; then # Filesystem module could be loaded and should be supported now : else # Not supported, try next one continue fi if which "mkfs.$fs" ; then case "$fs" in xfs) fopt="-f -b size=4k -s size=4k -l size=512b" # fastest formatting possible :) MOUNT_OPTIONS_SET_BY_FORMAT_DISK="-o noexec" ;; ext2) fopt="-Fq" MOUNT_OPTIONS_SET_BY_FORMAT_DISK="-o nocheck,noexec" ;; ext3|ext4) fopt="-Fq" MOUNT_OPTIONS_SET_BY_FORMAT_DISK="-o noexec" ;; reiserfs) fopt="-f" MOUNT_OPTIONS_SET_BY_FORMAT_DISK="-o noexec" ;; jfs) fopt="-q" MOUNT_OPTIONS_SET_BY_FORMAT_DISK="-o noexec" ;; *) fopt= MOUNT_OPTIONS_SET_BY_FORMAT_DISK= ;; esac mkfs.$fs ${fopt} "${target}" && return 0 # Success! fi done return 1 } mount_temp () { local PRE=$(pwd) if ! cd /tmp; then mount_temp_fallback $@ return $? fi mount $@ /tmp || return 1 chmod a+rwxt /tmp # Move stuff from working directory, which is old /tmp, to new /tmp just mounted mv ./* ./.[!.]* ./..?* /tmp/ 2> /dev/null local OLD=$(LANG=C ls -alh | grep -v -E ' \.\.?$' | grep -v '^total') [ -n "$OLD" ] && echo -- "Leftovers:" && echo -- "$OLD" cd "$PRE" } mount_temp_fallback () { mkdir -p /tmptmp mv /tmp/* /tmp/.* /tmptmp/ 2> /dev/null mount $@ /tmp || return 1 chmod a+rwxt /tmp mv /tmptmp/* /tmptmp/.* /tmp/ rmdir /tmptmp return 0 } fdisk -l | grep '^/dev/' > "$PARTITION_FILE" if [ ! -s "$PARTITION_FILE" ]; then udevadm trigger sleep 3 udevadm settle fdisk -l | grep '^/dev/' > "$PARTITION_FILE" fi echo "Partitions:" cat "$PARTITION_FILE" # Get all partitions with given id (list of /dev/sdXX) get_all_with_id () { [ -z "$1" ] && return local ID=$1 awk '{if (($2 == "*" && $6 == "'$ID'") || ($2 != "*" && $5 == "'$ID'")) print $1}' "$PARTITION_FILE" # watch out for non-spaced '$ID' } # Check for standard swap partitions and make them available to the system HAVE_SWAP=no for PART_DEV in $(get_all_with_id 82); do echo -e "$PART_DEV\tswap\t\tswap\t\tdefaults\t 0 0" >> "/etc/fstab" swapon "$PART_DEV" -p 10 && HAVE_SWAP=yes # low priority, in case we have zram swap, prefer that) done # We use special non assigned partition type (id44) for harddisk scratch # space, thus no normal filesystem will be incidentally deleted or # corrupted HAVE_TEMP=no for PART_DEV in $(get_all_with_id 44); do # check for supported filesystem and formatter if format_disk "$PART_DEV"; then # echo "$PART_DEV is mounted to /mnt/tmp at $(sysup)" >/tmp/tmpready mount_temp "$MOUNT_OPTIONS_SET_BY_FORMAT_DISK" "$PART_DEV" || continue echo -e "${PART_DEV}\t/tmp\t\tauto\t\tnoexec\t 0 0" >> "/etc/fstab" HAVE_TEMP=yes break else echo "formatting failed for some reason" fi # Made this non-forking, systemd should handle it - 2013-05-28 done # Put detected linux partitions (83) into /etc/fstab with "noauto" HAVE_PARTITION_45=no for PART_DEV in $(get_all_with_id 83); do mkdir -p "/media/${PART_DEV#/dev/*}" echo -e "${PART_DEV}\t/media/${PART_DEV#/dev/*}\tauto\t\tnoauto,noexec\t 0 0" >> "/etc/fstab" done # special partition 45 (persistent scratch) to $MOUNT_POINT_45 for PART_DEV in $(get_all_with_id 45); do mkdir -p "$MOUNT_POINT_45" if ! mount -t auto -o noexec "${PART_DEV}" "$MOUNT_POINT_45"; then format_disk "$PART_DEV" "ext4 xfs jfs ext3" || continue mount -t auto -o noexec "${PART_DEV}" "$MOUNT_POINT_45" || continue fi echo -e "${PART_DEV}\t${MOUNT_POINT_45}\tauto\t\tnoauto,noexec\t\t 0 0" >> "/etc/fstab" HAVE_PARTITION_45=yes break done # and 46 to /media/devXX for PART_DEV in $(get_all_with_id 46); do mkdir -p "/media/${PART_DEV#/dev/*}" #mount -t auto ${PART_DEV} /mnt/media/${PART_DEV#/dev/*} \n\ #test -d /mnt/media/${PART_DEV#/dev/*}/home && \ # ln -sf /media/${PART_DEV#/dev/*} /var/home echo -e "${PART_DEV}\t/media/${PART_DEV#/dev/*}\tauto\t\tnoauto\t\t 0 0" >> "/etc/fstab" done if [ "$HAVE_PARTITION_45" = "no" ] && [ -d "$MOUNT_POINT_45" ]; then rm -f -- "$MOUNT_POINT_45" fi mount -a # Make tmpfs if nothing could be mounted for /tmp # 2016-10-12: Use a sane size of 66% which should be generous enough and prevent the machine from # just crashing if RAM is too full. We previously hugely oversized since vmware wants at least as # much free space as the VMs RAM; however, this requirement can be disabled with a vmx setting, # which we're now doing. if [ "$HAVE_TEMP" = "no" ]; then mount_temp -t tmpfs -o size=66% none slxlog "partition-temp" "Running /tmp on tmpfs only!" "$PARTITION_FILE" fi if [ "$HAVE_SWAP" = "no" ]; then TOTAL_RAM=$(grep ^MemTotal /proc/meminfo | awk '{print $2}') if [ -n "$TOTAL_RAM" ] && [ "$TOTAL_RAM" -lt "3000000" ]; then slxlog "partition-swap" "Have no (formatted) swap partition, using zram swap only!" "$PARTITION_FILE" fi fi exit 0