FreeBSD Suspend with VirtualBox Running

18 October 2020

I often have a couple of Virtual Machines running on my FreeBSD laptop. The main one is a Debian Linux VM that I use to run a couple of pieces of software that don’t support FreeBSD and that I need for work. I pretty much have this running 100% of time, since one of the things I use it for is Skype.

When I first set up my laptop, I was happy to get Sleep / Resume working. And then one day it stopped working, I could suspend fine, but I’d get a kernel panic on resume. Not fun. After some troubleshooting I discovered that the panic occurred if I had VirtualBox running. I was unable to find a fix for this, so a workaround would have to do. Suspending all my running VMs before Sleep works, but its kind of tedious to have to remember to suspend them before closing the lid, and then restart them after, so my goal was to script it to automatically suspend the VM when I closed the lid, and bring it back up on resume.

After some trial and error, the following changes did the trick.

  • In /etc/rc.suspend I added the lines
/usr/local/bin/VBoxManage controlvm debian-vm savestate 
/bin/sleep 3
/bin/pkill VirtualBox
/bin/pkill VBox
  • In /etc/rc.resume I added the line
    /usr/local/bin/VBoxManage startvm debian-vm --type=headless
    

My VM, obviously is called debian-vm. I run it headless since I don’t want a GUI on my desktop, I use it by ssh’ing into it and forwarding X with -X. If you wanted to be a bit more clever, you could change the command to look up all the running VMs and suspend them all. The sleep and pkill on suspend is kind of hacky, but I found it was necessary to reliably not panic. I ended up also adding some pkill -9 for those two processes later on just to make sure.

Since I wanted the VM always running, I also added the startvm command to /etc/rc.local and the savestate command to /etc/rc.shutdown

No more annoying kernel panics on Resume.

FreeBSD Hibernate to Disk on a Thinkpad T450

16 October 2020

FreeBSD is a great operating system, even on a laptop, but one thing it just doesn’t do is Hibernation. On some laptop models you can get working Sleep (S3), but in that state the laptop is in a low power mode and still draining power from the battery to keep RAM refreshed.

I knew I wanted to run FreeBSD on a laptop, but also knew I wanted Hibernation, so after some research I discovered the Intel Rapid Start Technology, a feature on some systems to allow a system to Hibernate without the involvement of the Operating System. FreeBSD just goes to sleep normal, and if kept in S3 longer than a threshold set in BIOS, the Computer will write its memory to disk and turn off. Then upon tuning on, realize It is hibernating, copy the data back to memory and let the OS Resume normally. the OS has no idea anything other than a normal sleep ha occurred. Sounds great, here’s what was needed to get this working on my FreeBSD Thinkpad T450

Requirements

  • A Laptop with Intel Rapid Start Technology. Thinkpad dropped this feature with the T460, so the T450 and T550 are the highest spec Thinkpads where this works.
  • The feature must be enabled in the BIOS and configured how long to wait when sleeping before hibernating
  • An SSD Drive, with a regular old HDD the feature will just silently fail
  • A dedicated partition on your SSD at least as large as your physical RAM. For GPT, the partion guid must be D3BFE2DE-3DAF-11DF-BA-40-E3A556D89593 for MBR the partion type must be 0x84

I made sure to purchase my T450 with an SSD and When I installed FreeBSD on my Thinkpad I was sure to create the hibernation partition. I actually created mine at 16Gigs even though I only have 8Gigs or RAM just in case I ever decide to max out the RAM.

Results

Turns out it works great. I put my laptop to sleep normally by closing the lid, and if I reopen it in less than 3 hours FreeBSD resumes normally, however if its been >3 hours, the laptop will be powered off and opening the lid does nothing. I then press the power button and am greeted by a special banner on th BIOS screen letting me know its resuming from Disk, then the normal FreeBSD resume process takes over and I’m back where I left off

Caveats

I’ve only noticed one issue using this feature in FreeBSD:

After Sleep -> Resume, sound works fine, but after Sleep -> Hibernate -> Resume, the speakers work fine but audio jack doesn’t. Plugging in a cable disables the speakers, but the audiojack still outputs silence. The work around is after waking up the laptop, put it back to sleep and resume immediately. This restores the audiojack. Generally only a slight annoyance, as long as I remember to do the quick Sleep -> Resume cycle before someone calls me on Skype.

FreeBSD on a Thinkpad T450

16 October 2020

I’ve been running FreeBSD 12.1 on a Thinkpad T450 from ebay for the past ten months, and I’ve been very happy with it. This was my first time running FreeBSD on actual hardware as opposed to in a Virtual Machine, so there was a bit of a learning curve getting everything set up and working.

The Thinkpad T450 was released in 2015. I paid $180 for one on eBay with the following specs:

  • Intel Processor i5-5300U @ 2.30Ghz
  • 8 Gigs RAM
  • 14” screen @ 1600x900 resolution (Intel i915)
  • 256 Gig SSD
  • Build in SmartCard Reader

I bought this computer as a lightweight portable system to complement my much larger and heavier Toshiba which runs Windows and had been serving as my ‘Desktop Replacement’ at the time. (I’ve since upgraded to an actual Desktop running GhostBSD). I was a bit concerned because the T450 has half the performance and half the RAM as my larger system, but in practice, I’ve never thrown more at this laptop than it can handle. I’ve patched and recompiled my kernel, recompiled bhyve, I’m always running a couple of VMs and probably a few too many tabs in Chromium, but I’ve never had a performance problem. FreeBSD runs smoother on this laptop, than Windows 10 does on my twice as powerful system.

What Works

  • Built in ethernet and Wifi (Though Wifi is slow on FreeBSD regardless of hardware)
  • Screen Backlight Adjustment
  • Touchpad / Trackpoint
  • Accelerated Graphics (I had to install drm-next from ports, the one from pkg caused a kernel panic)
  • Audio - Speakers and audio jack, including automaticly switching between the two)
  • SmartCard Reader
  • Fingerprint Reader (I only tested this as far as trying out the fprint-demo, but it was able to recognize my fingerprint)
  • Sleep / Resume, including the ability to Hibernate to Disk, thanks to the Intel Rapid Start Technology

The Problems

  • After Sleep -> Resume, sound works fine, but after Sleep -> Hibernate -> Resume, the speakers work fine but audio jack doesn’t. The work around is after waking up the laptop, put it back to sleep and resume immediately. This restores the audiojack.
  • Monitor Hotplug and XOrg. If I plug in a monitor, I have to restart XOrg for xrandr won’t see the change. I opened This Support Forum Thread but haven’t been able to resolve it. This was a bigger problem for me when I was trying to use the laptop as my main desktop system as well, ever since I got a dedicated desktop, this no longer bothers me.
  • Battery life is generally 2-4 hours, which isn’t great, but then again this is a used laptop from 2015, so that might just be the condition of the battery.
  • I’ve had the entire system hang 3 or 4 times on my in the ten months I’ve had it. Everything just locks up hard and I have to reboot
  • My experience with trying to use the laptop with a docking station has been mixed, I’ll write a separate post about that.

Overall FreeBSD on this laptop has been a great experience. There was a steep learning curve and a lot of troubleshooting for the first few weeks, but now that its set up, I really like it. When I first bought the laptop I was concerned about getting everything working well, and figured that if nothing else I could just install Linux, but my results so far have made me a fan, and I’ve since brought a second BSD system (My desktop which dual boots GhostBSD/NetBSD) into my life.