【Joytag 香蕉R1学习】OpenWrt for R1 组装脚本

自从着手弄这个R1,做了一系列尝试
【Joytag 香蕉R1学习】R1初印象并点亮
【Joytag 香蕉R1学习】让R1路由器工作起来(小白文,慎入)
【Joytag 香蕉R1学习】自己动手组装OpenWrt for R1
【Joytag 香蕉R1学习】让新系统(OpenWrt for R1)上网
【Joytag 香蕉R1学习】OpenWrt for R1有线路由, Telnet,SSH, LuCI
【Joytag 香蕉R1学习】OpenWrt for R1:设置主机名、时区、LED

存在的问题

实际操作过程中,发现几个问题,
一:因为操作过程进行了各种测试,难免各种操作互相影响
这也许不影响使用,但是写教程则会不那么严谨。
(比如说安装LuCI后,我随手就重启了,那么不重启R1的话uHTTPd是否真的需要手动启动呢?)
类似这样的问题很多。

二:因为使用的是trunk镜像,那么当镜像更新,而我们这个OpenWrt for R1却没有更新内核等相关内容
那么安装一些内核相关的软件包时,会出现无法安装的情况。

三:好吧,有时候胡乱操作,彻底崩溃了,又忘记自己都干什么了。从头做起,一步一步,似魔鬼的爪牙。。。

所以,将最最麻烦的操作,【Joytag 香蕉R1学习】自己动手组装OpenWrt for R1 ,做成脚本,这样我们就可以随时获得一个崭新的镜像了,然后就可以从头开搞了。

OpenWrt for R1 组装脚本

为了节省下载文件的时间,我将操作分成两个脚本:
1)getfiles.sh (获取所需的文件)
[list=1]
*]wget http://downloads.openwrt.org/snapshots/trunk/sunxi/generic/uboot-sunxi-Lamobo_R1/openwrt-sunxi-Lamobo_R1-u-boot-with-spl.bin
*]wget http://downloads.openwrt.org/snapshots/trunk/sunxi/generic/uboot-sunxi-Lamobo_R1/openwrt-sunxi-Lamobo_R1-uEnv.txt
*]wget http://downloads.openwrt.org/snapshots/trunk/sunxi/generic/uboot-sunxi-Lamobo_R1/openwrt-sunxi-Lamobo_R1-boot.scr
*]wget http://downloads.openwrt.org/snapshots/trunk/sunxi/generic/sun7i-a20-lamobo-r1.dtb
*]wget http://downloads.openwrt.org/snapshots/trunk/sunxi/generic/openwrt-sunxi-uImage
*]wget http://downloads.openwrt.org/snapshots/trunk/sunxi/generic/openwrt-sunxi-root.ext4
[/list]
复制代码

  1. mksys.sh (将OpenWrt for R1写入SD卡)
    [list=1]
    *]if $# != 1 ] ; then
    *]echo “usage: $0 disk”
    *]echo " e.g.: $0 /dev/sda"
    *]exit 1;
    *]fi
    *]
    *]echo -e “\n######### Prepare: Partition the disk ########”
    *]
    *]disk=$1
    *]
    *]p1=${disk}1
    *]p2=${disk}2
    *]
    *]umount $p1
    *]umount $p2
    *]
    *]fdisk $1<<EOF
    *]p
    *]d
    *]1
    *]d
    *]p
    *]n
    *]p
    *]1
    *]
    *]+15M
    *]n
    *]p
    *]2
    *]
    *]+240M
    *]t
    *]1
    *]c
    *]p
    *]w
    *]EOF
    *]
    *]echo -e “\n…1) Copy the SPL + U-boot image to the card”
    *]dd if=openwrt-sunxi-Lamobo_R1-u-boot-with-spl.bin of=$disk bs=1024 seek=8
    *]
    *]echo -e “\n…2) Create a boot (FAT32) partition”
    *]mkfs.vfat ${disk}1
    *]
    *]echo -e “\n…3) Mount the boot partition”
    *]mount -t vfat ${disk}1 /mnt
    *]
    *]echo -e “\n…4) Copy the U-Boot environment file uEnv.txt to the boot partition”
    *]cp openwrt-sunxi-Lamobo_R1-uEnv.txt /mnt/uEnv.txt
    *]
    *]echo -e “\n…5) Copy the device tree data to the boot partition”
    *]cp sun7i-a20-lamobo-r1.dtb /mnt/dtb
    *]
    *]echo -e “\n…6) Copy the kernel image to the boot partition.”
    *]cp openwrt-sunxi-uImage /mnt/uImage
    *]
    *]echo -e “\n…7) Copen the boot.scr to the boot partition.”
    *]cp openwrt-sunxi-Lamobo_R1-boot.scr /mnt/boot.scr
    *]
    *]echo -e “\n…8) Resize the root filesystem image to match the partition size.”
    *]resize2fs openwrt-sunxi-root.ext4 240M
    *]
    *]echo -e “\n…9) Create the root filesystem.”
    *]dd if=openwrt-sunxi-root.ext4 of=${disk}2 bs=128k
    *]
    *]echo -e “\n…10) Wrap up (flush buffers and unmount boot partition).”
    *]sync
    *]umount /mnt
    *]echo -e “\n…Finished!”
    *]
    [/list]
    复制代码
    其中:
    mkfs.vfat ${disk}1
    会对/dev/sda1进行格式化,清除原有内容。
    还是有必要的。

