ユーザ用ツール

サイト用ツール


サイドバー

最新の10件
一覧
linux:swap

メモリが足りなくなってswapがない 空き容量のあるdiskをリサイズしてswap領域にしてみる.

現在の状態./dev/dsa1 が / で空きがあるのでここからもらうことにする.

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 46.59 GiB, 50010783744 bytes, 97677312 sectors
Disk model: BlockVolume
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 1048576 bytes
Disklabel type: gpt
Disk identifier: EDBB87D5-40EB-445F-88E5-B62864198F22

Device      Start      End  Sectors  Size Type
/dev/sda1  227328 97677278 97449951 46.5G Linux filesystem
/dev/sda14   2048    10239     8192    4M BIOS boot
/dev/sda15  10240   227327   217088  106M EFI System

Partition table entries are not in disk order.

Partedで処理を行う.GUIの場合はgPartedが便利

$ sudo parted /dev/sda

現状

(parted) print free
Model: ORACLE BlockVolume (scsi)
Disk /dev/sda: 50.0GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
        17.4kB  1049kB  1031kB  Free Space
14      1049kB  5243kB  4194kB                     bios_grub
15      5243kB  116MB   111MB   fat32              boot, esp
 1      116MB   50.0GB  49.9GB  ext4

パーティション1を50GBから48GBにリサイズする

(parted) resizepart
Partition number? 1
Warning: Partition /dev/sda1 is being used. Are you sure you want to continue?
Yes/No? yes
End?  [50.0GB]? 48GB
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No? yes
(parted) print
Model: ORACLE BlockVolume (scsi)
Disk /dev/sda: 50.0GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
14      1049kB  5243kB  4194kB                     bios_grub
15      5243kB  116MB   111MB   fat32              boot, esp
 1      116MB   48.0GB  47.9GB  ext4

できた空き領域からswap用のパーティーションを作成

(parted) mkpart
Partition name?  []?
File system type?  [ext2]? linux-swap
Start? 48.0GB
End? 50GB
(parted) print
Model: ORACLE BlockVolume (scsi)
Disk /dev/sda: 50.0GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name  Flags
14      1049kB  5243kB  4194kB                        bios_grub
15      5243kB  116MB   111MB   fat32                 boot, esp
 1      116MB   48.0GB  47.9GB  ext4
 2      48.0GB  50.0GB  2009MB  linux-swap(v1)

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

fdiskでも確認

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 46.59 GiB, 50010783744 bytes, 97677312 sectors
Disk model: BlockVolume
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 1048576 bytes
Disklabel type: gpt
Disk identifier: EDBB87D5-40EB-445F-88E5-B62864198F22

Device        Start      End  Sectors  Size Type
/dev/sda1    227328 93750000 93522673 44.6G Linux filesystem
/dev/sda2  93751296 97675263  3923968  1.9G Linux swap
/dev/sda14     2048    10239     8192    4M BIOS boot
/dev/sda15    10240   227327   217088  106M EFI System

Partition table entries are not in disk order.

swap初期化

$ sudo mkswap /dev/sda2
Setting up swapspace version 1, size = 1.9 GiB (2009067520 bytes)
no label, UUID=4e27ed83-3d75-4c3d-87ac-f16e618bba4a

アドホックにswapを有効化してみる

$ sudo swapon -v UUID=4e27ed83-3d75-4c3d-87ac-f16e618bba4a
swapon: /dev/sda2: found signature [pagesize=4096, signature=swap]
swapon: /dev/sda2: pagesize=4096, swapsize=2009071616, devsize=2009071616
swapon /dev/sda2
$ free
              total        used        free      shared  buff/cache   available
Mem:         989340      311428       83696       12884      594216      513164
Swap:       1961980           0     1961980

一旦swapをoffにして永続化のために/etc/fstabに登録してswaponしてみる

$ sudo swapoff UUID=4e27ed83-3d75-4c3d-87ac-f16e618bba4a
$ sudo vi /etc/fstab
$ sudo git -C /etc diff /etc/fstab
diff --git a/fstab b/fstab
index e22bda5..72d64af 100644
--- a/fstab
+++ b/fstab
@@ -1,6 +1,6 @@
 LABEL=cloudimg-rootfs  /        ext4   defaults        0 1
 LABEL=UEFI     /boot/efi       vfat    umask=0077      0 1
-/var/tmp/swap  none    swap    pri=20  0 0
+UUID=4e27ed83-3d75-4c3d-87ac-f16e618bba4a      none    swap    pri=-1  0 0

 # CLOUD_IMG: This file was created/modified by the Cloud Image build process
 ######################################
ubuntu@instance-20220327-0134:~$ sudo swapon -a
ubuntu@instance-20220327-0134:~$ free
              total        used        free      shared  buff/cache   available
Mem:         989340      312360       80884       12884      596096      512232
Swap:       1961980           0     1961980

次回起動時から自動的に有効になるはず.

コメント

コメントを入力. Wiki文法が有効です:
  _   __  ____   _   __   ____   ____
 | | / / / __ \ | | / /  / __/  /  _/
 | |/ / / /_/ / | |/ /  _\ \   _/ /  
 |___/  \____/  |___/  /___/  /___/
 
linux/swap.txt · 最終更新: 2022/04/22 21:51 by matoken