如何在Linux中创建新分区

时间:2020-03-21 11:44:02  来源:igfitidea点击:

在Linux中创建和删除新分区是一种非常正常的做法。
在本文中,我们将通过简单的步骤在linux中创建一个新分区,对其进行格式化并将其安装在所需的安装点。

本文还将引导我们完成以下步骤:删除所需的分区,查看硬盘驱动器的几何形状,显示现有的分区表,显示分区的UUID以及有关分区创建,查看,帮助,分区命令行帮助和故障排除的更多信息。

步骤1 :(设备识别)

首先使用fdisk命令检查分区表。

使用Fdisk命令,可以识别其设备是内部硬盘还是外部硬盘。

/dev/sd(a,b,c)-----------> SCSI

/dev/hd(a,b,c)-----------> IDE

#fdisk -l
[root@myvm1 ~]# fdisk -l
Disk /dev/sda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1           6       48163+  83  Linux
/dev/sda2               7         515     4088542+  83  Linux
/dev/sda3             516        3133    21029085   83  Linux
/dev/sda4            3134        3263     1044225    5  Extended
/dev/sda5            3134        3263     1044193+  82  Linux swap/Solaris

我们已经看到已经创建了高达/dev/sda5的分区。

所以现在我们必须创建一个新分区,该分区将从/dev/sda6开始

在进行分区之前,我们需要了解所使用的硬盘。
我们在这里使用一些命令来了解我们正在使用的硬盘的详细信息,并且我们将进行分区。

[root@satish ~]# lspci|grep -i ide
00:1f.1 IDE interface: Intel Corporation 82801G (ICH7 Family) IDE Controller (rev 01)
00:1f.2 IDE interface: Intel Corporation 82801GB/GR/GH (ICH7 Family) SATA IDE Controller (rev 01)

[root@satish ~]# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: ST3160215AS      Rev: 4.AA     ------>> My hard Disk
  Type:   Direct-Access                    ANSI SCSI revision: 05
Host: scsi4 Channel: 00 Id: 00 Lun: 00
  Vendor: Kingston Model: DataTraveler G2  Rev: 1.00    ---->> My usb Device
  Type:   Direct-Access                    ANSI SCSI revision: 02
Host: scsi8 Channel: 00 Id: 00 Lun: 00
  Vendor: HUAWEI   Model: Mass Storage     Rev: 2.31    ---->> My HUWAEI USb Modem
  Type:   CD-ROM                           ANSI SCSI revision: 02
Host: scsi9 Channel: 00 Id: 00 Lun: 00
  Vendor: HUAWEI   Model: MMC Storage      Rev: 2.31      
  Type:   Direct-Access                    ANSI SCSI revision: 02

步骤2:了解如何使用fdisk工具创建新的Partiton。

(分区设备)

我们进行分区,或者可以说磁盘被分区以根据需要创建一个单独的文件系统。

#fdisk/dev/sda
[root@myvm1 ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 3263.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help):

注意:这里我们使用sda是因为fdisk -l向我们展示了我们的硬盘是sda类型,而不是hda或者hdb。

:n这将创建一个新分区
:l这将创建一个逻辑分区
: 只需按Enter即可恢复默认cylinder 值。

:+ 2000M,这意味着我们要创建一个大小约为2gb的分区,即2000Mb。

:w写更改并保存并退出

第三步:为什么我们在Linux中使用partprobe?

如果我们不想重新引导Linux系统以更新在上面的分区表中所做的更改,只需使用partprobe命令。

#partprobe

步骤4:如何在Linux中建立档案系统?

Mkfs或者mke2fs命令用于在Linux中创建文件系统。

因此,请创建一个ext3文件系统。 #mke2fs -j /dev/sda6or#mkfs -t ext3 /dev/sda6or#mkfs.ext3 /dev/sda6这将格式化/dev/sda6分区并创建日记文件系统ext3,我们的Linux操作系统可以识别该文件系统。

第五步:挂载点

现在创建一个新目录,并在其上挂载/dev/sda6新创建的分区。

mountpoint是映射文件系统的目录。

#mkdir /new

#mount /dev/sda6 /new

现在,上面的命令将/dev/sda6挂载在/new目录中。

