# Suspend/Resume to Disk  - Hibernation - iFFS/IRST Partition (Intel Fast Flash/Intel Rapid Start Technology)



## Mjölnir (Jun 17, 2020)

What is this and how does it work?

On newer Intel-based computers, the UEFI/BIOS offers a switch to enable a iFFS/IRST partition to support suspend/resume.
This is called _"Intel Rapid Start Technology"_. It affects mainly mobiles (laptops & tablets), but also desktop and maybe server systems.
Maybe there's similar functionality on AMD based systems?
This can serve as a _work-around_ to _suspend to disk_ for those who can not directly _suspend to disk_ (aka _hibernate_)
EDIT: Either the following is intended by the IRST specs, or a _"feature"_ of my UEFI: When I press the power button quickly after suspending (within ~5 seconds), the system enters _S4 state_.  This way, I can emulate _hibernation_ w/o having to wait for the timer to _fire_
ACPI Power State Cheat Sheet (S0-5) Note that this might not 100% accurate...

On _suspend to RAM_ a countdown timer is started.  The system enters the _S3 state_.
When the timer _fires, _the UEFI/BIOS writes the contents of the RAM to this partition, flags it, the system enters _S4 state_ and is switched off.
Obviously, this saves much energy (battery runtime), since a system that is switched off does not need to refresh the RAM like in _S3 state_. I.e. it needs zero except the self-drainage of the battery plus what the ME needs to run.
On restart, the UEFI/BIOS checks the iFFS/IRST partition for the flag.
If it is flag'ed, the system boots from it, bypassing the active boot partition and usual boot procedure (efiboot->...->FreeBSD loader(8))
Prerequisites

Make shure your system successfully resumes from _suspend-to-RAM_.
(mouse, USB, graphics device, etc.) Consult the handbook, wiki, and search the forum.
Check your UEFI/BIOS settings under _"Power"_ to see if your system supports IRST.
_Watch out for "Intel Rapid Start", "Intel Fast Flash", "IRST", "iFFS" or similar_
!The partition's size must be at least equal to the amount of RAM!
Check if you already have such a partition -- it's type is "!D3BFE2DE-3DAF-11DF-BA40-E3A556D89593"
`gpart show ada0`
This only works for GPT partition scheme.  You want that anyway.
Probably this only works with EFI boot; I did not try with the old legacy BIOS boot method.
NOTE: the IRST partition can not be used as swap device, unless IRST is disabled in the UEFI/BIOS or  the system will never _suspend-to-RAM_. It can be used as a dump device, since a crashed kernel will not _suspend-to-RAM_ after the crash.
_Using the IRST partition as dump device is not possible yet.  A patch is in the works._
Usually, one can not directly _suspend-to-disk_ (_S4_) to the IRST partition without going through _S3 state_ (_suspend-to-RAM_) first - this is completely out of control of the OS and managed solely by the UEFI's ACPI.
On some systems, it is neccessary to disable the TRM/TTP security chip.  So if you need TRM/TTP running, chances are it will not work for you.
Howto

Either change the type of your existing swap partition: Step #3 or...
_On ZFS, you can instead swap to a ZVOL:  zfs create -b 4k -V zpool/SWAP and add that to /etc/fstab
 Currently, you can not use the IRST as a dump device.  I'm working to provide a patch for this. AFAIK, dumping to a ZVOL swap is not possible either._
