简体   繁体   中英

python3/bash + how to automate creation of disk partitions in Linux

here is example from our rhel server machine

lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sdb                  8:16   0   20G  0 disk /data/sdb
sdc                  8:32   0   20G  0 disk /data/sdc
sdd                  8:48   0   20G  0 disk /data/sdd
sde                  8:64   0   20G  0 disk /data/sde
sdf                  8:80   0   42G  0 disk
sdg                  8:96   0   42G  0 disk
sdh                  8:112  0   42G  0 disk

we want to Create a Disk Partitions for the other disks as sdf,sdg,sdh , but all this process should be by bash script and we want to automate the process

first here is example how to create 2 partitions for sdf disk, so in this example we create two partitions each one will get 10G size

step 1 ( create partitions when each partition take 10G )

parted /dev/sdf

GNU Parted 3.1
Using /dev/sdf   
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel msdos      <-- sending text (1)
(parted) mkpart primary 0 10024MB      <-- sending text  (2)
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I        <-- sending text (3)
(parted)  mkpart primary 10024MB 20048MB     <-- sending text (4)
(parted) quit     <-- sending text (5)
Information: You may need to update /etc/fstab.

  

and now we get ( the expected results )

lsblk

sdf                  8:80   0   42G  0 disk
├─sdf1               8:81   0  9.3G  0 part
└─sdf2               8:82   0  9.3G  0 part

can we automate the parted process? or maybe by other approach ( for example by fdisk )?

in order to use this automated process in python3/bash script

Note - we not have expect on our Linux machines

reference - https://www.tecmint.com/create-disk-partitions-in-linux/

The option --script is what you are looking for.

Create a text file with the parted commands you want to execute (simulate interactive) and use the above option on the command line in the following manner:

parted --script ${batch_file}

I would do that for only one at at time until the batch_file content is verified to be correct and reliable.

One observation: you may want to modify

mkpart primary 0 10024MB

to show

mkpart primary 0 10080MB

to eliminate the mis-alignment being reported ( access hit from mis-alignment).

The approach is to calculate a number that is a multiple of 512 bytes, but divisible by 2048 or 4096 depending on what the disk reports is the physical sector size. For example:

4096 * 1024 * (2048 + 256 + 128 + 64 + 32 - 16 + 8) = 1279918080

1279918080 / (1024 * 1024) = 10080 GB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM