Getting started with Linux disk management

Source: Internet
Author: User
Tags hex code

Getting started with Linux disk management

Let's look at how disks are partitioned in Linux.
Disk Partition:
Fdisk: a common tool for operating disk partition tables in Linux:
Common Format: fdisk-l lists the disk devices used

[Root @ linux ~] # Fdisk-l

Disk/dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1*512 = 512 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes
Disk label type: dos
Disk identifier: 0x00029f42

Device Boot Start End Blocks Id System
/Dev/sda1*2048 1026047 512000 83 Linux
/Dev/sda2 1026048 84912127 41943040 83 Linux
/Dev/sda3 84912128 126855167 20971520 83 Linux
/Dev/sda4 126855168 419430399 146287616 5 Extended
/Dev/sda5 126859264 131055615 2098176 82 Linux swap/Solaris

Next we will explain how to manage Linux Disks Based on the experiment.
0. Create a primary partition (MBR ):
A) Fdisk/dev/sda enters the disk partition Environment
[Root @ bogon ~] # Fdisk/dev/sda

WARNING: DOS-compatible mode is deprecated. It's stronugly recommended
Switch off the mode (command 'C') and change display units
Sectors (command 'U ').

Command (m for help ):

Of course, when executing this command, you can use m to list the Command Options:
Command (m for help): m
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)

Command (m for help ):

B) n Add a new disk
Command (m for help): n
Command action
E extended
P primary partition (1-4)

E: extended partition, p: Primary partition (1-4)
[Extension] after that, the number of primary partitions is limited by the partition table. 64 partition tables can be divided into up to four primary partitions, or one extended partition or other three primary partitions, A common window is a primary partition and an extended partition (understand)
Command action
E extended
P primary partition (1-4)
P
Partition number (1-4): 3
First cylinder (7859-15665, default 7859 ):

C) Press enter to select the default start sector.
123 Partition number (1-4): 3
First cylinder (7859-15665, default 7859 ):
Using default value 7859

Here we press enter to indicate that we use the starting cylinder (in different system versions, some are based on the cylinder and some are slice, but there is no problem)

D) The input format is "+ [M, G]" disk space size.
1 Last cylinder, + cylinders or + size {K, M, G} (7859-15665, default 15665): + 10G

You can use the unit in the brackets, or you can use the cylindrical representation, but there is a plus sign in front of it.

E) w save and exit
Command (m for help): w
The partition table has been altered!

Calling ioctl () to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used
The next reboot or after you run partprobe (8) or kpartx (8)
Syncing disks.

All these operations are actually completed in the memory. Only w is used to save them. Let's verify it.

F) ls/dev/sda * or cat/proc/partitions to check whether a new partition exists.
[Root @ bogon ~] # Ls/dev/sda *
/Dev/sda/dev/sda1/dev/sda2
[Root @ bogon ~] # Cat/proc/partitions
Major minor # blocks name

8, 0, 125829120, sda
8 1 204800 sda1
8 2 62914560 sda2
253 0 20971520 dm-0
253 1 2097152 dm-1
253 2 10485760 dm-2
253 3 20971520 dm-3
[Root @ bogon ~] #

Strange. What's the problem? Didn't it succeed? No, this is because it is not synchronized to the kernel.

G) execute parted-a/dev/sda once or multiple times until the newly created partition is displayed.
[Root @ bogon ~] # Partx-a/dev/sda
BLKPG: Device or resource busy
Error adding partition 1
BLKPG: Device or resource busy
Error adding partition 2
[Root @ bogon ~] # Partx-a/dev/sda
BLKPG: Device or resource busy
Error adding partition 1
BLKPG: Device or resource busy
Error adding partition 2
BLKPG: Device or resource busy
Error adding partition 3
[Root @ bogon ~] # Fdisk-l

Disk/dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065*512 = 8225280 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes
Disk identifier: 0x0004a6ba

Device Boot Start End Blocks Id System
/Dev/sda1 × 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/Dev/sda2 26 7859 62914560 8e Linux LVM
/Dev/sda3 7859 9164 10489446 83 Linux

Partx-a actually means to notify (tell) the kernel that the disk information has changed. You need to update it once, but it is not displayed once. We can execute it to check the result, if it is 10 Gb, the primary partition is successfully created. Therefore, pay special attention to the two commands partx and fdisk.
We have created the primary partition, but how can we create a swap partition? Let's take a look.
As you know, swap partitions are memory space, which is different before and after the space. Let's take a look at the memory space.

[Root @ bogon ~] # Free-m
Total used free shared buffers cached
Mem: 845 157 687 0 12 55
-/+ Buffers/cache: 90 754
Swap: 2047 0 2047

Create a swap Partition

