summaryrefslogtreecommitdiffstats
path: root/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_partitions
blob: b282a48629221e9131b0dc6ff3479e6f5c89eb85 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/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

#############################################################################

# 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
				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_partition() {
	local mountpoint="$1"
	local partition="$2"
	local options="$3"
	[[ $options == *persistent* ]] && local persistent="1"

	mkdir -p "$mountpoint"
	if ! mount -t auto -o noexec "$partition" "$mountpoint" ; then
		if [[ $persistent == 1 ]]; then
			fsck "$partition" || return $?
			mount -t auto -o noexec "$partition" "$mountpoint" || return $?
		else
			diskfm "$partition" "jfs xfs ext3" || return $?
			mount -t auto -o noexec "$partition" "$mountpoint" || return $?
		fi
	fi
	echo -e "${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 -e $LINE >> "/etc/disk.partition"
			done
		fi
	done

	echo "Partitions:"
	cat "/etc/disk.partition"
}

# 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,persistent'
fi

for PARTITION in $SLX_PARTITION_TABLE; do
	IFS=,
	set $PARTITION
	id=$1; shift
	size=$1; shift
	[ -e $1 ] && mountpoint="$1"; shift
	[ $# -gt 0 ] && options="$*"

	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
				if diskfm "$tmppart"; then
					# echo "$tmppart is mounted to /mnt/tmp at $(sysup)" >/tmp/tmpready
					mount_temp "$mopt" "$tmppart" || continue
					echo -e "${tmppart}\t/tmp\t\tauto\t\tnoexec\t 0 0" >> "/etc/fstab"
					HAVE_TEMP="yes"
				else
					echo "formatting 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 -e "${openslxpart}\t/media/${openslxpart#/dev/*}\tauto\t\tnoauto\t\t 0 0" >> "/etc/fstab"
			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"
				swapon "$swppart" -p 10 && HAVE_SWAP=yes # low priority, in case we have zram swap, prefer that)
			done
			;;
		*)
			for hdpartnr in $(grep -e "${id}$\|${id}000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do
				mount_part "${mountpoint}" $hdpartnr $options
				if [ $? -ne 0 ]; then
					echo "Mount of partition $hdpartnr on ${mountpoint} failed with exit code: $?"
					[ -d "${mountpoint}" ] && rm -r "${mountpoint}"
				fi
			done
			;;
	esac

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 -e "${linuxpart}\t${mountpoint}/${linuxpart#/dev/*}\tauto\t\tnoauto,noexec\t 0 0" >> "/etc/fstab"
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