Situation: increase swap size (/dev/sda3) greater than Ram (8 GB) when HD 128 GB
Motivation: 8 GB RAM is too little; 30 GB free space in my SSD; I want to turn 20 GB to SSD swap
Characteristics of system
-
Swap non-immutable/changeable. I cannot find any evidence why
/mnt/.swapfileshould be immutable so you do not need the change the file attributes of the swapfilesudo lsattr /mnt/.swapfile -------------e-- /mnt/.swapfile
-
Command
sudo fdisk -lu /dev/sdagivesDisk /dev/sda: 113 GiB, 121332826112 bytes, 236978176 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: gpt Disk identifier: 082F85CA-EE3E-479C-8244-858B196FA5BA Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 220323839 220319744 105.1G Linux filesystem /dev/sda3 220323840 236976127 16652288 8G Linux swap
-
Command
df -hgivesFilesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 793M 9.4M 784M 2% /run /dev/sda2 104G 74G 25G 75% / tmpfs 3.9G 54M 3.9G 2% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 793M 64K 793M 1% /run/user/1000
-
Allocate more disk space for Swap in
/dev/sda3.
My unsuccessful workflow for the task when HD and Swap on the same partition, /dev/sda3
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="422f23312b022f23312b">[email protected]</a>:~$ sudo -i <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295b46465d6944485a40">[email protected]</a>:~# swapoff /dev/sda3 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb8994948fbb969a8892">[email protected]</a>:~# swapon [blank] <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72001d1d06321f13011b">[email protected]</a>:~# dd if=/dev/zero of=/dev/sda3 bs=20480 count=1M dd: error writing '/dev/sda3': No space left on device 416308+0 records in 416307+0 records out 8525971456 bytes (8.5 GB, 7.9 GiB) copied, 18.7633 s, 454 MB/s <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ffde0e0fbcfe2eefce6">[email protected]</a>:~# mkswap /dev/sda3 Setting up swapspace version 1, size = 8 GiB (8525967360 bytes) no label, UUID=245cb42c-1d4e-4e21-b544-16b64af962d6 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e694898992a68b87958f">[email protected]</a>:~# swapon -p 99 /dev/sda3 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2c0ddddc6f2dfd3c1db">[email protected]</a>:~# swapon NAME TYPE SIZE USED PRIO /dev/sda3 partition 8G 0B 99 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ceef3f3e8dcf1fdeff5">[email protected]</a>:~# vi /etc/fstab ...
HD and Swap on same Partition – Current Workflow [Ijaz, cas, FarazX]
Merging. Use fallocate at the beginning instead dd because no need to put zeros
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aec3cfddc7eec3cfddc7">[email protected]</a>:~$ sudo fallocate -l 20G /mnt/.swapfile
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d202c3e240d202c3e24">[email protected]</a>:~$ sudo mkswap /mnt/.swapfile
Setting up swapspace version 1, size = 20 GiB (21474832384 bytes)
no label, UUID=45df9e48-1760-47e8-84d7-7a14f56bbd72
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="513c302238113c302238">[email protected]</a>:~$ sudo swapon /mnt/.swapfile
swapon: /mnt/.swapfile: insecure permissions 0644, 0600 suggested.
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9df0fceef4ddf0fceef4">[email protected]</a>:~$ sudo chmod 600 /mnt/.swapfile
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec818d9f85ac818d9f85">[email protected]</a>:~$ free -m
total used free shared buff/cache available
Mem: 7925 1494 175 196 6255 5892
Swap: 28610 0 28610
Add the following line in your /etc/fstab which is better than adding the thing to your runlevels (/etc/rc.local), where I put the swapfile to the /mnt/.swapfile to maintain Linux/Unix philosophy and maintain the integrity of my system backup scripts; If swapping to an SSD, use the discard option so that the blocks are trimmed on every reboot, so not sw
# http://unix.stackexchange.com/a/298212/16920 # http://unix.stackexchange.com/a/298543/16920 # If swap is on SSD, trim blocks each time at startup. /mnt/.swapfile none swap defaults,discard 0 0 # If swap on External HDD, just use sw. #/media/masi/SamiWeek/.swapfile none swap sw 0 0
Sources
- How to increase swap space? https://askubuntu.com/a/178726/25388 General discussion about increasing swap space for beginners.
- Linux Partition HOWTO for HDDs, not SSDs: 4. Partitioning requirements. http://www.tldp.org/HOWTO/Partition/requirements.html So do not put your swap to outer tracks on SSDs but use
defaults,discardoptions to trim your blocks as proposed by @cas.
System: Linux Ubuntu 16.04 64 bit
Linux kernel: 4.6
Linux modules: wl
Hardware: Macbook Air 2013-mid
Ram: 8 GB
SSD: 128 GB
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You just want to increase the swap size on your system using the space from sda2. Your sda2
/dev/sda2 104G 74G 25G 75% /
You can add additional swap space to your system by using swap file created on / that will utilize your sda2. Just do:
dd if=/dev/zero of=/swapfile bs=20480 count=1M
and then do:
sudo mkswap /swapfile sudo swapon /swapfile
and check, you swap space will increase by that amount using free -m
and yes , to enable it at boot time add the entry in /etc/fstab
/swapfile none swap sw 0 0
Method 2
-
Memory management
-
To display swap usage summary by your device:
$ swapon -s
-
To displays the amount of your free and used physical and swap memory:
$ free -h
-
To preallocate space to /swapfile, you can use the line below:
$ fallocate -l 20G /swapfile % OR $ dd if=/dev/zero of=/swapfile bs=20480 count=1M
-
To display swap usage summary by your device:
-
Change permission and create/activate swap
$ chmod 600 /swapfile $ mkswap /swapfile $ swapon /swapfile
You can also improve your file security by changing your file attributes using
chattr. I recommend you read its manual page, or read this guide on Tecmint website. -
Verify it is enabled by viewing the output of the command
cat /proc/swaps, usefreecommand, or$ swapon -s
-
To enable it at boot time, edit
/etc/fstab(static information about the file system) to include the following where the fields arefs_spec,fs_file,fs_vfstype,fs_mntops,fs_freqandfs_passno, about which you can read byman 5 fstab:$ vi /etc/fstab /swapfile none swap defaults,discard 0 0
- In the fourt parameter fs_mntops, do not use just
swwhen swapping to an SSD butdefaults,discardsuch that the memory blocks are trimmed each time at startup, see the answer here about How to Do Error-trapping and Swapoff if Error/Warning?
Thediscardoption definitely works on partitions.
- In the fourt parameter fs_mntops, do not use just
Method 3
You question, “How to Allocate More Space to Swap and Increase its Size Greater than Ram?“, does not say anything about changing the way your system is set up.
Your fdisk and free output tells us:
- You have a a partition (/dev/sda3) dedicated to swap. If you just
resize that partition you should not need to change anything in your
system to use the extra space. (/etc/fstab). - You have a partition (/dev/sda2) that has the OS on it. This
partition has 26G free space. If you shrink this partition by 20G the
OS will have 5G left to grow and use.
The swap partition is not a extended partition, this makes it a little bit easier to increase the size.
Tools required:
- Bootable OS with gparted
Look here for one location of the last free version of Parted Magic
Instruction for resizing partitions with gparted
- boot into another OS that has gparted tool
- In gparted; resize OS partition (/dev/sda2). Shrink by amount to
donate to swap space. This could take a lot of time because all of the data that is in the space being freed up will need to be moved by gparted. - In gparted; resize swap partition (/dev/sda3). Move and extend to include all free space.
- reboot into sda2 OS.
- verify swap partition (/dev/sda3) is being used.
Remember that you are changing you disk and partitions on a low level and that any errors or crashes could damage you data.
Backup you data first.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0