# using opkg upgrade for Beaglebone rev A6 (white)/Angstrom

# Using Angstrom v2012.05 3.2.18 kernel, customized oebb image
# Motivated by need to build digi RealPort kernel modules on target, requires kernel headers
# When kernel headers package installed:

sudo opkg install kernel-headers
sudo opkg install kernel-headers-dev

# the kernel headers installed are for 3.2.42, and the drivers build OK, but won’t load under
# the 3.2.18 kernel b/c  they are built against 3.2.42 headers.

# opkg upgrade attempts to upgrade *everthing*, including the kernel (in this case, 3.2.18 -> 3.2.42), 

sudo opkg update
sudo opkg upgrade

# This would bring the headers and kernel into alignment;
# However, this doesn’t work cleanly, at least for 3.2.18:
# - by default, opkg uses /tmp to stage installation, which is too small and runs out of space
# - installing kernel code on a running system can break it, especially if things don’t go well
# - Some of the install scripts are slightly broken
# Even when the upgrade goes OK, none of the kernel modules will work until kernel uImage is 
# replaced

# The basic steps to making it work are these:
# - use opkg to download (but not install) the upgrades to a cache directory
# - put some links in place to fix some of the broken scripts
# - only after things are downloaded, use opkg to do the upgrade from the cache
#    using a temporary staging directory
# - fix the remaining broken scripts (created during the upgrade)
# - Re-run the upgrade to complete it

# Here’s the step-by-step:
# What you need:
# - plenty of space (on sdcard) for the packages and staging
# - a good network connection

# Make a cache directory and a working (tmp) directory (here, on the sdcard, /mnt/xfoce)
mkdir /mnt/xfoce/cache
mkdir /mnt/xfoce/tmp

# Make symlinks to fix some broken paths in the upgrade installation scripts:
sudo ln -s /sbin/depmod /bin/depmod
sudo ln -s /usr/sbin/update-modules /bin/update-modules
sudo ln -s /usr/sbin/update-rc.d /bin/update-rc.d

# With a good network connection, update the opkg repository listngs and download packages. 
# This may takes several minutes…

sudo opkg update
sudo opkg —cache=/mnt/xfoce/cache —tmp-dir=/mnt/xfoce/tmp —download-only upgrade

# If it fails, retry until it succeeds
# Once everything is downloaded, install the upgrades:

sudo opkg —cache=/mnt/xfoce/cache —tmp-dir=/mnt/xfoce/tmp upgrade

# In my case, this ran OK until the very end, at which point the following errors occurred

Configuring libsystemd-id128-0.
/mnt/xfoce/tmp2/opkg-SvwGEn/opkg-intercept-OA6P7W/update-modules: line 1: update-modules: command not found
/mnt/xfoce/tmp2/opkg-SvwGEn/opkg-intercept-OA6P7W/depmod: line 1: depmod: command not found
/mnt/xfoce/tmp2/opkg-SvwGEn/opkg-intercept-OA6P7W/depmod: line 2: depmod: command not found
/mnt/xfoce/tmp2/opkg-SvwGEn/opkg-intercept-OA6P7W/depmod: line 3: depmod: command not found
Collected errors:
 * pkg_run_script: package "dbus-1" postinst script returned status 127.
 * opkg_configure: dbus-1.postinst returned 127.

# The symlinks above should fix these, and the upgrade should just work.

# If the dbus-1 postinst error still occurs, edit /var/lib/opkg/info/dbus-1.postinst and change

update-rc.d -> /usr/sbin/update-rc.d

# If you patched dbus-1.postinst, run the upgrade again; it should complete normally

sudo opkg —cache=/mnt/xfoce/cache —tmp-dir=/mnt/xfoce/tmp upgrade

# Reboot the system:

sudo /sbin/reboot

# However, the kernel modules won’t be loaded:

lsmod
Module                  Size  Used by

# returns nothing. To restore the modules, we have to boot from the new kernel uImage.
# To install this, we need to copy it to boot partition of the sdcard
# mount boot partition
sudo mkdir /mnt/boot
sudo mount /dev/mmcblk0p1 /mnt/boot/

# Copy the kernel image to the sdcard boot partition
sudo cp /boot/uImage /mnt/boot/uImage 
sudo umount /dev/mmcblk0p1 
depmod

# Reboot:

sudo /sbin/reboot

# Now, the kernel version should be correct at the login prompt and via uname

sudo uname -r
3.2.42

# and lsmod should show all the modules
lsmod
Module                  Size  Used by
ip_tables               7854  0 
x_tables               14585  1 ip_tables
g_mass_storage         24058  0 
spidev                  3912  0 
rfcomm                 24391  0 
ircomm_tty             14101  0 
ircomm                  8474  1 ircomm_tty
irda                   85621  2 ircomm_tty,ircomm
hidp                   10240  0 
bluetooth             110309  4 rfcomm,hidp
rfkill                 14673  2 bluetooth
ipv6                  211076  18 

# Now, should be able to rebuild dgrp in usual way:

cd /usr/src/kernel/
sudo make scripts
sudo ln -s /usr/src/kernel /lib/modules/3.2.42/build
cd /mnt/xfoce/build/dgrp-1.9/
./configure
make
sudo make install

# Success!

####################
original working notes
####################

  486  less /var/lib/opkg/info/dbus-1.postinst 
# fix path to update-rc.d (it’s in /usr/sbin)… maybe could just make symlink, as below  
487  sudo vi /var/lib/opkg/info/dbus-1.postinst 

# make symlinks in bin for missing depmod, update modules:
  489  sudo ln -s /sbin/depmod /bin/depmod
  490  sudo ln -s /usr/sbin/update-modules /bin/update-modules
  491  sudo opkg --cache /mnt/xfoce/tmp --tmp-dir /mnt/xfoce/tmp2 upgrade

re-run upgrade:
cd /mnt/xfoce
sudo opkg —tmp-dir /mnt/xfoce/tmp2 —cache /mnt/xfoce/tmp upgrade

runs OK

Now, can rebuild dgrp in usual way!:
 452  cd /usr/src/kernel/
  455  sudo make scripts
   457  sudo ln -s /usr/src/kernel /lib/modules/3.2.42/build
  458  cd /mnt/xfoce/build/dgrp-1.9/
  459  ./configure
  460  make
  461  sudo make install
  462  sudo digicfg -a xf-sp1-0.shore.mbari.org -c init -i XP

works, but still no modules: need to copy new kernel image to sdcard and run depmod

mount boot partition
  485  mkdir /mnt/boot
  486  sudo mkdir /mnt/boot
  487  sudo mount /dev/mmcblk0p1 /mnt/boot/
  488  ls -l /mnt/boot/
  489  ls -l /boot/
  490  sudo cp /boot/uImage /mnt/boot/uImage 
  491  sudo umount /dev/mmcblk0p1 

cd /usr/lib/modules/3.2.42
sudo depmod
sudo /sbin/reboot