自動化分區腳本的實踐

使用FTP客戶端的好處是:可以直接將windows文件系統中的文件拖拽至linux文件系統中,而無須借用U盤轉存

一、SD卡燒寫

1.環境準備

筆記本系統:XP 32

虛擬機:VMwareWorkstation 9.0

Linux版本:CentOS 6.3 i386

SD卡和讀卡器

FTP客戶端:FileZilla

 

2.linux系統中根目錄“/”下新建一個名爲filename的目錄

[root@instructor~]# cd /                                           進入根目錄
[root@instructor~]# mkdir /filesystem/               創建名爲filename的文件夾


 

3.下載安裝FileZilla軟件,打開,如下圖所示:

wKioL1PdfEHgO9DSAASflJL3LCk745.jpg

主機:這裏填寫虛擬機中linux系統的IP地址,例如:192.168.5.107

           查看IP地址的命令:ifconfig

用戶名:root

密碼:root用戶的密碼

端口:22

填好後點擊快速連接,列出文件系統後,表示連接成功

其中左邊本地站點列出的是物理機的文件系統,右邊列出的是linux的文件系統

 

4.將文件從windows文件系統傳輸到linux文件系統中

找到燒寫程序所在的路徑,如下圖所示:

這裏mksd-new-14.tar文件就存放在桌面上

wKiom1Pde2mDZsKzAAK5k8tX29w560.jpg

mksd-new-14.tar文件直接拖動至右邊fliename文件夾中,如下圖所示:

wKioL1PdfLLRxeVIAAFwr5gXFN0462.jpg

傳輸進度表

wKiom1Pde8mjE7CWAALUXTlq4Pw843.jpg


傳輸完成

 

5.再回到linux系統中查看

[root@instructor~]# cd /filesystem/                       進入filename文件夾中
[root@instructorfilesystem]# ls                             使用ls命令查看文件夾中內容
mksd-new-14.tar.bz2                                              顯示文件已傳輸過來


 

6.解壓文件

[root@instructorfilesystem]# tar -jxvf mksd-new-14.tar.bz2 
  
不指定路徑則解壓到當前目錄


以下是解壓過程:

mksd-new-14/
mksd-new-14/vfat/
mksd-new-14/vfat/MLO
mksd-new-14/vfat/boot.scr
mksd-new-14/vfat/u-boot.bin
mksd-new-14/vfat/uImage
mksd-new-14/vfat/system.tar.gz
mksd-new-14/rootfs/
mksd-new-14/rootfs/SD_rootfs.tar
mksd-new-14/rootfs/readme.txt
mksd-new-14/mksd-ti816x-sdx.sh

解壓完成

再次執行ls命令

[root@instructorfilesystem]# ls 
mksd-new-14 mksd-new-14.tar.bz2

此時會發現多出了一個與文件同名的文件夾mksd-new-14

7.SD卡連接電腦

wKioL1PdfRGQu6WnAAKu1eBnlio274.jpg

linux中連上SD

查看SD卡在linux系統中所顯示的設備名

[root@instructormksd-new-14]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_instructor-lv_root
                      7.5G  5.7G 1.7G  78% /
tmpfs                 504M  112K 504M   1% /dev/shm
/dev/sda1             485M   32M 428M   7% /boot
/dev/sdb1             485M   11M 449M   3% /mnt/new
/dev/sdb2              97M   16M  77M  17% /mnt/test
/dev/sr0              3.5G  3.5G    0 100% /media/CentOS_6.3_Final
/dev/sdc1              70M   34M  37M  48% /media/boot
/dev/sdc2             7.3G  215M 6.7G   4% /media/rootfs

可以看到/dev/sdc即爲SD的設備名,其中分了兩個區

 

8.執行文件夾中的shell腳本

[root@instructorfilesystem]# cd mksd-new-14                進入mksd-new-14
[root@instructormksd-new-14]# ls –l                         以長文件名的形式列出文件
total 12
-rwxr-xr-x. 1 root root 1126 Jul  8 09:42 mksd-ti816x-sdx.sh       以.sh結尾的即爲shell腳本
drwxr-xr-x. 2 root root 4096 Jul  8 09:42 rootfs                    rootfs目錄
drwxr-xr-x. 2 root root 4096 Jul  8 09:42 vfat                      vfat目錄


執行帶參數的腳本

命令格式:mksd-ti816x-sdx.sh 設備名 vfat路徑 rootfs文件路徑

[root@instructormksd-new-14]# ./mksd-ti816x-sdx.sh /dev/sdc vfat/ rootfs/SD_rootfs.tar
All data on /dev/sdc now will be destroyed!Continue? [y/n]
y
[Partitioning /dev/sdc...]
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.694147 s,1.5 MB/s
DISK SIZE - 7948206080 bytes
CYLINDERS - 966
Checking that no-one is using this diskright now ...
BLKRRPART: Device or resource busy
 
This disk is currently inuse - repartitioning is probably a bad idea.
Umount all file systems,and swapoff all swap partitions on this disk.
Use the --no-reread flagto suppress this check.
Use the --force flag tooverrule all checks.
 [Makingfilesystems...]
[Copying files...]
[Done]

注意:上面紅色字體的報錯信息表示,SD卡的兩個分區正在使用,需要卸載後才行

