非交互式添加分区

前端之家收集整理的这篇文章主要介绍了非交互式添加分区前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

 

非交互式添加分区

方法

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

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

 

方法

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

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

  1. [root@local ~]# cat part.txt
  2. mkpart
  3. part4
  4. ext4
  5. 3073
  6. 4096
  7. q

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

  1. parted /dev/sdb < part.txt

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

  1. [root@local ~]# fdisk -l /dev/sdb
  2. WARNING: fdisk GPT support is currently new,and therefore in an experimental phase. Use at your own discretion.
  3.  
  4. Disk /dev/sdb: 5368 MB,5368709120 bytes,10485760 sectors
  5. Units = sectors of 1 * 512 = 512 bytes
  6. Sector size (logical/physical): 512 bytes / 512 bytes
  7. I/O size (minimum/optimal): 512 bytes / 512 bytes
  8. Disk label type: gpt
  9.  
  10. # Start End Size Type Name
  11. 1 2048 2000895 976M Microsoft basic part1
  12. 2 2001953 4000000 975.6M Microsoft basic part2 

接下来运行命令:

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

再来查看分区情况

  1. [root@local ~]# fdisk -l /dev/sdb
  2. WARNING: fdisk GPT support is currently new,10485760 sectors
  3. Units = sectors of 1 * 512 = 512 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk label type: gpt
  7.  
  8. # Start End Size Type Name
  9. 1 2048 2000895 976M Microsoft basic part1
  10. 2 2001953 4000000 975.6M Microsoft basic part2
  11. 3 6002688 7999487 975M Microsoft basic part4

  

 

方法

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

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

(2)编辑分区用到的文件

  1. [root@local ~]# cat gdisk.txt
  2. n
  3. ##空行,但必须有
  4. ##空行,但必须有
  5. +1G
  6. ##空行,但必须有
  7. w
  8. y 

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

  1. gdisk /dev/sdb < gdisk.txt

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

  1. [root@local ~]# gdisk -l /dev/sdc
  2. GPT fdisk (gdisk) version 0.8.6
  3.  
  4. Partition table scan:
  5. MBR: protective
  6. BSD: not present
  7. APM: not present
  8. GPT: present
  9.  
  10. Found valid GPT with protective MBR; using GPT.
  11. Disk /dev/sdc: 10485760 sectors,5.0 GiB
  12. Logical sector size: 512 bytes
  13. Disk identifier (GUID): F227EC43-CB17-4248-9B1A-13A35CEF8E92
  14. Partition table holds up to 128 entries
  15. First usable sector is 34,last usable sector is 10485726
  16. Partitions will be aligned on 2048-sector boundaries
  17. Total free space is 6291389 sectors (3.0 GiB)
  18.  
  19. Number Start (sector) End (sector) Size Code Name
  20. 1 2048 4196351 2.0 GiB 8300 Linux filesystem

下来运行命令:

  1. gdisk /dev/sdb < gdisk.txt
  1. [root@local ~]# gdisk /dev/sdb < gdisk.txt
  2. GPT fdisk (gdisk) version 0.8.6
  3.  
  4. Partition table scan:
  5. MBR: protective
  6. BSD: not present
  7. APM: not present
  8. GPT: present
  9.  
  10. Found valid GPT with protective MBR; using GPT.
  11.  
  12. 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'
  13. Hex code or GUID (L to show codes,Enter = 8300): Changed type of partition to 'Linux filesystem'
  14. Command (? for help):
  15. Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
  16. PARTITIONS!!
  17.  
  18. Do you want to proceed? (Y/N): OK; writing new GUID partition table (GPT) to /dev/sdb.
  19. The operation has completed successfully. 

再来查看分区情况

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

可以看到已经添加成功

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

 

猜你在找的Linux相关文章