summaryrefslogtreecommitdiff
path: root/packages/altboot/files/altboot-menu/Advanced/80-copyrootfs
blob: 13f98a0d414aa5d5d4f8f3498e0cfad31ea82379 (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# !/bin/sh
M_TITLE="Copy rootfs to SD/CF"

# Unfinished script.
exit 0

die() {
	echo "ERROR: $1" >/dev/tty0
	exec $SH_SHELL </dev/tty0 >/dev/tty0 2>&1
}

ask_target() {
	available_disks="`mount | grep "/media" | grep -v ram | awk '{print $3}'`"
	
	if test -z "$available_disks" 
	then
		die "No mounted targets found!"
	fi
	
	cnt=1
	for d in $available_disks
	do
		echo -e "\t[$cnt] $d"
		let cnt=$cnt+1
	done

	while test -z "$ROOTFS_TARGET"
	do	
		echo -n "Target: "
		read junk </dev/tty1
		
		x=1
		for d in $available_disks
		do			
			if test "$junk" = "$x"
			then
				ROOTFS_TARGET="$d"				
				break
			fi
			let x=$x+1
		done
		
		
	done	
	
	ROOTFS_TARGET_DEV="`mount | grep "$ROOTFS_TARGET " | awk '{print $1}'`"
	ROOTFS_TARGET_FS="`mount | grep "$ROOTFS_TARGET " | awk '{print $5}'`"
	
	echo "Using [$ROOTFS_TARGET] on [$ROOTFS_TARGET_DEV] with [$ROOTFS_TARGET_FS] filesystem"				
}

ask_format() {
	if test "$ROOTFS_TARGET_FS" != ext2
	then
		echo -e "\nYou are not using the ext2 filesystem on your target ($ROOTFS_TARGET_FS)$.\nYou now have two choices:"
		echo -e "\t[1] Reformat to ext2"
		echo -e "\t[2] Use an image-file ontop of the existing filesystem"
		
		while true
		do
			echo -n "Your choice: "
			read junk </dev/tty1
			
			case "$junk" in
			1)	ROOTFS_TARGET_TYPE="ext2"
				break ;;
			2)	ROOTFS_TARGET_TYPE="image"
				break ;;
			esac
		done			
			
		echo "Mode: [$ROOTFS_TARGET_TYPE]"
	else
		echo -e "\nYou are using the ext2 filesystem on your target.\nYou now have two choices:"
		echo -e "\t[1] Install to the target directly"
		echo -e "\t[2] Use an image-file ontop of the existing filesystem\n"
		
		while true
		do
			echo -n "Your choice: "
			read junk </dev/tty1
			
			case "$junk" in
			1)	ROOTFS_TARGET_TYPE="direct"
				break ;;
			2)	ROOTFS_TARGET_TYPE="image"
				break ;;
			esac
		done			
			
		echo "Mode: [$ROOTFS_TARGET_TYPE]"
		
	fi
}

ask_confirm() {
	echo -e "\nYour choices are:"
	echo -e "\tTarget:\t$ROOTFS_TARGET_DEV (currently mounted as $ROOTFS_TARGET)"
	case "$ROOTFS_TARGET_TYPE" in
		direct)	echo -e "\tType:\t${C_RED}direct install, reformat if required${C_RESET}";;
		image) echo -e "\tType:\tinstall into image-file";;
	esac
	
	while true
	do
		echo -n "Continue? [y|n] "
		read junk </dev/tty1
		
		case "$junk" in
		y)	break ;;
		n)	exit 1
		esac
	done
}

direct_install() {

	#format_target
		
	echo -n "Creating temporary directory..."
	mkdir -p /media/temp && echo ok || die "mkdir -p /media/temp failed!"
	
	echo -n "Mounting [$ROOTFS_TARGET_DEV] as /media/temp..."
	mount "$ROOTFS_TARGET_DEV" /media/temp && echo "ok" || die "mount "$ROOTFS_TARGET_DEV" /media/temp FAILED"
	
	copy_files
}

format_target() {
	echo -e "\n\nI'm about to format your target ($ROOTFS_TARGET_DEV) to the ext2 filesystem\n"
	echo -e "${C_RED}YOU WILL LOSE ALL DATA ON YOUR TARGET IF YOU CONTINUE${C_RESET}\n"
	
	while true
	do
		echo -n "Continue? [y|n] "
		read junk </dev/tty1
		
		case "$junk" in
		y)	break ;;
		n)	die "User aborted mkfs"
			break ;;
		esac
	done
	
	echo -n "Umounting $ROOTFS_TARGET_DEV..."
	umount "$ROOTFS_TARGET_DEV" && echo "ok" || die "umount $ROOTFS_TARGET_DEV failed!"
	
	
	echo "Formatting..."
	/sbin/mkfs.ext2 -m0 "$ROOTFS_TARGET_DEV" && echo -e "\nmkfs.ext2 finished" || die "\nmkfs.ext2 FAILED"
 }