使用方法:
将两个文件保存到工作目录,假定为/root/R1
cd /root/R1
chmod 777 getfiles.sh mksys.sh
./getfiles.sh
**./mksys.sh /dev/sda
*(:因为将TF卡插入读卡器连接至香蕉派,所以TF卡被识别为/dev/sda,请务必知道自己要操作的设备,以免对原系统造成破坏)
崭新的系统诞生啦,然后,搞吧。

脚本中需要完善的地方

getfiles.sh
1:可以将两个脚本合二为一,通过命令行参数判断是否需要重新下载文件。
2:获取文件之前,应该先删除本地已有的相关文件
3:对获取情况进行判断。

mksys.sh
1:发现存在分区的TF卡插入读卡器后会自动mount,所以先调用了umount.
完善点的脚本应该先判断mount的情况,再根据情况执行umount,这样就没有错误信息了。

2:正常情况应该先判断disk的分区情况,然后删除所有分区。并重新分区。
(实在是懒得弄了。没有分区去删除,虽然会有错误提示,但是没啥影响)

3:用默认值的情况,fdisk输出不太直观。

4:应该对执行的操作进行一些判断。

5:其它…

好吧,虽然问题多多,但是可用,我们的目的不是为了写一个多么完善的脚本,只是为了方便进一步折腾而已。
基本达成目标。
(之所以这么说,是为了掩盖我小白的本质)

附:./mksys.sh /dev/sda的输出
[list=1]
*] # ./mksys.sh /dev/sda
*]
*]######### Prepare: Partition the disk ########
*]umount: /dev/sda1: not mounted
*]umount: /dev/sda2: not mounted
*]
*]Command (m for help):
*]Disk /dev/sda: 15.9 GB, 15931539456 bytes
*]64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x5452574f
*]
*] Device Boot Start End Blocks Id System
*]/dev/sda1 2048 32767 15360 c W95 FAT32 (LBA)
*]/dev/sda2 32768 524287 245760 83 Linux
*]
*]Command (m for help): Partition number (1-4):
*]Command (m for help): Selected partition 2
*]
*]Command (m for help):
*]Disk /dev/sda: 15.9 GB, 15931539456 bytes
*]64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x5452574f
*]
*] Device Boot Start End Blocks Id System
*]
*]Command (m for help): Partition type:
*] p primary (0 primary, 0 extended, 4 free)
*] e extended
*]Select (default p): Partition number (1-4, default 1): First sector (2048-31116287, default 2048): Using default value 2048
*]Last sector, +sectors or +size{K,M,G} (2048-31116287, default 31116287):
*]Command (m for help): Partition type:
*] p primary (1 primary, 0 extended, 3 free)
*] e extended
*]Select (default p): Partition number (1-4, default 2): First sector (32768-31116287, default 32768): Using default value 32768
*]Last sector, +sectors or +size{K,M,G} (32768-31116287, default 31116287):
*]Command (m for help): Partition number (1-4): Hex code (type L to list codes): Changed system type of partition 1 to c (W95 FAT32 (LBA))
*]
*]Command (m for help):
*]Disk /dev/sda: 15.9 GB, 15931539456 bytes
*]64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x5452574f
*]
*] Device Boot Start End Blocks Id System
*]/dev/sda1 2048 32767 15360 c W95 FAT32 (LBA)
*]/dev/sda2 32768 524287 245760 83 Linux
*]
*]Command (m for help): The partition table has been altered!
*]
*]Calling ioctl() to re-read partition table.
*]
*]WARNING: If you have created or modified any DOS 6.x
*]partitions, please see the fdisk manual page for additional
*]information.
*]Syncing disks.
*]
*]…1) Copy the SPL + U-boot image to the card
*]343+1 records in
*]343+1 records out
*]352244 bytes (352 kB) copied, 0.090026 s, 3.9 MB/s
*]
*]…2) Create a boot (FAT32) partition
*]mkfs.vfat 3.0.13 (30 Jun 2012)
*]
*]…3) Mount the boot partition
*]
*]…4) Copy the U-Boot environment file uEnv.txt to the boot partition
*]
*]…5) Copy the device tree data to the boot partition
*]
*]…6) Copy the kernel image to the boot partition.
*]
*]…7) Copen the boot.scr to the boot partition.
*]
*]…8) Resize the root filesystem image to match the partition size.
*]resize2fs 1.42.5 (29-Jul-2012)
*]The filesystem is already 61440 blocks long. Nothing to do!
*]
*]
*]…9) Create the root filesystem.
*]1920+0 records in
*]1920+0 records out
*]251658240 bytes (252 MB) copied, 31.8408 s, 7.9 MB/s
*]
*]…10) Wrap up (flush buffers and unmount boot partition).
*]
*]…Finished!
[/list]
复制代码