<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Linux on Terence&#39;s blog</title>
    <link>https://logs.eu.org/categories/linux/</link>
    <description>Recent content in Linux on Terence&#39;s blog</description>
    <image>
      <title>Terence&#39;s blog</title>
      <url>https://logs.eu.org/images/avatar.png</url>
      <link>https://logs.eu.org/images/avatar.png</link>
    </image>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Thu, 11 Aug 2022 01:21:50 +0800</lastBuildDate>
    <atom:link href="https://logs.eu.org/categories/linux/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Create a Swap File on Linux</title>
      <link>https://logs.eu.org/posts/make-swapfile/</link>
      <pubDate>Thu, 11 Aug 2022 01:21:50 +0800</pubDate>
      <guid>https://logs.eu.org/posts/make-swapfile/</guid>
      <description>&lt;p&gt;A swap file lets the kernel move idle pages out of RAM onto disk. It&amp;rsquo;s handy on
small-memory hosts (VPS, VMs) that have no dedicated swap partition, and unlike a
partition it can be resized or removed without repartitioning.&lt;/p&gt;
&lt;h3 id=&#34;create-the-swap-file&#34;&gt;Create the swap file&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Allocate a 1 GiB file. dd is preferred over fallocate here because&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# swapon rejects files with holes on some filesystems (e.g. ext4, XFS).&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;dd &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;/dev/zero &lt;span class=&#34;nv&#34;&gt;of&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;/swapfile &lt;span class=&#34;nv&#34;&gt;bs&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;1M &lt;span class=&#34;nv&#34;&gt;count&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1024&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;status&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;progress
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;chmod &lt;span class=&#34;m&#34;&gt;0600&lt;/span&gt; /swapfile   &lt;span class=&#34;c1&#34;&gt;# only root may read/write the swap file&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;mkswap /swapfile       &lt;span class=&#34;c1&#34;&gt;# format it as swap space&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;swapon /swapfile       &lt;span class=&#34;c1&#34;&gt;# enable it immediately&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;make-it-persistent&#34;&gt;Make it persistent&lt;/h3&gt;
&lt;p&gt;Add an entry to &lt;code&gt;/etc/fstab&lt;/code&gt; so the swap file is mounted on every boot. The
&lt;code&gt;grep&lt;/code&gt; guard keeps this idempotent — running it twice won&amp;rsquo;t add a duplicate line.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