[Root @ bogon ~] # Mkswap/dev/sda4
Mkswap:/dev/sda4: warning: don't erase bootbits sectors
(Dos partition table detected). Use-f to force.
Setting up swapspace version 1, size = 31463296 KiB
No label, UUID = c0c990f9-9d68-41d6-8e75-1eb41b08c4fc
[Root @ bogon ~] # Mkswap-f/dev/sda4
Setting up swapspace version 1, size = 31463296 KiB
No label, UUID = 5017d30a-1d96-4e47-b4ba-274d132afeb9
[Root @ bogon ~] # Free-m
Total used free shared buffers cached
Mem: 845 157 687 0 12 55
-/+ Buffers/cache: 90 754
Swap: 2047 0 2047
[Root @ bogon ~] # Swapon/dev/sda4
[Root @ bogon ~] # Free-m
Total used free shared buffers cached
Mem: 845 180 664 0 12 55
-/+ Buffers/cache: 112 732
Swap: 32773 0 32773
[Root @ bogon ~] #

Of course, we can also change a created partition to a swap partition.
A) Fdisk/dev/sda enters the disk partition Environment
B) n Add a new disk
C) Press enter to select the default start sector.
D) The input format is "+ [MG]" disk space size.
E) l display the System ID of the adjusted partition, swap (82)
F) t adjust the partition
G) enter the adjusted Partition Number.
H) enter the ID of the partition type to be adjusted (82)
I) p view partition adjustment results
J) w save
K) Use kpartx-af/dev/sda to repeat or repeat once or twice, or manually adjust it. Use kpartx-a/dev/sda #, # The adjusted Partition Number, manually add a partition
L) fdisk-l/dev/sda to check whether the display is successful
The following shows the source code: the steps are clear.
[Root @ bogon ~] # Fdisk/dev/sda

WARNING: DOS-compatible mode is deprecated. It's stronugly recommended
Switch off the mode (command 'C') and change display units
Sectors (command 'U ').

Command (m for help): n
First cylinder (9165-13081, default 9165 ):
Using default value 9165
Last cylinder, + cylinders or + size {K, M, G} (9165-13081, default 13081): + 100 M

Command (m for help): l

0 Empty 24 nec dos 81 Minix/old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap/So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32 M 41 PPC PReP Boot 85 Linux extended c7 syr.pdf
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M/CTOS /.
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
A OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
B W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
C W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
E W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
F W95 Ext 'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC B
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 gnu hurd or Sys AB Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS/HFS + fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap/Solaris)

Command (m for help): p

Disk/dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065*512 = 8225280 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes
Disk identifier: 0x0004a6ba

Device Boot Start End Blocks Id System
/Dev/sda1 × 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/Dev/sda2 26 7859 62914560 8e Linux LVM
/Dev/sda3 7859 9164 10489446 83 Linux
/Dev/sda4 9165 13081 31463302 + 5 Extended
/Dev/sda5 9165 9178 112423 + 82 Linux swap/Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl () to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used
The next reboot or after you run partprobe (8) or kpartx (8)
Syncing disks.
[Root @ bogon ~] # Kpartx-a/dev/sda
Device-mapper: reload ioctl on sda1 failed: Invalid argument
Create/reload failed on sda1
Device-mapper: reload ioctl on sda2 failed: Invalid argument
Create/reload failed on sda2
Device-mapper: reload ioctl on sda3 failed: Invalid argument
Create/reload failed on sda3
Device-mapper: reload ioctl on sda4 failed: Invalid argument
Create/reload failed on sda4
Device-mapper: reload ioctl on sda5 failed: Invalid argument
Create/reload failed on sda5

2. Create a File System
The mkfs formatting tool is used here. It can format partitions as mkfs. cramfs mkfs. ext2 mkfs. ext3 mkfs. ext4 mkfs. ext4dev mkfs. msdos mkfs. vfat file systems such:
[Root @ bogon ~] # Mkfs. ext4/dev/sda3
Mke2fs 1.41.12 (17-May-2010)
Filesystem label =
OS type: Linux
Block size = 4096 (log = 2)
Fragment size = 4096 (log = 2)
Stride = 0 blocks, Stripe width = 0 blocks
655776 inodes, 2622361 blocks
131118 blocks (5.00%) reserved for the super user
First data block = 0
Maximum filesystem blocks = 2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768,983 04, 163840,229 376, 294912,819 200, 884736,160 5632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs-c or-I to override.

A) mkfs. ext2/dev/sda # format sda # partition to become the file system type of ext2 (damage existing file system types)
[Root @ bogon ~] # Mkfs. ext2/dev/sda3
Mke2fs 1.41.12 (17-May-2010)
Filesystem label =
OS type: Linux
Block size = 4096 (log = 2)
Fragment size = 4096 (log = 2)
Stride = 0 blocks, Stripe width = 0 blocks
655776 inodes, 2622361 blocks
131118 blocks (5.00%) reserved for the super user
First data block = 0
Maximum filesystem blocks = 2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768,983 04, 163840,229 376, 294912,819 200, 884736,160 5632

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs-c or-I to override.

B) blkid/dev/sda # Check whether the partition is formatted as the target file system type
[Root @ bogon ~] # Blkid/dev/sda3
/Dev/sda3: UUID = "228c316c-b9f8-4339-90eb-b072c60b3433" TYPE = "ext2"

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.