增加加密交换的大小

增加加密交换的大小

Ubuntu 18.04 默认在每次启动时使用 dm-crypt/Swap 加密创建一个随机加密交换。对我来说,这是创建一个 1gb 交换,但我找不到在哪里增加它在每次启动时创建的交换的大小。我们如何增加每次启动时分配的交换大小?我不想切换到静态交换文件。没有/swapfile或分区。

这似乎是一个新功能,每次系统启动时都会动态创建交换空间。我该如何增加它?

在/etc/crypttab中

nvme0n1p3_crypt UUID=1e092df8-5c37-4409-8ac8-361402244f69 none luks,discard
cryptswap1 UUID=aba1b825-a77b-4384-a40f-fdc6fdaaced0 /dev/urandom swap,offset=1024,cipher=aes-xts-plain64

/etc/fstab

/dev/mapper/cryptswap1 none swap sw 0 0
# swapon -s
Filename                Type        Size    Used    Priority
/dev/dm-3                               partition   1003004 601088  -2

顶部

gparted 没有显示任何交换分区。这是唯一的驱动器。

分区

在这里的回答中,建议这样做:

sudo swapoff -a  
sudo cryptsetup resize cryptswap1 --size (in sectors)  
sudo mkswap /dev/cryptswap1  
sudo swapon -a  

因此我运行:cryptsetup resize cryptswap1 --size 7812500。这会导致错误:Device /dev/mapper/mint--vg-swap_1 is too small.

所以我尝试:cryptsetup resize /dev/mapper/mint--vg-swap_1 --size 7812500

这不会产生错误,但parted --list; fdisk -l也不会显示任何变化:

Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/cryptswap1: 979.5 MiB, 1027080192 bytes, 2006016 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

并尝试再次运行cryptsetup resize cryptswap1 --size 7812500产生Device /dev/mapper/mint--vg-swap_1 is too small.

我也尝试过:resize2fs /dev/mapper/mint--vg-swap_1 -s 4G产生了以下结果:

resize2fs 1.44.1 (24-Mar-2018)
resize2fs: Device or resource busy while trying to open /dev/mapper/mint--vg-swap_1
Couldn't find valid filesystem superblock.
# lsblk
NAME                  MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
loop0                   7:0    0  54.6M  1 loop  /snap/core18/1279
loop1                   7:1    0  89.1M  1 loop  /snap/core/8213
loop2                   7:2    0  89.1M  1 loop  /snap/core/8268
loop3                   7:3    0 497.4M  1 loop  /snap/wickrme/246
loop4                   7:4    0 497.2M  1 loop  /snap/wickrme/247
loop5                   7:5    0  54.6M  1 loop  /snap/core18/1288
nvme0n1               259:0    0 465.8G  0 disk  
├─nvme0n1p1           259:1    0   512M  0 part  /boot/efi
├─nvme0n1p2           259:2    0   732M  0 part  /boot
└─nvme0n1p3           259:3    0 464.6G  0 part  
  └─nvme0n1p3_crypt   253:0    0 464.6G  0 crypt 
    ├─mint--vg-root   253:1    0 463.6G  0 lvm   /
    └─mint--vg-swap_1 253:2    0   980M  0 lvm   
      └─cryptswap1    253:3    0 979.5M  0 crypt [SWAP]

答案1

本答案基于 Ubuntu 22.04 LTS

lslbk
   # └─sda6                  8:6    0 464,6G  0 part  
   #   └─sda6_crypt        253:0    0 464,5G  0 crypt 
   #     ├─vgubuntu-root   253:1    0 463,6G  0 lvm   /
   #     └─vgubuntu-swap_1 253:2    0   980M  0 lvm   [SWAP]

之前没有提到的一件事是,为了增加lvm 交换大小,lvm 根大小必须是减少第一的!

(至少当lsblk(如上)显示 root + swap 构成整个加密设备时。)


TL;DR(但要备份重要数据!)

