Delayed hibernation on Linux with SystemD

One thing that I liked on modern laptops with MS Windows or Apple macos was this power saving technique od delayed hibernation. On set timeout of inactivity or after closing laptop’s lid it goes into suspend and then after sometime into hibernation. That’s great because it stops battery drainage needed to support RAM during regular suspend. With it I can go for days of work without the need to recharge my laptop. From many methods to achieve this with SystemD this one worked always and keeps working for me to this day.

But first things first. You need to check if your machine is capable of hibernation. Generally you need to have a swap partition at least a little bigger then your RAM capacity. My machine is also encrypted with LUKS so my kernel line has the following line included:

  resume=/dev/mapper/vg0-swap

While adding it to your bootloader config make sure that you account for your distro specific variables like different name of swap partition in LVM config (you can check it by listing /dev/mapper/). Additionally my mounting of swap partition in /etc/fstab goes like this:

UUID=r5f026te-e9d2-4973-81fd-998e04dae75f    none    swap    defaults    0 0

After your swap is properly setup you can go ahead and check if hibernation works by executing:

sudo systemctl hibernate

If your laptop went dark after a minute or so and resumed after you pushed the power button you’re good to go. Now it’s time for the actual delayed hibernation setup.

Backups, backups, backups. To stay on the safe side if you have file /etc/systemd/system/systemd-suspend.service present, make a copy:

sudo cp /etc/systemd/system/systemd-suspend.service /etc/systemd/system/systemd-suspend.service.orgi

Then we do the actual change to replace original suspend action with our desired one:

sudo ln -s /usr/lib/systemd/system/systemd-suspend-then-hibernate.service /etc/systemd/system/systemd-suspend.service

And that’s mostly it. Now we can adjust the timeout after which our machine will go from suspend to hibernation. This one takes some experimentation. For me optimal value is 15 minutes. If I didn’t open my laptop after that time I’m probably out commuting to another location or done fo the day. To make the adjustment we edit /etc/systemd/sleep.conf, uncomment the following line and set the time:

HibernateDelaySec=15min

Now any suspend action called by you or any kind of automation will trigger delayed hibernation.

Please note that in different distros documentation you can find many other tutorials and ways to set this up. Most of them is outdated. The one presented above while being the oldest one, is working for me at any point of time and in any GNU/Linux distribution with SystemD.

When you’ll find that you lack some of the files or some other differences please follow this thread on mastodon and feel free to contact me.

For example on Ubuntu you might find service file that you need to symlink under:

/lib/systemd/system/systemd-suspend-then-hibernate.service
Continue Reading

Upgrade your Samsung Galaxy S (GT-I9000) to KitKat

Warning: tasks described below will lead to complete removal of data from your smartphone and might lead to bricking it.

Root and CWM Recovery

It’s good to start by having rooted phone with Android Gingerbread 2.3.5 on board. During the process you might find yourself in hard position of trying to select proper builds for your system so I recommend wiping it completely and flashing software stored in the following package: https://archive.org/details/i9000_galaxysmtd.7z. It contains:

  • Windows drivers for Samsung smartphones;
  • right version of Odin flashing software which works with our smartphone;
  • complete image of gingerbread android, root and kernel with CWM recovery;
  • last release of cyanogenmod 11 for galaxysmtd;
  • selected gapps.

Another warning: You might want to follow these instructions on a Windows system which you’re ready to reinstall afterwards. It might get infected during the process and virtual machine will not suffice.

First step is installation of Samsung drivers and Windows reboot afterwards. Then we’ll start Odin. 1.83 is the last release which supports I9000 correctly, otherwise it might get stuck on „initializing connection”.

Let’s get to the phone. We need to start it in Download mode by pressing and holding VOL DOWN + POWER + HOME until it flashes with yellow icon and text. Then we connect the phone to computer, Odin should react with message containing Added.

Download mode

Now in Odin I9000XXJW4_I9000OXAJW4_XEU we open the following files:

  • PIT: s1_odin_20100512.pit
  • BOOTLOADER: APBOOT_I9000XXJW4_CL1043937.tar.md5
  • PDA: CODE_I9000XXJW4_CL1043937.tar.md5
  • PHONE: MODEM_I9000XXJW4_CL1118084.tar.md5
  • CSC: GT-I9000-CSC-HOME-MULTI-OXAJW4-LITE.tar.md5

