summaryrefslogtreecommitdiff
path: root/build.sh
blob: a7f6c78f3c3ff7854f2592beb90e77b6e8f76aa0 (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
#!/bin/sh

set -e

arch=x86
subarch=64
arch_lxd=${arch}_${subarch}
arch_dash=${arch}-${subarch}
ver=17.01.4
image=openwrt
name=openwrt
dist=lede

rootfs_url=https://downloads.openwrt.org/releases/${ver}/targets/${arch}/${subarch}/${dist}-${ver}-${arch_dash}-generic-rootfs.tar.gz
rootfs_sum=43886c6b4a555719603286ceb1733ea2386d43b095ab0da9be35816cd2ad8959
rootfs=dl/$(basename $rootfs_url)

sdk_url=https://downloads.openwrt.org/releases/${ver}/targets/${arch}/${subarch}/${dist}-sdk-${ver}-${arch}-${subarch}_gcc-5.4.0_musl-1.1.16.Linux-${arch}_${subarch}.tar.xz
sdk_sum=ef8b801f756cf2aa354198df0790ab6858b3d70b97cc3c00613fd6e5d5bb100c
sdk_tar=dl/$(basename $sdk_url)
sdk=build_dir/sdk

procd_url=https://github.com/openwrt/openwrt/branches/lede-17.01/package/system/procd
procd_extra_ver=lxd-3

lxc_tar=bin/${dist}-${ver}-${arch_dash}-lxd.tar.gz
metadata=bin/metadata.yaml

download_rootfs() {
	download $rootfs_url $rootfs
	check $rootfs $rootfs_sum
}

download_sdk() {
	download $sdk_url $sdk_tar
	check $sdk_tar $sdk_sum
	if ! test -e $sdk; then
		test -e build_dir || mkdir build_dir
		tar xvpf $sdk_tar -C build_dir
		(cd build_dir && ln -sf ${dist}-sdk-* sdk)
	fi
}

download() {
	url=$1
	dst=$2
	dir=$(dirname $dst)

	if ! test -e "$dst" ; then
		echo Downloading $url
		test -e $dir || mkdir $dir

		wget -O $dst "$url"
	fi
}

check() {
	dst=$1
	dst_sum=$2

	sum=$(sha256sum $dst| cut -d ' ' -f1)
	if test $dst_sum != $sum; then
		echo Bad checksum $sum of $dst
		exit 1
	fi
}

download_procd() {
	if ! test -e dl/procd; then
		svn co $procd_url dl/procd
		sed -i -e "s/PKG_RELEASE:=\(\S\+\)/PKG_RELEASE:=\1-${procd_extra_ver}/" dl/procd/Makefile
	fi

	test -e dl/procd/patches || mkdir dl/procd/patches
	cp -a patches/procd/* dl/procd/patches
}

build_procd() {
	if ! test -e $sdk/package/lxd-procd; then
		ln -s $(pwd)/dl/procd $sdk/package/lxd-procd
	fi
	(cd $sdk
	./scripts/feeds update base
	./scripts/feeds install libubox
	make package/libubox/compile
	./scripts/feeds install ubus
	make package/ubus/compile
	make package/lxd-procd/compile
	)
	local date=$(grep PKG_SOURCE_DATE:= dl/procd/Makefile | cut -d '=' -f 2)
	local version=$(grep PKG_SOURCE_VERSION:= dl/procd/Makefile | cut -d '=' -f 2 | cut -b '1-8')
	local release=$(grep PKG_RELEASE:= dl/procd/Makefile | cut -d '=' -f 2)
	local procd_ipkg="$sdk/bin/targets/${arch}/${subarch}/packages/procd_${date}-${version}-${release}_${arch}_${subarch}.ipk"
	test -e bin/packages/ || mkdir bin/packages/
	ln -sf ../../$procd_ipkg bin/packages/
}

build_tarball() {
	export SDK="$(pwd)/${sdk}"
	fakeroot ./build_rootfs.sh $rootfs $metadata $lxc_tar
}

build_metadata() {
	stat=`stat -c %Y $rootfs`
	date=`date -R -d "@${stat}"`

	cat > $metadata <<EOF
architecture: "$arch_lxd"
creation_date: $(date +%s)
properties:
 architecture: "$arch_lxd"
 description: "OpenWrt $ver $arch_lxd ($date)"
 os: "OpenWrt"
 release: "$ver"
templates:
EOF

## Add templates
#
# templates:
#   /etc/hostname:
#     when:
#       - start
#     template: hostname.tpl
}

build_image() {
	lxc image import $lxc_tar --alias $image
}

download_rootfs
download_sdk
download_procd
build_procd
build_metadata
build_tarball
build_image

echo \# start
echo lxc launch --config "raw.lxc=lxc.aa_profile=lxc-container-default-without-dev-mounting" --profile openwrt $image $name
#lxc config
echo \# set root password
echo lxc exec $name passwd root
#echo 'echo "148.251.78.235 downloads.openwrt.org"