Table of Contents

LVM

Logical Volume Manager

Saber el sistema de ficheros de un LV (como el fdisk)

# file --special-files /dev/mapper/datavg-nom_lv
/dev/mapper/datavg-nom_lv: Linux rev 1.0 ext2 filesystem data (mounted or unclean)

Montar LVM desde Live CD

ANTECEDENTES

Tenemos un disco duro con dos particiones:

/dev/sda1 /boot
/dev/sda5 extendida LVM

Hemos trasteado el LVM y no arranca. Para montar LVM desde una live-cd:

1. Arrancar desde la live-cd

2. Buscar el VG:

vgscan

3. Activar el VG:

vgchange -a y vg_encontrado_paso_2

4. Montar el LV en cuestión, en mi caso el raíz:

4.1 mkdir /mnt/auxiliar

4.2 mount /dev/mapper/vg_encontrado_paso_2-root /mnt/auxiliar

Reducir tamanyo LV

https://www.redhat.com/archives/linux-lvm/2006-August/msg00081.html

ANTECEDENTES

Tenemos un VG (Grupo de Volúmenes) con 3 LV (Volumen Lógico):

/
/home
/swap

Queremos achicar el LV /home para dejar espacio con el objeto de crear en ese espacio liberado un cuarto LV, donde montar /var

1. (Todo como root) obtenemos información del VG:

clear && lvdisplay

--- Logical volume ---
LV Name                /dev/volumen/root
VG Name                volumen
LV UUID                ErCMtp-5H0L-cUv9-AS7L-B1Gq-9fuM-xFtcDN
LV Write Access        read/write
LV Status              available
# open                 1
LV Size                6,68 GB
Current LE             1709
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:0
 
--- Logical volume ---
LV Name                /dev/volumen/swap_1
VG Name                volumen
LV UUID                ly0S8s-MC2A-31yG-1dba-NYZk-ohQ0-yg1o6l
LV Write Access        read/write
LV Status              available
# open                 2
LV Size                2,59 GB
Current LE             664
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:1
 
--- Logical volume ---
LV Name                /dev/volumen/home
VG Name                volumen
LV UUID                uQkCYh-3u1z-s6yq-0dpA-Q6MK-5PIO-7zAoy9
LV Write Access        read/write
LV Status              available
# open                 1
LV Size                65,00 GB
Current LE             16639
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:2

2. Desmontar la partición (por si las moscas, pero creo que desde el kernel 2.6 con sistemas de ficheros EXT3 NO es necesario). Es mejor si salimos del entorno gráfico:

umount /dev/mapper/volumen-home

3. Entrar en la consola de lvm y realizar unas operaciones:

3.1 lvm 3.2 Buscamos VG:

vgscan

3.3 Activamos el VG:

vgchange -a y VolGroup00

3.4 Generamos los ficheros de dispositivos ¿?:

vgmknodes

3.5 Salimos de la consola lvm:

exit

4. Achicar el sistema de ficheros

4.1 Comprobar el sistema de ficheros del LV:

e2fsck -f /dev/mapper/volumen-home

4.2 Redimensionar la partición ¿? (por ejemplo a 35 Gigas):

resize2fs /dev/mapper/volumen-home 35G

5. Achicar el LV:

5.1 Entrar en la consola lvm:

lvm

5.2 Reducir el LV (NOTA: según el tutorial la ruta es distinta, pero a mí tras avisarme que se podían perder datos me funcionó):

lvreduce -L36G /dev/volumen/home

O:

lvextend -L36G /dev/volumen/home

Creo que tanto da que da lo mismo

5.3 Salir de la consola:

exit

6. Expandir el sistema de ficheros al tamaño total del volumen.

resize2fs /dev/mapper/volumen-home

A mi me dijo que no se hacia nada

7. Aqui se puede volver a montar el sismte a de ficheros y ver los cambios. Más simple reiniciar

Forma 2 en caliente

