Configure the U disk to two ext3 partitions

Keywords: shell Linux ascii

#!/bin/bash  

echo -e "\\033[1;34m start make sd \\033[0m"


#Getting sd card information
sd_main=`ls -l /dev/disk/by-id/ | grep usb- | awk -F '/'  'NR==1{print $3}'`
sd_1=`ls -l /dev/disk/by-id/ | grep usb- | awk -F '/'  'NR==2{print $3}'`
sd_2=`ls -l /dev/disk/by-id/ | grep usb- | awk -F '/'  'NR==3{print $3}'`

if [ -d "/dev/$sd_main" ]; then
    echo"please insert the sd card!"
    exit
fi

#Unmount before partitioning
disk="/dev/$sd_1"
mount | grep $disk
if [ $? = '0' ]; then
    echo umount "$disk"
    umount $disk
fi

disk="/dev/$sd_2"
mount | grep $disk
if [ $? = '0' ]; then
    echo umount "$disk"
    umount $disk
fi

#Getting Disk Information
disk="/dev$sd_main"

#Disk partition, default two partitions
echo -e "\\033[1;34m fdisk $disk now ... \\033[0m "

#Delete partition information
dd if=/dev/zero of=$disk bs=1 count=64 seek=446 >/dev/null

#Repartitioning, the first partition allocates 2G and the second partition is the remaining size (the following partition parameters can not be changed at will)
fdisk $disk >/dev/null <<EOF
n
p
1

4196352
n
p
2


w
EOF

#Partition formatting
echo -e "\\033[1;34m format $disk ... \\033[0m "

#Get partition 1 information
sd_1=`ls -l /dev/disk/by-id/ | grep usb- | awk -F '/'  'NR==2{print $3}'`

#Format partition 1 into ext4 format
mkfs.ext3 -L rootfs /dev/$sd_1
if [ $? = '0' ]; then
    echo -e "\\033[1;34m mkfs.ext3 /dev/$sd_1 success! \\033[0m"
else
    echo -e "\\033[1;34m mkfs.ext3 /dev/$sd_1 error! \\033[0m"
fi

#Get partition 2 information
sd_2=`ls -l /dev/disk/by-id/ | grep usb- | awk -F '/'  'NR==3{print $3}'`

#Format partition 2 into ext4 format
mkfs.ext3 -L logfs /dev/$sd_2
if [ $? = '0' ]; then
    echo -e "\\033[1;34m mkfs.ext3 /dev/$sd_2 success! \\033[0m"
else
    echo -e "\\033[1;34m mkfs.ext3 /dev/$sd_2 error! \\033[0m"
fi

This script is mainly to find the storage device under usb, and then get the device number for partition formatting

echo -e "\\033[1;34m start make host sd \\033[0m"

This is mainly shown in blue characters on black background (1 for black background, 34m for blue characters)

sd_main=`ls -l /dev/disk/by-id/ | grep usb- | awk -F '/'  'NR==1{print $3}'`

| It's a pipe in the shell that takes the result of the previous command as input to the next command

grep linux text search tool

awk Text Analysis Tool - F Specifies Text Splitter for Input Files

Awk-F'/''NR== 1 {print $3}'denotes the third parameter of the separator with and without.

dd if=/dev/zero of=$disk bs=1 count=64 seek=446 >/dev/null

/ dev/null, it's an empty device. As a "black hole", it's very equivalent to a write-only file, and everything written to it will be lost forever.

/ dev/zero is also a pseudo-file, but it actually generates a continuous stream of null (binary zero stream, not ASCII type). The output written to it will be lost.

#Repartition, the first partition allocates 2G and the second partition is the remaining size (the following partition parameters can not be changed at will)
fdisk $disk >/dev/null <<EOF
n
p
1

4196352
n
p
2


w
EOF

Use''EOF'to tell the main shell that the subsequent input is the input of other commands or sub-shells, and return to the main shell until EOF is encountered.

Here is partitioning using fdisk

Computation of partition size; My first partition is 2G, and the input parameter on it is 4196352.

First look at the disk information fdisk-l

sdb is U disk information

Termination Cylinder Number = Starting Cylinder Number + [(size of partition required * 1024 * 1024) / units]

4196352=2048+[(2048*1024*1024)/512]

mkfs.ext3 -L logfs /dev/$sd_2

After partitioning is completed, the partitioned disk is finally formatted into ext3 format, of course, ext4, fat format can also be formatted.

Posted by kidgeek_dfw on Sat, 18 May 2019 09:33:13 -0700