summaryrefslogtreecommitdiffstats
path: root/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_partitions
blob: 748e017d967a836f391e1c5b369e656bc1a8bc84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
# 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

#############################################################################
CONFIGPATH="./config"
. $CONFIGPATH

# 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
				mkfs.$fs ${fopt} "${target}"
			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/ 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
}

mount_part() { # $1=mountpoint, $2=partition(/dev/*)
	mkdir -p "$1"
	if ! mount -t auto -o noexec "${2}" "$1" 2> /dev/null ; then
		diskfm "$2" "jfs xfs ext3" || continue
		mount -t auto -o noexec "${2}" "$1" || continue
	fi
	echo -e "${2}\t${1}\tauto\t\tnoauto,noexec\t\t 0 0" >> "/etc/fstab"
}

# default case
if [ -z "$SLX_PARTITION_TABLE" ]; then
	SLX_PARTITION_TABLE='			
	44,10G,/tmp
	45,10G,/var/scratch'
fi

declare -A PARTTBL
COUNTER=1
for PARTITION in $SLX_PARTITION_TABLE; do
	IFS=,
	set $PARTITION
	PARTTBL["$COUNTER/id"]=$1
	PARTTBL["$COUNTER/size"]=$(echo $2 | egrep -o "[0-9]*")
	PARTTBL["$COUNTER/mountpoint"]=$3
	if [ -e $4 ]; then
	  PARTTBL["$COUNTER/bootable"]=0
	else
	  PARTTBL["$COUNTER/bootable"]=1
	fi
	PARTTBL["$COUNTER/persistent"]="no"
	COUNTER=$(($COUNTER+1))
done
unset IFS


# 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 -e $LINE >> "/etc/disk.partition"
		done
	fi
done

echo "Partitions:"
cat "/etc/disk.partition"
# 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"
	swapon "$swppart" -p 10 && HAVE_SWAP=yes # low priority, in case we have zram swap, prefer that)
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
	mkdir -p "/media/${linuxpart#/dev/*}"
	echo -e "${linuxpart}\t/media/${linuxpart#/dev/*}\tauto\t\tnoauto,noexec\t 0 0" >> "/etc/fstab"
done

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 -e "${openslxpart}\t/media/${openslxpart#/dev/*}\tauto\t\tnoauto\t\t 0 0" >> "/etc/fstab"
done

HAVE_TEMP="no"
for (( i = 1; i < $COUNTER; i++ )); do
	for hdpartnr in $(grep -e ${PARTTBL["$i/id"]}"$\|"${PARTTBL["$i/id"]}"000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do
		if [ "${PARTTBL["$i/mountpoint"]}" = "/tmp" ]; then
			if diskfm "$hdpartnr"; then
				# echo "$hdpartnr is mounted to /mnt/tmp at $(sysup)" >/tmp/tmpready
				mount_temp "$mopt" "$hdpartnr" || continue
				echo -e "${hdpartnr}\t/tmp\t\tauto\t\tnoexec\t 0 0" >> "/etc/fstab"
				HAVE_TEMP="yes"
				PARTTBL["$i/persistent"]="yes"
			else
				echo "formatting failed for some reason"
			fi # Made this non-forking, systemd should handle it - 2013-05-28
		else
			mount_part "${PARTTBL["$i/mountpoint"]}" $hdpartnr
			PARTTBL["$i/persistent"]="yes"
		fi
	done
	[ "${PARTTBL["$i/persistent"]}" = "no" -a -d "${PARTTBL["$i/mountpoint"]}" ] && rm -f "${PARTTBL["$i/mountpoint"]}"
done

mount -a
# Make huge tmpfs if nothing could be mounted for /tmp
if [ "$HAVE_TEMP" = "no" ]; then
	 mount_temp -t tmpfs -o size=20G 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