这里我们将根目录减少 -40G 并增加交换 +40G:

# Boot Ubuntu Live/Flash/"Try Ubuntu" AND open terminal
# Run subsequent commands as superuser
sudo -i
    # `sudo`    => Execute a command as another user.
    # `sudo -i [-u user]` => Run an interactive session with substitute user, default is root.
    # `sudo [-u user] command` => Run a command with a substitute user, default is root.

# Encrypted device should NOT be unlocked
lsblk # => list block devices
    # └─sda6 => no `crypt`/`lvm``
# Unlock encrypted device
cryptsetup open /dev/sda6 crypt # Enter passphrase
    # `cryptsetup` => Manage dm-crypt + LUKS encrypted volumes.
    # `cryptsetup open <device> <name>` => Opens encrypted lv as <name>

# Get logical volume identifiers
lsblk
    # └─sda6                  8:6    0 464,6G  0 part
    #   └─sda6_crypt        253:0    0 464,5G  0 crypt
    #     ├─vgubuntu-root   253:1    0 463,6G  0 lvm   /
    #     └─vgubuntu-swap_1 253:2    0   980M  0 lvm   [SWAP]

# Shrink logical root volume AND filesystem
lvresize --verbose --resizefs -L -40G /dev/mapper/vgubuntu-root
    # `lvresize` <volume> => resize a logical volume
    #   --verbose  => Give more info.
    #   --resizefs => Resize filesystem AND LV with fsadm(8).
    #   -L         => Specifies the new size of the LV, 
    #                 +/- add/subtracts to/from current size, g|G is GiB.

# Check filesystem of logical root volume for errors
e2fsck -f /dev/mapper/vgubuntu-root
    # `e2fsck`<fs-path> => Check a Linux ext2/ext3/ext4 file system
    #   -f => Force checking even if the file system seems clean.

# Increase swapsize
lvresize --verbose -L +40G /dev/mapper/vgubuntu-swap_1

重启后,Ubuntu 应该正常启动,swap 应该调整大小:

lslbk
    # └─sda6                  8:6    0 464,6G  0 part  
    #   └─sda6_crypt        253:0    0 464,5G  0 crypt 
    #     ├─vgubuntu-root   253:1    0 423,6G  0 lvm   /
    #     └─vgubuntu-swap_1 253:2    0    41G  0 lvm   [SWAP]

如果系统监视器仍然只有初始的1G需要重写逻辑交换设备:

swapon --show
    # NAME      TYPE      SIZE USED PRIO
    # /dev/dm-2 partition 976M   0B   -2

swapoff -v /dev/dm-2
    # swapoff /dev/dm-2

mkswap /dev/dm-2
    # mkswap: /dev/dm-2: warning: wiping old swap signature.
    # Setting up swapspace version 1, size = 41 GiB (43973079040 bytes)
    # no label, UUID=...

swapon -a
    # Turn on swapspace

答案2

sudo swapoff -a  
sudo cryptsetup resize cryptswap1 --size (in sectors)  
sudo mkswap /dev/mapper/cryptswap1  
sudo swapon -a  

如果省略--size,它将使用整个设备。

更新- 根据反馈。 重启并确保 mint--vg-swap_1 仍为 980M。
如果是,请执行以下步骤。——首先尝试手动增加容器的大小(我认为这没有必要)。您的评论说您正在尝试将交换设置为 4G(+3G),因此:

sudo swapoff -a  
sudo lvresize -L+3G /dev/mapper/mint--vg-swap_1
sudo cryptsetup resize cryptswap1 --size 7812500
sudo mkswap /dev/mapper/cryptswap1  
sudo swapon -a  

另一方面,如果 mint--vg-swap_1现在是4G,然后运行此命令(考虑标题的大小):

sudo swapoff -a  
sudo cryptsetup resize cryptswap1 --size 7808404   
sudo mkswap /dev/mapper/cryptswap1  
sudo swapon -a  

参考

相关内容