linux 挂载硬盘,创建新分区,删除分区

来源:互联网 发布:toefl培训知乎 编辑:程序博客网 时间:2024/05/16 02:09

**

1. 主分区,扩展分区,逻辑分区的联系和区别

**

一个硬盘可以有1到3个主分区和1个扩展分区,也可以只有主分区而没有扩展分区,但主分区必须至少有1个,扩展分区则最多只有1个,且主分区+扩展分区总共不能超过4个。逻辑分区可以有若干个

**

2. 几个比较重要的命令

**

fdisk           磁盘分区相关操作df              系统分区挂载信息mount           挂载分区umount          卸载分区mkfs.ext4       格式化分区

**

3. 查看当前磁盘信息

**

*执行 fdisk -l
可以看到系统有sda, sdb 两块硬盘,其中 sdb 是我刚刚加的5G硬盘, 可以看到里面是没有任何分区的

[root@freeman ~]# fdisk -lDisk /dev/sdb: 5368 MB, 5368709120 bytes255 heads, 63 sectors/track, 652 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xcca98924   Device Boot      Start         End      Blocks   Id  SystemDisk /dev/sda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000dd21a   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          64      512000   83  Linux/dev/sda2              64        2611    20458496   8e  Linux LVM

*执行 df -lh
查看当前磁盘信息
sda2 挂载在根目录
sda1 挂载在/boot目录

[root@freeman ~]# df -lhFilesystem                      Size  Used  Avail  Use% Mounted on/dev/mapper/vg_freeman-lv_root  18G    12G  4.9G   71%  /tmpfs                           242M     0  242M   0%   /dev/shm/dev/sda1                       477M   29M  424M   7%   /boot

**

4. 创建分区

**

[root@freeman ~]# fdisk /dev/sdbCommand (m for help): n     <- 新建分区Command action              <- 选择要创建的分区类型   e   extended <- 扩展分区   p   primary partition (1-4) <- 主分区p <- 输入建立主分区Partition number (1-4):1    <- 分区编号First cylinder (1-652, default 1):  <- 柱面起始值,直接回车默认Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):     +1G  <-分区大小Command (m for help): w     <- 保存分区表, 完毕会退出fdisk命令The partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks....

*重复同样的方法再建立
——2G大小的主分区sdb2
——500M大小的扩展分区大小sdb3
——100M大小的逻辑分区sdb5, sdb6

[root@freeman ~]# fdisk /dev/sdbCommand (m for help): p     <- 打印当前磁盘的分区信息   Device Boot      Start         End      Blocks   Id  System/dev/sdb1               1         132     1060258+  83  Linux/dev/sdb2             133         394     2104515   83  Linux/dev/sdb3             395         459      522112+   5  Extended/dev/sdb5             395         408      112423+  83  Linux/dev/sdb6             409         422      112423+  83  Linux

我已经将sdb已经做了5个分区(实际上4个有效,因为sdb3是扩展分区,sdb5 是第一个逻辑分区所以起始柱面和sdb3一样从395开始)

建立好分区后,先不要急着去挂载,否则提示必须知道文件系统类型 ,需要先格式化分区

[root@freeman /]# mount /dev/sdb2 /my_mount2mount: you must specify the filesystem type

*格式化分区
这里我使用ext4 filesystem type, 有关文件系统类型自己google

[root@freeman /]# mkfs.ext4 /dev/sdb1[root@freeman /]# mkfs.ext4 /dev/sdb2[root@freeman /]# mkfs.ext4 /dev/sdb5[root@freeman /]# mkfs.ext4 /dev/sdb6

如果出现下面提示,说明分区信息没有生效,重启系统然后再格式化

[root@freeman /]# mkfs.ext4 /dev/sdb2mke2fs 1.41.12 (17-May-2010)无法对 /dev/sdb2 进行 stat 调用 --- 没有那个文件或目录The device apparently does not exist; did you specify it correctly?

**

5. 挂载分区

**

