Buildrootで小さなAPを作る(前編)

前回はRaspberry Pi OSを使用して4GBのSDカードでAPを作ってみましたが、今回は更に小さい2GBのSDカードに収まるAPを構築したいと思います。

今回使用するのは、Buildrootという組込み Linux システムの構築を自動化できるシステムです。

Build rootのインストール

まずは、作業ディレクトリを準備します。

$ mkdir rpi3bplus_hostapd
$ cd rpi3bplus_hostapd

次にBUildrootからbuildroot-2021.02.8.tar.bz2をダウンロードして展開します。

$ wget https://buildroot.org/downloads/buildroot-2021.02.8.tar.bz2
$ tar jxvf buildroot-2021.02.8.tar.bz2
$ cd buildroot-2021.02.8

今回はRaspberry Pi 3B+向けのコンフィグレーションを利用してビルドします。

$ make raspberrypi3_defconfig
$ make all

一度、ビルドが通るか確認したいと思います。

$ make raspberrypi3_defconfig
$ make all

このままだと、リモートログインで作業できないのでsshdを有効にします。

$ make menuconfig

    Target packages  --->
    Networking applications  --->
    [*] openssh
    [*]   client (NEW)
    [*]   server (NEW)
    [*]   key utilities (NEW)
        *** openswan needs a glibc or musl toolchain w/ headers >= 3.4 ***

MainメニューからTarget packagesを選択します。

次にNetworking applicationsを選択します。

そして、opensshを有効にします。

設定を終えたら、保存して終了します。

再度、ビルドを行います。

$ make

出来上がったイメージを書き込んでみましょう。
書き込み対象となるイメージは output/images/sdcard.img です。

$ ls -lh output/images/sdcard.img
-rw-r--r-- 1 developer developer 153M  1月 29 17:47 output/images/sdcard.img

153M と非常に小さいです。

これを、SDカードに書き込みます。

$ sudo dd bs=4M if=output/images/sdcard.img of=/dev/sdb status=progress conv=fsync
$ sync

Root filesystem overlay の設定

SDカードを起動し、SSHでログインを試みるとログインできません。
組み込まれている etc/ssh/sshd_config の中身を確認します。

$ nano  output/target/etc/ssh/sshd_config

PermitRootLogin の設定を探すと下記のようになっています。

#PermitRootLogin prohibit-password

今回は、ユーザーアカウントを追加せずに root からログインできるように Yes に変更したいのですが、このファイルは直接編集しません。
buildroot の機能である Root filesystem overlay を使用します。

まず、環境の準備を行うために作業ルートに移動します。

$ cd rpi3bplus_hostapd
$ mkdir rootfs_overlay
$ nano setup.env

下記の内容を記述します。

export ENV_BASE_ROOT=$(pwd)
export ENV_ROOTFS_OVERLAY=${ENV_BASE_ROOT}/rootfs_overlay

続けて、setup.env を読み込みます。

$ source setup.env

これで下準備ができました。

SSHの導入

続けて、rootfs_overlay に環境を構築します。

$ mkdir -p ${ENV_ROOTFS_OVERLAY}/etc/ssh
$ cp buildroot-2021.02.8/output/target/etc/ssh/sshd_config ${ENV_ROOTFS_OVERLAY}/etc/ssh/

今回コピーした ${ENV_ROOTFS_OVERLAY}/etc/ssh/sshd_config を編集します。

$ nano ${ENV_ROOTFS_OVERLAY}/etc/ssh/sshd_config

先ほど確認したPermitRootLogin の設定を修正します。

 :
#PermitRootLogin prohibit-password
PermitRootLogin yes
 :

buildroot に Root filesystem overlay を使用する設定を行います。

$ cd buildroot-2021.02.8
$ make menuconfig

    System configuration  --->
    (${ENV_ROOTFS_OVERLAY}) Root filesystem overlay directories

MainメニューからSystem Configurationを選択します。

次に、Root filesystem overlay directories を指定します。

また、root にもパスワードを設定しておきます。

    System configuration  --->
[*] Enable root login with password
(rPi3pAss) Root password

設定を保存します。

さて、再度 make を行います。

$ make

無事にオーバーレイできているか確認しておきます。

$ nano output/target/etc/ssh/sshd_config

下記のようになっていれば成功です。

 :
#PermitRootLogin prohibit-password
PermitRootLogin yes
 :

間違いないようです。

SDイメージを書き込んで再起動してみます。

