aboutsummaryrefslogtreecommitdiffstats
path: root/packages/altboot/files/nslu2le/altboot-menu/30-bootUSB-Stick
blob: c6c215ecc43befd0b475c6498012ff1025753c6c (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
# !/bin/sh
#
# Copyright Matthias Hentges (c) 2005
#
# License: GPL (see http://www.gnu.org/licenses/gpl.txt for a copy of the GPL)


M_TITLE="USB Attached Storage"

test "$USB_HOST_AVAILABLE" = "yes" || exit 0
       
# This function is activated by init.altboot by calling this script with the "run" option
run_module() {
	
	test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"
	
	# Mount /proc, etc
	init_rootfs

	echo -e "\nStarting USB..."
	
	for module in $USB_STORAGE_MODULES
	do
		echo -en "\t - $module: "
		modprobe "$module" >/dev/null 2>&1 && echo ok || die "Failed to modprobe [$module]"
	done

	echo ""
	scan_devices
	usb_show_menu 
		
#	echo -n "Mounting $USB_STORAGE_PARTITION..."  >/dev/tty0
#	
#	mkdir -p /media/usb-storage >/dev/null 2>&1
#	
#	sleep "$USB_STORAGE_WAIT"
#	
#	/bin/mount -t auto -o defaults,noatime $USB_STORAGE_PARTITION /media/usb-storage >/dev/null 2>&1 && echo ok  >/dev/tty0|| die "/bin/mount -t auto -o defaults,noatime $SD_DEVICE $SD_MOUNTPOINT failed"
#		
#	echo ""	
#	
#	# Check for a real fs and loop-images.
#	check_target "/media/usb-storage" >/dev/tty0		
}

scan_devices() {
	reset_pref "available_devices"

	cnt=1	
	
	for scsi_disk in `ls -1 /sys/block/ | grep ^sd`
	do
		scsi_disk_name="`echo "$scsi_disk" | awk '{printf("%s\n",toupper($0))}'`"
		test -e /sys/block/$scsi_disk/device/vendor && HDD_VENDOR="`cat /sys/block/$scsi_disk/device/vendor | sed "s/\ $//"`" 
		test -z "$HDD_VENDOR" && HDD_VENDOR="Unknown Vendor"

		test -e /sys/block/$scsi_disk/device/model && HDD_MODEL="`cat /sys/block/$scsi_disk/device/model`" 
		test -z "$HDD_MODEL" && HDD_MODEL="Unknown Model"

		scsi_disk_partition_cnt="`ls -1 /sys/block/$scsi_disk | grep ^$scsi_disk | wc -l | tr -d " "`"
		test "$scsi_disk_partition_cnt" -gt 1 && scsi_disk_partition_cnt="$scsi_disk_partition_cnt partitions" || scsi_disk_partition_cnt="$scsi_disk_partition_cnt partition"
		
		set_pref "available_devices" "$cnt" "$scsi_disk_name ( $HDD_VENDOR $HDD_MODEL with $scsi_disk_partition_cnt)"
		set_pref "available_devices_short" "$cnt" "$scsi_disk"	
		set_pref "available_devices_type" "$cnt" "usb"	
		
		let cnt=$cnt+1
	done
	
}

usb_show_menu() {		

	cnt2=0 ; let cnt=$cnt-1	
	while test "$cnt" != "$cnt2"
	do
		let cnt2=$cnt2+1
		get_pref "available_devices" "$cnt2" dev
		
		echo -e "\t[$cnt2] - $dev"			
	done
	
	while test -z "$selected_dev"
	do	
		echo -en "\nYour choice: "
		read junk
		get_pref "available_devices" "$junk" selected_dev
		get_pref "available_devices_type" "$junk" part_mode
		
#		debug_echo "show_menu(): selected: [$selected_dev]"
	done
	
	if ( echo "$selected_dev" | grep -q "^Flash" )
	then
		boot_from flash		
	else
		get_pref "available_devices_short" "$junk" selected_dev
#		debug_echo "show_menu(): selected_devices_short: [$selected_dev]"
		
		partitions="`ls -1 /sys/block/$selected_dev|grep ^$selected_dev`"
		test -z "$partitions" && die "No partitions found on /dev/$selected_dev!"
		
		
		echo -e "\nPlease select a partition on $selected_dev to boot from:\n"
		
		cnt=1
		for partition in $partitions
		do			
			# We assume that partitions with a "size" < 10 are extended partitions
			# and should not be listed. TYPE=swap is blacklisted as well.
			part_size="`cat /sys/block/$selected_dev/$partition/size`"
			part_type="`blkid -s TYPE -o value /dev/$partition`"		
			
			if test "$part_size" -gt 10 -a "$part_type" != "swap"
			then
				let part_size="($part_size/2)/1000"
				echo -e "\t[$cnt] $partition (~ ${part_size}Mb, $part_type)"	
				set_pref "available_partitions" "$cnt" "$partition"
				
				let cnt=$cnt+1			
			fi
		done
		
		while test -z "$selected_partition"
		do
			echo -en "\nYour choice: "	
			read junk	
			get_pref "available_partitions" "$junk" selected_partition						
		done
		
		part_uuid="`blkid -c /dev/null -s UUID -o value /dev/$selected_partition`"
					
		boot_from "$part_mode" "$selected_partition" "$part_uuid"		
	fi				
}	

boot_from() {
	debug_echo "boot_from() [$*]"
	
	part_mode="$1"
	part_name="$2"
	part_uuid="$3"
	
	case "$part_mode" in 
	usb)		umount /tmp/mnt.set-bootdev >/dev/null 2>&1
			rm -rf /tmp/mnt.set-bootdev ; mkdir -p /tmp/mnt.set-bootdev

			if ! ( mount -U "$part_uuid" /tmp/mnt.set-bootdev )
			then
				echo "** Note: UUID mount for $part_name failed"
				mount /dev/$part_name /tmp/mnt.set-bootdev && echo "** Note: Normal mount for $part_name successful"
			fi

			if ! test -e /tmp/mnt.set-bootdev/sbin/init.sysvinit
			then
				copy_rootfs /tmp/mnt.set-bootdev/
			else
				umount /tmp/mnt.set-bootdev
				echo -e "\nSetting /dev/$part_name as boot-partition"
				echo "3 $part_name $part_uuid" > /etc/altboot.last
			fi
			;;
	esac
}

copy_rootfs() {
	debug_echo "copy_rootfs() [$*]"
}

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