树莓派安装小记

去年从 v2ex 上收了一个树莓派,但一直放在那吃灰,今天闲来无事,拿出来折腾了一把。

在SD卡上安装raspbian系统

安装系统是比较简单的,下载好系统后,直接使用 dd 命令即可。

sudo dd bs=1m if=raspbian.img of=/dev/diskn

其中把diskn换成对应的SD卡位置即可。

扩展 raspbian 的系统容量

使用dd命令的一个副作用是,SD卡的容量显示会变小。我买的SD卡容量是 8G,但写入系统后,仅显示 1.3G的空间:

[email protected]:~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.3G  981M  350M  74% /
devtmpfs        214M     0  214M   0% /dev
tmpfs           218M     0  218M   0% /dev/shm
tmpfs           218M  4.4M  213M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           218M     0  218M   0% /sys/fs/cgroup
/dev/mmcblk0p1   60M   20M   41M  34% /boot

想要恢复SD的完整容量并不难,使用partedresize2fs 这两个命令就可以了,具体可以参考raspberry论坛的Extend root filesystem using CLI parted & resize2fs

谨慎起见,在恢复容量前,先备份下SD卡里面的内容。

先使用parted删除旧分区,然后使用新的大小重建分区。

[email protected]:~ $ sudo !!
sudo parted /dev/mmcblk0
GNU Parted 3.2
Using /dev/mmcblk0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a
        particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) print
Model: SD SS08G (sd/mmc)
Disk /dev/mmcblk0: 7948MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  67.1MB  62.9MB  primary  fat16        lba
 2      67.1MB  7948MB  7881MB  primary  ext4

(parted) rm 2                                                             
Error: Partition(s) 2 on /dev/mmcblk0 have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will
remain in use.  You should reboot now before making further changes.
Ignore/Cancel? i                                                          
(parted) mkpart primary 6,127,57  1946,198,43
Error: Partition(s) 2 on /dev/mmcblk0 have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will
remain in use.  You should reboot now before making further changes.
Ignore/Cancel? i                                                          
(parted) quit                                                             
Information: You may need to update /etc/fstab.

注意其中的两次Ignore/Cancel?,一定要输入i。退出parted后,输入reboot进行重启。

重新登录后,使用fdisk -l可以看到SD卡对应的容量已经变大,但使用df -h依旧没有变化,这时需要使用resize2fs做进一步操作。

[email protected]:~ $resize2fs /dev/mmcblk0p2 
resize2fs 1.42.3 (14-May-2012)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p2 is now 3896320 blocks long.

这时SD卡中raspberry系统的容量就变大了。我的显示是6G空间,这么大就足够做很多事了~

设置无线

到目前为止,树莓派联网还是使用的网线,有无线网卡还是要利用一下的。

使用wpa_passphrase对无线密码进行加密:

[email protected] ~ $ wpa_passphrase ssid password
network={
    ssid="ssid"
    #psk="password"
    psk=44116ea881531996d8a23af58b376d70f196057429c258f529577a26e727ec1b
}

新建文件/etc/wpa.config文件,把生成的network那段的信息写进去,并删掉明文命名password那一行。然后编辑/etc/network/interfaces文件:

[email protected]:~ $ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa.config
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

接着就使用下面的命令,连接无线:

[email protected]:~ $ sudo ifup wlan0

如果遇到ifup: interface wlan0 already configured的问题,先尝试sudo ifdown wlan0,再使用ifup命令。

[email protected]:~ $ sudo ifup wlan0
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
Internet Systems Consortium DHCP Client 4.3.1
Copyright 2004-2014 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/e8:4e:06:19:b4:37
Sending on   LPF/wlan0/e8:4e:06:19:b4:37
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 8
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 7
DHCPREQUEST on wlan0 to 255.255.255.255 port 67
DHCPOFFER from 192.168.1.1
DHCPACK from 192.168.1.1
RTNETLINK answers: File exists
bound to 192.168.1.10 -- renewal in 111483 seconds.

这时,看到最后一行,就OK了。