Next click on Start. Process will take a while and our I9000 might restart few times, but do not react. When Odin tells you it’s finished close it and disconnect the phone. We need to restart the phone into Downloadmode again by pressing and holding VOL DOWN + POWER + HOME until it flashes with yellow icon and text. Then we connect it to the computer and wait for Odin response.

This time from I9000XXJW4_I9000OXAJW4_XEU_DEODEX_ROOT directory we select these files:

  • PIT: s1_odin_20100512.pit
  • PDA: CODE_I9000XXJW4_ROOT_DEODEX.tar.md5

And click on Start. This process should take less time. When Odin tells us it finished our Samsung will be in the process of another restart. We need to close Odin again, and restart our phone int o Download mode.

Those restarts are necessary in order for the whole process to go smoothly, other wise you might get stuck with Odin not flashing files or not recognizing connected phone.

One last time from CF-Root-XX_OXA_JW4-v4.4-CWM3RFS directory we select CF-Root-XX_OXA_JW4-v4.4-CWM3RFS.tar as PDA and click on Start. Afterwards phone will reboot into system showing us first time wizard. We need to go through until we have our system ready to work.

Upgrade to Android KitKat 4.4

After reconnecting phone to the computer it should announce itself in the system and let us copy files onto it. We copy:

  • cm-11-20150901-SNAPSHOT-XNG3CAO3G0-galaxysmtd.zip
  • gapps-kk-20140606-signed.zip

To the main memory of the I9000, not the SD card. Than disconnect the phone.

This time we need to boot the phone into Recovery mode by pressing and holding VOL UP + POWER + HOME until it displays another menu of Clockworkmod recovery. We go through it using VOL UP (up), VOL DOWN (down) and HOME (select). Before flashing new system on our Samsung we need t finish the following tasks:

  • wipe data/factory reset
  • wipe cache partition
  • wipe dalvik cache (from advanced submenu)

Now that our phone is wiped clean we can install CM11 by choosing install zip from sdcard option, selecting cm-11-20150901-SNAPSHOT-XNG3CAO3G0-galaxysmtd.zip from its memory and confirming the task. After the start phone will reboot few times showing different screens, please do not react and wait for it to finish.

When it’s done it will show you different Recovery screen ready for another tasks. Now we flash gapps by selecting install zip from sdcard option, selecting gapps-kk-20140606-signed.zip and confirming the process. This time phone will not restart, just wait patiently for it to finish. Et voilà, please restart your phone by choosing reboot system now. After standard Samsung screen you’ll see Cyanogenmod’s boot screen. This start will take a long time so please wait for it to finish. When you see first time wizard you have succeed and are ready to go!

Recovering I9000 from brick

There are many ROMS and versions available, including Android 5, 6 and even 7. Not all of them are as easy to flash and as stable as CM11. Users often note high battery usage and overall slowdown of the system. There are also failing installs which left you with not working TWRP or bootloop. If you will find yourself in one of those brick situations or in need to reverse to the stable system please follow Root and CWM Recovery section of this guide. You’ll be up and running in no time.

Continue Reading

History migration from ZSH to BASH

I’ve decided to move back from zsh to bash shell. It appeared that history migration is much bigger problem in this direction, mostly due to prefix added by zsh:

: 2145123543:0;git push origin master

Numbers are random, I’ve just wanted to show the example. I don’t know if it was created by my specific zsh config based on [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh] or if zsh does that by default. Fortunately there is this simple one-liner which will solve the problem and migrate your history in seconds:

cat ~/.zsh_history | sed 's/\:...........\:.\;//g' > ~/.bash_history

It removes the prefix and saves the file in bash default location. After reloading bash you’ll be able to search through your history. Another tip that I like to use in my computing is .inputrc modification which does the autocomplete based on the history on any stage of building the command, by using arrow keys “↑” and “↓”. You can find it in my configs repository and put it in your home directory.

Continue Reading