Create a partition of the size of your RAM and any enlargements you plan to stick in:
`gpart add -t ms-basic-data -i 3 -s 12G ada0` The type does not matter and will be changed later.
_Adjust "12G" to the size of your RAM, the index of the partition and name of your disk device_
Or when the installer reaches the task to partition the disk/SSD, let the installer create it.
Either give the partition a type not used by FreeBSD, or open a shell and...
Change the partition type to the iFFS _magic number_: `gpart modify -i 3 -t \!D3BFE2DE-3DAF-11DF-BA40-E3A556D89593 ada0`
_Replace "-i 3" with the index of the IRST partition, and "ada0" with your disk device and number.  Note the backslash!_
Optionally label the partition:  `gpart -i 3 -l IRST ada0` _iFFS_ would be a good label, too.
Enable IRST in the UEFI/BIOS and set the timer to a very low value for testing purpose (e.g. 1 minute).
Optionally de-activate or disable the TRM/TTP security chip in the UEFI/BIOS.
_This applies to Lenovo (at least Thinkpad T450s, very likely other models as well) and probably other brands, too._
In the running system, enter _suspend-to-RAM_:
`zzz` or `acpiconf -s 3`, or use your GUI's way to do that.
_If zzz complains, do  sysctl hw.acpi.suspend_state=S3_
Optionally, start the timer of you mobile phone. Wait until the timer _fires_.
_Most computers will give feedback by a slowly blinking LED light, or in a small status LCD. Listen to the noise of the fan and disk_
After the timeout, it takes a few seconds to save the contests of RAM to disk, then the system switches off.
_LED/LCD goes off, fan and disk will stop_
Start the computer the usual way: Press the Power button or issue a wakeup packet on the network.
_The system will show something like "resuming from deep sleep state"_
If the system restarts successfully into the previous state:
Click _Thanks_ on this posting to give some feedback, so we can guess for how many it works.
In the UEFI/BIOS, set the timer to a larger value, e.g. 15 minutes, else switch IRST off completely.
_See topic #6: TRM security chip and 1st topic of the Prerequisites_
Help, It did not work!

If _suspend-to-disk_ does not work with the IRST partition, it can be used otherwise, e.g. as swap or /var/tmp. Of course you'll have to change the partition type accordingly:
`gpart modify -i 3 -t freebsd-swap` and optionally add a ZVOL to enlarge the swap space.
Destroy the partition with gpart(8) and enlarge it's neighbor to use the space with `gpart resize`
A ZFS zpool(8) can be enlarged to use new diskspace: `zpool set autoexpand=on`.
Use growfs(8) to enlarge an UFS filesystem.
See also
gpart(8), zpool(8), zzz(8), acpiconf(8), `sysctl hw.acpi | grep -E 's(leep|uspend)'`

Good luck!
_If you are a native english speaker and want to correct my "gernglish", kindly ask the moderators to allow you to polish this._


----------



## Mjölnir (Sep 16, 2020)

I forgot to note to set `hw.acpi.s4bios=1` in sysctl.conf(4) if your system resumes correctly from S3 state (suspend to RAM).
EDIT The sysctl(8) knob `hw.acpi.s4bios` enables the OS to go directly into S4 state (suspend to disk), and must be supported by the UEFI/BIOS.  It does not work for me (does not resume), YMMV.


----------



## a6h (Sep 16, 2020)

Thanks for tutorial. I appreciate your low-level introduction to tutorial. Providing an in-depth introductory remarks before beginning the lesson
is a good style of writing. Everyone should do that. Tutorial without in-depth introduction is mechanical and not very helpful. Thanks.


----------



## Crivens (Sep 18, 2020)

Dang! I have set up my SSD without a swap because I already use a ZVOL. 

Also a caveat: you can only enlarge partitions upwards wits ufs because the superblock would not be found otherwise. No Idea right now about ZFS, my memory says something about markers at the end, but that might not be an issue.


----------



## Mjölnir (Sep 18, 2020)

Crivens said:


> Dang! I have set up my SSD without a swap because I already use a ZVOL.


Dong!  IIRC, a ZVOL can't be used as a dump device, thus this is not recommended on a single disk setup, anyway.


Crivens said:


> Also a caveat: you can only enlarge partitions upwards wits ufs because the superblock would not be found otherwise. No Idea right now about ZFS, my memory says something about markers at the end, but that might not be an issue.


Yes, we have growfs(8) but no shrinkfs(8).  Dito for ZFS: there is `zpool set autoexpand=on`, but neither `zpool shrink` nor `zpool resize`.  The only shrinkable filesystems I know are FAT, NTFS & ext{2,3,4}fs.

