0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu24.04でswapを永続的に無効化する

Posted at

問題

  • Raspberry Pi5にUbuntu24.04を入れてkubernetesを動作させるため、swapをoffしようとした
  • /etc/fstab/swapfile none swap sw 0 0UUID=xxx-xxx-xxx-xxx none swap sw 0 0といった行をコメントアウトすればよいという情報がいくつかあったが、実際に中身を見て見るとそれに類する記述はなかった
    $ cat /etc/fstab
    LABEL=writable  /       ext4    discard 0       1
    LABEL=system-boot       /boot/firmware  vfat    defaults        0       1
    
  • 実際にsudo swap off -a後に再起動するとswapが有効になっており、どうすればswapを永続的に無効化できるか調べた

解決策

  1. swapfile.swapを無効化して、停止
    $ sudo systemctl disable swapfile.swap
    $ sudo systemctl stop swapfile.swap
    
  2. swapfileを削除
    $ sudo rm /swapfile
    
  3. swapを無効化
    $ sudo swapoff -a
    

上記の方法で、再起動してもswapが無効化されていることを確認した。

解決までの経緯

  1. Ubuntu 24.04をインストール直後にswapが有効であることを確認

    $ free -h
                   total        used        free      shared  buff/cache   available
    Mem:           7.8Gi       659Mi       6.5Gi        17Mi       767Mi       7.1Gi
    Swap:          1.0Gi          0B       1.0Gi
    
  2. swapを動かしてそうなサービスを検索

    % sudo ls /etc/systemd/system | grep swap
    swap.target.wants
    
  3. swap.target.wantsのディレクトリを確認

    $ sudo ls /etc/systemd/system/swap.target.wants
    growroot-almost.service  mkswap.service  swapfile.swap
    
  4. growroot-almost.servicemkswap.serviceswapfile.swapの状態を確認

    $ sudo systemctl status growroot-almost.service
    Warning: The unit file, source configuration file or drop-ins of growroot-almost.service changed on disk. Run 'systemct>
    ○ growroot-almost.service - Grow the root file-system, leaving 16MB space for LUKS headers
         Loaded: loaded (/usr/lib/systemd/system/growroot-almost.service; enabled; preset: enabled)
         Active: inactive (dead)
      Condition: start condition unmet at Tue 2024-08-27 23:36:50 JST; 1 month 4 days ago
    
    Aug 27 23:36:50 master-desktop systemd[1]: growroot-almost.service - Grow the root file-system, leaving 16MB space for >
    
    $ sudo systemctl status mkswap.service
    Warning: The unit file, source configuration file or drop-ins of mkswap.service changed on disk. Run 'systemctl daemon->
    ○ mkswap.service - Create the default swapfile
         Loaded: loaded (/usr/lib/systemd/system/mkswap.service; enabled; preset: enabled)
         Active: inactive (dead)
      Condition: start condition unmet at Tue 2024-08-27 23:36:50 JST; 1 month 4 days ago
    
    Aug 27 23:36:50 master-desktop systemd[1]: mkswap.service - Create the default swapfile was skipped because of an unmet>
    
    $ sudo systemctl status swapfile.swap
    Warning: The unit file, source configuration file or drop-ins of swapfile.swap changed on disk. Run 'systemctl daemon-r>
    ● swapfile.swap - The default swapfile
         Loaded: loaded (/usr/lib/systemd/system/swapfile.swap; enabled; preset: enabled)
         Active: active since Tue 2024-08-27 23:36:50 JST; 1 month 4 days ago
           What: /swapfile
          Tasks: 0 (limit: 9068)
         Memory: 340.0K (peak: 1.5M)
            CPU: 11ms
         CGroup: /system.slice/swapfile.swap
    
    Aug 27 23:36:50 master-desktop systemd[1]: Activating swap swapfile.swap - The default swapfile...
    Aug 27 23:36:50 master-desktop systemd[1]: Activated swap swapfile.swap - The default swapfile.
    
  5. swapfile.swapが、enabledかつactiveだったので、無効化して停止

    $ sudo systemctl disable swapfile.swap
    Removed "/etc/systemd/system/swap.target.wants/swapfile.swap".
    $ sudo systemctl stop swapfile.swap
    
  6. swapfile.swapが無効化されたことを確認

    $ sudo systemctl status swapfile.swap
    ○ swapfile.swap - The default swapfile
         Loaded: loaded (/usr/lib/systemd/system/swapfile.swap; disabled; preset: enabled)
         Active: inactive (dead) since Tue 2024-10-01 23:38:20 JST; 1s ago
       Duration: 1month 4d 13h 31min 29.715s
           What: /swapfile
            CPU: 15ms
    
    Aug 27 23:36:50 master-desktop systemd[1]: Activating swap swapfile.swap - The default swapfile...
    Aug 27 23:36:50 master-desktop systemd[1]: Activated swap swapfile.swap - The default swapfile.
    Oct 01 23:38:20 master-desktop systemd[1]: Deactivating swap swapfile.swap - The default swapfile...
    Oct 01 23:38:20 master-desktop systemd[1]: swapfile.swap: Deactivated successfully.
    Oct 01 23:38:20 master-desktop systemd[1]: Deactivated swap swapfile.swap - The default swapfile.
    
  7. swapfileを削除

    $ sudo rm /swapfile
    
  8. swapを無効化

    $ sudo swapoff -a
    
  9. 再起動

    $ sudo reboot
    
  10. 起動後、swapが無効化されていることを確認

    $ free -h
                   total        used        free      shared  buff/cache   available
    Mem:           7.8Gi       678Mi       6.7Gi        17Mi       640Mi       7.1Gi
    Swap:             0B          0B          0B
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?