#!/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 ############################################################################# # read global OpenSLX config . /opt/openslx/config || { echo "Could not source config!"; exit 23; } # General formatter for the /tmp partition on a local harddisk diskfm () { mopt="" # 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 unset available case $(cat /proc/filesystems) in *${fs}*) available=yes;; *) modprobe "${fs}" && available=yes;; esac if [ -n "${available}" ]; then unset found if which "mkfs.$fs" ; then found=yes case "mkfs.$fs" in mkfs.xfs) fopt="-f -b size=4k -s size=4k -l size=512b" # fastest formatting possible :) mopt="-o noexec" ;; mkfs.ext2) fopt="-Fq" mopt="-o nocheck,noexec" ;; mkfs.ext3|mkfs.ext4) fopt="-Fq" mopt="-o noexec" ;; mkfs.reiserfs) fopt="-f" mopt="-o noexec" ;; mkfs.jfs) fopt="-q" mopt="-o noexec" ;; esac echo "formatting ${target} with $fs..." mkfs.$fs ${fopt} "${target}" > /dev/null 2>&1 fi [ -n "$found" ] && break fi done } 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/ > /dev/null 2>&1 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/ > /dev/null 2>&1 mount $@ /tmp || return 1 chmod a+rwxt /tmp mv /tmptmp/* /tmptmp/.* /tmp/ rmdir /tmptmp return 0 } mount_partition () { local mountpoint="$1" local partition="$2" mkdir -p "$mountpoint" if ! blkid -s TYPE | grep "${partition}" ; then echo "No fs found for ${partition}, formating..." diskfm "$partition" "jfs xfs ext3" || return $? mount -t auto -o noexec "$partition" "$mountpoint" || return $? else if ! mount -t auto -o noexec "$partition" "$mountpoint" ; then echo "Mount of $partition on $mountpoint failed with code $?, trying fsck..." fsck "$partition" || return $? mount -t auto -o noexec "$partition" "$mountpoint" || return $? fi fi echo "${partition}\t${mountpoint}\tauto\t\tnoauto,noexec\t\t 0 0" >> "/etc/fstab" } read_partitions () { # create the /etc/disk.partition file with all the partitions and respective id's (MSDOS and GPT) echo "PARTITION - ID" > "/etc/disk.partition" for hd in $(cat /proc/partitions | tr -s ' ' | cut -d ' ' -f5 | grep -o -e "[a-z]*$"); do echo -n "$hd (" >> "/etc/disk.partition" sfdisk -d /dev/$hd 2>&1 | grep 'GPT' > /dev/null if [ $? -eq 1 ]; then echo "MSDOS)" >> "/etc/disk.partition" fdisk /dev/$hd -l | sed -n "/^\/dev\//p" | tr -d '*' | tr -s ' ' | cut -d ' ' -f1,5 >> "/etc/disk.partition" else echo "GPT)" >> "/etc/disk.partition" for part in $(cat /proc/partitions | tr -s ' ' | cut -d ' ' -f5 | grep -o -e "$hd[0-9][0-9]*$"); do LINE="/dev/$part " LINE=${LINE}$(sgdisk /dev/$hd -i ${part:3} | grep 'GUID code' | cut -d ' ' -f4) echo $LINE >> "/etc/disk.partition" done fi done echo "Partitions:" cat "/etc/disk.partition" echo "------------------------------------------------------" } # default partitions, if not specifies in config (note: size is irrelevant for setup_partitions) if [ -z "${SLX_PARTITION_TABLE}" ]; then SLX_PARTITION_TABLE=' 44,10G,/tmp 45,10G,/var/scratch 82,4G' fi read_partitions for PARTITION in $SLX_PARTITION_TABLE; do IFS=, set $PARTITION id=$1 shift size=$1 shift mountpoint="$1" shift options="$*" unset IFS case $id in 44) HAVE_TEMP="no" TMP_SIZE=$size for tmppart in $(grep -e "44$\|44000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do echo "tmp partition found, formatting and mounting..." if diskfm "$tmppart"; then # echo "$tmppart is mounted to /mnt/tmp at $(sysup)" >/tmp/tmpready mount_temp "$mopt" "$tmppart" || continue echo "${tmppart}\t/tmp\t\tauto\t\tnoexec\t 0 0" >> "/etc/fstab" HAVE_TEMP="yes" echo "$tmppart mounted on /tmp" else echo "formatting tmp partition failed for some reason" fi # Made this non-forking, systemd should handle it - 2013-05-28 done ;; 46) for openslxpart in $(grep -e "46$\|46000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do mkdir -p "/media/${openslxpart#/dev/*}" #mount -t auto ${openslxpart} /mnt/media/${openslxpart#/dev/*} \n\ #test -d /mnt/media/${openslxpart#/dev/*}/home && \ #ln -sf /media/${openslxpart#/dev/*} /var/home echo "${openslxpart}\t/media/${openslxpart#/dev/*}\tauto\t\tnoauto\t\t 0 0" >> "/etc/fstab" echo "special partition ${openslxpart} mounted on /media/${openslxpart#/dev/*}" done ;; 82) # Check for standard swap partitions and make them available to the system HAVE_SWAP=no for swppart in $(grep -e "82$\|0657FD6D-A4AB-43C4-84E5-0933C84B4F4F$" /etc/disk.partition | cut -d ' ' -f1); do echo -e "$swppart\tswap\t\tswap\t\tdefaults\t 0 0" >> "/etc/fstab" mkswap "$swppart" && swapon "$swppart" -p 10 && HAVE_SWAP=yes # low priority, in case we have zram swap, prefer that) [ $HAVE_SWAP = "yes" ] && echo "swap partition found and activated" done ;; *) for hdpartnr in $(grep -e "${id}$\|${id}000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do mount_partition "${mountpoint}" $hdpartnr mount_status=$? if [ $mount_status -ne 0 ]; then echo "Mount of partition $hdpartnr on ${mountpoint} failed with exit code: $mount_status" [ -d "${mountpoint}" ] && rm -r "${mountpoint}" else echo "special partition $hdpartnr mounted on ${mountpoint}" fi done ;; esac echo "------------------------------------------------------" done #Put detected linux partitions (83) into /etc/fstab with "noauto" for linuxpart in $(grep -e "83$\|0FC63DAF-8483-4772-8E79-3D69D8477DE4$" /etc/disk.partition | cut -d ' ' -f1); do mountpoint="/media" mkdir -p "${mountpoint}/${linuxpart#/dev/*}" echo "${linuxpart}\t${mountpoint}/${linuxpart#/dev/*}\tauto\t\tnoauto,noexec\t 0 0" >> "/etc/fstab" echo "linux partition ${linuxpart} mounted on ${mountpoint}/${linuxpart#/dev/*}" echo "------------------------------------------------------" done mount -a # Make huge tmpfs if nothing could be mounted for /tmp if [ "$HAVE_TEMP" = "no" ]; then mount_temp -t tmpfs -o size=${TMP_SIZE} none slxlog "partition-temp" "Running /tmp on tmpfs only!" "/etc/disk.partition" fi if [ "$HAVE_SWAP" = "no" ]; then slxlog "partition-swap" "Have no (formatted) swap partition, using zram swap only!" "/etc/disk.partition" fi exit 0