新建4个文件夹用来挂载分区, sdb3是扩展分区不能用来挂载,他的逻辑分区sdb5和sdb6是可以挂载的

[root@freeman /]# mount /dev/sdb1 /my_mount1[root@freeman /]# mount /dev/sdb2 /my_mount2[root@freeman /]# mount /dev/sdb5 /my_mount5[root@freeman /]# mount /dev/sdb6 /my_mount6

*执行df
看到新建的4个分区都挂载好了

[root@freeman /]# df -lhFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/vg_freeman-lv_root                       18G   12G  4.9G  71% /tmpfs                 242M     0  242M   0% /dev/shm/dev/sda1             477M   29M  424M   7% /boot/dev/sdb1             988M  1.3M  935M   1% /my_mount1/dev/sdb2             2.0G  3.1M  1.9G   1% /my_mount2/dev/sdb5             103M  1.6M   96M   2% /my_mount5/dev/sdb6             103M  1.6M   96M   2% /my_mount6

*自动挂载
打开/etc/fstab在最后添加挂载配置fstab里面的参数释义请自行google

[root@freeman /]# vim /etc/fstab/dev/sdb1               /my_mount1              ext4    defaults        0 2/dev/sdb2               /my_mount2              ext4    defaults        0 2/dev/sdb5               /my_mount5              ext4    defaults        0 2/dev/sdb6               /my_mount6              ext4    defaults        0 2

*添加权限
如果是挂载空间要给普通用户使用,给相应的目录添加访问权限

[root@freeman /]# chmod 777 /my_mount1

**

6. 卸载分区

**

很简单,使用umount命令就可以了

[root@freeman /]# umount /dev/sdb1

**

7. 删除分区

**
*可以先umount 卸载分区,以免出现不必要的问题

[root@freeman ~]# fdisk /dev/sdb   <-进入sdb硬盘WARNING: DOS-compatible mode is deprecated. It's strongly recommended to         switch off the mode (command 'c') and change display units to         sectors (command 'u').Command (m for help): p             <- 输入p打印当前硬盘的分区信息Disk /dev/sdb: 5368 MB, 5368709120 bytes255 heads, 63 sectors/track, 652 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xcca98924   Device Boot      Start         End      Blocks   Id  System/dev/sdb1               1         523     4200966   83  Linux/dev/sdb2             524         537      112455   83  Linux/dev/sdb3             538         563      208845    5  Extended/dev/sdb5             538         563      208813+  83  Linux/dev/sdb6             409         422      112423+  83  LinuxCommand (m for help): d     <- 输入d删除分区Partition number (1-5):     <- 输入要删除的分区编号...Command (m for help): w     <- 分区删除完毕,输入w,写入磁盘分区表The partition table has been altered!... The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)

*重复上面操作可以删除任意分区
使用partprobe或者kpartx使分区表立刻生效, 如果不行的话就重启一下,如果要删除一个磁盘所有分区,更简单的做法是格式化磁盘

[root@freeman /]# mkfs.ext4 /dev/sdbmke2fs 1.41.12 (17-May-2010)/dev/sdb is entire device, not just one partition!无论如何也要继续? (y,n) y

**

8. 修改分区系统类型id,例如 Linux LVM

**

下面将把sdb2分区系统类型id修改为 Linux LVM

[root@freeman /]# fdisk /dev/sdbCommand (m for help): t         <- 输入tPartition number (1-6): 2       <- 2号分区Hex code (type L to list codes): 8e     <- 8e就是 LVM类型Changed system type of partition 2 to 8e (Linux LVM)Command (m for help): pDisk /dev/sdb: 5368 MB, 5368709120 bytes255 heads, 63 sectors/track, 652 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xcca98924   Device Boot      Start         End      Blocks   Id  System/dev/sdb1               1         132     1060258+  83  Linux/dev/sdb2             133         394     2104515   8e  Linux LVM/dev/sdb3             395         459      522112+   5  Extended/dev/sdb5             395         408      112423+  83  Linux/dev/sdb6             409         422      112423+  83  Linux
0 0
原创粉丝点击