setup-disk.in 3.62 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1
2
3
4
5
#!/bin/sh

PREFIX=
. "$PREFIX/lib/libalpine.sh"

6
MBR=${MBR:-"/usr/share/syslinux/mbr.bin"}
Natanael Copa's avatar
Natanael Copa committed
7
8
9
10
11
12
13
14
15
16
17

in_list() {
	local i="$1"
	shift
	while [ $# -gt 0 ]; do
		[ "$i" = "$1" ] && return 0
		shift
	done
	return 1
}

18
enumerate_fstab() {
19
20
21
22
	local mnt="$1"
	[ -z "$mnt" ] && return
	awk "\$2 == \"$mnt\" {print \$0}" /proc/mounts | \
		sed "s-$mnt-/-; s-/+-/-; s: :\t:g"
23
24
25
26
}

install_mounted_root() {
	local mnt="$1"
27
	local features="ata base bootchart cdrom ext2 ext3 ide scsi usb"
28

29
	rootdev=$(awk "\$2 == \"$mnt\" { print \$1 }" /proc/mounts)
30
31
32
33
34
	if [ -z "$rootdev" ]; then
		echo "$mnt does not seem to be a mount point" >&2
		return 1
	fi

35
36
37
38
39
40
	local fs=$(awk "\$1 == \"$rootdev\" {print \$3}" /proc/mounts)
	if [ "$fs" != "ext2" ] && [ "$fs" != "ext3" ]; then
		echo "$fs is not supported. Only ext2 and ext3 are supported" >&2
		return 1
	fi
		
41
42
43
44
	rootdisk=${rootdev%[0-9]*}

	echon "Installing system on $rootdev: "
	lbu package - | tar -C "$mnt" -zx
45
46
47
48
	# apk reads config from target root so we need to copy the config
	mkdir -p "$mnt"/etc/apk/keys/
	cp /etc/apk/keys/* "$mnt"/etc/apk/keys/

49
	apk add -q --progress --root "$mnt" $(cat "$mnt"/var/lib/apk/world) \
50
		acct linux-grsec alpine-base >/dev/null || return 1
51
52
53
	echo ""
	# make things bootable
	kernel=$(ls "$mnt"/lib/modules)
54
55
56
57
58
59
60
61
62
	if [ "$rootdisk" = "/dev/md" ]; then
		local md=${rootdev#/dev/}
		features="$features raid"
		raidmod=$(cat /sys/block/$md/md/level)
		raidmod=",$raidmod"
		raidopt="-r"
		# get a list of slaves
		rootdisk=
		for i in /sys/block/$md/slaves/*; do
63
64
65
			j=${i##*/}
			i=${j%[0-9]*}
			rootdisk="$rootdisk /dev/${i}"
66
67
		done
	fi
68
69
70

	# create an extlinux.conf
	sed '/append initrd/d' /media/*/syslinux.cfg > "$mnt"/boot/extlinux.conf
71
	echo -e "\tappend initrd=/boot/grsec.gz root=$rootdev modules=sd-mod,usb-storage,ext3$raidmod quiet" >> "$mnt"/boot/extlinux.conf
72
73
74
75
76
77

	# fix the fstab
	enumerate_fstab "$mnt" >> "$mnt"/etc/fstab

	# install extlinux
	apk add -q syslinux
78
	extlinux -i $raidopt "$mnt"/boot
79
80
	umount "$mnt"

81
82
	# fix mbr for all disk devices
	for i in $rootdisk; do
83
84
85
86
		local errmsg
		echo "Writing MBR to $i"
		errmsg=$(dd if="$MBR" of=$i 2>&1) \
			|| echo "$errmsg"
87
	done
88
89
90
91
92
	echo ""
	echo "Installation is done. Please reboot."
	apk del -q syslinux
}

Natanael Copa's avatar
Natanael Copa committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
useall() {
	local i size
	echo "Creating root partition..."
	apk_add -q parted e2fsprogs
	# erase all partitions
	for i in $(parted /dev/$rootdisk print | awk '$1 ~ /[0-9]+/ {print $1}'); do
		parted /dev/$rootdisk rm $i >/dev/null
	done
	# create new partition
	size=$(parted /dev/$rootdisk print | awk '/^Disk / {print $3}')
	parted /dev/$rootdisk mkpart primary 0 $size >/dev/null
	parted /dev/$rootdisk set 1 boot on >/dev/null
 
	# create device node if not exist
	mdev -s
	rootdev=/dev/${rootdisk}1

	mkfs.ext3 -q $rootdev
	# we are done with parted and dont want it in the lbu package
	apk del -q parted e2fsprogs

	mount -t ext3 $rootdev /mnt || return 1
115
	install_mounted_root /mnt
Natanael Copa's avatar
Natanael Copa committed
116
117
}

118
119
if [ -d "$1" ]; then
	# install to given mounted root
120
	install_mounted_root "${1%/}"
121
122
123
	exit $?
fi

Natanael Copa's avatar
Natanael Copa committed
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
usbdisk=$(readlink /dev/usbdisk)
disks=
cd /dev
for i in sd[a-z] hd[a-z]; do
	case "$usbdisk" in
		$i[0-9]*);;
		*) [ -b "$i" ] && disks="$disks $i";;
	esac
done

# no disks so lets exit quietly.
[ -z "$disks" ] && exit 0

rootdisk=
while ! in_list "$rootdisk" $disks "none" "abort"; do
	echo "Available disks are: $disks"
	echon "Which one is the root disk? (or none) [none] "
	default_read rootdisk "none"
done

[ -b "/dev/$rootdisk" ] || exit 0

echon "Do you want use *all* of $rootdisk for Alpine? (y/n) [n] "
default_read useall "n"
case "$useall" in
	[Yy]*) useall="yes";;
esac

if [ "x$useall" != "xyes" ]; then
	echo "Only 'use all' option is available at the moment. Sorry"
	exit 1
fi

useall