Raspberry Pi Zero W配置Wi-Fi AP

最近在配置Raspberry Pi Zero W,使用的系统为2018-06-27-raspbian-stretch-lite,我们的需求是把这台Raspberry Pi Zero W配置为开放Wi-Fi模式的AP

Raspberry Pi Zero W只有一块无线网卡,如果被配置成AP模式不是太好操作,可以通过预留的Macro USB接口外接一个USB有线网卡来实现远程访问,当然也可以直接接入显示器,USB Hub外接鼠标键盘操作。

执行如下脚本配置:

#目前测试发现udhcpd在标准树莓派上能正常工作,但是在Pi Zero W上,重启之后,
#不能正常分配IP,应该是服务在无线网卡没有初始化完成就已经启动导致的,
#我们使用dnsmasq替代后可以正常工作

$ sudo apt-get -y remove udhcpd

$ sudo apt-get -y install hostapd dnsmasq

#备份配置文件
$ sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

#配置分配的IP段
$ sudo sed -i "s/^#dhcp-range=192.168.0.50,192.168.0.150,12h/dhcp-range=192.168.0.50,192.168.0.150,12h/g" /etc/dnsmasq.conf

#AP名字
$ export AP_NAME="AP"

#为无线网卡配置静态IP
$ export WLAN_IP=192.168.0.1

$ sudo ifconfig wlan0 $WLAN_IP

$ sudo sed -i '$a\interface wlan0' /etc/dhcpcd.conf

$ echo "static ip_address=${WLAN_IP}/24" | sudo tee -a /etc/dhcpcd.conf

#hostapd配置,配置为开放模式
$ sudo touch /etc/hostapd/hostapd.conf

$ echo "interface=wlan0" | sudo tee -a /etc/hostapd/hostapd.conf

$ echo "ssid=${AP_NAME}" | sudo tee -a /etc/hostapd/hostapd.conf

#WiFi工作的频段 1-13
$ echo "channel=9" | sudo tee -a /etc/hostapd/hostapd.conf

#硬件工作模式 g simply means 2.4GHz
$ echo "hw_mode=g" | sudo tee -a /etc/hostapd/hostapd.conf

#验证方式为开放模式 1=wpa, 2=wep, 3=both
$ echo "auth_algs=1" | sudo tee -a /etc/hostapd/hostapd.conf
    
# 802.11n support	 	 
$ echo "ieee80211n=1" | sudo tee -a /etc/hostapd/hostapd.conf

#备份配置文件
$ sudo cp /etc/default/hostapd /etc/default/hostapd.bak

#修改配置文件
$ sudo sed -i "s/#DAEMON_CONF=\"\"/DAEMON_CONF=\"\/etc\/hostapd\/hostapd.conf\"/g" /etc/default/hostapd

#启动networking和hostapd服务,注意先后顺序,先使用networking服务设置IP,再更新hostapd
$ sudo service networking restart

$ sudo service hostapd restart

$ sudo service dnsmasq restart

#设置开机启动
$ sudo update-rc.d hostapd enable

#重启设备,检查配置是否已经生效
$ sudo reboot

参考链接


解决macOS High Sierra使用dd命令向USB设备拷贝数据非常缓慢的问题

最近在使用Raspberry Pi Zero W,在创建系统镜像的时候,使用如下命令,发现非常缓慢,时间往往以小时计算:

$ diskutil unmountDisk /dev/disk2

$ sudo dd if=~/Downloads/2018-06-27-raspbian-stretch-lite.img of=/dev/disk2

如果要解决这个问题,那么可以使用如下方式:

$ diskutil unmountDisk /dev/rdisk2

$ sudo dd if=~/Downloads/2018-06-27-raspbian-stretch-lite.img of=/dev/rdisk2 bs=1m

注意两个命令的区别,一个是 `/dev/disk2` ,一个是 `/dev/rdisk2` , 两者的区别可以通过如下命令来查看:

$ man hdiutil

可以看到如下介绍:

..........................

DEVICE SPECIAL FILES
     Since any /dev entry can be treated as a raw disk image, it is worth not-
     ing which devices can be accessed when and how.  /dev/rdisk nodes are
     character-special devices, but are "raw" in the BSD sense and force
     block-aligned I/O.  They are closer to the physical disk than the buffer
     cache.  /dev/disk nodes, on the other hand, are buffered block-special
     devices and are used primarily by the kernel's filesystem code.

     It is not possible to read from a /dev/disk node while a filesystem is
     mounted from it, but anyone with read access to the appropriate
     /dev/rdisk node can use hdiutil verbs such as fsid or pmap with it.
     Beware that information read from a raw device while a filesystem is
     mounted may not be consistent because the consistent data is stored in
     memory or in the filesystem's journal.

     The DiskImages framework will attempt to use authopen(1) to open any
     device which it can't open (due to EACCES) for reading with open(2).
     Depending on session characteristics, this behavior can cause apparent
     hangs while trying to access /dev entries while logged in remotely (an
     authorization panel is waiting on console).

     Generally, the /dev/disk node is preferred for imaging devices (e.g.
     convert or create -srcdevice operations), while /dev/rdisk is usable for
     the quick pmap or fsid.  In particular, converting the blocks of a
     mounted journaled filesystem to a read-only image will prevent the volume
     in the image from mounting (the journal will be permanently dirty).

................................

根据介绍,rdisk属于原始设备(rawdisk),不必经过操作系统的文件系统缓冲处理,相当于直接操作硬件,速度非常快。但是像macOS High Sierra这种出现20x速度差别的情况,就不是太好理解了。

后面 `bs=1m` 参数也很重要,要求拷贝写入的时候整块 (`1MB`) 写入(否则是逐个字节操作,写入次数非常多,性能很差),这样才能起到加速作用。

参考链接