SUGGERIMENTO:questo script è compatibile con parted v3+ OOTB, se hai parted 2, devi cambiare parted resizepart
a parted resize
mettiamolo in uno script, il comando acualy è un onliner, aggiungiamo solo molto altro per assicurarci che i primi 2 parametri siano impostati:
#!/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
utilizzo
Salva lo script sopra come resize.sh
e renderlo eseguibile
# 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
Ad esempio, nel caso in cui tu abbia un vg vgdata con un lv 'data' su /dev/sdb1 mentre usi LVM, l'intera storia sarebbe simile a
./resize.sh /dev/sdb 1 apply
pvresize /dev/sdb1
lvextend -r /dev/mapper/vgdata-data -l 100%FREE
Ecco fatto, volume logico ridimensionato incluso filesystem ridimensionato ( -r ) - tutto fatto, controllalo con df -h
:)
Spiegazione
Quello che usiamo per trovare la dimensione del disco è
resizepart ${PARTNR} `parted -l | grep ${DEVICE} | cut -d' ' -f3 | tr -d MB
a) Quindi ottieni le informazioni sul disco del nostro dispositivo in questione printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda
usiamo printf %s\\n 'unit MB print list'
per garantire che le unità siano visualizzate come MB, poiché altrimenti varierà in base alla dimensione del disco ( MB, G, T ) e non esiste ancora un modo migliore per farlo con parted 3 o 4
b) quindi utilizzare la terza colonna dell'output (dimensione del disco) cut -d' ' -f3
(diviso per spazio)
c) e infine tagliare l'unità 'MB' con blanc usando tr -d MB
Follow-up
Ho pubblicato lo script su https://github.com/EugenMayer/parted-auto-resize, quindi se qualcosa deve migliorare le funzionalità, usa le richieste pull lì (qualsiasi cosa non rientri nell'ambito di questa domanda)