Sys­tems Ubuntu swap....................

Mats Tage Ax­els­son re­veals meth­ods for mi­grat­ing your sys­tem, en­abling you to take full ad­van­tage of your avail­able disk space.

Linux Format - - CONTENTS -

Mats Tage Ax­els­son mi­grates your sys­tem to take full ad­van­tage of your disk space.

Back in the early days of Linux, we mea­sured RAM in megabytes. Un­der those con­di­tions, you could only run a few ap­pli­ca­tions be­fore you ran out. A swap par­ti­tion was nec­es­sary to keep the sys­tem run­ning.

How­ever, new com­put­ers won’t run out of mem­ory, so what’s the point of hav­ing swap space at all? The an­swer is that it’s a good idea to have some swap space for the few oc­ca­sions you do run out of mem­ory.

Ubuntu uses a swap file by de­fault in­stead of a par­ti­tion by de­fault. The rec­om­men­da­tion for a swap file is ei­ther two per cent of the avail­able disk space or 2GB, which­ever is smaller. Con­sider that a par­ti­tion would be dou­ble the size of your RAM, in my case 4GB times two equals 8GB. That means I can save 6GB of disk space.

It’s eas­ier to cre­ate a new swap file than cre­ate a new par­ti­tion or change the size of it. When you do a fresh install of Ubuntu 17.04, the in­staller will cre­ate a swap file, un­less you specif­i­cally ask for a swap par­ti­tion. The up­grade pro­ce­dure, on the other hand, uses your cur­rent swap par­ti­tion.

Where does that leave users who up­grade? They don’t want to be left be­hind, but also have no wish to re­in­stall com­pletely. For­tu­nately, the pro­ce­dure isn’t com­pli­cated, al­though a de­gree of cau­tion is ad­vised – so make sure you have a re­cov­ery disc to hand. Don’t worry if you’re not us­ing Ubuntu. The pro­ce­dure is sim­ple enough and can be ap­plied to other dis­tros.

Swap cre­ation

We can use ei­ther fal­lo­cate or dd to cre­ate a swap file. Root will be the file’s des­ti­na­tion. The fastest method is to use fal­lo­cate , but it uses the filesys­tem and so isn’t ideal with all filesys­tems. How­ever, it’s fine to use with the ext4 file sys­tem. $sudo fal­lo­cate -l 2G /Swap­file

Us­ing dd is slower but will re­li­ably cre­ate a file space: $sudo dd if=/dev/zero of=/swap­file bs=1024 count=2147483648

Se­cure the swap file. all pro­grams use this file, so sen­si­tive data may be in­side it. Give per­mis­sions only to root. Then en­sure the file has the cor­rect per­mis­sions and own­er­ship: $sudo chown root:root /swap­file $sudo chmod 0600 /swap­file

Ini­ti­ate the file as swap: $mk­swap /swap­file

Check that your cur­rent swap has pri­or­ity -1, which means no pri­or­ity as­signed. $ swapon –show

To make sure the sys­tem uses your new swap first, use the pri­or­ity pa­ram­e­ter when you ini­ti­ate the swap file. The swap space with the low­est pri­or­ity will be quicker to turn off.

Your cur­rent swap par­ti­tion will have it turned off (set to -1) by de­fault. $ sudo swapon -p 10 /swap­file

Check your swap us­age: you won’t see the file fill up im­me­di­ately. The ker­nel keeps a lot of mem­ory in the cache and on the hard disk. Fi­nally, you may end up with fol­low­ing: $ swapon –show NAME TYPE SIZE USED PRIO /dev/sda10 par­ti­tion 12.1G 0B -1 /Swap­file file 2G 11.8M 15

When you can see that the sys­tem is us­ing your swap file, you can turn off swap (you can use swapoff -a ) for your par­ti­tion. Ide­ally, your old swap par­ti­tion should be empty, but this isn’t nec­es­sary: $ sudo swapoff /dev/sda10 The com­mand swapoff may fail due to a ker­nel bug, with­out any ob­vi­ous sys­tem ef­fects, so check if the par­ti­tion has dis­ap­peared from the list­ing: $ swapon –show NAME TYPE SIZE USED PRIO /Swap­file file 2G 11.8M 15

