Ik wil weten hoe ik een opslagschijf kan formatteren vanaf de terminal. Handige dingen om in het antwoord te geven zouden vaak gebruikte opties voor commando's zijn en basiskennis die men kan gebruiken om toekomstig gebruik te extrapoleren. Specifiek zou ik willen weten hoe te formatteren in de verschillende bestandssystemen zoals NTFS, FAT32, EXT4, enz. Informatie over hoe de schijf te partitioneren via terminal is ook gewenst.
Ik probeer een externe harde schijf met hoge capaciteit (EHDD) te formatteren in NTFS - vanaf de terminal.
Ik weet dat ik gparted hiervoor kan gebruiken, evenals andere GUI programma's, maar ik wil nog steeds weten hoe ik het vanaf de terminal moet doen.
Er zijn een paar opties beschikbaar:
fdisk
(ouder, ondersteunt'geen GPT4).parted
(het CLI-broertje van GParted).mkfs
programma's, als u al partities heeft en deze wilt formatteren.
fdisk
en parted
zijn interactief, en hebben help commando's, zodat u altijd hulp kunt zoeken binnen het programma. Beide zijn ook scriptbaar. De mkfs
commando's zijn niet interactief.fdisk
fdisk
verwacht een apparaat (zoals /dev/sda
) als argument. Het heeft de volgende commando's:
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the DOS compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
fdisk
niet zo vaak. Ik'zal me alleen richten op:parted
parted
heeft geen argument nodig (het probeert te "raden"), maar je moet altijd de schijf specificeren. Als je de keus hebt, is parted
het programma dat je zou moeten verkiezen. Het heeft de volgende commando's:
align-check TYPE N check partition N for TYPE(min|opt) alignment
check NUMBER do a simple check on the file system
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER
mkpart PART-TYPE [FS-TYPE] START END make a partition
mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system
resizepart NUMBER END resize partition NUMBER
move NUMBER START END move partition NUMBER
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resize NUMBER START END resize partition NUMBER and its file system
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
De commando's kunnen worden verkort tot een uniek voorvoegsel (b.v. h
is een afkorting voor help
).
Ik'ga een tijdelijk bestand gebruiken (/tmp/part
) dat ik gemaakt heb om je de commando's te laten zien, dus de afmetingen zullen wat klein zijn. U zou dat moeten vervangen door het apparaat dat u nodig heeft (/dev/sda
, bijvoorbeeld).
Ten eerste, als je schijf geen partitietabel heeft, moeten we er een maken:
parted /tmp/part mklabel gpt
of mklabel msdos
, als u de oude-school 4-primary-partitie ding wilt (genaamd MBR of MSDOS partitietabel).
Dan maken we, laten we zeggen, een ext4 partitie beginnend bij 3GB (dus de initiële 3G vrij latend) en van grootte 2GB (dus eindigend bij 5GB). parted
verwacht locaties in MB voor mkpartfs
, maar we kunnen het achtervoegsel opgeven:
parted /tmp/part mkpart primary ext4 3G 5G
En nog een, nu een NTFS partitie van 1GB:
parted /tmp/part mkpart primary ntfs 5G 6G
Resultaat:
# parted /tmp/part print
Model: (file)
Disk /tmp/blah: 10.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 3000MB 5000MB 2000MB primary
2 5000MB 6000MB 1000MB primary msftdata
Merk op hoe het SI voorvoegsels gebruikt, terwijl GParted standvastig binaire voorvoegsels gebruikt (en de domme i
laat vallen).
Ik zal de partities labelen:
# parted /tmp/part name 1 hello
# parted /tmp/part name 2 world
# parted /tmp/part print
Model: (file)
Disk /tmp/blah: 10.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 3000MB 5000MB 2000MB hello
2 5000MB 6000MB 1000MB world msftdata
Terwijl parted
prima partities van het bestandssysteem ntfs
kan maken, kan het een bestaande partitie (!) niet formatteren naar NTFS:
mkfs partition fs-type
Make a filesystem fs-type on partition. fs-type can be one
of "fat16", "fat32", "ext2", "linux-swap", or "reiserfs".
mkfs
mkfs
, net als fsck
, is in essentie een frontend voor diverse bestandssysteem-specifieke commando's. Op mijn systeem zijn bijvoorbeeld mkfs.bfs
, mkfs.cramfs
, mkfs.ext2
, mkfs.ext3
, mkfs.ext4
, mkfs.ext4dev
, mkfs.fat
, mkfs.minix
, mkfs.msdos
, mkfs.ntfs
, mkfs.vfat
beschikbaar.
Nu, helaas, terwijl parted
prima werkt op een bestand, zoals degene die ik hierboven gebruikte, kan mkfs
niet op jacht gaan naar partities in zulke bestanden. In feite verwacht het blokapparaten, dus als ik een nieuw bestand /tmp/file
voor mkfs
ga gebruiken, moet ik het dwingen om dat te doen. U'zult het blokapparaat gebruiken dat overeenkomt met de partitie die u wilt formatteren, zoals /dev/sda2
.
De algemene syntaxis voor mkfs
is:
# mkfs --help
Usage: mkfs [options] [-t type fs-options] device [size]
Options:
-t, --type=TYPE file system type, when undefined ext2 is used
fs-options parameters to real file system builder
device path to a device
size number of blocks on the device
-V, --verbose explain what is done
defining -V more than once will cause a dry-run
-V, --version output version information and exit
-V as version must be only option
-h, --help display this help and exit
For more information, see mkfs(8).
Zoals u kunt zien, laat de -t
vlag ons bestandssysteem-specifieke vlaggen doorgeven. Bijvoorbeeld, NTFS vlaggen:
# mkfs.ntfs --help
Usage: mkntfs [options] device [number-of-sectors]
Basic options:
-f, --fast Perform a quick format
-Q, --quick Perform a quick format
-L, --label STRING Set the volume label
-C, --enable-compression Enable compression on the volume
-I, --no-indexing Disable indexing on the volume
-n, --no-action Do not write to disk
Advanced options:
-c, --cluster-size BYTES Specify the cluster size for the volume
-s, --sector-size BYTES Specify the sector size for the device
-p, --partition-start SECTOR Specify the partition start sector
-H, --heads NUM Specify the number of heads
-S, --sectors-per-track NUM Specify the number of sectors per track
-z, --mft-zone-multiplier NUM Set the MFT zone multiplier
-T, --zero-time Fake the time to be 00:00 UTC, Jan 1, 1970
-F, --force Force execution despite errors
Output options:
-q, --quiet Quiet execution
-v, --verbose Verbose execution
--debug Very verbose execution
Help options:
-V, --version Display version
-l, --license Display licensing information
-h, --help Display this help
Developers' email address: [email protected]
News, support and information: http://tuxera.com
Dus laten we een NTFS partitie maken, met snel formatteren (-Q
), dwingen om te werken op een niet-blok-device bestand (-F
), en een label instellen (-L "hello world"
).
# mkfs -t ntfs -F -Q -L "hello world" /tmp/file
/tmp/file is not a block device.
mkntfs forced anyway.
The sector size was not specified for /tmp/file and it could not be obtained automatically. It has been set to 512 bytes.
The partition start sector was not specified for /tmp/file and it could not be obtained automatically. It has been set to 0.
The number of sectors per track was not specified for /tmp/file and it could not be obtained automatically. It has been set to 0.
The number of heads was not specified for /tmp/file and it could not be obtained automatically. It has been set to 0.
Cluster size has been automatically set to 4096 bytes.
To boot from a device, Windows needs the 'partition start sector', the 'sectors per track' and the 'number of heads' to be set.
Windows will not be able to boot from this device.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.
Het vond het duidelijk niet leuk om aan een bestand te werken. :) Maak je geen zorgen, het zou de meeste waarden automatisch moeten detecteren als je op een echte schijf werkt. Zelfs dit "file" werkt prima als een bestandssysteem:
# mount -t ntfs-3g /tmp/file /mnt
# touch "/mnt/a file in mnt"
# ls -l /mnt
total 0
-rwxrwxrwx 1 root root 0 Aug 29 06:43 a file in mnt
# umount /mnt
# ls -l /mnt
total 0
sudo
nog nergens in dit antwoord gebruikt. Omdat ik op bestanden werkte, en bestanden die mijn eigendom zijn, had ik sudo
niet nodig. parted
zal je hiervoor waarschuwen. Voor blokapparaten, die meestal altijd eigendom zijn van root
, heb je sudo
nodig (of je'moet een root-shell gebruiken via sudo -i
of sudo su -
).parted
is een GNU programma, en zoals veel GNU programma's, heeft het uitgebreide documentatie in het info
formaat. Installeer parted-doc
(sudo apt-get install parted-doc
) en voer dan info parted
uit. Je kunt ook de online user's manual bekijken.mkfs
programma aanroept (mkntfs
, in dit geval - mkfs.ntfs
is slechts een link naar mkntfs
). Het stelt ook een aantal parameters in. In feite kun je voor de meeste operaties de details van de GParted berichten bekijken om te zien welke commando's zijn uitgevoerd.parted
of fdisk
(behalve misschien voor een eerste stap van het aanmaken van partities voor hun gebruik).parted
gebruik:De syntaxis van het parted
programma is:
parted [options] [device [command [options...]...]]
Als je parted
uitvoert zonder een commando, zoals:
parted /tmp/parted
krijgt u een eenvoudige commandoregel te zien, waar u de bovenstaande commando's kunt uitvoeren. Deze commando's kunnen echter ook direct worden uitgevoerd met het parted
programma. Dus deze drie zijn gelijkwaardig:
# parted /tmp/parted
GNU Parted 2.3
Using /tmp/parted
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
En
# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /tmp/parted
Using /tmp/parted
(parted) mklabel gpt
En
parted /tmp/parted mklabel gpt
Merk ook op dat, wanneer je partities maakt met parted
, een nuttige indicator voor het einde van partities -1s
is (dit is "1" tussen het koppelteken en de "s"). Dit is handig als u wilt dat uw partitie zich uitstrekt van een gespecificeerd begin tot de rest van de schijf. Om specifieker te zijn, het uitvoeren van
parted /dev/sda -- mkpart primary ext4 3G -1s
zal een partitie van /dev/sda
aanmaken die begint op 3G en eindigt op de laatste sector van de /dev/sda
schijf (dus van 3G tot de hele rest van de schijf). Merk op dat de --
nodig is, om te voorkomen dat 1s
wordt geïnterpreteerd als een ongeldige optie.
Eerst moet je je harde schijf partitioneren met het fdisk programma.
Linux staat slechts 4 primaire partities toe. Je kunt een veel groter aantal logische partities hebben door een van de primaire partities onder te verdelen.
Slechts één van de primaire partities kan worden onderverdeeld.
fdisk wordt gestart door als root fdisk device te typen op de opdrachtprompt.
Het apparaat kan iets zijn als /dev/sda of /dev/sdb
De basiscommando's voor fdisk die je nodig hebt zijn:
p print the partition table
n create a new partition
d delete a partition
q quit without saving changes
w write the new partition table and exit
Wijzigingen die u in de partitietabel aanbrengt, worden pas van kracht als u het commando write (w) uitvoert.
Hier is een voorbeeld van een partitietabel:
Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 184 370912+ 83 Linux
/dev/sdb2 185 368 370944 83 Linux
/dev/sdb3 369 552 370944 83 Linux
/dev/sdb4 553 621 139104 82 Linux swap
Voorbeeld:
Start fdisk vanaf de shell prompt:
sudo su
fdisk /dev/sdb
Wat aangeeft dat u de tweede schijf op uw SATA controller gebruikt.
Command (m for help): p
Disk /dev/hdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes
That makes for 384Mb per partition.
Now You get to work.
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-621, default 1):<RETURN>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-621, default 621): +384M
Next, You set up the partition You want to use for swap:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (197-621, default 197):<RETURN>
Using default value 197
Last cylinder or +size or +sizeM or +sizeK (197-621, default 621): +128M
Nu ziet de partitietabel er als volgt uit:
Device Boot Start End Blocks Id System
/dev/sdb1 1 196 395104 83 Linux
/dev/sdb2 197 262 133056 83 Linux
Tenslotte, maak je de eerste partitie bootable:
Command (m for help): a
Partition number (1-4): 1
And You make the second partition of type swap:
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap)
Command (m for help): p
Het eindresultaat:
Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 196 395104+ 83 Linux
/dev/sdb2 197 262 133056 82 Linux swap
Tenslotte geeft U het schrijfcommando (w) om de tabel op de schijf te schrijven.
Het hulpprogramma mkfs wordt gebruikt om bestandssystemen (ext2, ext3, ext4, enz.) op uw Linux-systeem aan te maken.
U moet de apparaatnaam aan mkfs opgeven waarop het bestandssysteem moet worden aangemaakt.
Bekijk de beschikbare commando's voor bestandssysteembouwer
De bestandssysteem bouwers (mkfs* commando's) worden meestal gezocht in directories zoals /sbin/, /sbin/fs, /sbin/fs.d, /etc/fs en /etc.
Indien niet gevonden, wordt tenslotte gezocht in de directories die in de PATH variabele staan.
De volgende lijst toont de beschikbare mkfs* commando's in een systeem.
sudo su
cd /sbin
ls mkfs*
mkfs mkfs.bfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev
mkfs.minix mkfs.msdos mkfs.ntfs mkfs.vfat
Bouw een bestandssysteem op een specifiek apparaat
Om een bestandssysteem te bouwen met het mkfs commando, zijn de benodigde argumenten device-filename en filesystem-type zoals hieronder getoond.
Het volgende voorbeeld maakt ext4 bestandssysteem op /dev/sdb1 partitie.
sudo su
mkfs -t ext4 /dev/sdb1
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1120112 inodes, 4476416 blocks
223820 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
137 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
Merk op dat het standaard bestandssysteemtype voor het mkfs commando ext2 is.
Als je geen "-t" optie specificeert, zal het een ext2 bestandssysteem maken.
Je kan ook de methode gebruiken die we eerder besproken hebben om te bepalen of je ext2, ext3 of ext4 bestandssysteem hebt.
Een NTFS-schijf formatteren
Eerst moet je in staat zijn om NTFS bestandssystemen te maken, dus installeer ntfsprogs:
sudo su
apt-get install ntfs-3g
Ten tweede, blaas je de partitie weg en maak je hem opnieuw aan als NTFS.
sudo su
umount /dev/sdb1
fdisk /dev/sdb
Options to select:
‘d’ to delete the partition
‘n’ to create a new partition
‘p’ for primary
‘1’ for partition number
‘Enter’ for first cylinder (default 1)
‘Enter’ for last cylinder (default of max size)
‘t’ for type
‘L’ to list codes, and enter code for HPFS/NTFS. In my case, it’s ‘7’
‘w’ to write changes to disk, and exit
umount /dev/sdb1
In de laatste stap, ontkoppelt U de partitie, omdat Ubuntu hem weer automatisch voor U monteert.
Nu moet U het bestandssysteem aanmaken. Er zijn twee manieren om dit te doen: de ongeduldige manier (Quick Format), of de betere maar veel langere manier (Full Format).
Snel Formatteren
Dit wijst alleen de schijfruimte toe, maar maakt de schijf niet nul of controleert op slechte sectoren. Dit betekent dat het een paar seconden duurt.
sudo su
mkfs.ntfs -f /dev/sdb1
Volledig formatteren
Als je meer bezorgd bent over de integriteit van de gegevens en het niet erg vindt om te wachten, doe dan een volledig formaat.
Dit kan een paar uur duren om een grote schijf op nul te zetten!
sudo su
mkfs.ntfs /dev/sdb1