lvreduce -L19G /dev/mapper/fixe-home
fixe:/home/jose/Desktop/Downloads# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/fixe/root
  VG Name                fixe
  LV UUID                pi5FAr-znWl-Wkma-a5FB-Lvku-Mmv3-e1vcJO
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                332.00 MB
  Current LE             83
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0
   
  --- Logical volume ---
  LV Name                /dev/fixe/usr
  VG Name                fixe
  LV UUID                AHwTdB-ev37-LmDi-4ldW-k0fT-GD3Z-ajzZjl
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                4.66 GB
  Current LE             1192
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1
   
  --- Logical volume ---
  LV Name                /dev/fixe/var
  VG Name                fixe
  LV UUID                41g0b1-YrLd-cHte-AoOT-Wtuz-4OSy-B59aJl
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                2.79 GB
  Current LE             715
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:2
   
  --- Logical volume ---
  LV Name                /dev/fixe/swap_1
  VG Name                fixe
  LV UUID                ndI2u3-Si6h-tB1U-lLoB-b7o6-cjQt-pIGoAH
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                3.84 GB
  Current LE             983
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:3
   
  --- Logical volume ---
  LV Name                /dev/fixe/tmp
  VG Name                fixe
  LV UUID                PuyyLe-lwAF-xsK3-XSbX-UabV-YKea-ZUnLl4
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                380.00 MB
  Current LE             95
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:4
   
  --- Logical volume ---
  LV Name                /dev/fixe/home
  VG Name                fixe
  LV UUID                DTGGm1-lOem-0po1-BApp-ave5-wh2p-qLWCVk
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                19.00 GB
  Current LE             4864
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:5
lvreduce -L19G /dev/mapper/fixe-home
  WARNING: Reducing active and open logical volume to 19.00 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce home? [y/n]: y
  Reducing logical volume home to 19.00 GB
  Logical volume home successfully resized

lvextend -L1G /dev/mapper/fixe-root

  Extending logical volume root to 1.00 GB
  Logical volume root successfully resized
resize2fs /dev/mapper/fixe-root
resize2fs 1.41.3 (12-Oct-2008)
Filesystem at /dev/mapper/fixe-root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/mapper/fixe-root to 1048576 (1k) blocks.
The filesystem on /dev/mapper/fixe-root is now 1048576 blocks long.
resize2fs /dev/mapper/fixe-home 
resize2fs 1.41.3 (12-Oct-2008)
Filesystem at /dev/mapper/fixe-home is mounted on /home; on-line resizing required
On-line shrinking from 6564864 to 4980736 not supported.

Reducir un LV y crear uno nuevo

Tenemos 80 Gb en home y queremos pasar 20 a una unidad de apoyo

1. Siempre es mejor desmontar

umount /dev/vg_manager2/lv_home

2. Achichar espacio en disco, en este caso el nuevo tamanyo sera 60G

resize2fs /dev/vg_manager2/lv_home 60G

3. Achicar Logical Volume:

lvreduce -L60G /dev/vg_manager2/lv_home

4. Crear el nuevo LV con todo el espacio libre:

lvcreate -n lv_iso -l 100%FREE vg_manager2

5. Formatear el nuevo LV

mkfs.ext4 /dev/vg_manager2/lv_iso

Ahora ya se pueden montar los 2 LV

Renombrar volumenes

Renombrar un Grupo de Volúmenes (VG)

(Creo que todo como root)

1. Obtenemos el nombre del Grupo de Volúmenes

calero:~# vgscan
Reading all physical volumes.  This may take a while...
Found volume group "calero" using metadata type lvm2

2. Renombramos:

vgrename calero nuevo_nombre_vg

Renombrar un Volumen Lógico (LV)

(Creo que todo como root)

1. Obtenemos el nombre de todos los Volúmenes Lógicos del Grupo de Volúmenes:

calero:~# clear && lvdisplay 
  1. – Logical volume —

