Cleaning Linux: Jed's Nappy/Boot

My home NAS machine is an Ubuntu 14.04 machine with a ZFS volume. I need the linux-headers packages in order to compile my ZFS dkms modules. Those take more space than the kernels tend to, so I try and stay on top of removing them. Wonder how many I have?
  • > dpkg-query -l linux-header* | grep 'ii ' | wc -l
  • 45
Oh. Guess I got a bit lazy there. Notice how I combine dpkg-query and grep ‘ii ‘ (with a space after ‘ii’). This filters out what’s installed. However, I have kernels to boot into that I have booted yet and I need to filter out which of those actual packages are behind my present kernel and not un-install anything newer.
dpkg-query -l linux-header* | grep 'ii ' | while read k ; do  v=`echo "$k"| cut -d- -f4|cut -d' ' -f1` ; [ ! -z "$v" ] && [ "$v" -lt 65 ] && echo $k|cut -d' ' -f2 ; done
Now, I had to work on that one for a bit. I really did it as a one-liner but I would up-arrow and edit up-arrow and edit until I got it printing what I wanted. Let’s look at it a bit more like a shell script:
  • dpkg-query -l linux-header* \
  • | grep 'ii ' \
  • | while read k ; do \
  • v=`echo "$k" \
  • | cut -d- -f4 \
  • | cut -d' ' -f1`;
  • [ ! -z "$v" ] \
  • && [ "$v" -lt 65 ] \
  • && echo $k \
  • |cut -d' ' -f2
  • done
And that gives me output like:
linux-headers-3.13.0-59
linux-headers-3.13.0-59-generic
linux-headers-3.13.0-61
linux-headers-3.13.0-61-generic
linux-headers-3.13.0-62
linux-headers-3.13.0-62-generic
linux-headers-3.13.0-63
linux-headers-3.13.0-63-generic
…but much more. I’m going to uninstall these immediately by piping this to xargs dpkg -r:
dpkg-xargs
And what else do I need to remove? Well, a whole lot of installed kernels, too:
  • > dpkg-query -l linux-image* | grep 'ii ' \
  • | fgrep '3.5' | awk '{print $2}'
linux-image-3.5.0-25-generic
linux-image-3.5.0-27-generic
linux-image-3.5.0-38-generic
linux-image-extra-3.5.0-25-generic
linux-image-extra-3.5.0-27-generic
linux-image-extra-3.5.0-38-generic
And of course, we remove those by piping to xargs dpkg -r again. I will not bother you with the output of that because it’s pages of post-install grub output.
But that just leaves me with two kernels installed and I’ve cleaned up a lot of space.

No comments :