[root@instructormksd-new-14]# umount /dev/sdc1
[root@instructormksd-new-14]# umount /dev/sdc2
[root@instructormksd-new-14]# ./mksd-ti816x-sdx.sh /dev/sdc vfat/ rootfs/SD_rootfs.tar
All data on /dev/sdc now will be destroyed!Continue? [y/n]
y
[Partitioning /dev/sdc...]
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.983607 s,1.1 MB/s
DISK SIZE - 7948206080 bytes
CYLINDERS - 966
Checking that no-one is using this diskright now ...
OK
 
Disk /dev/sdc: 966 cylinders, 255 heads, 63sectors/track
 /dev/sdc: unrecognized partition table type
Old situation:
No partitions found
New situation:
Units = cylinders of 8225280 bytes, blocksof 1024 bytes, counting from 0
 
  Device Boot Start     End  #cyls    #blocks   Id System
/dev/sdc1  *      0+      8      9-     72261    c W95 FAT32 (LBA)
/dev/sdc2          9    965     957    7687102+ 83  Linux
/dev/sdc3          0       -      0          0    0 Empty
/dev/sdc4          0       -      0          0    0 Empty
Successfully wrote the new partition table
 
Re-reading the partition table ...
 
If you created or changed a DOS partition,/dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
[Making filesystems...]
[Copying files...]
[Done]

 

 

二、相關命令及參數解釋

1.ls

功能說明:列出目錄內容

常用參數:
  -1   每列僅顯示一個文件或目錄名稱。
  -a--all   下所有文件和目錄。

2.tar

功能說明:文件打包或解包

常用參數:

 -j 通過bzip2進行壓縮/解壓縮,此時文件名最好爲*.tar.bz2

 -z 通過gzip進行壓縮/解壓縮,此時文件名最好爲*.tar.gz

 -x 解包或解壓縮,可以跟 –C來指定壓縮文件存放目錄,若不指定,則在當前目錄解壓

 -v 在壓縮/解壓縮的過程中,將正在處理的文件名顯示出來

 -f 後面跟被處理的文件名

3.cd

功能說明:切換目錄

舉例:cd 切換至用戶家目錄

           cd ..返回上一級目錄

4.df

功能說明:顯示磁盤的相關信息

常用參數:
  -a--all   包含全部的文件系統。
  --block-size=<區塊大小>   以指定的區塊大小來顯示區塊數目。
  -h--human-readable   以可讀性較高的方式來顯示信息。


三、腳本詳解

[root@instructor mksd-new-14]# cat mksd-ti816x-sdx.sh 
#!/bin/bash                                       //使用bash作爲解釋器
if [[ -z $1 || -z $2  || -z $3 ]] #|| -z $4 ]]    //判斷位置變量(腳本後接參數)是否存在
then                                              //若任一參數不存在,則提示腳本用法
        echo "mksd-ti816x Usage:"
        echo "  mksd-ti816x <device> <vfat path> <rootfs tar.gz >"
        echo "  Example: mksd-ti816x /dev/sdc ../vfat nfs.tar.gz"
        exit
fi                                                //if語句結束
if ! [[ -e $2 ]]                                  //如果第二個參數存在,即vfat不存在
then                                              //則提示錯誤信息並退出腳本
        echo "Incorrect vfat location!"
        exit
fi
if ! [[ -e $3 ]]                   //如果第三個參數不存在,即rootfs/SD_rootfs.tar不存在
then                                             //則提示錯誤信息並退出腳本
        echo "Incorrect rootfs location!"
        exit
fi                                               //if語句結束
echo "All data on "$1" now will be destroyed! Continue? [y/n]"    //屏幕輸出信息,$1代表第一個參數,即SD卡的設備名
read ans                                        //接收用戶輸入,並存入變量ans
if ! [ $ans == 'y' ]                            //判斷,如果用戶輸入的字符不是"y",則退出
then
        exit
fi                                             //if語句結束
echo "[Partitioning $1...]"                    //屏幕輸出信息
DRIVE=$1                                       //將SD卡設備名賦值給DRIVE
dd if=/dev/zero of=$DRIVE bs=1024 count=1024            
         
SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`        //使用fdisk命令查看SD設備容量,並截取其容量大小賦值給SIZE變量
         
echo DISK SIZE - $SIZE bytes                    //屏幕輸出SIZE大小
 
CYLINDERS=`echo $SIZE/255/63/512 | bc`          //將SIZE轉換爲柱面大小
 
echo CYLINDERS - $CYLINDERS                    //屏幕輸入柱面數
{                                              //使用sfdisk命令將SD卡分爲兩個區
echo ,9,0x0C,*
echo ,,,-
} | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE
echo "[Making filesystems...]"
mkfs.vfat -F 32 -n boot "$1"1 &> /dev/null     //將第一個分區使用vfat格式化,命名爲boot
mkfs.ext3 -L rootfs "$1"2 &> /dev/null         //將第二個分區使用ext3格式化,命名爲rootfs
echo "[Copying files...]"
mount "$1"1 /mnt                               //掛載SD卡第一個分區到 /mnt
cp -rf $2/* /mnt                               //將vfat目錄下的所有文件複製到 /mnt下
#cp $2 /mnt/MLO
#cp $3 /mnt/u-boot.bin
#cp $4 /mnt/uImage
umount "$1"1                                   //卸載第一個分區
mount "$1"2 /mnt                               //將第二個分區掛載到 /mnt
tar xvf $3 -C /mnt &> /dev/null                //將rootfs中的SD_rootfs.tar文件解壓縮到 /mnt下
chmod 755 /mnt                                 //修改/mnt權限爲755
sleep 1                                        //暫停1秒
umount "$1"2                                   //卸載第二個分區
echo "[Done]"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章