Linux系统分区管理

管理分区

 

 

查看系统中的分区4种方式

[root@localhost ~]# ls /dev/sd* [来自内存]

[root@localhost ~]# cat /proc/partitions

[root@localhost ~]# lsblk

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5

 

[root@localhost ~]# fdisk -l 看磁盘中的分区表

磁盘中的分区表可能和内存中的分区表是不同步的

 

parted命令

  1. parted的操作都是实时生效的,小心使用
  2. 用法:parted [选项]… [设备 [命令 [参数]…]…]
  3. parted /dev/sdb  mklabel gpt|msdos
  4. parted /dev/sdb  print
  5. parted /dev/sdb  mkpart primary 1 200 (默认M)
  6. parted /dev/sdb  rm 1
  7. parted -l
  • mklabel gpt|msdos 用于指定创建的是GPT分区还是MBR分区

 

  • 创建分区

[root@localhost app]# parted /dev/sdb mkpart primary  1 1000

Information: You may need to update /etc/fstab.

 

[root@localhost app]# lsblk

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda      8:0    0  200G  0 disk

├─sda1   8:1    0    1G  0 part /boot

├─sda2   8:2    0   50G  0 part /

├─sda3   8:3    0   20G  0 part /app

├─sda4   8:4    0    1K  0 part

└─sda5   8:5    0    2G  0 part [SWAP]

sdb      8:16   0   20G  0 disk

└─sdb1   8:17   0  953M  0 part

GPT分区和MBR分区是互不兼容的,也不存在转换的问题,一旦设置了GPT分区或者MBR分区,要想装换只能重新分区

 

 

分区工具fdisk和gdisk

 

  1. gdisk /dev/sdb 类fdisk 的GPT分区工具
  2. fdisk -l [-u] [device…] 查看分区
  3. fdisk /dev/sdb  管理分区
  4. 子命令:
  5. p 分区列表
  6. t 更改分区类型
  7. n 创建新分区
  8. d 删除分区
  9. v 校验分区
  10. u 转换单位
  11. w 保存并退出
  12. q 不保存并退出

 

 

[root@localhost app]# gdisk /dev/sdb

GPT fdisk (gdisk) version 0.8.6

 

Partition table scan:

MBR: protective

BSD: not present

APM: not present

GPT: present

 

Found valid GPT with protective MBR; using GPT.

#gdisk的帮助信息

Command (? for help): ?

b back up GPT data to a file        备份GPT分区

c change a partition’s name       修改分区名

d delete a partition       删除分区

i show detailed information on a partition

l list known partition types

n add a new partition        添加一个新分区

o create a new empty GUID partition table (GPT)        创建一个GUID分区表

p print the partition table        打印分区表

q quit without saving changes不保存退出

r recovery and transformation options (experts only)       恢复修改的选项

s sort partitions       分区排序

t change a partition’s type code       修改分区类型代码

v verify disk        验证磁盘

w write table to disk and exit写入磁盘并且退出

x extra functionality (experts only)        扩展选项

? print this menu        打印出菜单

 

Command (? for help):

 

#把分区表转换为MBR类型

[root@localhost ~]# parted /dev/sdb mklabel msdos

Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do

you want to continue?

Yes/No? yes

Information: You may need to update /etc/fstab.

 

[root@localhost ~]#

 

fdisk分区工具的使用

[root@localhost ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

 

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

 

 

Command (m for help): m

Command action

a   toggle a bootable flag

b   edit bsd disklabel

c   toggle the dos compatibility flag 不以dos兼容的模式

d   delete a partition

g   create a new empty GPT partition table

G   create an IRIX (SGI) partition table

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 以扇区为单位进行显示(单位为K)

v   verify the partition table

w   write table to disk and exit

x   extra functionality (experts only)

 

Command (m for help):

 

在CentOS6中:

[root@localhost ~]# fdisk /dev/sda

 

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’).基于dos兼容模式是弃用。这是强烈推荐关掉模式(命令“c”)和改变显示单位部门(命令“u”)。也就是用传统的柱面的方式来划分去分区,并且要求以整柱面位单位进行划分,

 

 

Command (m for help): p

 

Disk /dev/sda: 214.7 GB, 214748364800 bytes

255 heads, 63 sectors/track, 26108 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: 0x0008cdd6

 

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1         131     1048576   83  Linux

Partition 1 does not end on cylinder boundary. 默认以柱面为单位进行划分,不在整柱面上会提示

/dev/sda2             131        6658    52428800   83  Linux

/dev/sda3            6658        9269    20971520   83  Linux

/dev/sda4            9269       26109   135265280    5  Extended

/dev/sda5            9269        9530     2097152   82  Linux swap / Solaris

 

Command (m for help):

CentOS6默认以dos模式柱面为单位

