2011-09-05 21:38:11 +0000 2011-09-05 21:38:11 +0000
67
67
Advertisement

bashスクリプトを使ってパーティションを作成してフォーマットするには?

Advertisement

bashスクリプトを使ってパーティションを作成してフォーマットする方法はありますか?

fdiskでできると思うのですが、bashスクリプトからfdiskシェルにコマンドを入力してfdiskシェルを終了する方法がわかりません。

bashからパーティションを作成してntfsにフォーマットしたいのですが、どうすればいいのでしょうか?

Advertisement
Advertisement

回答 (10)

72
72
72
2015-10-10 03:03:55 +0000

以前の提案と同様に、コマンドを fidsk にパイプするという方法は、後続のメンテナのために詳細を残すために有用であることがわかりました。sed ビットは fdisk が入力を取得する前にすべてのコメントを削除します。

# to create the partitions programatically (rather than manually)
# we're going to simulate the manual input to fdisk
# The sed script strips off all the comments so that we can 
# document what we're doing in-line with the actual commands
# Note that a blank line (commented as "defualt" will send a empty
# line terminated with a newline to take the fdisk default.
sed -e 's/\s*\([0-9a-zA-Z]*\).*//' << EOF | fdisk ${TGTDEV}
  o # clear the in memory partition table
  n # new partition
  p # primary partition
  1 # partition number 1
    # default - start at beginning of disk 
  +100M # 100 MB boot parttion
  n # new partition
  p # primary partition
  2 # partion number 2
    # default, start immediately after preceding partition
    # default, extend partition to end of disk
  a # make a partition bootable
  1 # bootable partition is partition 1 -- /dev/sda1
  p # print the in-memory partition table
  w # write the partition table
  q # and we're done
EOF
68
68
68
2016-10-08 17:30:51 +0000

**

sfdiskfdisk の Scripted 版です util-linux の一部です と同様に fdisk と同じですので、可用性は同じはずです。

ディスク全体を取る1つのパーティションを持つパーティションテーブルは、

0x1で作成できます。

echo 'type=83' | sudo sfdisk /dev/sdX

より複雑なパーティションテーブルについては以下で説明します。

sudo sfdisk -d /dev/sda > sda.sfdisk

2x2& より複雑なパーティションテーブルについては以下で説明します。

label: dos
label-id: 0x7ddcbf7d
device: /dev/sda
unit: sectors

/dev/sda1 : start= 2048, size= 3072000, type=7, bootable
/dev/sda2 : start= 3074048, size= 195430105, type=7
/dev/sda3 : start= 948099072, size= 28672000, type=7
/dev/sda4 : start= 198504446, size= 749594626, type=5
/dev/sda5 : start= 198504448, size= 618891264, type=83
/dev/sda6 : start= 940277760, size= 7821312, type=82
/dev/sda7 : start= 817397760, size= 61437952, type=83
/dev/sda8 : start= 878837760, size= 61437500, type=83

私のLenovo T430 Windows 7 / Ubuntuデュアルブートでの出力例。

sudo sfdisk /dev/sdX < sda.sfdisk

スクリプトをファイルに保存したら、sdXに適用することができます。

start= 2048, size= 3072000, type=7, bootable

sfdisk の入力では、デバイス名を省略して、タイプの行を使用することができます。

0x1&

これらは存在する場合には無視され、デバイス名はコマンドライン引数から取得されます。

0x1&

これらは存在する場合は無視され、デバイス名はコマンドライン引数から取得されます。

fdisksfdisk コマンドを使って I スクリプトを読むこともできます。

Ubuntu 16.04, fdisk 2.27.1でテスト。

sfdisk

2.27.1でテスト済み。 https://stackoverflow.com/questions/10949169/how-to-create-a-multi-partition-sd-disk-image-without-root-privileges/52850819#52850819

56
Advertisement
56
56
2011-09-06 03:34:53 +0000
Advertisement

