From Mageia wiki
Revision as of 21:46, 10 March 2019 by Psyca (talk | contribs)
Jump to: navigation, search


Drakconf multiflag.png
Other languages
Deutsch ; English ; Français ;


Introduction

SSD are very fast, silent, and are becoming cheaper; so let's use them ! But you need to be careful, without some simple precautions, a SSD may die prematurely because on such a device, erasing/writing cycles can't be so numerous than on a traditional hard disk.

You need to follow some rules so there will be fewer erasing/writing cycles. With these rules in place, recent SSDs can last at least 5 years, even on a loaded server.

Firmware

After purchasing and before using a new SSD it's worth checking that the firmware is up to date. Most manufacturers provide tools to update their firmware and since this is a rapidly evolving technology, important updates may already be available for your new drive.

Partitions alignment

Simple test

Until now, partitioning tools were optimized to create partitions in a hard disk with disks, cylinders and heads. On SSDs those no longer exist. Optimizing partitions mean aligning them with the SSD clusters. To know about your device, try this command in a console with root rights :

# fdisk -lu /dev/sdx

where sdx is your drive (sda for example). The system answers something like:

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x0f2a40f9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    61442047    30720000    7  HPFS/NTFS/exFAT
/dev/sda2        61444089   203736329    71146120+   5  Extended
Partition 2 does not start on physical sector boundary.
/dev/sda5        61444096    84566159    11561032   83  Linux
/dev/sda6        84570112   147669479    31549684   83  Linux
/dev/sda7       147673088   155846564     4086738+  82  Linux swap / Solaris
/dev/sda8       155846628   178546409    11349891   83  Linux
Partition 8 does not start on physical sector boundary.
/dev/sda9       178546473   203736329    12594928+  83  Linux
Partition 9 does not start on physical sector boundary.
[root@localhost ~]#

In this case, the system tells which partitions are not correct (2, 8 and 9). For each partition just divide the number in the column start by 2048 (2048 clusters in a MiB). For example, sda6 starts at 84570112, divided by 2048 gives 41294, that is an integer so this partition is OK.

New partitioning

If what you plan needs a new partioning, do it before starting the installation of Mageia because it is not sure DrakDisk will create aligned partitions. GParted is recommended. If you aren't an expert, Live GParted is more simple (no mounted partitions) :

  • Boot on the Live GParted CD, accept defaults settings and choose your language
  • In the main screen, select your disk (in the upper right corner) and click on New partition (first icon with a green +)
  • In the new dialog, check the field "Free space preceding (MiB)" is set to 1 MiB for the first partition on the SSD (place needed for the MBR). For the next partitions it must be set at 0.
  • The field "Align to" must be on MiB.
  • File system must be on ext4

Gparted1.png

Note:
The command fdisk give sizes in blocks of 512 bytes. So, if with GParted, you started a partition at 1 MiB, fdisk will print 2048 (2048*512=1 MiB)

Partitioning already existing

Too late, Mageia is running and the partitions aren't aligned ! It isn't a problem, GParted can rectify the partitioning. Have a look here: Speed Up Your SSD By Correctly Aligning Your Partitions

The trim command

Presentation

It is impossible to overwrite on a SSD, the system must delete the old data before re-writing. The command trim allows the system to delete the data when the SSD is idling and then gain speed when writing. trim also avoids the need to move data to free space, increasing SSD lifetime.

trim can be effective if you have :

  • Linux-kernel 2.6.33 or later
  • Filesystem ext4, Btrfs, or swap.
  • Hardware interface SATA
Note:
trim doesn't support RAID or SCSI technologies

Using the trim command

There is two ways to enable trim:

  • add the discard option in a configuration file (e.g./etc/fstab). Enabling in then permanent.
  • use the fstrim either in a console or better in a script to automatically enable trim at a chosen time.

The discard option

Note:
As per http://www.howtogeek.com/176978/ubuntu-doesnt-trim-ssds-by-default-why-not-and-how-to-enable-it-yourself/ it is no longer recommended to use the discard

option in /etc/fstab entries, as it slows down every file delete. The only exception, is if a swap partition is on an ssd drive (as swap partitions do not have a mount point). Using your favourite text editor, add the discard option to the entry for the swap, and only for the swap.

To enable the command trim, edit your /etc/fstab for example with vim

# vim /etc/fstab
# Entry for /dev/sda1 :
UUID=a008be25-1898-4149-af3e-6b22260586b0 / ext4 acl,relatime 1 1
# Entry for /dev/sda2 :
UUID=9af64cdc-a611-47c3-821e-94a9fd60251e swap swap defaults,discard 0 0
# Entry for /dev/sda5 :
UUID=4c5e50d9-16c0-4f24-acc9-ee13f899b1d1 /home ext4 acl,relatime 1 2
# Entry for /dev/sdb8 :
UUID=c651fd07-b486-4eee-aaab-4acf5220c763 /home/samageia/multimedia ext2 defaults 1 2
/dev/cdrom /media/cdrom auto umask=0,users,iocharset=utf8,noauto,ro,exec 0 0
# Entry for /dev/sdb1 :
UUID=7A60F79E60F75F77 /media/win_c ntfs-3g defaults,umask=000 0 0
# Entry for /dev/sdb7 :
UUID=445C-DFC4 /media/win_d vfat umask=000,iocharset=utf8 0 0
none /proc proc defaults 0 0