CentOS7默认就是以非dos模式以扇区为单位,默认相当于已经加了c u 了,但也是可以改为以柱面为单位进行显示,例如打开的时候以柱面为单位进行显示

[root@localhost ~]# fdisk -h

Usage:

fdisk [options] <disk>    change partition table

fdisk [options] -l <disk> list partition table(s)

fdisk -s <partition>      give partition size(s) in blocks

 

Options:

-b <size>             sector size (512, 1024, 2048 or 4096)

-c[=<mode>]           compatible mode: ‘dos’ or ‘nondos’ (default)

-h                    print this help text

-u[=<unit>]           display units: ‘cylinders’ or ‘sectors’ (default)

-v                    print program version

-C <number>           specify the number of cylinders

-H <number>           specify the number of heads

-S <number>           specify the number of sectors per track

 

[root@localhost ~]# fdisk -u=cylinders /dev/sda

 

WARNING: cylinders as display units are deprecated. Use command ‘u’ to

change units to sectors.

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1         131     1048576   83  Linux

/dev/sda5            9269        9530     2097152   82  Linux swap / Solaris

 

注意:注意不要在一个分区中的夹缝中分区,避免分出来的空间过小,或者分区的时候空间过大进行报错提示

Device Boot      Start         End      Blocks   Id  System

/dev/sdc1            2048        8192        3072+  83  Linux

/dev/sdc2           10240    62914559    31452160   83  Linux

Last sector, +sectors or +size{K,M,G} (8193-10239, default 10239): +10G

Value out of range.

分区不能调大调小,只能重头开始分区。例如删除分区6,分区7会变成分区6。

 

分区的标识、设备名是不稳定的,带来的问题就是将来在磁盘上进行分区配置文件的修改,写/sda5 sda6这样的名字,万一删掉了分区,设备名就变了,数据就找不到了配置文件就会出问题,不推荐写设备名称,分区完成后会生成UUID号,是唯一且不变的,建议写UUID号

 

在6中创建分区表同步失败

Command (m for help): n

First sector (148899903-419430399, default 148899903):

Using default value 148899903

Last sector, +sectors or +size{K,M,G} (148899903-148903935, default 148903935):

Using default value 148903935

 

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 or after you run partprobe(8) or kpartx(8)

Syncing disks.

[root@localhost ~]#

 

需要手动同步分区表

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/89200

(0)
无言胜千言无言胜千言
上一篇 2017-12-02 19:49
下一篇 2017-12-02 20:23

相关推荐

  • Linux基础

    计算机组成和功能 按照Von Neumann的理解 [1],计算机分为以下几部分:     输入 (Input Device, 用于提供给计算机待处理的数据)         e.g. 键盘,…    &nbs…

    Linux干货 2016-09-27
  • 进程和计划任务

    1、进程概念 内核的功用:进程管理、文件系统、网络功能、内存管理、驱动程序、安全功能等; 进程是运行中的程序的一个副本,是被载入内存的一个指令集合; 进程ID(PID )号码被用来标记各个进程; UID、GID 和SELinux 语境决定对文件系统的存取和访问权限,这些权限通常从执行进程的用户来继承并且存在生命周期; task struct:Linux内核存…

    Linux干货 2016-09-13
  • N26-第九周作业-邢岩

    马哥门徒-N26-邢岩 相信自己、勤奋努力、不断总结!shell脚本练习需要重复、重复、再重复!那么,我就继续来写、写、写。 1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #******************************…

    Linux干货 2017-04-03
  • 正则表达式

    grep:Global search REgular expression and Print out the line         文本搜索工具,根据用户指定的”模式“对目标文本逐行进行匹配检查;打印匹配到的行        模式:由正则表达式字符及文本字符所编写的过滤条件…

    Linux干货 2016-08-08
  • inotify介绍及rsync + inotify 实时同步备份

    1、前言 rsync (remote sync)是一款非常好的数据同步工具,能够通过对比同步双方的数据变动,实现增量同步,还可以通过LAN/WAN实现远程多台主机间文件的同步,还能结合crond任务计划来执行自动备份,又可以结合ssh实现远程数据备份的安全,种种特性使他看起来相当优秀。但如果需备份数据十分庞大时,它的不足之处就显现出来了,比如每次执…

    Linux干货 2016-06-15
  • Linux操作系统发展史

    内容摘要:Linux操作系统的诞生,Linux 的发展,Linux的特性决定其迅猛发展,Linux的应用领域与发展前景 关键词:Linux的发展  前景   1.  Linux操作系统的诞生 在1981年到1991年十年间,微软公司的MS-DOS系统一直主宰操作系统的市场,其价格十分昂贵,另一个操作系统UNIX的经销商为了高利润…

    Linux干货 2016-10-14