1

This is my current partition setup:

sda           111.8G
├─sda1 ext4     190M /boot
├─sda2            1K
└─sda5 ext4   111.6G /
sdb           465.8G
└─sdb1 swap    14.9G [SWAP]

Disk /dev/sda: 120GB
Number  Start   End    Size   Type      File system  Flags
 1      1049kB  200MB  199MB  primary   ext4         boot
 2      201MB   120GB  120GB  extended
 5      201MB   120GB  120GB  logical   ext4

Disk /dev/sdb: 500GB
Number  Start   End     Size    Type     File system     Flags
 1      1049kB  16.0GB  16.0GB  primary  linux-swap(v1)

I'd like to move swap from sdb (SATA) to sda (SSD) because I want faster swap. How to delete the existing swap, and how to create a new one on sda - should it be sda3, sda4, or sda6? Would it be safe to resize sda5 where Ubuntu is already installed so that there's enough space for the new swap partition? How do you recommend I do that?

(I'm planning to use a single partition for all of sdb as sdb1 for backups and symlinked storage.)

Thanks.

sTripey
  • 11
  • 1
    How much are you swapping? Can you change the way you use the computer in order to decrease swapping? Or can yan you increase the RAM? The reason why I ask is that swapping will cause a lot of wear on the SSD, and although new SSDs are more 'wear-resistent' than before, it is really a good idea to reduce the wear in order to avoid failures due to a massive damage of memory cells. - It is certainly OK to allow swapping on an SSD drive as an extra buffer for extreme cases, but don't rely on heavy swapping. – sudodus Mar 14 '17 at 12:13
  • I run some server software that build up swap usage over time, I think as a result of memory leaks. So during maybe the first week there's little to no swap, but sometime after that swap continuously builds up as much as 8GB or more. I can increase RAM, but am trying to stick with 16GB for now. I don't care about the SSD's lifespan. I do enough backups that in the event of failure, I'd just buy a new SSD and restore data from backups. – sTripey Mar 14 '17 at 14:04

2 Answers2

2

Notice to other users: This answer is specific for this particular question (this OP). You have to modify the partitions etc for other cases.


Edit: The solution in this case was to use a swap file instead of a swap partition.


In this case you can treat the SSD just like an HDD (and be prepared to replace the SSD when it fails).

  • Backup the whole SSD (to have a current version, if something would go wrong).

  • Boot a live session from another drive, for example an Ubuntu desktop USB drive.

  • Swapoff all partitions that are used, and unmount the ext4 partition

    sudo swapoff -a
    sudo umount /dev/sda5
    
  • Start gparted and use it to edit the partition table of the SSD (/dev/sda).

  • Shrink the ext4 partition /dev/sda5 to create space for the swap partition. (This might take long time.)

  • Use the unallocated space to create a swap partition.

  • Perform the actions by clicking on the check icon.

  • Start a terminal window

  • Run the following command to find the UUID of the swap partition

    sudo blkid
    

    You probably see something like the following line for your new swap partition

    /dev/sda6: UUID="03bed9e9-e39b-6307-8ce5-c2612af9a6aa" TYPE="swap" PARTUUID="731f415c-05"
    
  • Edit the file fstab of your installed system. Mount the partition where it is first,

    sudo mount /dev/sda5 /mnt
    sudo nano /mnt/etc/fstab
    

    and modify the line pointing to the current swap partition, so that it will point to the new swap partition.

    UUID=03bed9e9-e39b-6307-8ce5-c2612af9a6aa none  swap  sw  0  0
    

    Use the UUID from blkid without quotes.

  • Reboot

I hope I remembered all important steps. Anyway, you can remove the old swap partition now, and use that drive space for data storage.


The following link might help you, if you need help to get started with gparted,

GrowIt.pdf

But in this case you will 'ShrinkIt' ;-)

sudodus
  • 46,324
  • 5
  • 88
  • 152
  • Well, thanks, but I forgot to mention that I'm limited to command line.

    Is there any reason to not use a swap file instead of partition? For example, I could create a 8GB swap file in the sda5 ext4 partition and set its priority in /etc/fstab higher than the sdb1 swap partition. That way I'd have two swaps but the SSD would be used for swap more often. Is this dumb?

    – sTripey Mar 14 '17 at 14:44
  • Why are you limited to command line? What happens when you boot from an Ubuntu desktop boot drive, or maybe better with a light-weight Lubuntu desktop boot drive? - Anyway, you can use parted or fdisk instead of gparted but it is much more difficult, because you must also shrink the file system and in the correct sequence, otherwise it will be detroyed. -- And yes, you can use a swap file instead of a swap partition. That way you can avoid editing the ext4 partition. I think it will be a good solution with the old swap partition as 'backup'. – sudodus Mar 14 '17 at 14:49
  • 1
    @sudodus you forgot to use the swapoff and swapon -a commands. – heynnema Mar 14 '17 at 15:42
  • 1
    I'm accessing it remotely. I think I'll just do the swap file - seems easiest. Thanks. – sTripey Mar 14 '17 at 15:44
  • Good luck @sTripey :-) – sudodus Mar 14 '17 at 16:04
0

Having swap on a SSD is not recommended as it shortens its lifespan.

Refer to this question for more information.

steeef
  • 129
  • 1
  • 2
  • 11
  • That doesn't answer my question, though. :( I really don't care about the SSD's lifespan. – sTripey Mar 14 '17 at 14:05
  • No really. Recent SSDs with good capacity usually durable. You can check the real ssd life here. Based on the real test and manufacturer's data. – crea7or Mar 16 '17 at 09:29