Permalink
Browse files

complete restructuring, many bugfixes, lots of error checking, reboot…

… eliminated
  • Loading branch information...
1 parent aa0405c commit d6147376bdd0e98fd5f5d30f9e4f2aa9d874b47c @cr committed Jan 30, 2011
Showing with 358 additions and 184 deletions.
  1. +2 −2 src/control/control
  2. +19 −46 src/control/postinst
  3. +2 −0 src/control/prerm
  4. +24 −124 src/data/etc/init.d/piratebox
  5. +311 −0 src/data/etc/piratebox.common
  6. +0 −12 src/data/etc/piratebox.conf
View
4 src/control/control
@@ -1,6 +1,6 @@
Package: piratebox
Version: 1-1
-Depends: kmod-usb2, kmod-usb-storage, kmod-fs-vfat, kmod-nls-cp437, kmod-nls-cp850, kmod-nls-iso8859-1, kmod-nls-iso8859-15, kmod-fs-ext3, kmod-loop, block-mount
+Depends: kmod-usb2, kmod-usb-storage, kmod-fs-vfat, kmod-nls-cp437, kmod-nls-cp850, kmod-nls-iso8859-1, kmod-nls-iso8859-15, kmod-fs-ext3, block-mount, kmod-loop, losetup
Provides:
Source: package/piratebox
Section: net
@@ -9,6 +9,6 @@ Essential: no
Priority: optional
Maintainer: Christiane Ruetten <cr@23bit.net>
Architecture: all
-Installed-Size: 32768
+Installed-Size: 36864
Description: Turns OpenWRT into a PirateBox, see http://wiki.daviddarts.com/PirateBox
View
65 src/control/postinst
@@ -1,6 +1,7 @@
#!/bin/ash
-. /etc/piratebox.conf
+# include PirateBox shared functionality
+. /etc/piratebox.common
# check if root password is not set yet
if grep '^root:!:' /etc/passwd >/dev/null
@@ -13,49 +14,11 @@ then
echo "Next time you log in, you must use ssh."
fi
-# prepare usb disk for mounting
-mkdir -p "$pb_usbmount"
-id=$(uci add fstab mount)
-uci set "fstab.$id=mount"
-uci set "fstab.$id.target=$pb_usbmount"
-uci set "fstab.$id.device=$pb_usbdevice"
-uci set "fstab.$id.fstype=vfat"
-uci set "fstab.$id.options=rw,sync"
-uci set "fstab.$id.enabled=1"
-uci set "fstab.$id.enabled_fsck=0"
+# configure usb and network
+pb_usbconfig
+pb_netconfig
-# configure network
-uci set "system.@system[0].hostname=$pb_hostname"
-echo "127.0.0.1 $pb_hostname localhost." >/etc/hosts
-echo "$pb_ip $pb_hostname" >>/etc/hosts
-uci set "wireless.radio0.disabled=0"
-uci set "wireless.@wifi-iface[0].ssid=$pb_wireless_ssid"
-uci set "network.lan.ipaddr=$pb_ip"
-
-# redirect all 80/tcp traffic to the PirateBox
-id=$(uci add firewall redirect)
-uci set "firewall.$id=redirect"
-uci set "firewall.$id.src=lan"
-uci set "firewall.$id.proto=tcp"
-uci set "firewall.$id.src_dport=80"
-uci set "firewall.$id.dest_ip=$pb_ip"
-uci set "firewall.$id.dest_port=80"
-
-# redirect all DNS requests to PirateBox IP
-id=$(uci add dhcp domain)
-uci set "dhcp.$id=domain"
-uci set "dhcp.$id.name=#"
-uci set "dhcp.$id.ip=$pb_ip"
-
-# CAVE: ugly hack, patching a preinstalled file
-# patch dnsmasq start script to properly handle domain wildcards
-sed -i 's#^.*\${fqdn\%\.\*}\" ==.*$## ; s#^.*fqdn=\"\$fqdn.*$##' \
- /etc/init.d/dnsmasq
-
-# commit all UCI changes
-uci commit
-
-# disable web interface, start droopy instead
+# disable web interface, start PirateBox instead
/etc/init.d/uhttpd stop
/etc/init.d/uhttpd disable
/etc/init.d/luci_fixtime stop
@@ -64,11 +27,21 @@ uci commit
/etc/init.d/luci_dhcp_migrate disable
/etc/init.d/piratebox enable
+/etc/init.d/piratebox init
+/etc/init.d/piratebox update
+
+/etc/init.d/firewall restart
+/etc/init.d/dnsmasq restart
+wifi
+
+/etc/init.d/piratebox start
+
+# give some user feedback
echo
echo "PirateBox wireless SSID: $pb_wireless_ssid"
echo "PirateBox DNS and hostname: $pb_hostname"
-echo "PirateBox IP address: $pb_ip"
+echo "PirateBox LAN IP address: $pb_ip"
echo
-echo "Reboot the router and reconnect wirelessly."
-
+echo "Setup complete, PirateBox started."
+echo "You can remove the WAN connection now."
View
2 src/control/prerm
@@ -7,3 +7,5 @@
/etc/init.d/uhttpd enable
/etc/init.d/piratebox disable
+/etc/init.d/piratebox stop
+
View
148 src/data/etc/init.d/piratebox
@@ -1,141 +1,41 @@
#!/bin/sh /etc/rc.common
-START=80
-
-. /etc/piratebox.conf
-
-piratebox_init() {
-
- #CAVE: /etc/config/fstab is currently broken
- #Workaround time
-
- # mount USB disk
- [ -d "$pb_usbmount" ] || mkdir -p "$pb_usbmount"
- if ! mount "$pb_usbdevice" "$pb_usbmount"
- then
- echo "ERROR: piratebox: unable to mount USB storage" >&2
- exit 5
- fi
-
- [ -d "$pb_usbdir" ] || mkdir -p "$pb_usbdir"
-
- # create a swap file
- echo "Creating swapspace..."
- swapoff "$pb_swapimg" &>/dev/null
- dd if=/dev/zero of="$pb_swapimg" bs=1M count=32
- mkswap "$pb_swapimg"
- uci set "fstab.@swap[0].device=$pb_swapimg"
- uci set "fstab.@swap[0].enabled=1"
-
- # create an ext3 image file
- echo "Creating ext loop image..."
- # use a preformatted image to get rid of e2fsprogs dependency
- #dd if=/dev/zero of="$pb_extimg" bs=1M count=16
- #echo y | mkfs.ext3 "$pb_extimg"
- umount "$pb_extimg" &>/dev/null
- gzip -dc /usr/share/piratebox/OpenWRT.img.gz >"$pb_extimg"
- [ -d "$pb_extmount" ] || mkdir -p "$pb_extmount"
- id=$(uci add fstab mount)
- uci set "fstab.$id=mount"
- uci set "fstab.$id.target=$pb_extmount"
- uci set "fstab.$id.device=$pb_extimg"
- uci set "fstab.$id.options=loop"
- uci set "fstab.$id.enabled=1"
-
- # temporarily manually mount ext fs
- if ! mount -o loop "$pb_extimg" "$pb_extmount"
- then
- echo "ERROR: piratebox: unable to mount ext loop image" >&2
- exit 5
- fi
- # prepare opkg, PATH and LD_LIBRARY_PATH for /usr/local
- grep "^dest piratebox" /etc/opkg.conf \
- || echo "dest piratebox $pb_extmount" >>/etc/opkg.conf
- # CAVE: a bug during startup time prevents LD_LIBRARY_PATH
- # and PATH from working as expected. For now, both are set
- # directly in the init script.
- #sed -i 's#export PATH=\(.*\)#export PATH=\1:/usr/local/bin\nexport LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib#' \
- # /etc/profile
- #sed -i 's#export PATH=\(.*\)#export PATH=\1:/usr/local/bin\nexport LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib#' \
- # /etc/preinit
-
- # fix for ext packages not installing to usr
- [ -d "$pb_extmount/usr/lib" ] || mkdir -p "$pb_extmount/usr/lib"
- ln -sf "$pb_extmount/usr/lib" "$pb_extmount/lib"
-
- # temporarily disable local DNS per dnsmasq
- cp /tmp/resolv.conf.auto /tmp/resolv.conf
-
- # install python to piratebox storage
- opkg update
- opkg -d piratebox install python
-
- # link ext /usr to local file system
- ln -s "$pb_extmount/usr" /usr/local
-
- # get PirateBox files
- [ -d "$pb_share" ] || mkdir -p "$pb_share"
- mkdir -p "$pb_share"
- cd "$pb_share"
- wget http://daviddarts.com/piratebox/piratebox-logo.png -O piratebox-logo.png
- wget http://daviddarts.com/piratebox/.READ.ME.htm -O .READ.ME.htm
- wget http://daviddarts.com/piratebox/.BACK.TO.MENU.htm -O .BACK.TO.MENU.htm
- # adjust hostname
- sed -i "s#://http://piratebox#http://$pb_hostname#g" .READ.ME.htm
- sed -i "s#://http://piratebox#http://$pb_hostname#g" .BACK.TO.MENU.htm
+NAME=piratebox
+START=80
+STOP=20
+EXTRA_COMMANDS="init update nodns"
+EXTRA_HELP=" init PirateBoxify a fresh USB disk
+ update Update PirateBox files on USB disk
+ nodns Disable local DNS redirection through dnsmasq"
- # install PirateBox's droopy port
- [ -d /usr/local/bin ] || mkdir -p /usr/local/bin
- cd /usr/local/bin
- wget http://daviddarts.com/piratebox/droopy -O droopy
- chmod +x droopy
+# include PirateBox shared functionality
+. /etc/piratebox.common
- # commit UCI changes
- uci commit
+nodns() {
+ pb_nodns
+}
+init() {
+ pb_init
}
-startsrv() {
- # CAVE: /etc/config/fstab is currently broken
- # Ugly workaround time
- # CAVE: more workarounds for early start-up
- export PATH=$PATH:/usr/local/bin
- export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib
- cd "$pb_share"
- python /usr/local/bin/droopy \
- -p "$pb_share/piratebox-logo.png" \
- -d "$pb_share" \
- -m "<p><br><b>1.</b> Learn more about the project \
- <a href="http://$pb_hostname:8001/.READ.ME.htm"><b>here</b></a>. \
- <p><b>2.</b> Click above to begin sharing.</p> \
- <b>3.</b> Browse and download files \
- <a href="http://$pb_hostname:8001"><b>here</b></a>." &
- python -m SimpleHTTPServer 8001 &
+update() {
+ pb_update
}
start() {
- if [ ! -e "$pb_usbdevice" ]
- then
- echo "ERROR: piratebox requires USB storage to run" >&2
- exit 5
- fi
-
- mount "$pb_device" "$pb_usbmount" &>/dev/null
- mount -o loop "$pb_extimg" "$pb_extmount" &>/dev/null
- swapon "$pb_swapimg" &>/dev/null
-
- if [ "$2" == "init" ] || [ ! -d "$pb_usbdir" ]
- then
- piratebox_init
- fi
- if [ "$2" != "init" ]
+ pb_mount usb
+ # init if we have a fresh USB disk
+ if [ ! -d "$pb_usbdir" ]
then
- startsrv &>/dev/null &
+ pb_init
+ pb_update
fi
+ # be silent
+ pb_start &>/dev/null
}
stop() {
- # CAVE: a bit strong
- killall python
+ pb_stop
}
View
311 src/data/etc/piratebox.common
@@ -0,0 +1,311 @@
+#############################################################################
+# PirateBox global config (do not change)
+
+pb_ip="192.168.1.1"
+pb_wireless_ssid="PirateBox"
+pb_hostname="piratebox"
+pb_usbdevice="/dev/sda1"
+pb_usbmount="/mnt/usb"
+pb_usbdir="$pb_usbmount/PirateBox"
+pb_share="$pb_usbdir/Shared"
+pb_extimg="$pb_usbdir/OpenWRT.img"
+pb_extmount="/mnt/ext"
+pb_swapimg="$pb_usbdir/OpenWRT.swap"
+
+
+#############################################################################
+# PirateBox helper functions
+
+pb_nodns() {
+ # disable local DNS per dnsmasq
+ cp /tmp/resolv.conf.auto /tmp/resolv.conf
+}
+
+pb_ismounted() {
+ mount | grep "on $1 " >/dev/null
+}
+
+pb_mount() {
+ parts=$*
+ [ "$parts" == "-a" ] && parts="usb ext swap"
+ for part in $*
+ do
+ case $part in
+ usb)
+ pb_ismounted "$pb_usbmount" && continue
+ [ -d "$pb_usbmount" ] || mkdir -p "$pb_usbmount"
+ if ! mount "$pb_usbdevice" "$pb_usbmount"
+ then
+ echo "${initscript}: ERROR: unable to mount USB storage" >&2
+ exit 5
+ fi
+ ;;
+ ext)
+ pb_ismounted "$pb_extmount" && continue
+ pb_mount usb
+ [ -d "$pb_extmount" ] || mkdir -p "$pb_extmount"
+ if ! mount -o loop,rw,sync "$pb_extimg" "$pb_extmount"
+ then
+ echo "${initscript}: ERROR: unable to mount ext image" >&2
+ exit 5
+ fi
+ ;;
+ swap)
+ pb_mount usb
+ swapon "$pb_swapimg" &>/dev/null
+ ;;
+ esac
+ done
+}
+
+pb_umount() {
+ parts=$*
+ [ "$parts" == "-a" ] && parts="swap ext usb"
+ for part in $*
+ do
+ case $part in
+ usb)
+ pb_ismounted "$pb_usbmount" || continue
+ pb_umount swap ext
+ [ -d "$pb_usbmount" ] || mkdir -p "$pb_usbmount"
+ if ! umount "$pb_usbmount"
+ then
+ echo "${initscript}: WARNING: unable to umount USB storage" >&2
+ fi
+ ;;
+ ext)
+ # losetup -d here just in case user umounted manually
+ losetup -d /dev/loop0 &>/dev/null
+ pb_ismounted "$pb_extmount" || continue
+ [ -d "$pb_extmount" ] || mkdir -p "$pb_extmount"
+ if ! mount -o loop,rw,sync "$pb_extimg" "$pb_extmount"
+ then
+ echo "${initscript}: WARNING: unable to mount ext image" >&2
+ fi
+ losetup -d /dev/loop0
+ ;;
+ swap)
+ #TODO: error handling
+ swapoff "$pb_swapimg" &>/dev/null
+ ;;
+ esac
+ done
+}
+
+pb_uciadd() {
+ local config=$1 ; shift
+ local section=$1 ; shift
+ local type=$1 ; shift
+ [ -e "/etc/config/$config" ] || touch "/etc/config/$config"
+ uci show "$config.$section" &>/dev/null \
+ || uci add "$config $section" &>/dev/null
+ uci set "$config.$section=$type"
+}
+
+pb_uciset() {
+ local config=$1 ; shift
+ local section=$1 ; shift
+ local type=$1 ; shift
+ pb_uciadd $config $section $type
+ for opt in "$@"
+ do
+ uci set "$config.$section.$opt"
+ done
+ uci commit
+}
+
+pb_netconfig() {
+ # reconfigure network
+ uci set "system.@system[0].hostname=$pb_hostname"
+ echo "127.0.0.1 $pb_hostname localhost." >/etc/hosts
+ echo "$pb_ip $pb_hostname" >>/etc/hosts
+ uci set "network.lan.ipaddr=$pb_ip"
+ #CAVE: wireless config might be platform specific
+ uci set "wireless.radio0.disabled=0"
+ uci set "wireless.@wifi-iface[0].ssid=$pb_wireless_ssid"
+
+ # redirect all 80/tcp traffic to the PirateBox
+ pb_uciset firewall piratebox redirect \
+ src=lan \
+ proto=tcp \
+ src_dport=80 \
+ dest_ip=$pb_ip \
+ dest_port=80
+
+ # redirect all DNS requests to PirateBox IP
+ pb_uciset dhcp piratebox domain \
+ name='#' \
+ ip=$pb_ip
+
+ # CAVE: ugly hack, patching a preinstalled file
+ # patch dnsmasq start script to properly handle domain wildcards
+ sed -i 's#^.*\${fqdn\%\.\*}\" ==.*$## ; s#^.*fqdn=\"\$fqdn.*$##' \
+ /etc/init.d/dnsmasq
+
+}
+
+pb_usbconfig() {
+ # prepare USB disk for mounting
+ [ -d "$pb_usbmount" ] || mkdir -p "$pb_usbmount"
+ pb_uciset fstab piratebox mount \
+ target=$pb_usbmount \
+ device=$pb_usbdevice \
+ fstype=vfat \
+ options=rw,sync \
+ enabled_fsck=0 \
+ enabled=1
+
+}
+
+pb_needswap() {
+ [ -e "$pb_swapimg" ]
+}
+
+pb_mkswap() {
+ pb_umount swap
+ # create a swap file
+ dd if=/dev/zero of="$pb_swapimg" bs=1M count=32
+ mkswap "$pb_swapimg"
+ pb_uciset fstab pirateswap swap \
+ device=$pb_swapimg \
+ enabled=1
+}
+
+pb_needextimg() {
+ [ -e "$pb_extimg" ]
+}
+
+pb_mkextimg() {
+ pb_umount usb
+ # use a preformatted image to get rid of e2fsprogs dependency
+ #dd if=/dev/zero of="$pb_extimg" bs=1M count=16
+ #echo y | mkfs.ext3 "$pb_extimg"
+ gzip -dc /usr/share/piratebox/OpenWRT.img.gz >"$pb_extimg"
+ [ -d "$pb_extmount" ] || mkdir -p "$pb_extmount"
+ pb_uciset fstab piratebox mount \
+ target=$pb_extmount \
+ device=$pb_extimg \
+ options=loop \
+ enabled=1
+}
+
+pb_init() {
+ echo "${initscript}: Mounting external USB storage..."
+ pb_mount usb
+ [ -d "$pb_usbdir" ] || mkdir -p "$pb_usbdir"
+
+ if pb_needswap
+ then
+ echo "${initscript}: Creating swapspace..."
+ pb_mkswap
+ fi
+
+ if pb_needextimg
+ then
+ echo "${initscript}: Creating ext loop image..."
+ pb_mkextimg
+ fi
+
+ pb_mount ext
+
+ # prepare opkg for /usr/local
+ grep "^dest piratebox" /etc/opkg.conf \
+ || echo "dest piratebox $pb_extmount" >>/etc/opkg.conf
+
+ # link ext usr to /usr/local
+ ln -sf "$pb_extmount/usr" /usr/local
+
+ # fix for ext packages not installing to usr
+ [ -d "$pb_extmount/usr/lib" ] || mkdir -p "$pb_extmount/usr/lib"
+ ln -sf "$pb_extmount/usr/lib" "$pb_extmount/lib"
+}
+
+pb_update() {
+ pb_mount ext
+
+ # disable local DNS redirection
+ pb_nodns
+
+ # CAVE: ugly hack
+ # in case we were called from the postinst, remove opkg lock
+ [ -e /var/lock/opkg.lock ] && \
+ mv /var/lock/opkg.lock /var/lock/opkg.lock_
+
+ # install python to piratebox storage
+ if ! opkg update
+ then
+ echo "${initscript}: ERROR: No Internet connection on WAN port" >&2
+ exit 5
+ fi
+ opkg -d piratebox install python
+
+ # CAVE: ugly hack
+ # in case we were called from the postinst, restore opkg lock
+ [ -e /var/lock/opkg.lock_ ] && \
+ mv /var/lock/opkg.lock_ /var/lock/opkg.lock
+
+ # get PirateBox files
+ [ -d "$pb_share" ] || mkdir -p "$pb_share"
+ mkdir -p "$pb_share"
+ cd "$pb_share"
+ wget http://daviddarts.com/piratebox/piratebox-logo.png -O piratebox-logo.png
+ wget http://daviddarts.com/piratebox/.READ.ME.htm -O .READ.ME.htm
+ wget http://daviddarts.com/piratebox/.BACK.TO.MENU.htm -O .BACK.TO.MENU.htm
+ # adjust hostname
+ sed -i "s#://http://piratebox#http://$pb_hostname#g" .READ.ME.htm
+ sed -i "s#://http://piratebox#http://$pb_hostname#g" .BACK.TO.MENU.htm
+
+ # install PirateBox droopy port
+ [ -d /usr/local/bin ] || mkdir -p /usr/local/bin
+ cd /usr/local/bin
+ wget http://daviddarts.com/piratebox/droopy -O droopy
+ chmod +x droopy
+}
+
+pb_start() {
+ pb_mount usb ext swap
+
+# PirateBox Welcome HTML
+local html=$(cat <<EOF
+<p><br>
+ <b>1.</b> Learn more about the project
+ <a href=\\"http://$pb_hostname:8001/.READ.ME.htm\\"><b>here</b></a>.
+</p>
+<p>
+ <b>2.</b> Click above to begin sharing.
+</p>
+<p>
+ <b>3.</b> Browse and download files
+ <a href=\\"http://$pb_hostname:8001\\"><b>here</b></a>.
+</p>
+EOF
+)
+
+ export PATH=$PATH:/usr/local/bin
+ export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib
+ cd "$pb_share"
+ start-stop-daemon -S -x python \
+ -p /var/run/${NAME}.droopy.pid \
+ -c nobody:nogroup -m -b --
+ /usr/local/bin/droopy \
+ -p "$pb_share/piratebox-logo.png" \
+ -d "$pb_share" -m "$html"
+ start-stop-daemon -S -x python \
+ -p /var/run/${NAME}.simple.pid \
+ -c nobody:nogroup -m -b -- \
+ -m SimpleHTTPServer 8001
+}
+
+pb_stop() {
+ # killing all piratebox processes
+ local pidfile
+ for pidfile in $(ls /var/run/${NAME}.*.pid 2>/dev/null)
+ do
+ start-stop-daemon -K -s HUP -p "${pidfile}"
+ rm -f "${pidfile}"
+ done
+ [ -z "${pidfile}" ] && echo "${initscript}: not running"
+ sleep 3
+ pb_umount usb
+}
+
View
12 src/data/etc/piratebox.conf
@@ -1,12 +0,0 @@
-# PirateBox config.
-pb_ip="192.168.23.1"
-pb_wireless_ssid="PirateBox"
-pb_hostname="piratebox"
-pb_usbdevice="/dev/sda1"
-pb_usbmount="/mnt/usb"
-pb_usbdir="$pb_usbmount/PirateBox"
-pb_share="$pb_usbdir/Shared"
-pb_extimg="$pb_usbdir/OpenWRT.img"
-pb_extmount="/mnt/ext"
-pb_swapimg="$pb_usbdir/OpenWRT.swap"
-

0 comments on commit d614737

Please sign in to comment.