X

Using Parted To Create A New Swap Disk

What is Parted?

Parted is a software package used to manipulate paritition tables. It is useful for formatting new disks, reorganinzing disks, and removing disk data

First select the disk you would like to use, if you are unsure you you can use fdisk to list all of the disks available to you

fdisk -l

Once you have located the correct disk you would like to use we will use it to enter the command prompt. In this case, we are creating a swap disk on a KVM virtual machine.

# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

Replacing /dev/vdb with the disk you want to use

Label The Disk

We will first need to make the label, at the prompt type mklabel

(parted) mklabel
New disk label type? msdos

It will warn you before erase the disk, go ahead type Yes as long as you are certain this is the correct disk

Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted)

Setting the size and type of the disk

Next you are going to format the disk, to do this type the following

(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? linux-swap
Start? 0%
End? 100%

We indicated this was going to be a primary partition, it was going to be linux-swap, that it was going to start at 0% of the disk and end at 100%. To verify what you have crated going ahead and type print to review it looks correct:

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
 1 1049kB 1074MB 1073MB primary

You can now exit out of parted:

(parted) quit
Information: You may need to update /etc/fstab.

Creating the swap file

You will then want tell the operating system to make the newly created partition into a swap file:

mkswap /dev/vdb1

And then enable swapping by entering the following

swapon /dev/vdb1

You can then finally add it to /etc/fstab to ensure it starts on boot

/dev/vdb1 swap swap defaults

 

LinuxAdmin.io
0 0 votes
Article Rating
LinuxAdmin.io:
Related Post