SSD

How to set up SSDs on Gentoo Linux. This article assumes you know how to set up, partition, and format a mechanical hard drive.

rootfs
You don't want to use -odiscard on a rootfs mount. discard is the "TRIM" command that tells the SSD to do its magic. Having this running potentially constantly may cause performance degredation (on old SDDs probably, on modern SDD the default is to use discard), and there are articles to suggest this all over the web. Rather, you want to use:

on a command line, as root. You can set this up as a cron (see below) to run twice a day, which should suffice for rootfs.

mountpoint
If you're going to have a low write directory mounted on SSD, using "discard" option will be fine in fstab. See below. If you're going to mount a database to an SSD, you probably want timed TRIM commands rather than the discard option.

Partitioning
You can use usual fdisk (sys-apps/util-linux>=2.24.1-r2). Here's example for GPT partition table with BIOS motherboard. Use "fdisk /dev/sda" to run fdisk, use m option for help. Type g and [Enter] to create a new empty GPT partition table on your SSD. Type n [Enter], [Enter], [Enter], +2M [Enter], t [Enter], 4 [Enter] to create BIOS boot partition.

Command (m for help): n  Partition number (1-128, default 1): First sector (2048-117231374, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-117231374, default 117231374): +2M Created a new partition 1 of type 'Linux filesystem' and of size 2 MiB. Command (m for help): t  Selected partition 1 Partition type (type L to list all types): 4 Changed type of partition 'Linux filesystem' to 'BIOS boot partition'. Then we create 2GB swap partition:

Command (m for help): n  Partition number (2-128, default 2): First sector (6144-117231374, default 6144): Last sector, +sectors or +size{K,M,G,T,P} (6144-117231374, default 117231374): +2G Created a new partition 2 of type 'Linux filesystem' and of size 2 GiB. Command (m for help): t  Partition number (1,2, default 2): Partition type (type L to list all types): 15 Changed type of partition 'Linux filesystem' to 'Linux swap'. Then we create system root partition and write changes:

Command (m for help): n  Partition number (3-128, default 3): First sector (4200448-117231374, default 4200448): Last sector, +sectors or +size{K,M,G,T,P} (4200448-117231374, default 117231374): Created a new partition 3 of type 'Linux filesystem' and of size 53.9 GiB. Command (m for help): p  Disk /dev/sda: 55.9 GiB, 60022480896 bytes, 117231408 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: E19B261A-21EA-4529-B736-1A4E48FC77A1 Device          Start          End   Size Type /dev/sda1        2048         6143     2M BIOS boot partition /dev/sda2        6144      4200447     2G Linux swap /dev/sda3     4200448    117231374  53.9G Linux filesystem Command (m for help): w  The partition table has been altered. Calling ioctl to re-read partition table. Syncing disks.

So, as you can see, you don't need anything special with new fdisk from linux utils. You can also use alternative common tool that you prefer: gdisk, gparted, parted there if you don't like fdisk. All of them (newer versions) handle partition alignment automatically. But if you have some doubt whether partition is aligned, check that each partition starts on a MiB boundary (multiple of 2048 sectors), which should be fine for any device.

LVM
LVM aligns to MiB boundaries and allows discards by default. No special configuration is required.

In order to TRIM all unused space in your VG, you can use the blkdiscard utility:

Alternatively, there is a discard option in lvm.conf which makes LVM discard entire LV on lvremove, lvreduce, pvmove and other actions that free physical extents (PE) in your VG. However, enabling it will immediately render you unable to undo any changes to your LV layout.

LUKS
If you have your root-device on LUKS, you need to set a kernel option:

Although this depends on your Initramfs implementation. In any case, for TRIM to work on encrypted LUKS devices, they have to be opened with the --allow-discards option.

Formatting
If you can find your erase block size, you can add some extended attributes that may help performance. For software raid, you really should know the erase block size. Consider this information when making your purchase. Regardless of whether you're using RAID or not, setting extended values is known to be beneficial. https://raid.wiki.kernel.org/index.php/RAID_setup#ext2.2C_ext3.2C_and_ext4 http://blog.nuclex-games.com/2009/12/aligning-an-ssd-on-linux/ The modern FAQ you can find on Arch-wiki too: https://wiki.archlinux.org/index.php/SSD