The whole pro­ce­dure can be done on a run­ning sys­tem and will only change things for the cur­rent ses­sion. Next, we will con­fig­ure the sys­tem to do this at boot.

Mak­ing it stick

To use the swap file, we need to make the sys­tem start it at boot. The eas­i­est way to do this is to add a line in the fstab file, such as the fol­low­ing: /swap­file none swap sw 00

You can pri­ori­tise the file in case you have sev­eral files on dif­fer­ent me­dia. For ex­am­ple, you may want to use an old slower drive as a sec­ond swap: /swap­file_1 none swap de­faults,pri=100 0 0 /swap­file_2 none swap de­faults,pri=10 0 0

The sys­tem uses swap­file_1 to a higher de­gree than swap­file_2, so this would only be use­ful if you had swap­file_2 on an­other disk. You’ll also need to re­move your swap par­ti­tion from the same file. But fstab is the older way of do­ing things and in most dis­tri­bu­tions, sys­temd-fstab­gen­er­a­tor will con­vert this to a swap unit file dur­ing boot.

Now, you might think that the sys­tem won’t use the swap par­ti­tion, but see what sys­temd-gpt-auto-gen­er­a­tor does. It finds all swap par­ti­tions and makes a unit file for it early in the boot se­quence. If you wanted to boot your sys­tem once or twice be­fore re­claim­ing the swap par­ti­tion space, other mea­sures are nec­es­sary. To have a swap par­ti­tion with­out us­ing it, you need to mask it: $ sys­tem­ctl mask dev-sdXX

Now try out the sys­tem for a while to make sure that there’s enough swap space. When we de­cide to re­claim our disk space, re­move the par­ti­tion. To re­move it, use GParted, fdisk or sim­i­lar. Sys­temd may end up show­ing a failed ser­vice. $ sys­tem­ctl --type swap -a UNIT LOAD AC­TIVE SUB DE­SCRIP­TION dev-sda10.swap loaded failed failed /dev/sda10 swap­file.swap loaded ac­tive ac­tive /swap­file To solve this in sys­temd, you need to re­set it. $ sys­tem­ctl re­set-failed dev-sdXX

What about hi­ber­nat­ing?

One way to hi­ber­nate is uswsusp. When you con­fig­ure uswsusp, it’s con­trolled by uswsusp.conf with the pa­ram­e­ters re­sume_de­vice and re­sume_off­set. You’ll find the off­set pa­ram­e­ter with swap-off­set on your file. $swap-off­set /swap­file

The same needs to go into the initramfs re­sume file, in a slightly dif­fer­ent for­mat. $ cat /etc/uswsusp.conf # /etc/uswsusp.conf(5) -- Con­fig­u­ra­tion file for s2disk/s2both re­sume de­vice = UUID=bb6a1ba2-1196-405b-825d5b7­caf5347cc re­sume off­set = 32937984 $ cat /etc/initramfs-tools/conf.d/re­sume re­sume=UUID=bb6a1ba2-1196-405b-825d-5b7­caf5347cc re­sume_off­set=32937984

The re­sume pa­ram­e­ter can be in /dev/sdXX for­mat too, but UUID is a more ro­bust so­lu­tion. To make the val­ues ac­tive, you need to up­date grub and initramfs. $ up­date-initramfs -u && up­date-grub2

If you skip one of the two, your sys­tem will lock up try­ing to find the re­sume file. But usu­ally you can re­cover us­ing the re­cov­ery op­tion from grub. The prob­lem is now the uswsusp en­cryp­tion scheme isn’t up to date. Sup­port for en­cryp­tion seems a long way off, judg­ing from ac­tiv­ity by the de­vel­oper.

What’s worse is that en­cryp­tion is au­to­mat­i­cally on if your home is en­crypted. To avoid this is­sue, you can use Tux­on­ice, which is avail­able as a sep­a­rate ker­nel. There’s a PPA avail­able if you have Ubuntu.

Use your par­ti­tion ed­i­tor to re­move the swap par­ti­tion. GParted, disks and fdisk work equally well.

Use conky or htop to see your swap space us­age. Note that the swapon – show com­mand only dis­plays data in text for­mat.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.