Espandere un disco LVM+ext4 con cifratura

Supponiamo di avere questa situazione su #Fedora.

Luks+LVM+ext4 e vogliamo recuperare lo spazio non partizionato.

Vediamo con lsblk

[user@localhost ~]$ lsblk 
NAME                                            MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sr0                                              11:0    1  1.9G  0 rom   
zram0                                           251:0    0  989M  0 disk  [SWAP]
vda                                             252:0    0   40G  0 disk  
├─vda1                                          252:1    0    1G  0 part  /boot
└─vda2                                          252:2    0   19G  0 part  
  ├─fedora_localhost--live-root                 253:0    0    9G  0 lvm   
  │ └─luks-dc94334a-0c53-433a-8ff3-2a55ee295f33 253:1    0    9G  0 crypt /
  └─fedora_localhost--live-home                 253:2    0   10G  0 lvm   
    └─luks-d96ecc6b-57b3-4d18-aa1d-debb33725bd7 253:3    0   10G  0 crypt /home

Questa la situazione del Volume Group usando il comando vgdisplay.

[user@localhost ~]$ sudo vgdisplay 
[sudo] password for user: 
  --- Volume group ---
  VG Name               fedora_localhost-live
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               S9z1Ja-C1Kd-hBlv-qRtb-C3MK-762H-pIo4UP

E questa la situazione dei Logical Volume con il comando lvdisplay.

[user@localhost ~]$ sudo lvdisplay 
  --- Logical volume ---
  LV Path                /dev/fedora_localhost-live/root
  LV Name                root
  VG Name                fedora_localhost-live
  LV UUID                JK75u6-xAVS-SP8h-K30B-M8Ts-IFOq-3ld7yl
  LV Write Access        read/write
  LV Creation host, time localhost-live, 2020-12-30 11:16:52 +0100
  LV Status              available
  # open                 1
  LV Size                9.00 GiB
  Current LE             2304
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/fedora_localhost-live/home
  LV Name                home
  VG Name                fedora_localhost-live
  LV UUID                UAQ2EG-rwZu-tyvj-JuFX-Adby-J442-kqZ31f
  LV Write Access        read/write
  LV Creation host, time localhost-live, 2020-12-30 11:17:08 +0100
  LV Status              available
  # open                 1
  LV Size                <10.00 GiB
  Current LE             2559
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

Questa la situazione dei volumi cifrati, con il comando cryptsetup (il nome lo ricaviamo dal comando lsblk).

[user@localhost ~]$ sudo cryptsetup status luks-d96ecc6b-57b3-4d18-aa1d-debb33725bd7 
/dev/mapper/luks-d96ecc6b-57b3-4d18-aa1d-debb33725bd7 is active and is in use.
  type:    LUKS2
  cipher:  aes-xts-plain64
  keysize: 512 bits
  key location: keyring
  device:  /dev/mapper/fedora_localhost--live-home
  sector size:  512
  offset:  32768 sectors
  size:    20930560 sectors
  mode:    read/write
  flags:   discards 
[user@localhost ~]$ sudo cryptsetup status luks-dc94334a-0c53-433a-8ff3-2a55ee295f33 
/dev/mapper/luks-dc94334a-0c53-433a-8ff3-2a55ee295f33 is active and is in use.
  type:    LUKS2
  cipher:  aes-xts-plain64
  keysize: 512 bits
  key location: keyring
  device:  /dev/mapper/fedora_localhost--live-root
  sector size:  512
  offset:  32768 sectors
  size:    18841600 sectors
  mode:    read/write
  flags:   discards 

Partiamo con una Live

Facciamo boot con una Live di Fedora.

Di nuovo, la situazione con lsblk è questa.

[liveuser@localhost-live ~]$ lsblk 
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                             7:0    0  1.8G  1 loop 
loop1                             7:1    0  7.5G  1 loop 
├─live-rw                       253:0    0  7.5G  0 dm   /
└─live-base                     253:1    0  7.5G  1 dm   
loop2                             7:2    0   32G  0 loop 
└─live-rw                       253:0    0  7.5G  0 dm   /
sr0                              11:0    1  1.9G  0 rom  /run/initramfs/live
zram0                           251:0    0  989M  0 disk [SWAP]
vda                             252:0    0   40G  0 disk 
├─vda1                          252:1    0    1G  0 part 
└─vda2                          252:2    0   19G  0 part 
  ├─fedora_localhost--live-root 253:2    0    9G  0 lvm  
  └─fedora_localhost--live-home 253:3    0   10G  0 lvm 

