非交互式添加分区

非交互式添加分区

方法一

添加/deb/sdb 下的分区,其实位置为11000M,第二个分区位置为10013000M,位置千万不能指定错误

parted  /dev/sdb  mkpart  primary  1  1000M
parted  /dev/sdb  mkpart primary  1001     3000M

方法二

1)将你要在parted命令行输入的命令实现写入一个文本文件,比如叫做part.txt

2)然后part.txt的内容类似于这样

      [root@local ~]# cat part.txt
         mkpart   
         part4       
         ext4         
         3073        
         4096        
         q               

3)然后用类似如下命令实现自动分区:

         parted  /dev/sdb < part.txt

首先来查看/dev/sdb现有分区情况

[root@local ~]# fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: gpt
#         Start          End    Size  Type            Name
 1         2048      2000895    976M  Microsoft basic part1
 2      2001953      4000000  975.6M  Microsoft basic part2

接下来运行命令:parted  /dev/sdb < part.txt

[root@local ~]# parted  /dev/sdb < part.txt
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart                                                          
Partition name?  []? part4                                                
File system type?  [ext2]? ext4                                          
Start? 3073                                                              
End? 4096                                                                
(parted) q                                                               
Information: You may need to update /etc/fstab.

再来查看分区情况

[root@local ~]# fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: gpt
#         Start          End    Size  Type            Name
 1         2048      2000895    976M  Microsoft basic part1
 2      2001953      4000000  975.6M  Microsoft basic part2
 3      6002688      7999487    975M  Microsoft basic part4

方法三

类似方法二,不过使用gdisk命令

1)写一个文本文件gdisk.txt

2)文本内容如下

[root@local ~]# cat gdisk.txt
n
     #空行
     #空行
+1G
     #空行
w
y

3)然后用类似如下命令实现自动分区:

         gdisk  /dev/sdb < gdisk.txt

首先来查看/dev/sdc现有分区情况

[root@local ~]# gdisk -l /dev/sdc
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.
Disk /dev/sdc: 10485760 sectors, 5.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F227EC43-CB17-4248-9B1A-13A35CEF8E92
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 10485726
Partitions will be aligned on 2048-sector boundaries
Total free space is 6291389 sectors (3.0 GiB)
 
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  Linux filesystem

下来运行命令:gdisk  /dev/sdb < gdisk.txt

[root@local ~]# gdisk  /dev/sdb < gdisk.txt
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.
 Command (? for help): Partition number (4-128, default 4): First sector (34-10485726, default = 7999488) or {+-}size{KMGTP}: Last sector (7999488-10485726, default = 10485726) or {+-}size{KMGTP}: Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem' 
Command (? for help):
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!! 
Do you want to proceed? (Y/N): OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
[root@local ~]#

再来查看分区情况

[root@local ~]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.6
[……]
Total free space is 2394845 sectors (1.1 GiB)
 
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2000895   976.0 MiB   0700  part1
   2         2001953         4000000   975.6 MiB   0700  part2
   3         6002688         7999487   975.0 MiB   0700  part4
   4         7999488        10096639   1024.0 MiB  8300  Linux filesystem
[root@local ~]#

可以看到已经添加成功

 

fdisk也可以通过这种方法实现非交互是分区

原创文章,作者:linux is not unix,如若转载,请注明出处:http://www.178linux.com/74074

(2)
linux is not unixlinux is not unix
上一篇 2017-04-24 19:11
下一篇 2017-04-24 19:13

相关推荐

  • Ansible应用介绍

    Ansible 简介 ansible是个什么东西呢?官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具。这个工具的目标有这么几项:自动化部署APP;自动化管理配置项;自动化的持续交互;自动化的(AWS)云服务管理。所有的这几个目标从本质上来说都是在一个台或者几台服务器上,执行一系列的命令而已。通俗的说…

    Linux干货 2015-07-03
  • 第六周作业

    一、Vim编辑器练习: 用Vim编辑器做下面的练习,我的Linux系统(CentOS 6.9)安装了精简版系统,没有Vim编辑器。 a、设置Yum源为网易开源镜像站。 b、选择对应“镜像名”,到对应的“CentOS使用说明”里面查看内容 c、看着CentOS说明操作就可以了 d、使用说明 首先备份/etc/yum.repos.d/CentOS-Base.re…

    Linux干货 2017-08-22
  • N25-第七周作业

    1、创建一个10G分区,并格式为ext4文件系统;    (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;     ~]# fdisk /dev/sda  (n, +10G 创建10G分区,w保存退出)   &nbsp…

    Linux干货 2017-01-22
  • sed 高级编辑命令、模式空间、保持空间、vi 可视化模式、vimrc、地址定界

    sed 高级编辑命令、模式空间、保持空间、vi 可视化模式、vimrc、地址定界 sed(stream deitor):行编辑器 【概述】sed是一种流式编辑器,它一次处理一行内容。处理时,把当前处理的行的所有内容存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,逐行处理,不修改文件。sed主要…

    Linux干货 2016-08-11
  • 计算机的组成

        计算机组成(computer composition)指的是系统结构的逻辑实现,包括机器机内的数据流和控制流的组成及逻辑设计等。 计算机组成的任务是在指令集系统结构确定分配给硬件系统的功能和概念结构之后,研究各组成部分的内部构造和相互联系,以实现机器指令集的各种功能和特性。这种联系包括各功能部件的内部和相   &nbsp…

    2017-03-26
  • 图解LVM(逻辑卷管理)

    图解LVM 1 什么是LVM?为什么要使用LVM? lvm(Logical Volume Manager)逻辑卷管理,是一种更为灵活弹性的磁盘管理工具。 通过逻辑卷管理,能够解决传统磁盘管理管理困难的问题,比如灵活的增删磁盘容量,增删磁盘分区等。 LVM由pv(Physical Volume),vg(Volume Group),lv(logical volu…

    2017-04-23

评论列表(1条)

  • renjin
    renjin 2017-04-28 10:04

    主要介绍了一种很好用的linux分区方法——非交互式分区,内容介绍的非常好,也很详细,排版很好, 由其是像这样的分区方式( parted /dev/sdb < part.txt)提高了分区时的安全性。排版也非常的好,加油!