因此,现在我们在新目录中编写的任何内容都将保存在新创建的分区/dev/sda6中

步骤6:验证文件系统是否已挂载。

#df -h这将清楚地告诉我们/dev/sda6已挂载在/new目录中

步骤7:重新启动后使文件系统永久存在。

要使此更改在重新启动后存在或者使其永久存在,我们必须在/etc/fstab文件中进行输入。

#vim /etc/fstab
/dev/sda6     /new     ext3    defaults  0 0

保存文件并退出

我们已经创建了一个新分区/dev/sda6,现在它可以正常工作了。

如何删除该分区?

逐步删除分区的说明。

步骤1:
首先卸载分区并从上面的/etc/fstab中删除条目

#umount/dev/sda6

第2步:
然后使用fdisk命令删除分区

#fdisk /dev/sda
:d    here d is used to delete the partion
:6     it means delete the partion /dev/sda6
:w     save the abve changes and exit

第三步:无需重新启动即可更新更改。

#partprobe

步骤4:现在检查分区表是否已更新。

#fdisk -l

我们会发现/dev/sda6已被删除。

如何查看系统是否存在文件系统类型创建命令?

[root@localhost sbin]# cd /sbin/mk
mkbootdisk   mke2fs       mkfs.ext2    mkfs.vfat    mkzonedb     
mkdosfs      mkfs         mkfs.ext3    mkinitrd     
mkdumprd     mkfs.cramfs  mkfs.msdos   mkswap

如何查看硬盘的排列结构?

[root@localhost ~]# fdisk -v
fdisk (util-linux 2.13-pre7)

[root@localhost ~]# parted /dev/sda print
Model: ATA ST3160215AS (scsi)
Disk /dev/sda: 160GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  106MB   105MB   primary   ntfs         boot
 2      106MB   31.5GB  31.4GB  primary   ntfs              
 3      31.5GB  94.4GB  62.9GB  primary   ntfs              
 4      94.4GB  160GB   65.7GB  extended                    
 5      94.4GB  155GB   60.8GB  logical   ext3              
Information: Don't forget to update /etc/fstab, if necessary.

如何显示分区的UUID?

[root@localhost ~]# blkid /dev/sda5
/dev/sda5: LABEL="/1" UUID="b8b36258-6c3f-43d9-9c4b-063070945c5c" TYPE="ext3" SEC_TYPE="ext2"

如何查看现有文件系统的大小?

[root@localhost ~]# fdisk -s /dev/sda
156290904

如何在Linux中创建vfat文件系统?

首先使用fdisk -l命令查看现有的选配表。

[root@localhost ~]# fdisk -l
Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400    7  HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2              13        3825    30617600    7  HPFS/NTFS
/dev/sda3            3825       11474    61440000    7  HPFS/NTFS
/dev/sda4           11475       19457    64123447+   5  Extended
/dev/sda5           11475       18868    59392273+  83  Linux
You have new mail in /var/spool/mail/root

现在应用fdisk命令创建一个新分区。

[root@localhost ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 19457.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): n
First cylinder (18869-19457, default 18869):
Using default value 18869
Last cylinder or +size or +sizeM or +sizeK (18869-19457, default 19457): +100M
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 at the next reboot.
Syncing disks.

现在,使用partprobe命令来更新分区表,而无需重新引导系统。

[root@localhost ~]# partprobe

现在检查系统是否支持vfat文件系统?

[root@localhost ~]# mkfs
mkfs         mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.msdos   mkfs.vfat

现在,使用mkfs.vfat命令创建vfat分区。

使用mkfs.vfat命令格式化/dev/sda6.

[root@localhost ~]# mkfs.vfat /dev/sda6
mkfs.vfat 2.11 (12 Mar 2005)

Create a mount point for newly created partition for example:for /dev/sda6 partition.[root@localhost ~]# mkdir /newoneNow mount the newly created partition using mount command.[root@localhost ~]# mount /dev/sda6 /newone/List or check whether partion got mounted or not by using df -h command.