A good nerd will regularly backup valuable data anyway.  You're doing that, don't you?  So then you can reinstall with a IRST partition, after you thoroughly verified your backups.
When did you latest verify your backups & excersized a recovery manœvre?


----------



## Crivens (Sep 18, 2020)

Last backup - two weeks (not doing much atm)
Last desaster check - 3 Month or so. Had to pull some stuff I accidently nuked out of that  second backup (yes, two different kinds/ways to backup, so if one fails...)


----------



## Mjölnir (Sep 18, 2020)

The more people verify & confirm the procedure described in this howto is more or less sound, the sooner it will go into the Handbook or an article in the FreeBSD docs.  Maybe eventually the necessary steps can be added to the bsdinstall(8).  IMHO 4 feedbacks until now, plus one who reported to me via e-mail, is too less.
EDIT I did a reinstallation + data migration after I found out how to gain hibernation.  Cost: ~1 day.  Good opportunity to get rid of superfluous stuff, too.


----------



## Crivens (Sep 18, 2020)

Can we please have a tool to resize and move ZFS partitions? This absolute genius did indeed add a swap partition - of 2GB. I could hit my head into a pillow. So you all be warned.

Edit: point being, simply by spending these few GBs, would have saved me the trouble. Now I would need to invest more time in this than can be recovered should it work.


----------



## Mjölnir (Sep 18, 2020)

Crivens said:


> Can we please have a tool to resize and move ZFS partitions? This absolute genius did indeed add a swap partition - of 2GB. I could hit my head into a pillow. So you all be warned.


The genius is bsdinstall(8)?  File in a bug report.  Well, depending on the size of your RAM, that might be large enough to hold a minidump -- IIRC that's standard; full dumps must be enabled by `sysctl debug.minidump=0`.  From my experience, a ratio of 1:3 for swap/dump partition:RAM is sufficient for a minidump.  Thus you can go on & create a 2nd swap on a ZVOL, or choose manual partitioning in the installer & enlarge the swap partition to 1-2 x RAM for RAM >= 4 GB.  For RAM <= 4 GB swap should be at least 2 x RAM.

On a SSD, it's fine to have `sysctl vfs.zfs.mdcomp_disable="1"` right from the start before installation starts & put that into loader.conf(5) (in /tmp/bsdinstall/... or such).  This will speed up metadata access on a SDD for operations on large # of files.


----------



## Crivens (Sep 18, 2020)

mjollnir said:


> The genius is bsdinstall(8)?  .


Nope, that genius was typing away on a root console with one hand, possibly holding some scotch in the other, setting up a new SSD on the USB2SATA to be good for a zfs send receive to clone the running system onto.


----------



## Snurg (Feb 10, 2021)

Wow... thats a great guide!
Sadly I have no UEFI computer, so I cannot test this.

Do you think the new LUA boot loader could do what most Linux boot loaders can do, check flag, load memory contents from swap, and then enter the existing S3 restoration routine?
If it would it be (relatively) easy to add such thing to the new bootloader, we could make FreeBSD able to do S4 hibernate.
For energy saving and other reasons I consider this a very important feature.

(If this cannot not be done using the Lua bootloader, would it be possible, maybe even easy using the old Forth bootloader?)


----------



## grahamperrin@ (May 11, 2021)

Off-topic …
… is it my imagination, or did I recently read of an approach that does *not* require FFS/RST? 

(I recall reading something very concise. Stupidly I didn't bookmark the page. I recall only that the approach was not for all types of hardware.)



Mjölnir said:


> (_suspend to swap_) never worked from my limited experiences.


----------



## MasterOne (May 12, 2021)

Interesting, didn't know about that approach at all. Not sure if my laptops support this, and too bad that the regular suspend-to-disk / hibernation has never been implemented in FreeBSD as I know and used it with Linux.


----------