# dmesg
[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 5.10.1-v7 (developer@debian) (arm-buildroot-linux-uclibcgnueabihf-gcc.br_real (Buildroot 2021.02.8) 9.4.0, GNU ld (GNU Binutils) 2.35.2) #1 SMP Wed Jan 19 17:26:11 JST 2022
[    0.000000] CPU: ARMv7 Processor [410fd034] revision 4 (ARMv7), cr=10c5383d
[    0.000000] CPU: div instructions available: patching division code
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] OF: fdt: Machine model: Raspberry Pi 3 Model B Plus Rev 1.3
[    0.000000] Memory policy: Data cache writealloc
[    0.000000] Reserved memory: created CMA memory pool at 0x35c00000, size 64 MiB
[    0.000000] OF: reserved mem: initialized node linux,cma, compatible id shared-dma-pool
[    0.000000] Zone ranges:
[    0.000000]   DMA      [mem 0x0000000000000000-0x0000000039bfffff]
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000000000000-0x0000000039bfffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000000000000-0x0000000039bfffff]
[    0.000000] On node 0 totalpages: 236544
[    0.000000]   DMA zone: 2079 pages used for memmap
[    0.000000]   DMA zone: 0 pages reserved
[    0.000000]   DMA zone: 236544 pages, LIFO batch:63
[    0.000000] percpu: Embedded 20 pages/cpu s50572 r8192 d23156 u81920
[    0.000000] pcpu-alloc: s50572 r8192 d23156 u81920 alloc=20*4096
[    0.000000] pcpu-alloc: [0] 0 [0] 1 [0] 2 [0] 3
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 234465
[    0.000000] Kernel command line: coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_compat_alsa=0 snd_bcm2835.enable_hdmi=1 bcm2708_fb.fbwidth=720 bcm2708_fb.fbheight=480 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000  root=/dev/mmcblk0p2 rootwait console=tty1 console=ttyAMA0,115200
[    0.000000] Dentry cache hash table entries: 131072 (order: 7, 524288 bytes, linear)
[    0.000000] Inode-cache hash table entries: 65536 (order: 6, 262144 bytes, linear)
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 854448K/946176K available (10240K kernel code, 1310K rwdata, 2964K rodata, 1024K init, 860K bss, 26192K reserved, 65536K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
[    0.000000] ftrace: allocating 31783 entries in 63 pages
[    0.000000] ftrace: allocated 63 pages with 6 groups
[    0.000000] rcu: Hierarchical RCU implementation.
[    0.000000]  Rude variant of Tasks RCU enabled.
[    0.000000]  Tracing variant of Tasks RCU enabled.
[    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 10 jiffies.
[    0.000000] NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16
[    0.000000] random: get_random_bytes called from start_kernel+0x3b0/0x598 with crng_init=0
[    0.000000] arch_timer: cp15 timer(s) running at 19.20MHz (phys).
[    0.000000] clocksource: arch_sys_counter: mask: 0xffffffffffffff max_cycles: 0x46d987e47, max_idle_ns: 440795202767 ns
[    0.000008] sched_clock: 56 bits at 19MHz, resolution 52ns, wraps every 4398046511078ns
[    0.000024] Switching to timer-based delay loop, resolution 52ns
[    0.000309] Console: colour dummy device 80x30
[    0.001101] printk: console [tty1] enabled
[    0.001172] Calibrating delay loop (skipped), value calculated using timer frequency.. 38.40 BogoMIPS (lpj=192000)
[    0.001230] pid_max: default: 32768 minimum: 301
[    0.001453] LSM: Security Framework initializing
[    0.001711] Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
[    0.001762] Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
[    0.003320] Disabling memory control group subsystem
[    0.003458] CPU: Testing write buffer coherency: ok
[    0.003959] CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
[    0.004951] Setting up static identity map for 0x100000 - 0x10003c
[    0.005178] rcu: Hierarchical SRCU implementation.
[    0.006101] smp: Bringing up secondary CPUs ...
[    0.007270] CPU1: thread -1, cpu 1, socket 0, mpidr 80000001
[    0.008556] CPU2: thread -1, cpu 2, socket 0, mpidr 80000002
[    0.009765] CPU3: thread -1, cpu 3, socket 0, mpidr 80000003
[    0.010009] smp: Brought up 1 node, 4 CPUs
[    0.010042] SMP: Total of 4 processors activated (153.60 BogoMIPS).
[    0.010073] CPU: All CPU(s) started in HYP mode.
[    0.010101] CPU: Virtualization extensions available.
[    0.011072] devtmpfs: initialized
[    0.028439] VFP support v0.3: implementor 41 architecture 3 part 40 variant 3 rev 4
[    0.029064] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
[    0.029125] futex hash table entries: 1024 (order: 4, 65536 bytes, linear)
[    0.032371] pinctrl core: initialized pinctrl subsystem
[    0.033578] NET: Registered protocol family 16
[    0.037611] DMA: preallocated 1024 KiB pool for atomic coherent allocations
[    0.043410] audit: initializing netlink subsys (disabled)
[    0.043751] audit: type=2000 audit(0.040:1): state=initialized audit_enabled=0 res=1
[    0.044335] thermal_sys: Registered thermal governor 'step_wise'
[    0.045229] hw-breakpoint: found 5 (+1 reserved) breakpoint and 4 watchpoint registers.
[    0.045291] hw-breakpoint: maximum watchpoint size is 8 bytes.
[    0.045583] Serial: AMBA PL011 UART driver
[    0.064384] bcm2835-mbox 3f00b880.mailbox: mailbox enabled
[    0.080159] raspberrypi-firmware soc:firmware: Attached to firmware from 2020-12-15T18:34:30, variant start
[    0.090171] raspberrypi-firmware soc:firmware: Firmware hash is 4b4aff21f72c5b9ba39d83c7b0f8fa910a6ef99b
[    0.139465] bcm2835-dma 3f007000.dma: DMA legacy API manager, dmachans=0x1
[    0.142038] SCSI subsystem initialized
[    0.142310] usbcore: registered new interface driver usbfs
[    0.142409] usbcore: registered new interface driver hub
[    0.142556] usbcore: registered new device driver usb
[    0.144438] clocksource: Switched to clocksource arch_sys_counter
[    1.753184] VFS: Disk quotas dquot_6.6.0
[    1.753326] VFS: Dquot-cache hash table entries: 1024 (order 0, 4096 bytes)
[    1.753559] FS-Cache: Loaded
[    1.753844] CacheFiles: Loaded
[    1.765040] NET: Registered protocol family 2
[    1.765989] tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 6144 bytes, linear)
[    1.766076] TCP established hash table entries: 8192 (order: 3, 32768 bytes, linear)
[    1.766227] TCP bind hash table entries: 8192 (order: 4, 65536 bytes, linear)
[    1.766441] TCP: Hash tables configured (established 8192 bind 8192)
[    1.766627] UDP hash table entries: 512 (order: 2, 16384 bytes, linear)
[    1.766705] UDP-Lite hash table entries: 512 (order: 2, 16384 bytes, linear)
[    1.767170] NET: Registered protocol family 1
[    1.768030] RPC: Registered named UNIX socket transport module.
[    1.768065] RPC: Registered udp transport module.
[    1.768095] RPC: Registered tcp transport module.
[    1.768124] RPC: Registered tcp NFSv4.1 backchannel transport module.
[    1.769701] hw perfevents: enabled with armv7_cortex_a7 PMU driver, 7 counters available
[    1.773493] Initialise system trusted keyrings
[    1.773777] workingset: timestamp_bits=14 max_order=18 bucket_order=4
[    1.783499] zbud: loaded
[    1.785640] FS-Cache: Netfs 'nfs' registered for caching
[    1.786556] NFS: Registering the id_resolver key type
[    1.786638] Key type id_resolver registered
[    1.786669] Key type id_legacy registered
[    1.786844] nfs4filelayout_init: NFSv4 File Layout Driver Registering...
[    1.788025] Key type asymmetric registered
[    1.788059] Asymmetric key parser 'x509' registered
[    1.788133] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 249)
[    1.788174] io scheduler mq-deadline registered
[    1.788205] io scheduler kyber registered
[    1.791634] bcm2708_fb soc:fb: FB found 1 display(s)
[    1.805262] Console: switching to colour frame buffer device 90x30
[    1.813846] bcm2708_fb soc:fb: Registered framebuffer for display 0, size 720x480
[    1.820340] Serial: 8250/16550 driver, 1 ports, IRQ sharing enabled
[    1.826134] bcm2835-rng 3f104000.rng: hwrng registered
[    1.829483] vc-mem: phys_addr:0x00000000 mem_base=0x3ec00000 mem_size:0x40000000(1024 MiB)
[    1.836463] gpiomem-bcm2835 3f200000.gpiomem: Initialised: Registers at 0x3f200000
[    1.853062] brd: module loaded
[    1.868399] loop: module loaded
[    1.873061] Loading iSCSI transport class v2.0-870.
[    1.877923] libphy: Fixed MDIO Bus: probed
[    1.881299] usbcore: registered new interface driver lan78xx
[    1.884601] usbcore: registered new interface driver smsc95xx
[    1.887639] dwc_otg: version 3.00a 10-AUG-2012 (platform bus)
[    2.618920] Core Release: 2.80a
[    2.621928] Setting default values for core params
[    2.625046] Finished setting default values for core params
[    2.828497] Using Buffer DMA mode
[    2.831493] Periodic Transfer Interrupt Enhancement - disabled
[    2.834590] Multiprocessor Interrupt Enhancement - disabled
[    2.837641] OTG VER PARAM: 0, OTG VER FLAG: 0
[    2.840626] Dedicated Tx FIFOs mode
[    2.844154]
[    2.844176] WARN::dwc_otg_hcd_init:1072: FIQ DMA bounce buffers: virt = b5d14000 dma = 0xf5d14000 len=9024
[    2.853161] FIQ FSM acceleration enabled for :
[    2.853161] Non-periodic Split Transactions
[    2.853161] Periodic Split Transactions
[    2.853161] High-Speed Isochronous Endpoints
[    2.853161] Interrupt/Control Split Transaction hack enabled
[    2.867419] dwc_otg: Microframe scheduler enabled
[    2.867490]
[    2.867500] WARN::hcd_init_fiq:457: FIQ on core 1
[    2.872636]
[    2.872646] WARN::hcd_init_fiq:458: FIQ ASM at 807cb66c length 36
[    2.877828]
[    2.877837] WARN::hcd_init_fiq:496: MPHI regs_base at ba010000
[    2.883114] dwc_otg 3f980000.usb: DWC OTG Controller
[    2.885917] dwc_otg 3f980000.usb: new USB bus registered, assigned bus number 1
[    2.888760] dwc_otg 3f980000.usb: irq 89, io mem 0x00000000
[    2.891544] Init: Port Power? op_state=1
[    2.894266] Init: Power Port (0)
[    2.897302] usb usb1: New USB device found, idVendor=1d6b, idProduct=0002, bcdDevice= 5.10
[    2.902811] usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[    2.905756] usb usb1: Product: DWC OTG Controller
[    2.908636] usb usb1: Manufacturer: Linux 5.10.1-v7 dwc_otg_hcd
[    2.911559] usb usb1: SerialNumber: 3f980000.usb
[    2.915263] hub 1-0:1.0: USB hub found
[    2.918166] hub 1-0:1.0: 1 port detected
[    2.921911] dwc_otg: FIQ enabled
[    2.921924] dwc_otg: NAK holdoff enabled
[    2.921936] dwc_otg: FIQ split-transaction FSM enabled
[    2.921954] Module dwc_common_port init
[    2.922334] usbcore: registered new interface driver usb-storage
[    2.925364] mousedev: PS/2 mouse device common for all mice
[    2.929359] bcm2835-wdt bcm2835-wdt: Broadcom BCM2835 watchdog timer
[    2.935229] sdhci: Secure Digital Host Controller Interface driver
[    2.938172] sdhci: Copyright(c) Pierre Ossman
[    2.941678] mmc-bcm2835 3f300000.mmcnr: could not get clk, deferring probe
[    2.945397] sdhost-bcm2835 3f202000.mmc: could not get clk, deferring probe
[    2.948668] sdhci-pltfm: SDHCI platform and OF driver helper
[    2.953474] ledtrig-cpu: registered to indicate activity on CPUs
[    2.957051] hid: raw HID events driver (C) Jiri Kosina
[    2.960271] usbcore: registered new interface driver usbhid
[    2.963328] usbhid: USB HID core driver
[    2.970842] Initializing XFRM netlink socket
[    2.973896] NET: Registered protocol family 17
[    2.977056] Key type dns_resolver registered
[    2.980516] Registering SWP/SWPB emulation handler
[    2.983643] registered taskstats version 1
[    2.986565] Loading compiled-in X.509 certificates
[    2.990251] Key type ._fscrypt registered
[    2.992980] Key type .fscrypt registered
[    2.995658] Key type fscrypt-provisioning registered
[    3.009397] uart-pl011 3f201000.serial: there is not valid maps for state default
[    3.012412] uart-pl011 3f201000.serial: cts_event_workaround enabled
[    3.015330] 3f201000.serial: ttyAMA0 at MMIO 0x3f201000 (irq = 114, base_baud = 0) is a PL011 rev2
[    4.058104] printk: console [ttyAMA0] enabled
[    4.068521] 3f215040.serial: ttyS0 at MMIO 0x3f215040 (irq = 86, base_baud = 50000000) is a 16550
[    4.084298] bcm2835-power bcm2835-power: Broadcom BCM2835 power domains driver
[    4.096386] mmc-bcm2835 3f300000.mmcnr: mmc_debug:0 mmc_debug2:0
[    4.105503] mmc-bcm2835 3f300000.mmcnr: DMA channel allocated
[    4.141008] sdhost: log_buf @ (ptrval) (f5d13000)
[    4.186449] mmc1: queuing unknown CIS tuple 0x80 (2 bytes)
[    4.194974] mmc0: sdhost-bcm2835 loaded - DMA enabled (>1)
[    4.206454] of_cfs_init
[    4.211927] of_cfs_init: OK
[    4.218615] uart-pl011 3f201000.serial: no DMA platform data
[    4.224605] Indeed it is in host mode hprt0 = 00021501
[    4.232720] Waiting for root device /dev/mmcblk0p2...
[    4.295263] mmc1: queuing unknown CIS tuple 0x80 (3 bytes)
[    4.305395] mmc1: queuing unknown CIS tuple 0x80 (3 bytes)
[    4.314021] random: fast init done
[    4.324936] mmc1: queuing unknown CIS tuple 0x80 (7 bytes)
[    4.355634] mmc0: host does not support reading read-only switch, assuming write-enable
[    4.370165] mmc0: new high speed SD card at address 0002
[    4.379742] mmcblk0: mmc0:0002 S02GF 1.84 GiB
[    4.390668]  mmcblk0: p1 p2
[    4.436531] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[    4.437726] mmc1: new high speed SDIO card at address 0001
[    4.450717] VFS: Mounted root (ext4 filesystem) readonly on device 179:2.
[    4.459287] usb 1-1: new high-speed USB device number 2 using dwc_otg
[    4.470536] devtmpfs: mounted
[    4.479373] Indeed it is in host mode hprt0 = 00001101
[    4.554580] Freeing unused kernel memory: 1024K
[    4.562477] Run /sbin/init as init process
[    4.569440]   with arguments:
[    4.569452]     /sbin/init
[    4.569463]   with environment:
[    4.569474]     HOME=/
[    4.569485]     TERM=linux
[    4.688194] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
[    4.724969] usb 1-1: New USB device found, idVendor=0424, idProduct=2514, bcdDevice= b.b3
[    4.739428] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[    4.751015] hub 1-1:1.0: USB hub found
[    4.758373] hub 1-1:1.0: 4 ports detected
[    4.825341] random: dd: uninitialized urandom read (512 bytes read)
[    5.084570] usb 1-1.1: new high-speed USB device number 3 using dwc_otg
[    5.093732] NET: Registered protocol family 10
[    5.103694] Segment Routing with IPv6
[    5.244864] usb 1-1.1: New USB device found, idVendor=0424, idProduct=2514, bcdDevice= b.b3
[    5.259966] usb 1-1.1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[    5.272028] hub 1-1.1:1.0: USB hub found
[    5.279495] hub 1-1.1:1.0: 3 ports detected
[    5.640217] dwc_otg_handle_wakeup_detected_intr lxstate = 2
[    6.074480] usb 1-1.1.1: new high-speed USB device number 4 using dwc_otg
[    6.225013] usb 1-1.1.1: New USB device found, idVendor=0424, idProduct=7800, bcdDevice= 3.00
[    6.240222] usb 1-1.1.1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[    6.517574] lan78xx 1-1.1.1:1.0 (unnamed net_device) (uninitialized): No External EEPROM. Setting MAC Speed
[    6.536172] libphy: lan78xx-mdiobus: probed
[    6.652730] lan78xx 1-1.1.1:1.0 (unnamed net_device) (uninitialized): int urb period 64
[    7.218754] random: mktemp: uninitialized urandom read (6 bytes read)
[    9.230156] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[   10.839168] random: mktemp: uninitialized urandom read (6 bytes read)
[   12.514498] random: crng init done
#

無事にroot でログインできました。

次回は、hostapd の設定を行います。

ちなみに電源を切るときは

# poweroff

です。