[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5              55G   19G   34G  36% /
tmpfs                 502M     0  502M   0% /dev/shm
/dev/sda6             102M     0  102M   0% /newone

检查分区是否已创建?

[root@localhost ~]# parted /dev/sda print
Model: ATA ST3160215AS (scsi)
Disk /dev/sda: 160GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  106MB   105MB   primary   ntfs         boot
 2      106MB   31.5GB  31.4GB  primary   ntfs              
 3      31.5GB  94.4GB  62.9GB  primary   ntfs              
 4      94.4GB  160GB   65.7GB  extended                    
 5      94.4GB  155GB   60.8GB  logical   ext3              
 6      155GB   155GB   107MB   logical   fat16             
Information: Don't forget to update /etc/fstab, if necessary.

[root@localhost ~]# file -sL /dev/sda6
/dev/sda6: x86 boot sector, mkdosfs boot message display, code offset 0x3c, OEM-ID " mkdosfs", sectors/cluster 4, root entries 512, Media descriptor 0xf8, sectors/FAT 204, heads 255, sectors 208782 (volumes > 32 MB) , serial number 0x517ba11a, label: "           ", FAT (16 bit)

如何检查创建的分区的文件系统类型?

root@localhost ~]# fsck -N /dev/sda6
fsck 1.39 (29-May-2006)
[/sbin/fsck.vfat (1) -- /dev/sda6] fsck.vfat /dev/sda6

使用fsck命令检查新创建的文件系统上是否存在任何错误。

[root@localhost ~]# fsck /dev/sda6
fsck 1.39 (29-May-2006)
dosfsck 2.11, 12 Mar 2005, FAT32, LFN
/dev/sda6: 0 files, 0/52085 clusters

如果在文件系统上发现错误,则fcsk将运行。

如何创建ext2文件系统。

[root@satish ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 4341414.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): n
First cylinder (4096001-4341414, default 4096001):
Using default value 4096001
Last cylinder or +size or +sizeM or +sizeK (4096001-4341414, default 4341414): +1G
Command (m for help): p
Disk /dev/sda: 160.0 GB, 160041885696 bytes
18 heads, 4 sectors/track, 4341414 cylinders
Units = cylinders of 72 * 512 = 36864 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              29      995584    35840000    7  HPFS/NTFS
/dev/sda2   *      995585     3299584    82944000    7  HPFS/NTFS
/dev/sda3         3299585     3697806    14335992   83  Linux
/dev/sda4         3896918     4341414    16001892    5  Extended
/dev/sda5         3896918     4096000     7166986   83  Linux
/dev/sda6         4096001     4123128      976606   83  Linux
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 at the next reboot.
Syncing disks.
[root@satish ~]# partprobe
[root@satish ~]# mkfs.ext2 /dev/sda6

现在挂载此ext2文件系统。

首先创建一个目录,然后挂载它。

[root@satish ~]# mkdir /shivangi
[root@satish ~]# mount /dev/sda6 /shivangi/

现在检查已挂载的文件系统。

[root@satish ~]# df -Th /dev/sda6
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda6     ext2    939M  1.2M  890M   1% /shivangi

我们可以在上面的输出中看到/dev/sda6的文件系统类型是ext2,并且已安装在/shivangi上。

现在,将此ext2文件系统转换为ext3文件系统。

在此处升级文件系统/dev/sda6.

  • 首先卸载已挂载的文件系统。
  • 然后转换为is或者使用tune2fs命令对其进行升级。
  • 然后将其安装回去。
  • 然后检查文件系统类型uding df命令。
[root@satish ~]# umount /dev/sda6
[root@satish ~]# tune2fs -j /dev/sda6
tune2fs 1.39 (29-May-2006)
Creating journal inode: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@satish ~]# mount /dev/sda6 /shivangi/
[root@satish ~]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda3     ext3     14G  6.3G  6.4G  50% /
/dev/sda5     ext3    6.7G  2.8G  3.6G  44% /var
tmpfs        tmpfs   1010M     0 1010M   0% /dev/shm
/dev/sda6     ext3    939M   18M  874M   2% /shivangi

在此处检查新创建的分区/dev/sda6上的坏块。

[root@satish ~]# mke2fs -c /dev/sda6

在已安装的设备上强制创建文件系统。

[root@satish ~]# mke2fs -F /dev/sda6