source: trunk/openwrt/install.sh @ 889

Last change on this file since 889 was 889, checked in by amain, 3 years ago

openwrt: small updates to install.sh

  • Property svn:executable set to *
File size: 4.0 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
56if echo ${dev} | grep -q "[0-9]"
57then
58    echo "Partition number found in ${dev}, please specify 'raw' block device"
59    exit 1
60fi
61
62if mount | grep -q ${dev}
63then
64    echo "Device ${dev} already mounted:"
65    mount | grep ${dev}
66    exit 1
67fi
68
69function get()
70{
71    local pat=${1}
72
73    find ${BASE_DIR} -name "${pat}" >/tmp/.install.pat.$$
74    c=$(wc -l /tmp/.install.pat.$$ | awk '{print $1}')
75
76    if [ ${c} -gt 1 ]
77    then
78        rm -f /tmp/.install.pat.$$
79        echo "Found multiple images for '${pat}', while we can only install one" 1>&2
80        exit 1
81    fi
82
83    file=$(cat /tmp/.install.pat.$$)
84
85    if [ "${file}" = "" ]
86    then
87        rm -f /tmp/.install.pat.$$
88        echo "No image found for '${pat}'" 1>&2
89        exit 1
90    fi
91
92    echo $(basename ${file})
93
94    rm -f /tmp/.install.pat.$$
95}
96
97function extract()
98{
99    local image=${1}
100    local to=${2}
101
102    ft=$(file -b ${BASE_DIR}/${image} | awk '{print $1}')
103    echo "Extracting (${ft}) '${image}'..."
104
105    case ${ft} in
106        bzip2) x=j;;
107        gzip)  x=z;;
108        *)     x=;;
109    esac
110
111    if type -p pv &>/dev/null
112    then
113        pv ${BASE_DIR}/${image} | tar x${x}f - -C ${to}
114    else
115        tar x${x}f ${BASE_DIR}/${image} -C ${to}
116    fi
117}
118
119echo "Looking for images in '${BASE_DIR}' ..."
120rootfs=$(get 'debwrt-rootfs-*.tar.bz2')  e1=$?
121modules=$(get 'debwrt-modules-*.tar.gz') e2=$?
122headers=$(get 'debwrt-headers-*.tar.gz') e3=$?
123firmware=$(get '*-squashfs-factory.*')   e4=$?
124[ $((e1+e2+e3+e4)) -gt 0 ] && exit 1
125
126if [ "${root}" = "" ]
127then
128    size=$(sfdisk -l ${dev} 2>&1 \
129             | grep '^Disk.*bytes' \
130             | sed 's/.* \([0-9]\+\) bytes.*/\1/')
131    swapb=$((swap * 1024 * 1024))
132    root=$(((size - swapb) / 1024 / 1024))
133fi
134
135cat <<EOF
136
137Intalling to   : ${dev}
138      root(MiB): ${root}
139      swap(MiB): ${swap}
140
141Root filesystem: ${rootfs}
142Modules        : ${modules}
143Headers        : ${headers}
144Firmware       : ${firmware}
145
146EOF
147
148read -p "Are you sure to install DebWrt to ${dev} [Y/N]? " a
149
150if [ ! "${a^^}" = "Y" ]
151then
152    echo "Aborting"
153    exit 1
154fi
155
156echo
157echo "Installing DebWrt...."
158echo
159
160cat >/tmp/.install_ptable.$$ <<EOF
161,${root}Mib,L
162,,L
163EOF
164sfdisk ${dev} </tmp/.install_ptable.$$
165rm -f /tmp/.install_ptable.$$
166
167mkfs.ext3 -F -L DEBWRT_ROOT ${dev}1
168mkswap -f -L DEBWRT_SWAP ${dev}2
169
170echo
171
172function _umount()
173{
174    echo
175    echo "Unmounting ${dev}1, this may take a while...."
176    umount ${BASE_DIR}/mnt &>/dev/null
177    rmdir ${BASE_DIR}/mnt &>/dev/null
178    echo
179    echo "done"
180}
181
182trap _umount EXIT
183mkdir -p ${BASE_DIR}/mnt
184mount ${dev}1 ${BASE_DIR}/mnt
185
186extract ${rootfs} ${BASE_DIR}/mnt   ; echo
187extract ${modules} ${BASE_DIR}/mnt  ; echo
188extract ${headers} ${BASE_DIR}/mnt  ; echo
189echo -n "Copy firmware (${firmware}) to /boot..."
190cp ${firmware} ${BASE_DIR}/mnt/boot
191echo "done"
192
193echo
194echo -n "Calculate module dependencies (depmod)..."
195depmod -b ${BASE_DIR}/mnt \
196       -a $(find ${BASE_DIR}/mnt/lib/modules/ \
197                 -maxdepth 1 \
198                 -mindepth 1 \
199                 -type d \
200                 -printf "%f\n" \
201             | head -1)
202echo "done"
203
Note: See TracBrowser for help on using the repository browser.