GNU/Linux >> Linux の 問題 >  >> Linux

非対話モードで parted を使用してパーティションのサイズを最大に変更します

ヒント:このスクリプトは parted v3+ OOTB と互換性があります。parted 2 を使用している場合は、parted resizepart を変更する必要があります。 parted resize まで

これをスクリプトに入れましょう。acualy コマンドは onliner です。最初の 2 つのパラメーターが確実に設定されるように、さらに多くのコマンドを追加します。

#!/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

使い方

上記のスクリプトを resize.sh として保存します 実行可能にする

# 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

たとえば、LVM の使用中に /dev/sdb1 に lv 'data' を持つ vg vgdata がある場合、全体のストーリーは次のようになります

./resize.sh /dev/sdb 1 apply
pvresize /dev/sdb1
lvextend -r /dev/mapper/vgdata-data -l 100%FREE

以上で、サイズ変更されたファイルシステムを含む論理ボリュームのサイズが変更されました( -r )-すべて完了しました。 df -h で確認してください :)

説明

ディスクサイズを見つけるために使用するのは

です
resizepart ${PARTNR} `parted -l | grep ${DEVICE} | cut -d' ' -f3 | tr -d MB

a) 問題のデバイス printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda のディスク情報を取得します printf %s\\n 'unit MB print list' を使用します 単位が MB として表示されるようにします。そうしないと、ディスク サイズ (MB、G、T) によって異なり、parted 3 または 4 でこれを行うより良い方法がまだないためです

b) 次に、出力の 3 列目を使用します (ディスク サイズ) cut -d' ' -f3 (スペースで区切る)

c) 最後に tr -d MB を使用して単位「MB」を空白で切り捨てます

フォローアップ

https://github.com/EugenMayer/parted-auto-resize でスクリプトを公開したので、機能を改善する必要がある場合は、プルリクエストを使用してください (この質問の範囲外のもの)


Linux
  1. Parted GPT を使用して Linux で 2TB サイズのパーティションを作成する方法

  2. Linux での GPT パーティションのライブ サイズ変更

  3. parted を使用したスクリプト可能な GPT パーティション

  1. LinuxでGNUPartedを使用してドライブをパーティション分割する

  2. Ubuntu – /でパーティションのサイズを変更しても安全ですか?

  3. 拡張パーティション内のLVMパーティションのサイズを変更する方法

  1. Partedコマンドを使用してディスクパーティションを削除する方法

  2. テキスト モードを使用して CentOS 7 をインストールする

  3. fdisk - 単一行のパーティション