闲的没事再看看Fuchsia

首先就是蛋疼的jiri update,最近恰逢网络维护节,因此托代码很难受。

本来想体验下国内镜像的,但我刚打开tg就看见大家在爱国…

下载

依旧去官网参考教程吧…

git config --global https.proxy 'http://127.0.0.1:7777'
git config --global http.proxy 'http://127.0.0.1:7777'
env http_proxy=http://127.0.0.1:7777 ./.jiri_root/bin/jiri update

安装

首先依旧替换系统python到python2

⋊> /s/w/p/f/fuchsia on dd551fb3e4  sudo rm /usr/bin/python
⋊> /s/w/p/f/fuchsia on dd551fb3e4  sudo ln -s /usr/bin/python2 /usr/bin/python 

执行fx set的时候提示数据收集

⋊> /s/w/p/f/fuchsia on dd551fb3e4  ./scripts/fx set core.x64                                                                                                                                                                                                                        07:32:42
WARNING: Please opt in or out of fx metrics collection.
You will receive this warning until an option is selected.
To check what data we collect, run `fx metrics`
To opt in or out, run `fx metrics <enable|disable>

Generating compile_commands took 1113ms
Done. Made 25631 targets from 2150 files in 29551ms

看一下fx metrics的说明

When enabled, this tool creates a random unique user ID (UUID) for the Fuchsia
repository and uses that to collect the following anonymized metrics of
the session and user workflow in order to improve the user experience:

1. For every "fx" invocation:
   - The output of "uname -rs" (Kernel name and release)
   - The name of the "fx" subcommand invoked
   - (in the future) Imports in BUILDDIR/args.gn for product and board
   - (in the future) Value FUCHSIA_ARCH from BUILDDIR/fx.config

2. For the following subcomand(s) [set]:
   - All arguments passed to the subcommand

3. (in the future) For the following subcomand(s) [set,build]:
   - Time taken to complete
   - Exit status

When metrics collection is disabled, any existing UUID is deleted, and a
new UUID will be created if metrics is later re-enabled.

When enabled, the UUID and the status are stored in /storage/wine/project/fuchsia/fuchsia/.fx-metrics-config

usage:
 fx metrics
      Show what metrics are collected when enabled

 fx metrics status
      Show if metrics collection is enabled or disabled

 fx metrics [--log=LOG_FILE] enable
      Enable metrics collection in fx tools
      --log=<LOG_FILE>  Log all metrics collected to LOG_FILE for
                     debugging purposes. The LOG_FILE setting is persisted
                     in a config file in the /tmp filesystem and will be
                     reset when the user logs out.

 fx metrics disable
      Disable metrics collection in fx tools

说的挺好的,比某些强制要权限的安卓软件强不少,我就启用了吧

⋊> /s/w/p/f/fuchsia on dd551fb3e4  ./scripts/fx metrics enable                                                                                                                                                                                                                      07:37:04
Collection of metrics is now enabled for /storage/wine/project/fuchsia/fuchsia

重新set一下

⋊> /s/w/p/f/fuchsia on dd551fb3e4  ./scripts/fx set core.x64                                                                                                                                                                                                                        07:37:10
Generating compile_commands took 604ms
Done. Made 25631 targets from 2150 files in 11538ms

于是开始编译fx full-build

⋊> /s/w/p/f/fuchsia on dd551fb3e4  time ./scripts/fx full-build
。。。。。。
ERROR: Unknown command full-build
Command exited with non-zero status 1
1.06user 0.38system 0:01.32elapsed 109%CPU (0avgtext+0avgdata 8120maxresident)k
0inputs+0outputs (0major+107996minor)pagefaults 0swaps

好吧,看来编译的脚本改了不少,这命令已经没了,执行fx build,依旧对硬盘要求很大,由于没用固态编译,所以速度还是很慢,link的时候内存上到了10左右(我偶然看到的,没一直看)不足16g内存编译需要swap

⋊> /s/w/p/f/fuchsia on dd551fb3e4  time ./scripts/fx build                                                                                                                                                                                                                          07:40:56
ninja: Entering directory `/storage/wine/project/fuchsia/fuchsia/out/default.zircon'
[39787/39787] STAMP obj/default.stamp
ninja: Entering directory `/storage/wine/project/fuchsia/fuchsia/out/default'
[16270/16358] ACTION //build/images:system_image.manifest(//build/toolchain/fuchsia:x64)
WARNING: no debug file found for ../../prebuilt/third_party/libvulkan_goldfish/x64/libvulkan_goldfish.so
[16358/16358] STAMP obj/default.stamp
26148.47user 1925.21system 1:03:52elapsed 732%CPU (0avgtext+0avgdata 1236300maxresident)k
5097256inputs+14035232outputs (20020major+238362932minor)pagefaults 0swaps

附一张系统状态的图

编译开始的时候系统状态

运行

经过艰难的编译等待(看电影一集),终于要运行了

⋊> /s/w/p/f/fuchsia on 24db8fdf7f ⨯ ./scripts/fx run -m 4096                                                                          09:49:26
ERROR: could not extend fvm, unable to stat fvm image
CMDLINE: TERM=screen-256color kernel.serial=legacy kernel.entropy-mixin=bcb8e29762ee95ba5422aec030bf808a7733262acb36a876425a6ec97746dea5 kernel.halt-on-panic=true
+ exec /storage/wine/project/fuchsia/fuchsia/buildtools/linux-x64/qemu/bin/qemu-system-x86_64 -kernel /storage/wine/project/fuchsia/fuchsia/out/default/../default.zircon/multiboot.bin -initrd /tmp/tmp.hZ2R9qWKFQ/fuchsia-ssh.zbi -m 4096 -nographic -drive file=/tmp/tmp.hZ2R9qWKFQ/fvm.b
lk,format=raw,if=none,id=mydisk -device ich9-ahci,id=ahci -device ide-drive,drive=mydisk,bus=ahci.0 -net none -smp 4,threads=2 -machine q35 -device isa-debug-exit,iobase=0xf4,iosize=0x04 -cpu Haswell,+smap,-check,-fsgsbase -append 'TERM=screen-256color kernel.serial=legacy kernel.ent
ropy-mixin=bcb8e29762ee95ba5422aec030bf808a7733262acb36a876425a6ec97746dea5 kernel.halt-on-panic=true '
qemu-system-x86_64: -drive file=/tmp/tmp.hZ2R9qWKFQ/fvm.blk,format=raw,if=none,id=mydisk: Could not open '/tmp/tmp.hZ2R9qWKFQ/fvm.blk': No such file or directory

emmm,很有意思,怎么又挂了,开始查下坑

./script/fx脚本得知所有像run,build这些命令存放在${fuchsia_dir}/tools/devshell/${cmd},也就是说run命令作为一个脚本放在devshell里面。报错代码位于50行

if [ -n "$IMAGE_FVM_RAW" ]; then
  fvmimg="${imgdir}/fvm.blk"
  args+=(-d -D "${fvmimg}" --diskfmt=raw)
  stat_flags=()
  if [[ $(uname) == "Darwin" ]]; then
    stat_flags+=("-x")
  fi
  stat_output=$(stat "${stat_flags[@]}" "${FUCHSIA_BUILD_DIR}/${IMAGE_FVM_RAW}")
  if [[ "$stat_output" =~ Size:\ ([0-9]+) ]]; then
    size="${BASH_REMATCH[1]}"
    newsize=$(($size * 2))
    # We must take a copy of the build artifact, rather than re-use it, as we
    # need to modify it in order to extend it.
    echo -n "Creating disk image..."
    cp "${FUCHSIA_BUILD_DIR}/${IMAGE_FVM_RAW}" "${fvmimg}"
    "${ZIRCON_TOOLS_DIR}/fvm" "${fvmimg}" extend --length "${newsize}"
    echo "done"
  else
    fx-error "could not extend fvm, unable to stat fvm image"
  fi
fi

为了调试在stat附近添加echo来获得更详细的信息

⋊> /s/w/p/f/fuchsia on 24db8fdf7f ⨯ ./scripts/fx run -m 4096                                                                          09:49:26
stat /storage/wine/project/fuchsia/fuchsia/out/default/obj/build/images/fvm.blk
文件:/storage/wine/project/fuchsia/fuchsia/out/default/obj/build/images/fvm.blk 大小:184713216 块:294176 IO 块:4096 普通文件 设备:812h/2066d Inode:652306 硬链接:1 权限:(0777/-rwxrwxrwx) Uid:( 0/ root) Gid:( 0/ root) 最近访问:2019-06-06 08:46:25.654604100 +0800 最近更改:20
19-06-06 08:46:33.984950900 +0800 最近改动:2019-06-06 08:46:33.984950900 +0800 创建时间:-
ERROR: could not extend fvm, unable to stat fvm image
CMDLINE: TERM=screen-256color kernel.serial=legacy kernel.entropy-mixin=bcb8e29762ee95ba5422aec030bf808a7733262acb36a876425a6ec97746dea5 kernel.halt-on-panic=true
+ exec /storage/wine/project/fuchsia/fuchsia/buildtools/linux-x64/qemu/bin/qemu-system-x86_64 -kernel /storage/wine/project/fuchsia/fuchsia/out/default/../default.zircon/multiboot.bin -initrd /tmp/tmp.hZ2R9qWKFQ/fuchsia-ssh.zbi -m 4096 -nographic -drive file=/tmp/tmp.hZ2R9qWKFQ/fvm.b
lk,format=raw,if=none,id=mydisk -device ich9-ahci,id=ahci -device ide-drive,drive=mydisk,bus=ahci.0 -net none -smp 4,threads=2 -machine q35 -device isa-debug-exit,iobase=0xf4,iosize=0x04 -cpu Haswell,+smap,-check,-fsgsbase -append 'TERM=screen-256color kernel.serial=legacy kernel.ent
ropy-mixin=bcb8e29762ee95ba5422aec030bf808a7733262acb36a876425a6ec97746dea5 kernel.halt-on-panic=true '
qemu-system-x86_64: -drive file=/tmp/tmp.hZ2R9qWKFQ/fvm.blk,format=raw,if=none,id=mydisk: Could not open '/tmp/tmp.hZ2R9qWKFQ/fvm.blk': No such file or directory

这样错误原因就很明显了,我的stat是中文输出的,不匹配正则表达式,因此修改正则,看着好怪异

if [ -n "$IMAGE_FVM_RAW" ]; then
  fvmimg="${imgdir}/fvm.blk"
  args+=(-d -D "${fvmimg}" --diskfmt=raw)
  stat_flags=()
  if [[ $(uname) == "Darwin" ]]; then
    stat_flags+=("-x")
  fi
  stat_output=$(stat "${stat_flags[@]}" "${FUCHSIA_BUILD_DIR}/${IMAGE_FVM_RAW}")
  echo stat "${stat_flags[@]}" "${FUCHSIA_BUILD_DIR}/${IMAGE_FVM_RAW}"
  echo $stat_output
  if [[ "$stat_output" =~ 大小:([0-9]+) ]]; then
    size="${BASH_REMATCH[1]}"
    newsize=$(($size * 2))
    # We must take a copy of the build artifact, rather than re-use it, as we
    # need to modify it in order to extend it.
    echo -n "Creating disk image..."
    cp "${FUCHSIA_BUILD_DIR}/${IMAGE_FVM_RAW}" "${fvmimg}"
    "${ZIRCON_TOOLS_DIR}/fvm" "${fvmimg}" extend --length "${newsize}"
    echo "done"
  else
    fx-error "could not extend fvm, unable to stat fvm image"
  fi
fi

于是就能运行了~~

随手一个ls,然后…

$ ls
/boot/bin/sh: 4: Cannot create child process: -1 (ZX_ERR_INTERNAL): failed to resolve fuchsia-pkg://fuchsia.com/ls#bin/ls

去看代码,这命令真没了o.o按理来说不应该啊

于是想了想,还是把tree再拉过来用下吧,顺便感谢@Patrick(https://blog.simplypatrick.com) 提醒,可以不用修改tree的代码,将BUILD.gn修改成这样

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("tree_config") {
  cflags = [
    "-Wno-discarded-qualifiers",
    "-Wno-implicit-fallthrough",
    "-Wno-sign-compare",
    "-Wno-unused-but-set-parameter",
    "-Wno-incompatible-pointer-types",
  ]
  cflags_c = [
    "-Wno-strict-prototypes",
  ]
}

executable("tree") {
  configs += [ ":tree_config" ]

  sources = [
    "color.c",
    "hash.c",
    "json.c",
    "tree.c",
    "unix.c",
    "file.c",
    "html.c",
    "strverscmp.c",
    "tree.h",
    "xml.c",
  ]

  deps = [
    "$zx/system/ulib/fdio",
  ]
}

运行日志https://gist.github.com/feilongfl/51b4b6dc26d6a60e53f61ad5069d2ed4

fuchsia过了几天之后的重新编译

前两天挂了一块硬盘,本来数据是没丢的,但是为了快速清理空间,我随手把fuchsia删了,于是说重新下载编译。

按照之前的教程,输入fx set x64会出现错误:

⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx set x64                                                                             23:21:55
ERROR: Unable to parse PRODUCT.BOARD goal "x64"
ERROR: You must specify which product and which board you wish to build.
ERROR: Example:
ERROR:   fx set core.x64
ERROR:
ERROR: Run "fx list-products" to see the available products.
ERROR: Run "fx list-boards" to see the available boards.
ERROR: Run "fx set --help" to see full usage.
⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯                                                                                                  23:21:58
⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx set core.x64                                                                        23:22:00
+ /storage/wine/project/fuchsia/fuchsia/zircon/prebuilt/downloads/gn gen /storage/wine/project/fuchsia/fuchsia/out/build-zircon --root=/storage/wine/project/fuchsia/fuchsia/zircon                                                                                                        
Done. Made 12811 targets from 924 files in 5950ms
Done. Made 20355 targets from 1898 files in 90543ms

解决方法也说的很明白,直接换命令。

看一下貌似对于编译有了更为详细的设定

⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx list-products                                                                                                                                                                                                                      23:26:50
bringup
core
*
router
sdk_image
speaker
terminal
workstation
⋊> /s/w/p/f/fuchsia on 547e5edc54 ⨯ ./scripts/fx list-boards                                                                                                                                                                                                                        23:27:01
arm64
cleo
eagle
*
hikey960
imx8mmevk
kirin970
msm8x53-som
mt8167s_ref
toulouse
vim2
x64

fuchsia df 命令小bug

今天翻了翻fuchsia的代码,翻到df命令的时候发现不愧是google大佬,容量单位都到了EB。但是比较有趣的是他们两段代码貌似不是一个人写的,出现了低级错误:)

EB丢了,8成不是一个人写的…

这几天看了Google的c代码,发现goto命令也没有书(时间太久远了,不记得谁写的,说尽量不goto来着)里面说的那么恐怖,用得好的话代码还是很简洁的。

为fuchsia添加tree命令

节约时间,修改后的tree版本传送门,https://github.com/feilongfl/tree-for-fuchsia

高高兴兴的把tree下载下来发现根本不能用:)

http://mama.indstate.edu/users/ice/tree/ 下载最新(2019.3.4)版本1.8

放在//zircon/system/uapp/tree下,并在uapp下的BUILD.gn添加tree

在tree目录下建立编译文件,忽略各种warrning,然后改掉tree里面各种空括号函数()=>(void)

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("tree_config") {
  cflags = [
    "-Wno-discarded-qualifiers",
    "-Wno-implicit-fallthrough",
    "-Wno-sign-compare",
    "-Wno-unused-but-set-parameter",
    "-Wno-incompatible-pointer-types"
  ]
}

executable("tree") {
  configs += [ ":tree_config" ]

  sources = [
    "color.c",
    "hash.c",
    "json.c",
    "tree.c",
    "unix.c",
    "file.c",
    "html.c",
    "strverscmp.c",
    "tree.h",
    "xml.c",
  ]
}

开始编译…….顺利通过

高高兴兴在系统中打开tree命令,顺利的看到崩溃堆栈(手动滑稽),什么鬼…

今天(2019.3.4)太晚了,随手搞搞,先开个坑

开始查坑

首先早上(2019.3.5)起来先写了个helloworld试试水,毕竟这才是正路啊~~

#include <stdio.h>

int main(int argc, const char** argv) {
  printf("Hello world!\n");
  printf("get arg nums [%d]\n",argc);
  for (int i = 0;i < argc;i++) {
    printf("args [%d] => %s\n", argc, argv[i]);
  }
  return 0;
}
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("helloworld_config") {
  cflags = [
  ]
}

executable("helloworld") {
  configs += [ ":helloworld_config" ]

  sources = [
    "helloworld.c",
  ]
}

然后编译运行,之后竟然也崩溃了。。。不是吧,c语言我还是很有自信的…

于是开始看别的包,打开i2c的包,发现怎么还有个deps

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

executable("i2c") {
  sources = [
    "i2c.c",
  ]
  deps = [
    "$zx/system/fidl/fuchsia-hardware-i2c:c",
    "$zx/system/ulib/fdio",
    "$zx/system/ulib/zircon",
  ]
}

第一个怎么看都是i2c硬件相关,helloworld用不到,中间那个fdio让人联想到file device i/o,最后那个肯定是系统了,但是helloworld跟系统貌似没什么太大关系,于是说只添加fdio尝试一下,可以运行了

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("helloworld_config") {
  cflags = [
  ]
}

executable("helloworld") {
  configs += [ ":helloworld_config" ]

  sources = [
    "helloworld.c",
  ]

  deps = [
    "$zx/system/ulib/fdio",
  ]
}
所以说c语言helloworld我还是能不调试一次写成功的(笑)

这样的话问题就比较清晰了,之前的tree命令没有依赖项。使用输入输出必须添加fdio依赖

google对fdio的说明

FDIO是一个客户端库,它通过紫红色系统接口中的底层fuchsia.io协议提供类似POSIX的抽象。FDIO is a client library that provides a POSIX-like abstraction over the underlying fuchsia.io protocol in the Fuchsia System Interface.

解决tree

添加fdio依赖即可运行(我已经修正github仓库)

# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

config("tree_config") {
  cflags = [
    "-Wno-discarded-qualifiers",
    "-Wno-implicit-fallthrough",
    "-Wno-sign-compare",
    "-Wno-unused-but-set-parameter",
    "-Wno-incompatible-pointer-types"
  ]
}

executable("tree") {
  configs += [ ":tree_config" ]

  sources = [
    "color.c",
    "hash.c",
    "json.c",
    "tree.c",
    "unix.c",
    "file.c",
    "html.c",
    "strverscmp.c",
    "tree.h",
    "xml.c",
  ]

  deps = [
    "$zx/system/ulib/fdio",
  ]
}

然后就可以查看fuchsia里面到底是什么目录结构了

想直接查看的朋友移步 https://gist.github.com/feilongfl/1a0d564b85d4de3747af9820abcc82fd

fuchsia编译记录

本文主要参考自 https://fuchsia-china.com/guide-of-fuchsia-os-compilation/zh-cn/

我的开发环境是manjaro,基于archlinux,所以一些命令与ubuntu不同。

关于下载代码的相关说明

1. 由于fuchsia托管在google,所以下载很麻烦。开始使用electron-ssr作为http代理,但是这个软件经常会弹出提示框然后打断网络。因此建议使用cow做socks转http,可以比较稳定下载。

2. 由于使用了python2所以需要对/usr/bin/python的符号链接进行替换,指向python2

3. 由于脚本限制,代理服务器需要写成http://127.0.0.1:7777的形式,不然在python简本中会报错

4. 有时候出现ERROR: Project integration(integration) contains uncommited changes 可以删除那个文件夹然后重新执行jiri update 然后你会发现还报这个错误(我没找到解决方法。。。)

5. 截至目前(2019.3.4)fuchsia源码大小20G

下载后各目录大小

开始编译

编译很快,链接超级慢,不知道为什么连接时候根本没有并发,cpu基本空载,内存貌似没上过8,保险起见建议小于16g内存用户开启swap

编译开始

如果使用固态编译应该会快不少,由于硬盘都存电影了,只能找了个闲置笔记本硬盘还是usb3连接的,速度比较慢,从netdata可以看到明显的io等待,不过没办法,还有种子要下(手动滑稽),估计连接也是卡在io上了,于是电脑慢慢编译,我去睡觉了,明早再看结果把。

emmm,临睡觉前编译完了:)完了今天不能睡觉了

[feilong@feilong-dev fuchsia]$ time ./scripts/fx full-build
ninja: Entering directory `/mnt/fuchsia/fuchsia/out/build-zircon'
[13573/13573] STAMP obj/manifest-x64.stamp
ninja: Entering directory `/mnt/fuchsia/fuchsia/out/x64'
[1/1] Regenerating ninja files
[9326/9326] STAMP obj/build/gn/default.stamp

real    132m37.180s
user    186m42.420s
sys     15m26.925s

编译了好久

运行

我内存也比较多,所以也4g扔出去:)

后来看了下运行代码,默认2g,推荐-k 参数,启用kvm