BASHスクリプトの先頭でパーティションセクタ値a、b、c、dを使用して3つのパーティションをどのように計算して再分割できますか?

BASHスクリプトの先頭でパーティションセクタ値a、b、c、dを使用して3つのパーティションをどのように計算して再分割できますか?

Linuxシステムに次のように始まる次のbashスクリプトがあるとします。

#!/bin/bash
#This setsetcor script takes four arguments: setsector a b c d
#Both a, b, and c and d are integers

a=$1; #The end sector (in 512 byte sectors) of the sdx1 partition
b=$2; #The end sector (in 512 byte sectors) of the sdx2 partition
c=$3; #The end sector (in 512 byte sectors) of the sdx3 partition
d=$4; #The end sector (in 512 byte sectors) of the free space

小さなSSDディスクドライブがあり、ddユーティリティを使用して大きなSSDまたはHDドライブにフルコピーを作成して初期ファイルを作成します。同じコピーより小さいドライブからより大きいドライブへ。

次に、スクリプトで実行された計算を使用して、次の操作を実行しようとします。

  • パーティション 1 と 2 を同じにしてください。
  • 次のコマンドを使用して、パーティション3の内容をdd次の場所にコピーします。終わり大型ディスクドライブスペース
  • コマンドを使用したcfdiskパーティションの削除パーティションテーブルにのみその内容には含まれていませんパーティション3の場合。
  • 最後がパーティション 3 コピーの先頭の前になるようにパーティション 2 を拡張します。
  • このコマンドを使用して、cfdiskディスクドライブの最後にパーティション3を元のサイズに戻します。
  • コマンドを使用してntfsresizeパーティション2を展開します。
  • そうであれば仮定します。DDこのオプションを使用すると、ドライブの末尾にあるパーティション番号3のNTFSサイズ変更は不要です。必要に応じて、この問題を解決するのに役立ちます。
  • これらすべてを計算するのは複雑なので、スクリプトは必要な手順を印刷してください。

これを実行するために引き続きコマンドを使用しようとしましcfdiskたが、まだ入力の正しい式を見つけることができませんでした。開始パーティションセクタ番号そしてパーティションセクタのサイズ変更パーティションを再作成するときの値。

どんな提案がありますか?変数によって指定されたパーティションセクタ終了値を使用して3つのパーティションを計算して再分割する方法a、b、c、dBashスクリプトの始めに

ベストアンサー1

次のコードは質問に対する回答を実装しています。

#!/bin/bash
#This setsetcor script takes four arguments: setsector a b c d
#Both a, b, and c and d are integers

a=$1; #The end sector (in 512 byte sectors) of the sdx1 partition
b=$2; #The end sector (in 512 byte sectors) of the sdx2 partition
c=$3; #The end sector (in 512 byte sectors) of the sdx3 partition
d=$4; #The end sector (in 512 byte sectors) of the free space

#Input quantities are printed out
printf "The end (in 512 byte sectors) of the sdx1 partition is a=%dS (512-Byte) Sectors.\n" $((a))
printf "The end (in 512 byte sectors) of the sdx2 partition is b=%dS (512-Byte) Sectors.\n" $((b))
printf "The end (in 512 byte sectors) of the sdx3 partition is c=%dS (512-Byte) Sectors.\n" $((c))
printf "The end (in 512 byte sectors) of the free space     is d=%dS (512-Byte) Sectors.\n" $((d))
printf "\n"

#Simple derivative quantities are calculated and printed
sizeofp2=$(($b-$a))
sizeofp2GB=$((10**2*($b-$a)*512/1024/1024/1024))

sizeofp3=$(($c-$b));
sizeofp3MB=$((10**2*($c-$b)*512/1024/1024));

freespace=$(($d-$c));
freespaceGB=$((10**2*($d-$c)*512/1024/1024/1024));

printf "The size of Partition 2 equals %3.1fGB and %dS (512-Byte) Sectors. \n" $(($sizeofp2GB))e-2 $sizeofp2
printf "The size of Partition 3 equals %3.1fMB and %dS (512-Byte) Sectors. \n" $(($sizeofp3MB))e-2 $sizeofp3
printf "The Free Space equals %3.2fGB and %dS (512-Byte) Sectors.\n" $(($freespaceGB))e-2 $freespace
printf "\n"

#Calculate the new partition #2 and partition #3, Sectors and MB.  Adjustments need to be made in Sectors to avoid rounding.
printf "Delete Partition 3; then delete the Partition 2 (without saving the partition table). \n\n"

printf "A new Partition 2 needs to be formed, (type 7) with the Sector size (of the origninal partition 2 Sectors \n"
printf "plus the original Free Space Sectors). \n\n"

printf "Then Partition 3 (type 27) is formed with its original Sector size, to avoid rounding errors:\n\n"

sizeofnewp2=$(($sizeofp2+$freespace))
sizeofnewp2GB=$((10**2*($sizeofp2+$freespace)*512/1024/1024/1024));

printf "The size of the new partition 2 equals %3.1fGB and %dS (512-Byte) Sectors. \n" $(($sizeofnewp2GB))e-2 $sizeofnewp2
printf "The size of the new partition 3 equals %3.1fMB and %dS (512-Byte) Sectors. \n\n" $(($sizeofp3MB))e-2 $sizeofp3

printf "After all of the ajustements are made (including setting the Partition types properly), save the Partition Table.\n"

場合によっては、元のパーティション3セクタにさらに多くのスペースを確保するために、パーティション2のサイズをわずかに小さくする必要があります(例:0.1 GB)。その他には関連があるようです。cfdisk。 (一部の丸めエラーのため、これらの小さな違いが発生していると思われますが、今では役に立つ情報があります。便宜上、使用されているcfdiskパーティションタイプを印刷しています。エンドユーザーの状況アプリケーションに合わせて調整する必要があります。)

おすすめ記事