How to change swap partition from primary to logical?

5,168

do I have to delete those partitions and reinstall Mint?

Absolutely not, you can do this from Mint while it's running.

First, turn the swap off -- swapoff -a. Next, fire up fdisk on the device containing the partition (if you have a GPT disk you may have to use gdisk, I believe they are very similar; fdisk will warn you if this is the case). You need to do this sudo or root:

> fdisk /dev/sda
Welcome to fdisk (util-linux 2.21.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): 

That's /dev/sda, the whole thing, not just /dev/sda1. Have a look at the partition table with p (you can see the list of commands first with m). Figure out which number the swap partition is, they are listed in order starting with 1. Delete it using d. Add a new partition with n, and you will be asked if you want it to be "primary" or "extended", choose extended. You can probably just accept the default "First sector" and "Last sector" values, this will make the partition use all available space.

The extended partition can contain a number of logical partitions. To do this, add another new partition with n -- now your choices will be "primary" or "logical". The default first sector for a new logical partition will be just about the first sector of the new extended partition; it is up to you whether you want to fill it.

Notice that the logical partitions are numbered from 5 and that the "Device" is, correspondingly, sda5. This is important because that's not the same as your old swap device node.

Change the type of the partition with t (remember, it's partition 5) to 82 (Linux swap). Check the table with p.

If you're sure everything's good, make it so with w. Up to this point (as in the point before you choose w), nothing you've done has been committed, so don't worry about mistakes; you can always bail with q and start again. You can do this with sda1, sda2, etc. mounted as long as you have not changed anything about those partitions. Presuming your swap was after the other partitions, this should not be a problem (otherwise, it becomes more complicated and you will need to use (g)parted).

You're done with fdisk, exit with q. Edit /etc/fstab to use the correct device node for swap and turn swap back on (swapon -a).

Share:
5,168

Related videos on Youtube

Chankey Pathak
Author by

Chankey Pathak

Note: Inactive since past 4 years (restrictions at workplace) Project: TutsWiki - An open source platform to provide collaborative tutorials. LinuxStall - A place for all your Linux needs. Find me: Google+ Twitter LinkedIn GitHub Email: [email protected]

Updated on September 18, 2022

Comments

  • Chankey Pathak
    Chankey Pathak over 1 year

    I was installing Mint on dual boot with Win8 and I accidentally created primary swap partition. Is there any way I can change the partition type from primary to logical? Or do I have to delete those partitions and reinstall Mint?

    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 10 years
      That depends: logical partitions have to be contiguous. What's your current partition setup? Post the output of fdisk -l /dev/sda. Why do you want to do that anyway? It doesn't make any difference.