LV Name /dev/calero/root

VG Name                calero
LV UUID                KopwfX-2SZG-JJNg-H0xs-6dnb-zwmB-zBUMZR
LV Write Access        read/write
LV Status              available
# open                 1
LV Size                6,52 GB
Current LE             1668
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:0
 
--- Logical volume ---
LV Name                /dev/calero/swap_1
VG Name                calero
LV UUID                sGM4EI-QMBx-428v-B7nc-OcHB-KSnj-H9I1ze
LV Write Access        read/write
LV Status              available
# open                 2
LV Size                2,53 GB
Current LE             648
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:1
 
--- Logical volume ---
LV Name                /dev/calero/home
VG Name                calero
LV UUID                jlNSaR-X2mN-l6Cs-epeu-PDRv-3iqi-izAUBP
LV Write Access        read/write
LV Status              available
# open                 1
LV Size                65,24 GB
Current LE             16702
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:2

2. Renombramos el Volumen Lógico que queramos:

lvrename /dev/calero/home nuevo_nombre_lv

Añadir disco físico

Añadimos el disco físicamente

Al hacer un fdisk veremos que lo tenemos sin particionar

# fdisk -l

Disk /dev/hda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1305    10377990   8e  Linux LVM

Disk /dev/hdd: 2147 MB, 2147483648 bytes
16 heads, 63 sectors/track, 4161 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

Disk /dev/hdd doesn't contain a valid partition table

1. Creamos partición LVM

Con fdisk creamos una partición en /dev/hdb1 y la creamos tipo LVM:

# fdisk /dev/hdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 4161.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-4161, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-4161, default 4161): 
Using default value 4161

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/hdd: 2147 MB, 2147483648 bytes
16 heads, 63 sectors/track, 4161 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdd1               1        4161     2097112+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

2. Creación PV

# pvcreate /dev/hdd1 
Physical volume "/dev/hdd1" successfully created

3. Ampliamos VG

# vgextend VolGroup00 /dev/hdd1 
Volume group "VolGroup00" successfully extended

4. Ampliamos LV

De los 2Gb añadimos solo 1Gb:

# lvextend -L +1G /dev/VolGroup00/LogVol00 /dev/hdd1 
Extending logical volume LogVol00 to 10.12 GB
Logical volume LogVol00 successfully resized

Vemos que todavía queda 1Gb libre (Free PE/Size 32/1.00GB):

# vgdisplay 
  1. – Volume group —

VG Name VolGroup00

System ID             
Format                lvm2
Metadata Areas        3
Metadata Sequence No  6
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                2
Open LV               2
Max PV                0
Cur PV                3
Act PV                3
VG Size               11.88 GB
PE Size               32.00 MB
Total PE              380
Alloc PE / Size       348 / 10.88 GB
Free  PE / Size       32 / 1.00 GB
VG UUID               bB6L14-jB12-AmVc-IhZ0-PsK0-2kUe-nAvaTZ

Para ampliar al máximo de file system es sin ningún parámetro:

# lvextend /dev/VolGroup00/LogVol00 /dev/hdd1 
Extending logical volume LogVol00 to 11.09 GB
Logical volume LogVol00 successfully resized

5. Ampliamos FS

# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2654208 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 2654208 blocks long.

Referencia:

http://www.linuxnetadmin.com/2008/01/how-to-add-disk-space-with-lvm-on-linux.html

Añadir LV

TODAVÍA NO ESTÁ TERMINADO EL ARTÍCULO

b) Crear un LV con el espacio libre de un VG:

1. Obtener el espacio libre del VG en cuestión:

vgdisplay

...
Free PE / Size 3839 / 15 GB
...

2. Crear el LV de nombre 'var' con un tamaño de 15 gigas:

Opción 1:
lvcreate -n var--size 15G nombre_VG

Opción 2:

lvcreate -n var -l 3839 nombre_VG

4. Doy formato al nuevo volumen (sistema de ficheros EXT3)

