DEV Community

Discussion on: Create swap file in Ubuntu

Collapse
 
peter279k profile image
peter279k

We can also use the parted command to create the swap partition.

Here are examples to do that:

pi@raspberrypi:~ $ free -mh
              total        used        free      shared  buff/cache   available
Mem:          7.7Gi       369Mi       6.8Gi        24Mi       579Mi       7.1Gi
Swap:          99Mi          0B        99Mi
pi@raspberrypi:~ $ sudo parted
(parted) print free
Model: SD SD (sd/mmc)
Disk /dev/mmcblk0: 62.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        16.4kB  4194kB  4178kB           Free Space
 1      4194kB  273MB   268MB   primary  fat32        lba
 2      273MB   31.9GB  31.6GB  primary  ext4
        31.9GB  62.1GB  30.2GB           Free Space

(parted) mkpart primary linux-swap
Start? 31.9GB
End? 44GB
(parted) print  free
Model: SD SD (sd/mmc)
Disk /dev/mmcblk0: 62.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
        16.4kB  4194kB  4178kB           Free Space
 1      4194kB  273MB   268MB   primary  fat32           lba
 2      273MB   31.9GB  31.6GB  primary  ext4
        31.9GB  31.9GB  524kB            Free Space
 3      31.9GB  44.0GB  12.1GB  primary  linux-swap(v1)  lba
        44.0GB  62.1GB  18.1GB           Free Space

(parted)
Information: You may need to update /etc/fstab.
pi@raspberrypi:~ $ sudo mkswap /dev/mmcblk0p3
Setting up swapspace version 1, size = 11.3 GiB (12084834304 bytes)
no label, UUID=d89e85d9-e8be-470f-9c67-86648cc590f7
pi@raspberrypi:~ $ sudo swapon /dev/mmcblk0p3
pi@raspberrypi:~ $ free -mh
              total        used        free      shared  buff/cache   available
Mem:          7.7Gi       377Mi       6.9Gi        24Mi       427Mi       7.1Gi
Swap:          11Gi          0B        11Gi
pi@raspberrypi:~ $
pi@raspberrypi:~ $ echo "UUID=d89e85d9-e8be-470f-9c67-86648cc590f7 swap swap defaults 0 0" | sudo tee -a /et
c/fstab
UUID=d89e85d9-e8be-470f-9c67-86648cc590f7 swap swap defaults 0 0
pi@raspberrypi:~ $ cat /etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=a2dee777-01  /boot           vfat    defaults          0       2
PARTUUID=a2dee777-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
UUID=d89e85d9-e8be-470f-9c67-86648cc590f7 swap swap defaults 0 0
Enter fullscreen mode Exit fullscreen mode