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

Come creare e formattare una partizione usando uno script bash?

C'è un modo per creare e formattare una partizione usando uno script bash?

Penso che possa essere fatto con fdisk ma non so come inserire i comandi dallo script bash nella shell di fdisk e poi uscire dalla shell di fdisk.

Voglio creare una partizione e formattarla in ntfs da bash.

答案 (10)

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

Simile ai suggerimenti precedenti, piping dei comandi a fidsk, ho trovato questo approccio utile per lasciare i dettagli ai manutentori successivi. I bit di sed rimuovono tutti i commenti prima che fdisk riceva l'input.

# 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

sfdisk

sfdisk è una versione scriptata di fdisk Fa parte di util-linux , proprio come fdisk, quindi la disponibilità dovrebbe essere la stessa.

Una tabella delle partizioni con una singola partizione che occupa l'intero disco può essere creata con:

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

e tabelle di partizione più complesse sono spiegate di seguito.

Per generare uno script di esempio, prendete la configurazione di uno dei vostri dischi:

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

Esempio di output sul mio Lenovo T430 Windows 7 / Ubuntu dual boot:

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

Una volta che avete lo script salvato in un file, potete applicarlo a sdX con:

sudo sfdisk /dev/sdX < sda.sfdisk

Per l'input di sfdisk, puoi semplicemente omettere i nomi dei dispositivi, e usare linee del tipo:

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

Sono semplicemente ignorati se presenti, e il nome del dispositivo è preso dall'argomento della linea di comando.

Alcune spiegazioni:

fdisk può anche leggere gli script sfdisk con il comando I, che li “origina” durante una sessione interattiva fdisk, permettendo ulteriori personalizzazioni prima di scrivere la partizione.

Testato su Ubuntu 16.04, sfdisk 2.27.1.

Formattare e popolare le partizioni un file immagine senza sudo

Questo è un buon modo per imparare ad usare sfdisk senza far saltare i propri hard disk: https://stackoverflow.com/questions/10949169/how-to-create-a-multi-partition-sd-disk-image-without-root-privileges/52850819#52850819

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

fdisk legge da stdin quindi dovete solo dargli i comandi appropriati. Per esempio, il seguente cancella la tabella delle partizioni, se ce n'è una, e ne crea una nuova che ha una sola partizione che è l'intero disco:

(
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

Vi consiglio di fare il compito che volete, registrando quello che digitate in modo da poterlo riprodurre.

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

Potete farlo con solo un paio di comandi, usate intro \n invece di echi multipli.

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

Il piping dei comandi su fdisk funziona bene come spiegato da altre persone, ma in questo modo è un po’ più elegante e leggibile:

fdisk /dev/sdc <<EOF
n
p
1

w
EOF

Anche il piping da un file (temporaneo) funziona:

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

Potete scrivere fdisk.

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

che crea una partizione di 200 MB su /dev/sdc

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

Creare una nuova etichetta disco di tipo gpt:

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

Partizionare il disco:

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
0
0
2017-07-14 12:07:51 +0000

Lo script usa fdisk per creare partizioni basate sulle risposte di altre persone.

Cambia quanto segue nello script:

NUM_PARTITIONS =5

PARTITION_SIZE =“+10G”

Un esempio di utilizzo è dato dopo lo script.

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

eseguito con:

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

Potete usare questo script. Assicuratevi solo di definire le vostre partizioni dove è definito hdd. Ci sono 2 spazi vuoti tra 1 e w; devono essere lì per prendere i valori di default.