mke2fs -j /dev/mapper/nombre_VG-var

5. Si es menester, lo meto en el /etc/fstab:

6. Arranca ok, pero no lo monta. Para ver el resultado:

lvdisplay

Aparece el nuevo volumen, con el valor '# open' a '0'

Crear VG

Antecedentes

La secuencia es:

1º crear un PV (Volumen Físico)

2º crear 1 VG (Grupo de Volúmenes) para ese PV

3º crear 1/+ LV (Volúmenes Lógicos) para ese VG

5. Crear un PV (Volumen Físico) en una partición:

pvcreate /dev/sda7

6. Crear un VG (Grupo de Volúmenes)

vgcreate vg /dev/sda7

7. Activarlo

vgchange -a y vg

Achicar LV swap

1. Desactivar todas la swap (se podría especificar la que usa el LV):

swapoff -a

2. Reducir 1 GB el tamaño del LV que usamos para swap

lvreduce /dev/vg/swap -L -1G
  WARNING: Reducing active logical volume to 4.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg/swap? [y/n]

Pulsar 'y' y 'enter'

  Size of logical volume vg/swap changed from 5.00 GiB (1280 extents) to 4.00 GiB (1024 extents).
  Logical volume vg/swap successfully resized.

3. Formatear como swap el dispositivo del LV

mkswap /dev/vg/swap
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=1c3bd974-d6c4-46e4-9f64-bc2445f9e200

4. Montar swap (previsamente ya tiene su entrada en '/etc/fstab'):

swapon -va
swapon: /dev/mapper/vg-swap: found signature [pagesize=4096, signature=swap]
swapon: /dev/mapper/vg-swap: pagesize=4096, swapsize=4294967296, devsize=4294967296
swapon /dev/mapper/vg-swap

5. Comprobar

cat /proc/swaps
Filename				Type		Size		Used		Priority
/dev/dm-1                               partition	4194300		0		-2
free -h
              total        used        free      shared  buff/cache   available
Mem:           31Gi       408Mi        21Gi       1.0Mi       9.6Gi        30Gi
Swap:         4.0Gi          0B       4.0Gi

Debian 12

Tenemos que desmontar /home

Miramos las particiones:

root@debian:~# df -h
Filesystem                   Size  Used Avail Use% Mounted on
udev                         3.8G     0  3.8G   0% /dev
tmpfs                        783M  688K  783M   1% /run
/dev/mapper/debian--vg-root   22G  1.5G   20G   7% /
tmpfs                        3.9G     0  3.9G   0% /dev/shm
tmpfs                        5.0M     0  5.0M   0% /run/lock
/dev/mapper/debian--vg-var   7.6G  262M  6.9G   4% /var
/dev/mapper/debian--vg-home   85G   40K   81G   1% /home
/dev/mapper/debian--vg-tmp   1.4G   40K  1.3G   1% /tmp
/dev/sda1                    455M   99M  332M  23% /boot
tmpfs                        783M     0  783M   0% /run/user/0
tmpfs                        783M     0  783M   0% /run/user/1000

Miramos LVM

