SUGERENCIA:este script es compatible con parted v3+ OOTB, si tiene parte 2, debe cambiar parted resizepart
a parted resize
pongamos esto en un script, el comando acualy es un onliner, solo agregamos mucho más para asegurarnos de que los primeros 2 parámetros estén configurados:
#!/bin/bash
set -e
if [[ $# -eq 0 ]] ; then
echo 'please tell me the device to resize as the first parameter, like /dev/sda'
exit 1
fi
if [[ $# -eq 1 ]] ; then
echo 'please tell me the partition number to resize as the second parameter, like 1 in case you mean /dev/sda1 or 4, if you mean /dev/sda2'
exit 1
fi
DEVICE=$1
PARTNR=$2
APPLY=$3
fdisk -l $DEVICE$PARTNR >> /dev/null 2>&1 || (echo "could not find device $DEVICE$PARTNR - please check the name" && exit 1)
CURRENTSIZEB=`fdisk -l $DEVICE$PARTNR | grep "Disk $DEVICE$PARTNR" | cut -d' ' -f5`
CURRENTSIZE=`expr $CURRENTSIZEB / 1024 / 1024`
# So get the disk-informations of our device in question printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda we use printf %s\\n 'unit MB print list' to ensure the units are displayed as MB, since otherwise it will vary by disk size ( MB, G, T ) and there is no better way to do this with parted 3 or 4 yet
# then use the 3rd column of the output (disk size) cut -d' ' -f3 (divided by space)
# and finally cut off the unit 'MB' with blanc using tr -d MB
MAXSIZEMB=`printf %s\\n 'unit MB print list' | parted | grep "Disk ${DEVICE}" | cut -d' ' -f3 | tr -d MB`
echo "[ok] would/will resize to from ${CURRENTSIZE}MB to ${MAXSIZEMB}MB "
if [[ "$APPLY" == "apply" ]] ; then
echo "[ok] applying resize operation.."
parted ${DEVICE} resizepart ${PARTNR} ${MAXSIZEMB}
echo "[done]"
else
echo "[WARNING]!: Sandbox mode, i did not size!. Use 'apply' as the 3d parameter to apply the changes"
fi
uso
Guarde el script anterior como resize.sh
y hacerlo ejecutable
# resize the fourth partition to the maximum size, so /dev/sda4
# this is no the sandbox mode, so no changes are done
./resize.sh /dev/sda 4
# apply those changes
./resize.sh /dev/sda 4 apply
Por ejemplo, en caso de que tenga un vg vgdata con un lv 'datos' en /dev/sdb1 mientras usa LVM, la historia completa se vería como
./resize.sh /dev/sdb 1 apply
pvresize /dev/sdb1
lvextend -r /dev/mapper/vgdata-data -l 100%FREE
Eso es todo, volumen lógico redimensionado, incluido el sistema de archivos redimensionado ( -r ) - todo listo, verifíquelo con df -h
:)
Explicación
Lo que usamos para encontrar el tamaño del disco es
resizepart ${PARTNR} `parted -l | grep ${DEVICE} | cut -d' ' -f3 | tr -d MB
a) Obtenga la información del disco de nuestro dispositivo en cuestión printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda
usamos printf %s\\n 'unit MB print list'
para asegurarse de que las unidades se muestren como MB, ya que de lo contrario variará según el tamaño del disco (MB, G, T) y no hay mejor manera de hacerlo con 3 o 4 partes todavía
b) luego use la tercera columna de la salida (tamaño del disco) cut -d' ' -f3
(dividido por espacios)
c) y finalmente cortar la unidad 'MB' con blanc usando tr -d MB
Seguimiento
Publiqué el script en https://github.com/EugenMayer/parted-auto-resize, por lo que si hay algo que mejorar en cuanto a funciones, use solicitudes de extracción allí (cualquier cosa que no esté dentro del alcance de esta pregunta)