image_install() {
	flash_size="` df -h | grep "/"$| awk '{print $3}'| sed s/M//`"
	
	echo -e "\nHow many MB do you want to configure for the image file?"
	echo -e "It is generally a good idea to use 2.5x the used flash size ($flash_size)\n"
	
	while test -z "$ROOTFS_IMAGE_SIZE"
	do
		echo -n "Size in MegaBytes: "
		read junk
		
		# test fails if $junk isn't a number
		if test "$junk" -gt 0 >/dev/null 2>&1
		then
			echo -n "Use [$junk]MB? [y|n] "
			read junk2
			if test "$junk2" = y
			then
				ROOTFS_IMAGE_SIZE="$junk"
			fi
		fi
	done
	
	echo -e "\nPlease enter a name for the image file."
	echo "The name must _not_ contain whitespaces or the '-' sign."
	
	while test -z "$ROOTFS_IMAGE_NAME"
	do
		echo -n "File name: "
		read junk
		
		if ! test -z "$junk"
		then
			echo -n "Use [$junk] as image name? [y|n] "
			read junk2
			
			if test "$junk2" = y
			then
				ROOTFS_IMAGE_NAME="$junk"			
			fi
		fi
	done			
	
	echo -e "\nDo you want to format [$ROOTFS_TARGET_DEV] to the ext2 filesystem?"
	
	while true
	do
		echo -n "Format to ext2? [y|n] "
		read junk
		case "$junk" in
		y)	format_target
			break ;;
		n)	break ;;
		esac
	done
	
	/etc/init.d/devices start
	
	echo -en "\nCreating image file [$ROOTFS_TARGET/$IMAGE_PATH/$ROOTFS_IMAGE_NAME-rootfs.bin] (${ROOTFS_IMAGE_SIZE}MB)..."
	mkdir -p "$ROOTFS_TARGET/$IMAGE_PATH"
	
	dd if=/dev/zero of="$ROOTFS_TARGET/$IMAGE_PATH/$ROOTFS_IMAGE_NAME-rootfs.bin" bs=1024k count=$ROOTFS_IMAGE_SIZE	 >/dev/null 2>&1 && echo ok || die "FAILED"
	losetup /dev/loop1 "$ROOTFS_TARGET/$IMAGE_PATH/$ROOTFS_IMAGE_NAME-rootfs.bin"
	
	echo -n "Creating filesystem..."
	mkfs.ext2 -m0 /dev/loop1 >/dev/null 2>&1 && echo ok || die FAILED
	
	mkdir -p /media/temp
	mount /dev/loop1 /media/temp || die "mount /dev/loop1 /media/temo FAILED!"
	
	copy_files
			
}

copy_files() {
	echo -e "\nCopying files..."
	
	exclude_list="tmp sys proc var dev media root"
	mkdir_list="var proc sys dev media/card media/cf /media/hdd"
		
	if (cat /etc/fstab | grep -q "/home")
	then
		echo "Note: /home appears to be mounted on a different flash partition: not copying /home"
		exclude_list="$exclude_list home"
		mkdir_list="$mkdir_list home/root"
		
	fi
	

	
	source_dirs="`ls -1 /`"
	
	echo -n "Excluding ["
	for d in $exclude_list
	do
		echo -n "$d "
		source_dirs="`echo "$source_dirs" | grep -v "$d"`"
	done
	echo "] from copy"
				
	
	cd /
	for dir in $source_dirs
	do
		if test -d "$dir"
		then
			echo -n "Copying [$dir]..."
			cp -a $dir /media/temp && echo ok || echo FAILED
		fi
	done
	
	for dir in $mkdir_list
	do
		mkdir -p /media/temp/$dir
	done
	
	rm /media/temp/etc/rcS.s/S39sd
	
}
run_module() {
	
	test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"
	
	echo -e "${C_RED}* * * * * WARNING * * * * *${C_RESET}"
	echo -e "${C_RED}Continueing will delete the content of the selected target device.\nTHIS IS NO JOKE. If you do now know what this menu-item does, exit NOW${C_RESET}"
	
	mount -o remount,rw /
	
	while true
	do
		echo -en "\nContinue? [y|n] "
		read junk </dev/tty1
		
		if test "$junk" = n
		then
			exit 0
		fi
		
		if test "$junk" = y
		then
			break
		fi
	done	
	
# 	echo -n "Trying to activate PCMCIA..."
# 	cardmgr -o >/dev/null 2>&1 && echo ok || echo "FAILED"
# 	
# 	echo -n "Trying to mount SD card..."
# 	/etc/init.d/sd start >/dev/null 2>&1
# 	sleep 3
# 	/etc/sdcontrol insert >/dev/null 2>&1 && echo ok || echo "FAILED"
	
	#exit 0
	echo -e "\nPlease select the target device:"	
	
	# Sets ROOTFS_TARGET*
	ask_target
	
	# Sets ROOTFS_TARGET_TYPE ([direct | image]
	ask_format
	
	# Ask confirmation
	ask_confirm
	
	case "$ROOTFS_TARGET_TYPE" in
	direct)	direct_install;;
	image)	image_install;;
	esac
}

case "$1" in
title)	echo "$M_TITLE";;
run)	run_module;;
esac