fdiskstdin から読み取るので、適切なコマンドを与える必要があります。例えば、以下の例では、パーティションテーブルがあればクリアし、ディスク全体のパーティションを持つ新しいパーティションを作成します。

(
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number
echo # First sector (Accept default: 1)
echo # Last sector (Accept default: varies)
echo w # Write changes
) | sudo fdisk

0x1&

再現できるように、入力した内容を記録しておくことをお勧めします。

37
37
37
2014-04-09 12:53:18 +0000

複数のエコーの代わりにイントロの**\n*を使ってください。

echo -e "o\nn\np\n1\n\n\nw" | fdisk /dev/sda
5
Advertisement
5
5
2015-02-25 15:44:20 +0000
Advertisement

fdiskへのコマンドのパイピングは、他の人が説明しているようにうまく動作しますが、この方法の方が少しエレガントで読みやすいです。

fdisk /dev/sdc <<EOF
n
p
1

w
EOF

(一時的な)ファイルからのパイピングも動作します。

fdisk /dev/sdc < /tmp/fdisk.cmds
5
5
5
2012-06-08 15:42:12 +0000

fdiskをスクリプト化することができます。

(echo n; echo p; echo 1; echo 1; echo 200; echo w) | fdisk /dev/sdc

これで/dev/sdcに200MBのパーティションが作成されます。

5
Advertisement
5
5
2017-10-06 19:03:35 +0000
Advertisement

gpt タイプの新しいディスクラベルを作成します。

sudo /sbin/parted /dev/xvdf mklabel gpt --script

ディスクのパーティションを作成します。

sudo /sbin/parted /dev/xvdf mkpart primary 0% 100% --script

1
1
1
2016-03-23 19:29:01 +0000
printf 'o\nn\np\n1\n\n\nw' | fdisk /dev/sda
0
Advertisement
0
0
2017-07-14 12:07:51 +0000
Advertisement

スクリプトはfdiskを使用して、他の人の回答に基づいてパーティトンを作成します。

スクリプトの以下を変更します。

NUM_PARTITIONS =5

PARTITION_SIZE =“+10G”

スクリプトの後に使用例を示します。

#!/bin/bash
if [$# -eq 0]
then
  echo "input the device"
  exit
fi

NUM_PARTITIONS=5
PARTITION_SIZE="+10G"    

SED_STRING="o"
TAIL="p
w
q
"

NEW_LINE="
"
LETTER_n="n"
EXTENDED_PART_NUM=4
TGTDEV=$1

SED_STRING="$SED_STRING$NEW_LINE"
for i in $(seq $NUM_PARTITIONS)
do
  if [$i -lt $EXTENDED_PART_NUM]
  then
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$NEW_LINE$NEW_LINE$PARTITION_SIZE$NEW_LINE"
  fi
  if [$i -eq $EXTENDED_PART_NUM]
  then
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$NEW_LINE$NEW_LINE"
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$PARTITION_SIZE$NEW_LINE"
  fi
  if [$i -gt $EXTENDED_PART_NUM]
  then
    SED_STRING="$SED_STRING$LETTER_n$NEW_LINE$NEW_LINE$PARTITION_SIZE$NEW_LINE"
  fi
done
SED_STRING="$SED_STRING$TAIL"

sed -e 's/\s*\([0-9a-zA-Z]*\).*//' << EOF | fdisk ${TGTDEV}
  $SED_STRING
EOF

と実行します。

sudo sh mk_partition.sh /dev/sdxxx.

-2
-2
-2
2016-07-15 12:49:49 +0000
#!/bin/sh
hdd="/dev/hda /dev/hdb /dev/hdc"
for i in $hdd;do
echo "n
p
1

w
"|fdisk $i;mkfs.ext3 "$i1";done

このスクリプトを使うことができます。hdd が定義されているパーティションを自分で定義するようにしてください。1w の間には 2 つの空白があります。

Advertisement

関連する質問

11
6
3
9
7
Advertisement