Without knowing the erase block size
#Formatting the rootfs partition /dev/sda3: #Using 4096 byte blocks by default aligns the SSD for writes (see [defaults] section of /etc/mke2fs.conf and also   #man mkfs.ext4 about "-b block-size" and "-T usage-type[,...]" inside it):

mkfs.ext4 /dev/sda3

With knowing the erase block size | this info can be outdated
#Formatting the rootfs partition /dev/sda3: #Using 4096 byte blocks aligns the SSD for writes; #Using ERASE_BLOCK_SIZE / 4 as the stride and stripe width size; #In this example, OCZ Vertex drives have 512 kibbibyte - #Erase Block size, therefore stride/stripe-width = 512/4 = 128:

mkfs.ext4 -E stride=128,stripe-width=128 /dev/sda3

If you are formatting a previously used device and your preferred  does not support bulk discards when creating the filesystem, you can use   (from  or later) before creating the filesystem.

Mounting
Given the considerations above, either add something similar to this: /dev/sda3         /          ext4          defaults,relatime,discard          0 1 Or this: /dev/sda3         /          ext4          defaults,relatime                  0 1 If you choose the latter, see the section on the cronjob needed if you don't want to manually TRIM the drive.

Once you've set up /etc/fstab you can run the following command to have the drive mounted: mount -a

In the same way you can add discard option to the swap line in fstab too. The following example shows you how you can parallelize your SSD disk swap with your SATA disk swap: /dev/sda2         none           swap         sw,pri=3,discard          0 0 /dev/sdb2         none           swap         sw,pri=3                  0 0

Once you've set up swap in fstab you can run the following command to make swap available: swapon -a

cron
Run fstrim -v / from cron twice a day to automatically do "discard": #Mins Hours  Days   Months  Day of the week   command 15    1,13   *      *       *                 /sbin/fstrim -v /

There is also a semi-automatic cronjob available on github called SSDcronTRIM which has the following features:


 * Distribution Independent script (developed on my Gentoo system).
 * The script decides every time depending on the disk usage how often (monthly, weekly, daily, hourly) each partition has to be trimmed.
 * Recognizes if it should install itself into /etc/cron.{monthly,weekly,daily,hourly}, /etc/cron.d or any other defined directory and if it should make an entry into crontab.
 * Checks if the kernel meets the requirements, the filesystem is able to and if your ssd supports trimming.
 * simply install it by running it once without any option and deinstall it with the -d option

Future version should implement:


 * Use of nice and ionice to let trimm only run when the disc is not busy.
 * Overgive options to fstrim (e.g. '-m 1M')
 * support for encrypted filesystem (LUKS)

fstrimDaemon
If your computer turned off when cron scheduled its job, fstrim would not be called at all. You can install fstrimDaemon instead to solve this problem.

tmpfs
When using an SSD, administrators generally want to reduce the amount of writes performed. On Gentoo, portage is a huge source of writes, and putting these writes into system memory rather than a disk is beneficial. If you have more than 8gb of ram you may want to consider using tmpfs, which is a way to dedicate some system memory to storage on a temporary basis. It's generally advised to not use more than 1/2 of your available system memory for tmpfs unless you know what you're doing. So let's assume you have 12GB of memory and want to allow up to 6 gigs of tmpfs. Edit /etc/fstab and add: tmpfs			/tmp		tmpfs		noatime,nodiratime,size=6G 	0 0 You can then tell portage to use this as it's compile scratch space in /etc/portage/make.conf: PORTAGE_TMPDIR="/tmp"
 * 1) this may fail on large compiles unless you have >20GB of system memory. scratch space for libreoffice can exceed 8GB, and the entire compile will fail if it runs out of space.
 * 2) Ram used by TMPFS is freed back to the pool when it is no longer in use, so when using portage or other software that uses /tmp you will notice a huge performance gain.

XDG cache
If you are running gentoo on desktop, many programs, using X windows systems (like: chromium, firefox, skype) are making frequent disk I/O every few seconds to cache. Default cache dir is ~/.cache, which is on hard drive.

Edit /etc/env.d/30xdg-data-local and add line:

XDG_CACHE_HOME="/tmp/.cache"

Don't forget:

Warning: With above change, auto unlocking the gnome keyring didn't work anymore. /var/log/messages said the following:

May 5 10:02:52 localhost gnome-keyring-daemon[5561]: couldn't bind to control socket: /home/david/.cache/keyring-hRt5QC/control: No such file or directory