source: trunk/openwrt/install.sh @ 888

Last change on this file since 888 was 888, checked in by amain, 2 years ago

openwrt: deliver install.sh

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1#!/bin/bash
2#
3# Install DebWrt helper script
4#
5# Copyright 2016 Johan van Zoomeren <amain@debwrt.net>
6#
7
8BASE_DIR=$(dirname ${0})
9
10if [ ! $(id -u) -eq 0 ]
11then
12    echo "Must be root"
13    exit 1
14fi
15
16dev=${1}
17shift
18
19if [ -n "$@" ]
20then
21   eval "$@" 2>/dev/null
22   if [ ! $? -eq 0 ]
23   then
24       echo "Incorrect argument"
25       exit 1
26   fi
27fi
28
29swap=${swap:-512}
30
31if [ ! -b "${dev}" ]
32then
33    cat <<EOF
34usage: $(basename ${0}) <block device> [swap=<size>] [root=<size>]
35
36Install DebWrt on <block devices:
37* Create Linux partition
38* Create swap partition
39* Create an ext3 filesystem on the Linux partition
40* Create a swap filesysttem on the swap partition
41* Extract the root file system image (debwrt-rootfs-*.tar.bz2)
42* Extract the modules image (debwrt-modules-*.tar.gz)
43* Extract the headers image (debwrt-headers-*.tar.gz)
44* Copy firmware image to /boot (*-squashfs-factory.*)
45
46Default swap size: 512 (MB), rest will be used for the root fs partition.
47
48example: $(basename ${0}) /dev/sdb
49
50WARNING: will overwrite everything on <block device>!
51
52EOF
53    exit 1
54fi
55
56function get()
57{
58    local pat=${1}
59
60    find ${BASE_DIR} -name "${pat}" >/tmp/.install.pat.$$
61    c=$(wc -l /tmp/.install.pat.$$ | awk '{print $1}')
62
63    if [ ${c} -gt 1 ]
64    then
65        rm -f /tmp/.install.pat.$$
66        echo "Found multiple images for '${pat}', while we can only install one" 1>&2
67        exit 1
68    fi
69
70    file=$(cat /tmp/.install.pat.$$)
71
72    if [ "${file}" = "" ]
73    then
74        rm -f /tmp/.install.pat.$$
75        echo "No image found for '${pat}'" 1>&2
76        exit 1
77    fi
78
79    echo $(basename ${file})
80
81    rm -f /tmp/.install.pat.$$
82}
83
84function extract()
85{
86    local image=${1}
87    local to=${2}
88
89    ft=$(file -b ${BASE_DIR}/${image} | awk '{print $1}')
90    echo "Extracting (${ft}) '${image}'..."
91
92    case ${ft} in
93        bzip2) x=j;;
94        gzip)  x=z;;
95        *)     x=;;
96    esac
97
98    if type -p pv &>/dev/null
99    then
100        pv ${BASE_DIR}/${image} | tar x${x}f - -C ${to}
101    else
102        tar x${x}f ${BASE_DIR}/${image} -C ${to}
103    fi
104}
105
106echo "Looking for images in '${BASE_DIR}' ..."
107rootfs=$(get 'debwrt-rootfs-*.tar.bz2')  e1=$?
108modules=$(get 'debwrt-modules-*.tar.gz') e2=$?
109headers=$(get 'debwrt-headers-*.tar.gz') e3=$?
110firmware=$(get '*-squashfs-factory.*')   e4=$?
111[ $((e1+e2+e3+e4)) -gt 0 ] && exit 1
112
113if [ "${root}" = "" ]
114then
115    size=$(sfdisk -l ${dev} 2>&1 \
116             | grep '^Disk.*bytes' \
117             | sed 's/.* \([0-9]\+\) bytes.*/\1/')
118    swapb=$((swap * 1024 * 1024))
119    root=$(((size - swapb) / 1024 / 1024))
120fi
121
122cat <<EOF
123
124Intalling to   : ${dev}
125    root(bytes): ${root}
126    swap(bytes): ${swap}
127
128Root filesystem: ${rootfs}
129Modules        : ${modules}
130Headers        : ${headers}
131Firmware       : ${firmware}
132
133EOF
134
135read -p "Are you sure to install DebWrt to ${dev} [Y/N]? " a
136
137if [ ! "${a^^}" = "Y" ]
138then
139    echo "Aborting"
140    exit 1
141fi
142
143echo
144echo "Installing DebWrt...."
145echo
146
147cat >/tmp/.install_ptable.$$ <<EOF
148,${root}Mib,L
149,,L
150EOF
151sfdisk ${dev} </tmp/.install_ptable.$$
152rm -f /tmp/.install_ptable.$$
153
154mkfs.ext3 -F -L DEBWRT_ROOT ${dev}1
155mkswap -f -L DEBWRT_SWAP ${dev}2
156
157echo
158
159function _umount()
160{
161    echo
162    echo "Unmounting ${dev}1, this may take a while...."
163    umount ${BASE_DIR}/mnt &>/dev/null
164    rmdir ${BASE_DIR}/mnt &>/dev/null
165    echo
166    echo "done"
167}
168
169trap _umount EXIT
170mkdir -p ${BASE_DIR}/mnt
171mount ${dev}1 ${BASE_DIR}/mnt
172
173extract ${rootfs} ${BASE_DIR}/mnt
174extract ${modules} ${BASE_DIR}/mnt
175extract ${headers} ${BASE_DIR}/mnt
176echo "Copy firmware (${firmware}) to /boot..."
177cp ${firmware} ${BASE_DIR}/mnt/boot
178
Note: See TracBrowser for help on using the repository browser.