Recent work requires the use of the hard disk shell script Automation partition and mount operation, Google has some information, the following is a summary.
If the hard disk is not partitioned (logical partition or extended partition, about the concept of the two, self-Google), we will not be able to use the hard disk to read and write. We need to take the following three steps to use a hard drive:
- Partition the hard drive;
- Format the partition;
- Mount the partition to a directory on the system to access it.
This note will focus on the Fdisk partitioning feature involved in the first step and how to use the shell for automated processing, as well as a brief explanation of the next two steps.
FDISK partitions the hard disk
Fdisk is a hard disk partitioning tool provided by the Linux system. A detailed description of Fdisk can be used by Google or man Fdisk. Below we directly explain the operation steps.
First look at what drives are currently on the system:
[email protected]:~$ ls /dev | grep sd
sda
sda1
sdb
sdc
sdd
sdd1
Let's choose/DEV/SDC to do it. Check the partition table for this hard disk by fdisk-l:
[email protected]:~$ sudo fdisk -l /dev/sdc
Disk /dev/sdc: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders, total 20971520 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 identifier: 0xacb1d488
Device Boot Start End Blocks Id System
With the above information, we can see that the drive has a total of 10.7GB, but the partition table is empty (the output information at the back of the command is the partition table information). Let's run the Fdisk command to perform an interactive partitioning operation on the hard disk. After entering the FDISK/DEV/SDC command, you will be prompted what to do, if not clear, you can enter M and then enter to view the operating instructions. As shown below,
[email protected]:~$ sudo fdisk /dev/sdc
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
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
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help):
We want to create a new partition, enter N, and then enter. Tip the hard disk currently has 0 primary partitions (primary), 0 extended partitions (extended), you can create 4 partitions, and then let's choose to create a primary partition or an extended partition. Enter P and enter (or directly enter) and select Create Primary partition.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Then prompt to enter the partition number (only 1 to 4, default is 1), we enter 1 and return,
Partition number (1-4, default 1): 1
Enter the start sector of the partition, we directly enter the default configuration,
First sector (2048-20971519, default 2048):
Using default value 2048
Enter the end sector of the partition and return directly to the default configuration,
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
Using default value 20971519
At this point our partition is built, but we also need to enter W and return to save.
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
The partition of the hard drive is set up, and the final effect is checked by fdisk-l.
[email protected]:~$ sudo fdisk -l /dev/sdc
Disk /dev/sdc: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders, total 20971520 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 identifier: 0xacb1d488
Device Boot Start End Blocks Id System
/dev/sdc1 2048 20971519 10484736 83 Linux
The new partition we created is/DEV/SDC1. Next, we format the partition,
[email protected]:~$ sudo mkfs -t ext3 /dev/sdc1
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621184 blocks
131059 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
Formatting is complete. The partition is then mounted to the directory/MYSDC,
[email protected]:~$ sudo mkdir -p /mysdc
[email protected]:~$ sudo mount /dev/sdc1 /mysdc
Take a look through the DF command.
[email protected]:~$ sudo df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 20509308 13221580 6222872 68% /
none 4 0 4 0% /sys/fs/cgroup
udev 2013184 4 2013180 1% /dev
tmpfs 404808 576 404232 1% /run
none 5120 0 5120 0% /run/lock
none 2024032 2084 2021948 1% /run/shm
none 102400 0 102400 0% /run/user
/dev/sdc1 10189112 23160 9641716 1% /mysdc
Complete.
Shell Scripting Automation
Above, we are using the Fdisk tool to interactively partition the hard disk. So how do we write it into Shell scripting automation. Look back at the sequence of operations that we just entered when we were interactively partitioning, and then read the following script yourself, too simple, nothing to say.
#!/bin/bash echo "n p 1 w " | fdisk /dev/sdc && mkfs -t /dev/sdc1
Note: between 1 and W is two blank lines.
Delete Partition
Also delete the partition through FDISK, the operation is as follows, do not elaborate,
[email protected]:~$ sudo fdisk /dev/sdc
Command (m for help): d
Selected partition 1
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
For Shell automation scripts, refer to the shell script that created the partition to write it yourself.
(done)
FDISK partition hard disk and Shell scripting Automation