非交互式添加分区

非交互式添加分区

方法一

添加/deb/sdb 下的分区,其实位置为1到1000M,第二个分区位置为1001至3000M,位置千万不能指定错误

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

相关推荐

  • 源码编译安装dropbear

    tar -jxf dropbear-2013.58.tar.bz2 cd dropbear-2013.58 ./configure make PROGRAMS=”dropbear dbclient dropbearkey dropbearconvert scp” make PROGRAMS=”dropbear dbclie…

    Linux干货 2017-10-21
  • 基于haproxy实现wordpress动静分离

    环境:centos 6.8 注:此处省略对于各服务器的IP配置。 一:图示讲解      用户访问vip,通过haproxy代理得到服务器的资源。此架构下基于keepalived对haproxy做负载均衡(此种两种软件装在同一台服务器),基于haproxy对Nginx和apache做负载均衡,Nginx和Apache使用共享存储…

    2017-05-18
  • 马哥教育21期网络班—第11周课程+练习—-成长进行时–不退步–上

    1、详细描述一次加密通讯的过程,结合图示最佳。 对称加密: 加密和解密使用同一个密钥; 缺点:如何通信方多的话,需要保存多组密钥 公钥加密:密钥是成对儿出现 公钥:公开给所有人;pubkey 私钥:自己留存,必须保证其私密性;secret key 特点:用公钥加密的数据,只能使用与之配对儿的私钥解密;反之亦然; 数字签名:主要在于让接收方确认发送方…

    Linux干货 2016-09-19
  • bash编程初体验(二)

    bash编程初体验(二) read if case 概述 在本篇文章中,我们将介绍bash编程中有关if语句的简单用法,if语句的基本思路是判断给定的条件是否满足,即结果是真还是假,从而选择执行哪种操作。如此,如果条件为真,if会执行一种指令,如果条件为假,if会选择执行另一种指令,这种执行就是所谓的选择结构,它能够改变命令的基本顺序流结构,以选择流的形式运…

    Linux干货 2016-08-19
  • 推荐-LVS专题: LVS的工作模型和调度算法介绍

    LVS专题: LVS的工作模型和调度算法介绍 LVS专题: LVS的工作模型和调度算法介绍 前言 什么是负载均衡? 什么是LVS? LVS的架构: LVS的实现模型: NAT实现原理: DR实现原理: TUN实现原理: FULLNAT实现原理: LVS的调度算法 静态调度算法(4种) 动态调度算法(6种): 总结 前言 本文大概介绍一下LVS的工作方式和实现…

    Linux干货 2016-04-05
  • 马哥教育网络班21期-第5周课程练习

    第5周课程练习 1、 显示/boot/grub/grub.conf中以至少一个空白字符开头的行; # grep "^[[:space:]]\+" /boot/grub/grub.conf 2、 显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; # egrep "…

    Linux干货 2016-08-02

评论列表(1条)

  • renjin
    renjin 2017-04-28 10:04

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