and you have just to add "discard" to the entry for the swap, if it is on an ssd drive. In this example, sda is a SSD

Some help with vim? Strike in this order :

  • i -> mode insertion
  • arrow keys -> to move in the text
  • enter the text
  • esc -> end of insertion mode
  •  : -> mode command, appear in the bottom of the screen
  • wq -> write and quit (or q! to quit without save)

If you are using lvm physical volumes on an ssd drive, edit /etc/lvm/lvm.conf and change the value for issue_discards from 0 to 1.

If you are using encrypted file systems, mounted at boot time, edit /etc/crypttab and add the discard option. For example (using an encrypted file system on lvm)

cat /etc/crypttab 
crypt_vgc_lvc2 /dev/vgc/lvc2 - discard

If manually mounting the encrypted filesystem, or using your own scripts, the --allow-discards option has to be added to the cryptsetup luksOpen command.

The fstrim command

To launch a trim, use the fstrim <filesystem mount point> command in a console with root rights, for example:

Template:Root console The filesystem must be mounted.

To have an automatic action, add the following script to /etc/cron.daily, which will trim all deleted files, without delaying, or interfering with regular write operations. ...

#!/bin/bash
IFS=$'\n'
for device in /dev/sd?; do
    drive=${device##*/}
    if  $(udevadm info -q env -p /block/"$drive"|egrep -q 'ID_ATA_ROTATION_RATE_RPM=0'); then
        # ssd drive found
        mounts=($(grep /dev/"$drive" /proc/mounts|grep -v -w ro))
        # mounts is now a list of writeable mounts on the ssd drive
        for mount in "${mounts[@]}"; do
            mountnodev=${mount#* } # strip first space and everything before it
            mountpoint="${mountnodev%% *}" # strip first space and everything after it
            fstrim "$mountpoint" # Inform device which blocks on the filesystem are no longer in use
        done
    fi
done
mounts=($(grep ^/dev/mapper /proc/mounts|grep -v -w ro))
    # mounts is now a list of mounted lvm or encrypted writeable mounts which may be on the ssd drive
for mount in "${mounts[@]}"; do
    mountnodev=${mount#* } # strip first space and everything before it
    mountpoint="${mountnodev%% *}" # strip first space and everything after it
    fstrim "$mountpoint" # Inform device which blocks on the filesystem are no longer in use
done
unset IFS

Noatime

In this same /etc/fstab it is also possible to change relatime by noatime to ask the system not writing the access dates when reading the files. This will avoid many writing on the SSD but these dates are sometimes needed by software (kmail for example).

Some tricks

It is possible to save more useless writing on your SSD.

Scheduler

The kernel scheduler is geared for spindle based storage e.g., disk drives which allows for access, latency timing. These do not exist with SSD's so you need to make a change to the scheduler for the SSD drive ONLY. Do all steps as root in a terminal box:

1. The following assumes that your SSD is at sda so if another port change accordingly: Create rc.local using your favourite editor as /etc/rc.d/rc.local with:

  #!/bin/sh
  echo deadline >/sys/block/sda/queue/scheduler
  exit 0

2. Now set up the service on all reboots:

  sudo chmod +x /etc/rc.d/rc.local
  systemctl status rc-local                                                  
  systemctl start rc-local                                                   
  systemctl status rc-local   [to check that it is now running]                                                   
 

3. Confirm that the service will run at boot via MCC - System - System Services - rc-local

4. If you wish to do the same now so you do not need to reboot then again, as root/sudo then do

  echo deadline >/sys/block/sda/queue/scheduler

Now in future the system when booted, will run the I/O scheduler for the SSD as deadline and all other HDD will continue to default to cfq. This will help keep the SSD running at full speed.

Swap

Because the /swap file is often written, it is a good idea to mount the /swap file elsewhere than on a SSD. If you have a hard disk besides the SSD, find a little place for the /swap file. It is also possible to live without /swap.

The log directory

IF like me you will continue to have a HDD as the second device holding /home etc you can reduce unneeded writes by holding the log directory on the HDD.

Doing all as root: In my case I created /home/system-jobs/var/log Then copy /var/log to /home/system-jobs/var, moved the /var/log to /var/log-old [Just in case] Then finished off with linking the HDD to the SSD so the commands for this little block via a terminal is:

 su -
 {root password}
 mkdir -p /home/system-jobs/var
 cp -vp /var/log /home/system-jobs/var
 mv /var/log /var/log-old
 ln -s /home/system-jobs/var/log /var/log

You should check that is has worked by doing ls -la /var/log/ which should show a list of your log files. Note that the trailing '/' is important.

Now all of the logging will go to your disk drive and any operations on it e.g., logrotate will continue to work.

This procedure will help ensure that the SSD has idle time to run its garbage collection process.

Firefox

Firefox makes a large use of temporary files. If you have more than 2 GB RAM, We will tell it to stop the writing in / to put it in the RAM.

  • Open Firefox and type about:config in the URL bar
  • Look in the list for browser.cache.disk.enable and change true to false with the right click
  • Look in the list for browser.cache.memory.enable, may be you will have to create it with a right click new->String, set the value true. Now we have disabled the cache on the disk and enable a cache on the RAM.
  • Create a new string called browser.cache.memory.capacity and set a value in KiB for the maximum size of the memory. (200000 is a good choice).

References