summaryrefslogtreecommitdiffstats
path: root/core/modules/gdisk/data/inc/prepare_localhd.functions
blob: 50ac0a70fff9185233e93031d79f6cc44d1b4f27 (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
#!/bin/ash
# -----------------------------------------------------------------------------
#
# Copyright (c) 2014..2018 bwLehrpool-Projektteam
#
# This program/file is free software distributed under the GPL version 2.
# See https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
#
# If you have any feedback please consult https://bwlehrpool.de and
# send your feedback to bwlehrpool@hs-offenburg.de.
#
# General information about bwLehrpool can be found at https://bwlehrpool.de
#
# Local hard disk autodetection script for OpenSLX linux stateless clients,
# detecting special GPT partitions

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

# Patition IDs
# Prefix for all paritions is 0FC63DAF-8483-4772-8E79-9999999999
# Suffix:
#  44: non-persistent scratch partition
#  45: persistent partiton
#  46: non-persistent openslx partition for config, overlayfs and qcow images

# We use special non assigned partition type for harddisk scratch
# space, thus no normal filesystem will be incidentally deleted or
# corrupted

# Set disks to none
ID44=
ID45=
ID46=

# General formatter for the /tmp partition on a local harddisk
diskfm () {
	mopt="" # Global var!
	local target="$1"
	local fslist="xfs jfs 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" >/dev/null; then
				found=yes
				case "mkfs.$fs" in
					mkfs.xfs)
						fopt="-fq"
					;;
					mkfs.jfs)
						fopt="-q"
					;;
					mkfs.ext4)
						fopt="-Fq"
					;;
				esac
				mkfs.$fs ${fopt} "${target}" 
			fi
			[ -n "$found" ] && break
		fi
	done
}

# function format_pid () 
# Formats partition with id 44, 45, 46
# expects id as parameter
# usage: format_pid [44|45|46]
format_pid () {
	local fmtpid=$1
	local target
	case $fmtpid in
		44)
			target=$ID44
		;;
		45)
			target=$ID45
		;;
		46)
			target=$ID46
		;;
	esac
	if echo $target | grep -q '/dev/disk/'
		then
		# check for supported filesystem and formatter
		diskfm $target
	else
		echo "Could not format partition ID $fmtpid"
	fi
}

# function mount_pid () 
# Mounts partition with id 44, 45, 46
# expects id and mountpoint as parameter
# usage: mount_pid [44|45|46] $MNT
mount_pid () {
	local mntpid=$1
	local target=$2
	local source
	if [ "x$2" = "x" ]
		then
		echo "No mountpoint specified dummy!"
	else
		case $mntpid in
			44)
				source=$ID44
			;;
			45)
				source=$ID45
			;;
			46)
				source=$ID46
			;;
		esac
		mkdir -p $target
		if busybox mount -t auto "$source" "$target"
			then
			[ "x$mntpid}" = "x44" ] && chmod a+rwxt $target
			if echo "$target" | grep -Eq "^mnt/|^/mnt/"
				then
				fstabtarget=$(echo $target | sed -re "s/[/]{0,1}mnt//")
				echo -e "$source\t$fstabtarget\tauto\tnoexec\t0 0" \
				  >>/mnt/etc/fstab
			fi
		else
			echo "Could not mount partition ID $mntpid"
		fi
	fi
}

# Get partition types
hdisks=$(ls /dev/disk/by-path/*-part[0-9]* \
         | sed -re "s,(.*)-part[0-9]*,\1," \
         | sort -u)

if echo $hdisks | grep -q '/dev/disk/'
	then
	for hd in $(echo $hdisks)
		do
		upartid=$(sgdisk -p $hd 2>/dev/null | awk '$6~/FFFF/ {print $1}')
		for upt in $(echo $upartid)
			do
			echo "${hd}-part${upt} $(sgdisk -i $upt $hd)" \
			  | awk '$5 ~ /0FC63DAF-8483-4772-8E79-[0]{10}4[4-6]/ \
			         {print $5 "=" $1}' \
			  | sed -re "s,0FC63DAF-8483-4772-8E79-[0]{10},ID," \
			  >> /etc/hdisks.conf
		done
	done
        if [ -r /etc/hdisks.conf ]
		then
		. /etc/hdisks.conf
		echo -e "Partitions found:\n$(cat /etc/hdisks.conf | cut -d'=' -f1)"
	else
		echo "No pratition IDs 44, 45 or 46 found"
	fi

fi