Linux Format

Systems Ubuntu swap....................

Mats Tage Axelsson reveals methods for migrating your system, enabling you to take full advantage of your available disk space.

-

Mats Tage Axelsson migrates your system to take full advantage of your disk space.

Back in the early days of Linux, we measured RAM in megabytes. Under those conditions, you could only run a few applicatio­ns before you ran out. A swap partition was necessary to keep the system running.

However, new computers won’t run out of memory, so what’s the point of having swap space at all? The answer is that it’s a good idea to have some swap space for the few occasions you do run out of memory.

Ubuntu uses a swap file by default instead of a partition by default. The recommenda­tion for a swap file is either two per cent of the available disk space or 2GB, whichever is smaller. Consider that a partition would be double the size of your RAM, in my case 4GB times two equals 8GB. That means I can save 6GB of disk space.

It’s easier to create a new swap file than create a new partition or change the size of it. When you do a fresh install of Ubuntu 17.04, the installer will create a swap file, unless you specifical­ly ask for a swap partition. The upgrade procedure, on the other hand, uses your current swap partition.

Where does that leave users who upgrade? They don’t want to be left behind, but also have no wish to reinstall completely. Fortunatel­y, the procedure isn’t complicate­d, although a degree of caution is advised – so make sure you have a recovery disc to hand. Don’t worry if you’re not using Ubuntu. The procedure is simple enough and can be applied to other distros.

Swap creation

We can use either fallocate or dd to create a swap file. Root will be the file’s destinatio­n. The fastest method is to use fallocate , but it uses the filesystem and so isn’t ideal with all filesystem­s. However, it’s fine to use with the ext4 file system. $sudo fallocate -l 2G /Swapfile

Using dd is slower but will reliably create a file space: $sudo dd if=/dev/zero of=/swapfile bs=1024 count=2147483648

Secure the swap file. all programs use this file, so sensitive data may be inside it. Give permission­s only to root. Then ensure the file has the correct permission­s and ownership: $sudo chown root:root /swapfile $sudo chmod 0600 /swapfile

Initiate the file as swap: $mkswap /swapfile

Check that your current swap has priority -1, which means no priority assigned. $ swapon –show

To make sure the system uses your new swap first, use the priority parameter when you initiate the swap file. The swap space with the lowest priority will be quicker to turn off.

Your current swap partition will have it turned off (set to -1) by default. $ sudo swapon -p 10 /swapfile

Check your swap usage: you won’t see the file fill up immediatel­y. The kernel keeps a lot of memory in the cache and on the hard disk. Finally, you may end up with following: $ swapon –show NAME TYPE SIZE USED PRIO /dev/sda10 partition 12.1G 0B -1 /Swapfile file 2G 11.8M 15

When you can see that the system is using your swap file, you can turn off swap (you can use swapoff -a ) for your partition. Ideally, your old swap partition should be empty, but this isn’t necessary: $ sudo swapoff /dev/sda10 The command swapoff may fail due to a kernel bug, without any obvious system effects, so check if the partition has disappeare­d from the listing: $ swapon –show NAME TYPE SIZE USED PRIO /Swapfile file 2G 11.8M 15

The whole procedure can be done on a running system and will only change things for the current session. Next, we will configure the system to do this at boot.

Making it stick

To use the swap file, we need to make the system start it at boot. The easiest way to do this is to add a line in the fstab file, such as the following: /swapfile none swap sw 00

You can prioritise the file in case you have several files on different media. For example, you may want to use an old slower drive as a second swap: /swapfile_1 none swap defaults,pri=100 0 0 /swapfile_2 none swap defaults,pri=10 0 0

The system uses swapfile_1 to a higher degree than swapfile_2, so this would only be useful if you had swapfile_2 on another disk. You’ll also need to remove your swap partition from the same file. But fstab is the older way of doing things and in most distributi­ons, systemd-fstabgener­ator will convert this to a swap unit file during boot.

Now, you might think that the system won’t use the swap partition, but see what systemd-gpt-auto-generator does. It finds all swap partitions and makes a unit file for it early in the boot sequence. If you wanted to boot your system once or twice before reclaiming the swap partition space, other measures are necessary. To have a swap partition without using it, you need to mask it: $ systemctl mask dev-sdXX

Now try out the system for a while to make sure that there’s enough swap space. When we decide to reclaim our disk space, remove the partition. To remove it, use GParted, fdisk or similar. Systemd may end up showing a failed service. $ systemctl --type swap -a UNIT LOAD ACTIVE SUB DESCRIPTIO­N dev-sda10.swap loaded failed failed /dev/sda10 swapfile.swap loaded active active /swapfile To solve this in systemd, you need to reset it. $ systemctl reset-failed dev-sdXX

What about hibernatin­g?

One way to hibernate is uswsusp. When you configure uswsusp, it’s controlled by uswsusp.conf with the parameters resume_device and resume_offset. You’ll find the offset parameter with swap-offset on your file. $swap-offset /swapfile

The same needs to go into the initramfs resume file, in a slightly different format. $ cat /etc/uswsusp.conf # /etc/uswsusp.conf(5) -- Configurat­ion file for s2disk/s2both resume device = UUID=bb6a1ba2-1196-405b-825d5b7caf­5347cc resume offset = 32937984 $ cat /etc/initramfs-tools/conf.d/resume resume=UUID=bb6a1ba2-1196-405b-825d-5b7caf5347­cc resume_offset=32937984

The resume parameter can be in /dev/sdXX format too, but UUID is a more robust solution. To make the values active, you need to update grub and initramfs. $ update-initramfs -u && update-grub2

If you skip one of the two, your system will lock up trying to find the resume file. But usually you can recover using the recovery option from grub. The problem is now the uswsusp encryption scheme isn’t up to date. Support for encryption seems a long way off, judging from activity by the developer.

What’s worse is that encryption is automatica­lly on if your home is encrypted. To avoid this issue, you can use Tuxonice, which is available as a separate kernel. There’s a PPA available if you have Ubuntu.

 ??  ?? Use your partition editor to remove the swap partition. GParted, disks and fdisk work equally well.
Use your partition editor to remove the swap partition. GParted, disks and fdisk work equally well.
 ??  ??
 ??  ?? Use conky or htop to see your swap space usage. Note that the swapon – show command only displays data in text format.
Use conky or htop to see your swap space usage. Note that the swapon – show command only displays data in text format.

Newspapers in English

Newspapers from Australia