File: crypto

package info (click to toggle)
partman-crypto 135
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,272 kB
  • sloc: sh: 1,874; ansic: 174; makefile: 29
file content (197 lines) | stat: -rwxr-xr-x 3,835 bytes parent folder | download
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
#!/bin/sh

# This script creates partman cryptdisks for the encrypted devices
# setup in choose_partition/crypto/do_option.

. /usr/lib/partman/lib/base.sh

dev_to_devdir () {
	echo $DEVICES/$(echo $1 | tr / =)
}

create_disk () {
	device=$1
	model=$2
	size=$3

	devdir=$(dev_to_devdir $device)
	mkdir $devdir || return 1
	cd $devdir

	echo $device > $devdir/device
	echo $model > $devdir/model
	echo $size > $devdir/size

	open_dialog OPEN $device
	read_line response
	close_dialog
	if [ "$response" = failed ]; then
		rm -rf $devdir
		return 1
	fi

	return 0
}

create_partition () {
	local id num size type fs path name free_space free_size filesystem
	filesystem=$2

	cd $(dev_to_devdir $1)

	open_dialog NEW_LABEL loop
	close_dialog

	# find the free space
	open_dialog PARTITIONS
	free_space=''
	while { read_line num id size type fs path name; [ "$id" ]; }; do
		case $fs in
		    free|unknown)
			free_space=$id
			free_size=$size
			free_fs=$fs
			# we can't break here
			;;
		esac
	done
	close_dialog

	# create partition in the free space
	if [ "$free_space" ]; then
		if [ "$free_fs" = unknown ]; then
			# parted >= 3.2 gives us a partition automatically.
			id=$free_space
		else
			# With parted < 3.2 we must create a partition
			# manually.
			open_dialog NEW_PARTITION primary $filesystem $free_space full $free_size
			read_line num id size type fs path name
			close_dialog
		fi
		if [ -z "$id" ]; then
			log "error: NEW_PARTITION returned no id"
			return
		fi
	fi
	open_dialog DISK_UNCHANGED
	close_dialog

	mkdir -p $id
	echo $id
}

create_cryptdisk () {
	local dev id num size path cryptdev cipher
	dev=$1
	id=$2
	num=$3
	size=$4
	path=$5

	cipher=$(cat $id/cipher)
	keytype=$(cat $id/keytype)

	templ="partman-crypto/text/cryptdev_description"
	db_subst $templ CIPHER $cipher
	db_subst $templ KEYTYPE $keytype
	db_metaget $templ description || RET=''
	model="$RET"
	if [ -z "$model" ]; then
		model="${cipher} ${keytype}"
	fi

	# Tell partman about the crypt disk
	cryptdev=$(cat $id/crypt_active)
	cryptdir=$(dev_to_devdir $cryptdev)
	if [ ! -d $cryptdir ]; then
		if ! create_disk $cryptdev "$model" $size; then
			return 2
		fi
	fi

	db_get partman/default_filesystem
	default_fs="$RET"

	case $keytype in
		random)
			filesystem=linux-swap
			;;
		keyfile|*)
			filesystem="$default_fs"
			;;
	esac

	# Create a new partition in there
	cryptid=$(create_partition $cryptdev $filesystem)
	if [ -z $cryptid ]; then
		return 3
	fi
	cryptpart=$cryptdir/$cryptid

	# Make sure partition hasn't been processed already
	if [ -e $cryptpart/method ]; then
		return 0
	fi

	# Select defaults
	case $filesystem in
		linux-swap)
			echo swap > $cryptpart/method
			>$cryptpart/format
			;;

		$default_fs)
			echo format > $cryptpart/method
			>$cryptpart/format
			>$cryptpart/use_filesystem
			echo $filesystem > $cryptpart/filesystem
			;;
	esac

	update_partition $cryptdir $cryptid

	echo $path:$num:$dev/$id > $cryptdir/crypt_realdev
	return 0
}

db_register partman-crypto/confirm partman-crypto/confirm_nooverwrite

for dev in /var/lib/partman/devices/*; do
	[ -d "$dev" ] || continue
	cd $dev
	partitions=
	open_dialog PARTITIONS
	while { read_line num id size type fs path name; [ "$id" ]; }; do
		if [ "$fs" != free ]; then
			partitions="$partitions $id:$num:$size:$path"
		fi
	done
	close_dialog

	for p in $partitions; do
		set -- $(IFS=: && echo $p)
		id=$1
		num=$2
		size=$3
		path=$4

		cd $dev

		[ -f $id/method ] || continue
		[ -f $id/crypto_type ] || continue
		[ -f $id/cipher ] || continue
		[ -f $id/crypt_active ] || continue

		method=$(cat $id/method)
		[ $method = crypto ] || continue

		if ! create_cryptdisk $dev $id $num $size $path; then
			db_fset partman-crypto/init_failed seen false
			db_input critical partman-crypto/init_failed
			db_go || true
			continue
		fi
	done
done