Creiamo una partizione con fdisk

Creiamo una nuova partizione con fdisk sullo spazio non allocato.

/dev/vda è il disco su cui si trova lo spazio non allocato/partizionato.

La sequenza dei comandi usati in fdisk è questa:
p stampa lo schema delle partizioni
n nuova partizione
p partizione primaria
3 Partition number
invio per First sector
invio per Last sector
p stampa lo schema delle partizioni
w scrivi le modifiche

[liveuser@localhost-live ~]$ sudo fdisk /dev/vda

Welcome to fdisk (util-linux 2.36).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: dos
Disk identifier: 0x682c9d55

Device     Boot   Start      End  Sectors Size Id Type
/dev/vda1  *       2048  2099199  2097152   1G 83 Linux
/dev/vda2       2099200 41943039 39843840  19G 8e Linux LVM

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 
First sector (41943040-83886079, default 41943040): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (41943040-83886079, default 83886079): 

Created a new partition 3 of type 'Linux' and of size 20 GiB.

Command (m for help): p
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: dos
Disk identifier: 0x682c9d55

Device     Boot    Start      End  Sectors Size Id Type
/dev/vda1  *        2048  2099199  2097152   1G 83 Linux
/dev/vda2        2099200 41943039 39843840  19G 8e Linux LVM
/dev/vda3       41943040 83886079 41943040  20G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

La situazione con lsblk adesso è questa (notare vda3).

[liveuser@localhost-live ~]$ lsblk 
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                             7:0    0  1.8G  1 loop 
loop1                             7:1    0  7.5G  1 loop 
├─live-rw                       253:0    0  7.5G  0 dm   /
└─live-base                     253:1    0  7.5G  1 dm   
loop2                             7:2    0   32G  0 loop 
└─live-rw                       253:0    0  7.5G  0 dm   /
sr0                              11:0    1  1.9G  0 rom  /run/initramfs/live
zram0                           251:0    0  989M  0 disk [SWAP]
vda                             252:0    0   40G  0 disk 
├─vda1                          252:1    0    1G  0 part 
├─vda2                          252:2    0   19G  0 part 
│ ├─fedora_localhost--live-root 253:2    0    9G  0 lvm  
│ └─fedora_localhost--live-home 253:3    0   10G  0 lvm  
└─vda3                          252:3    0   20G  0 part 

Espandiamo il VG

Espandiamo il Volume Group includendo anche la nuova partizione.

[liveuser@localhost-live ~]$ sudo vgextend fedora_localhost-live /dev/vda3
  Physical volume "/dev/vda3" successfully created.
  Volume group "fedora_localhost-live" successfully extended

Vediamo con pvdisplay lo stato dei Physical Volume.

[liveuser@localhost-live ~]$ sudo pvdisplay 
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               fedora_localhost-live
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               kjwVuW-3xI0-37Ow-5WoL-zxDD-CKYg-GEagIp
   
  --- Physical volume ---
  PV Name               /dev/vda3
  VG Name               fedora_localhost-live
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               5119
  Allocated PE          0
  PV UUID               nVgnn8-CgZG-xgeH-hTBr-cTc0-1G18-trQQls

E con vgdisplay lo stato del Volume Group.

[liveuser@localhost-live ~]$ sudo vgdisplay 
  --- Volume group ---
  VG Name               fedora_localhost-live
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               38.99 GiB
  PE Size               4.00 MiB
  Total PE              9982
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               S9z1Ja-C1Kd-hBlv-qRtb-C3MK-762H-pIo4UP

Espandiamo il LV “root”

Aggiungiamo, in questo caso, 10GB al volume logico “root”.

[liveuser@localhost-live ~]$ sudo lvextend -L+10G /dev/fedora_localhost-live/root
  Size of logical volume fedora_localhost-live/root changed from 9.00 GiB (2304 extents) to 19.00 GiB (4864 extents).
  Logical volume fedora_localhost-live/root successfully resized.

“Apriamo” il volume cifrato. Il nome “secret-root” è arbitrario, lo ritroveremo sotto /dev/mapper