root@debian:~# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/debian-vg/root
  LV Name                root
  VG Name                debian-vg
  LV UUID                sM1VB5-QkRX-BXM1-No2F-S23M-YwQU-Cg5351
  LV Write Access        read/write
  LV Creation host, time debian, 2023-10-04 18:21:09 -0400
  LV Status              available
  # open                 1
  LV Size                22.25 GiB
  Current LE             5697
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0
   
  --- Logical volume ---
  LV Path                /dev/debian-vg/var
  LV Name                var
  VG Name                debian-vg
  LV UUID                u5Hxq6-k9eJ-25WS-AK02-afVC-o76u-vlYnyd
  LV Write Access        read/write
  LV Creation host, time debian, 2023-10-04 18:21:09 -0400
  LV Status              available
  # open                 1
  LV Size                <7.73 GiB
  Current LE             1978
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1
   
  --- Logical volume ---
  LV Path                /dev/debian-vg/swap_1
  LV Name                swap_1
  VG Name                debian-vg
  LV UUID                r88cII-vYx4-aAmF-85yJ-vjq6-8jkR-vS5JtK
  LV Write Access        read/write
  LV Creation host, time debian, 2023-10-04 18:21:09 -0400
  LV Status              available
  # open                 2
  LV Size                976.00 MiB
  Current LE             244
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:2
   
  --- Logical volume ---
  LV Path                /dev/debian-vg/tmp
  LV Name                tmp
  VG Name                debian-vg
  LV UUID                mZHGHj-VUxN-QQhR-q8Lb-DAvW-E9sn-8ZuZ15
  LV Write Access        read/write
  LV Creation host, time debian, 2023-10-04 18:21:09 -0400
  LV Status              available
  # open                 1
  LV Size                1.37 GiB
  Current LE             351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:3
   
  --- Logical volume ---
  LV Path                /dev/debian-vg/home
  LV Name                home
  VG Name                debian-vg
  LV UUID                69Y7rL-hzIr-21jn-KV3R-8qP5-XLdg-arYSL7
  LV Write Access        read/write
  LV Creation host, time debian, 2023-10-04 18:21:10 -0400
  LV Status              available
  # open                 1
  LV Size                <86.43 GiB
  Current LE             22126
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:4

Quitamos 40Gbs a /home:

lvreduce -L -40G /dev/debian-vg/home
  WARNING: Reducing active and open logical volume to <46.43 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce debian-vg/home? [y/n]: y
  Size of logical volume debian-vg/home changed from <86.43 GiB (22126 extents) to <46.43 GiB (11886 extents).
  Logical volume debian-vg/home successfully resized.

Vemos que hay 40Gb libres:

root@debian:~# vgdisplay 
  --- Volume group ---
  VG Name               debian-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               118.76 GiB
  PE Size               4.00 MiB
  Total PE              30403
  Alloc PE / Size       20156 / 78.73 GiB
  Free  PE / Size       10247 / <40.03 GiB
  VG UUID               RjQDgJ-Nz9a-WDQG-4JoJ-KPHc-CUG4-zed2BI

Se las añadimos a /var

lvextend -L +40G /dev/debian-vg/var
  Size of logical volume debian-vg/var changed from <7.73 GiB (1978 extents) to <47.73 GiB (12218 extents).
  Logical volume debian-vg/var successfully resized.

Vemos que todavía no lo ha añadido:

df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/debian--vg-root   22G  1.5G   20G   7% /
/dev/mapper/debian--vg-home   85G   40K   81G   1% /home
/dev/mapper/debian--vg-tmp   1.4G   40K  1.3G   1% /tmp
/dev/mapper/debian--vg-var   7.6G  262M  6.9G   4% /var

Pero con lvdisplay si aparece:

  --- Logical volume ---
  LV Path                /dev/debian-vg/var
  LV Name                var
  VG Name                debian-vg
  LV UUID                jud91P-5LXr-t0KI-5KSM-nim3-Xhvk-oSwkw5
  LV Write Access        read/write
  LV Creation host, time debian, 2023-10-04 19:47:42 -0400
  LV Status              available
  # open                 1
  LV Size                <47.73 GiB
  Current LE             12218
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

Hay que extender el FS:

resize2fs /dev/debian-vg/var
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/debian-vg/var is mounted on /var; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 6
The filesystem on /dev/debian-vg/var is now 12511232 (4k) blocks long.

Ahora si que aparece:

root@debian:~# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/debian--vg-root   22G  1.5G   20G   7% /
/dev/mapper/debian--vg-home   85G   40K   81G   1% /home
/dev/mapper/debian--vg-tmp   1.4G   40K  1.3G   1% /tmp
/dev/mapper/debian--vg-var    47G  262M   45G   1% /var