sudo cryptsetup luksOpen /dev/mapper/fedora_localhost--live-root secret-root
Enter passphrase for /dev/mapper/fedora_localhost--live-root:

Estendiamo il file system

Controlliamo il file system con fsck prima di estenderlo.

[liveuser@localhost-live ~]$ sudo e2fsck -f /dev/mapper/secret-root 
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/secret-root: 160322/589824 files (0.2% non-contiguous), 1655214/2355200 blocks

A questo punto estendiamo il file system contenente la root.

[liveuser@localhost-live ~]$ sudo resize2fs -p /dev/mapper/secret-root 
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/mapper/secret-root to 4976640 (4k) blocks.
The filesystem on /dev/mapper/secret-root is now 4976640 (4k) blocks long.

Espandiamo il LV “home”

Facciamo la stessa operazione sul Logical Volume “home”, assegnandogli tutto lo spazio rimanente nel Volume Group.

[liveuser@localhost-live ~]$ sudo lvextend -l +100%FREE /dev/fedora_localhost-live/home
  Size of logical volume fedora_localhost-live/home changed from <10.00 GiB (2559 extents) to 19.99 GiB (5118 extents).
  Logical volume fedora_localhost-live/home successfully resized.

“Apriamo” il volume cifrato home.

[liveuser@localhost-live ~]$ sudo cryptsetup luksOpen /dev/mapper/fedora_localhost--live-home secret-home
Enter passphrase for /dev/mapper/fedora_localhost--live-home:

Estendiamo il file system

Eseguiamo anche qui fsck.

[liveuser@localhost-live ~]$ sudo e2fsck -f /dev/mapper/secret-home 
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/secret-home: 380/654080 files (2.6% non-contiguous), 74434/2616320 blocks

E ridimensioniamo il file system.

[liveuser@localhost-live ~]$ sudo resize2fs -p /dev/mapper/secret-home 
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/mapper/secret-home to 5236736 (4k) blocks.
The filesystem on /dev/mapper/secret-home is now 5236736 (4k) blocks long.

Uscita dalla sessione Live

“Chiudiamo” i dischi cifrati.

[liveuser@localhost-live ~]$ sudo cryptsetup luksClose secret-home
[liveuser@localhost-live ~]$ sudo cryptsetup luksClose secret-root

E riavviamo il sistema (non in Live).

La nuova situazione

Una volta dentro al sistema, la situazione è questa.

Con lsblk.

[user@localhost ~]$ lsblk 
NAME                                            MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sr0                                              11:0    1  1.9G  0 rom   /run/media/user/Fedora-WS-Live-33-1-2
zram0                                           251:0    0  989M  0 disk  [SWAP]
vda                                             252:0    0   40G  0 disk  
├─vda1                                          252:1    0    1G  0 part  /boot
├─vda2                                          252:2    0   19G  0 part  
│ ├─fedora_localhost--live-root                 253:0    0   19G  0 lvm   
│ │ └─luks-dc94334a-0c53-433a-8ff3-2a55ee295f33 253:1    0   19G  0 crypt /
│ └─fedora_localhost--live-home                 253:2    0   20G  0 lvm   
│   └─luks-d96ecc6b-57b3-4d18-aa1d-debb33725bd7 253:3    0   20G  0 crypt /home
└─vda3                                          252:3    0   20G  0 part  
  ├─fedora_localhost--live-root                 253:0    0   19G  0 lvm   
  │ └─luks-dc94334a-0c53-433a-8ff3-2a55ee295f33 253:1    0   19G  0 crypt /
  └─fedora_localhost--live-home                 253:2    0   20G  0 lvm   
    └─luks-d96ecc6b-57b3-4d18-aa1d-debb33725bd7 253:3    0   20G  0 crypt /home

Con df.

[user@localhost ~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        969M     0  969M   0% /dev
tmpfs           990M     0  990M   0% /dev/shm
tmpfs           396M  1.5M  395M   1% /run
/dev/dm-1        19G  6.2G   12G  35% /
tmpfs           990M   48K  990M   1% /tmp
/dev/vda1       976M  185M  725M  21% /boot
/dev/dm-3        20G   75M   19G   1% /home
tmpfs           198M  140K  198M   1% /run/user/1000

Con gparted (nota: non gestisce LVM).

Con blivet-GUI