-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenwrt.html
1088 lines (981 loc) · 86.8 KB
/
openwrt.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html><head>
<title>openwrt</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<style>
/**
* prism.js Github theme based on GitHub's theme.
* @author Sam Clarke
*/
code[class*="language-"],
pre[class*="language-"] {
color: #333;
background: none;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
word-wrap: normal;
line-height: 1.4;
-moz-tab-size: 8;
-o-tab-size: 8;
tab-size: 8;
-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}
/* Code blocks */
pre[class*="language-"] {
padding: .8em;
overflow: auto;
/* border: 1px solid #ddd; */
border-radius: 3px;
/* background: #fff; */
background: #f5f5f5;
}
/* Inline code */
:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
white-space: normal;
background: #f5f5f5;
}
.token.comment,
.token.blockquote {
color: #969896;
}
.token.cdata {
color: #183691;
}
.token.doctype,
.token.punctuation,
.token.variable,
.token.macro.property {
color: #333;
}
.token.operator,
.token.important,
.token.keyword,
.token.rule,
.token.builtin {
color: #a71d5d;
}
.token.string,
.token.url,
.token.regex,
.token.attr-value {
color: #183691;
}
.token.property,
.token.number,
.token.boolean,
.token.entity,
.token.atrule,
.token.constant,
.token.symbol,
.token.command,
.token.code {
color: #0086b3;
}
.token.tag,
.token.selector,
.token.prolog {
color: #63a35c;
}
.token.function,
.token.namespace,
.token.pseudo-element,
.token.class,
.token.class-name,
.token.pseudo-class,
.token.id,
.token.url-reference .token.variable,
.token.attr-name {
color: #795da3;
}
.token.entity {
cursor: help;
}
.token.title,
.token.title .token.punctuation {
font-weight: bold;
color: #1d3e81;
}
.token.list {
color: #ed6a43;
}
.token.inserted {
background-color: #eaffea;
color: #55a532;
}
.token.deleted {
background-color: #ffecec;
color: #bd2c00;
}
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}
/* JSON */
.language-json .token.property {
color: #183691;
}
.language-markup .token.tag .token.punctuation {
color: #333;
}
/* CSS */
code.language-css,
.language-css .token.function {
color: #0086b3;
}
/* YAML */
.language-yaml .token.atrule {
color: #63a35c;
}
code.language-yaml {
color: #183691;
}
/* Ruby */
.language-ruby .token.function {
color: #333;
}
/* Markdown */
.language-markdown .token.url {
color: #795da3;
}
/* Makefile */
.language-makefile .token.symbol {
color: #795da3;
}
.language-makefile .token.variable {
color: #183691;
}
.language-makefile .token.builtin {
color: #0086b3;
}
/* Bash */
.language-bash .token.keyword {
color: #0086b3;
}
/* highlight */
pre[data-line] {
position: relative;
padding: 1em 0 1em 3em;
}
pre[data-line] .line-highlight-wrapper {
position: absolute;
top: 0;
left: 0;
background-color: transparent;
display: block;
width: 100%;
}
pre[data-line] .line-highlight {
position: absolute;
left: 0;
right: 0;
padding: inherit 0;
margin-top: 1em;
background: hsla(24, 20%, 50%,.08);
background: linear-gradient(to right, hsla(24, 20%, 50%,.1) 70%, hsla(24, 20%, 50%,0));
pointer-events: none;
line-height: inherit;
white-space: pre;
}
pre[data-line] .line-highlight:before,
pre[data-line] .line-highlight[data-end]:after {
content: attr(data-start);
position: absolute;
top: .4em;
left: .6em;
min-width: 1em;
padding: 0 .5em;
background-color: hsla(24, 20%, 50%,.4);
color: hsl(24, 20%, 95%);
font: bold 65%/1.5 sans-serif;
text-align: center;
vertical-align: .3em;
border-radius: 999px;
text-shadow: none;
box-shadow: 0 1px white;
}
pre[data-line] .line-highlight[data-end]:after {
content: attr(data-end);
top: auto;
bottom: .4em;
}html body{font-family:"Helvetica Neue",Helvetica,"Segoe UI",Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ul,html body>ol{margin-bottom:16px}html body ul,html body ol{padding-left:2em}html body ul.no-list,html body ol.no-list{padding:0;list-style-type:none}html body ul ul,html body ul ol,html body ol ol,html body ol ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;background-color:#f0f0f0;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:bold;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:bold}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em !important;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::before,html body code::after{letter-spacing:-0.2em;content:"\00a0"}html body pre>code{padding:0;margin:0;font-size:.85em !important;word-break:normal;white-space:pre;background:transparent;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;font-size:.85em !important;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:before,html body pre tt:before,html body pre code:after,html body pre tt:after{content:normal}html body p,html body blockquote,html body ul,html body ol,html body dl,html body pre{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body pre,html body code{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview .pagebreak,.markdown-preview .newpage{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center !important}.markdown-preview:not([for="preview"]) .code-chunk .btn-group{display:none}.markdown-preview:not([for="preview"]) .code-chunk .status{display:none}.markdown-preview:not([for="preview"]) .code-chunk .output-div{margin-bottom:16px}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0}@media screen and (min-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{font-size:14px !important;padding:1em}}@media print{html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,0.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{padding:0 1.6em;margin-top:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc li{margin-bottom:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{list-style-type:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% - 300px);padding:2em calc(50% - 457px - 150px);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/* https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */
</style>
</head>
<body for="html-export">
<div class="mume markdown-preview ">
<h1 class="mume-header" id="openwrt%E5%85%A5%E9%97%A8">OpenWrt入门</h1>
<p>记录了OpenWrt的入门步骤和踩的一些坑。</p>
<ul>
<li><a href="#openwrt%E5%85%A5%E9%97%A8">OpenWrt入门</a>
<ul>
<li><a href="#%E5%9B%BA%E4%BB%B6%E7%BC%96%E8%AF%91helloworldhelloworld">固件编译Helloworld</a>
<ul>
<li><a href="#%E6%9E%84%E5%BB%BA%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83">构建编译环境</a></li>
<li><a href="#%E8%8E%B7%E5%8F%96openwrt%E7%9A%84%E6%BA%90%E4%BB%A3%E7%A0%81">获取OpenWrt的源代码</a></li>
<li><a href="#%E6%9B%B4%E6%96%B0%E5%92%8C%E5%AE%89%E8%A3%85feeds">更新和安装<code>feeds</code></a></li>
<li><a href="#%E8%AE%BE%E7%BD%AE%E7%BC%96%E8%AF%91%E9%80%89%E9%A1%B9">设置编译选项</a>
<ul>
<li><a href="#%E5%AE%89%E8%A3%85luci">安装LuCI</a></li>
</ul>
</li>
<li><a href="#%E7%BC%96%E8%AF%91">编译</a></li>
<li><a href="#%E7%83%A7%E5%86%99">烧写</a>
<ul>
<li><a href="#%E4%BD%BF%E7%94%A8u-boot-tftp%E7%83%A7%E5%86%99">使用u-boot + TFTP烧写</a></li>
</ul>
</li>
<li><a href="#%E5%B7%B2%E7%9F%A5%E9%97%AE%E9%A2%98">已知问题</a></li>
</ul>
</li>
<li><a href="#%E5%86%99%E4%B8%80%E4%B8%AAinitd%E8%84%9A%E6%9C%AC">写一个init.d脚本</a>
<ul>
<li><a href="#sysv%E9%A3%8E%E6%A0%BCsysv_styleopenwrt_doc_initscripts">sysv风格</a></li>
<li><a href="#procd%E9%A3%8E%E6%A0%BCprocd_styleopenwrt_wiki_procd_style">procd风格</a></li>
<li><a href="#%E5%9C%A8%E7%BC%96%E8%AF%91%E6%97%B6%E5%8C%85%E5%90%AB%E8%84%9A%E6%9C%AC">在编译时包含脚本</a></li>
</ul>
</li>
<li><a href="#openwrt%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">OpenWrt目录结构</a></li>
<li><a href="#uci">UCI</a>
<ul>
<li><a href="#uci%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6">UCI配置文件</a></li>
<li><a href="#uci%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%B7%A5%E5%85%B7">UCI命令行工具</a></li>
</ul>
</li>
<li><a href="#luci">LuCI</a>
<ul>
<li><a href="#%E5%85%B3%E4%BA%8E%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83">关于开发环境</a></li>
<li><a href="#luci-app%E5%BC%80%E5%8F%91helloworldluci_module_helloworld">LuCI app开发Helloworld</a>
<ul>
<li><a href="#%E5%88%9B%E5%BB%BA%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">创建目录结构</a></li>
</ul>
</li>
<li><a href="#dispatching-tree">dispatching tree</a></li>
<li><a href="#%E7%BB%93%E6%9E%84">结构</a>
<ul>
<li><a href="#%E4%BB%93%E5%BA%93%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">仓库目录结构</a></li>
<li><a href="#module%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">module目录结构</a></li>
<li><a href="#%E6%9D%BF%E4%B8%8A%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">板上目录结构</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#%E6%90%AD%E5%BB%BAvmware%E8%BF%90%E8%A1%8C%E7%9A%84x86-openwrt">搭建VMware运行的x86 OpenWrt</a>
<ul>
<li><a href="#%E7%BC%96%E8%AF%91%E5%9B%BA%E4%BB%B6">编译固件</a></li>
<li><a href="#%E8%AE%BE%E7%BD%AE%E8%99%9A%E6%8B%9F%E6%9C%BA">设置虚拟机</a></li>
</ul>
</li>
<li><a href="#%E4%BD%BF%E7%94%A8docker%E5%9C%A8%E9%AB%98%E7%89%88%E6%9C%ACubuntu%E4%B8%8B%E7%BC%96%E8%AF%91%E6%97%A7%E7%89%88%E6%9C%ACopenwrt">使用docker在高版本Ubuntu下编译旧版本OpenWrt</a>
<ul>
<li><a href="#%E5%AE%89%E8%A3%85docker">安装docker</a></li>
<li><a href="#%E9%85%8D%E7%BD%AE%E9%95%9C%E5%83%8F%E5%92%8C%E5%85%B6%E4%B8%AD%E7%9A%84%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83">配置镜像和其中的编译环境</a></li>
<li><a href="#%E5%9C%A8%E5%AE%B9%E5%99%A8%E4%B8%AD%E7%BC%96%E8%AF%91openwrt">在容器中编译OpenWrt</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 class="mume-header" id="%E5%9B%BA%E4%BB%B6%E7%BC%96%E8%AF%91helloworldhelloworld">固件编译Helloworld<sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup></h2>
<h3 class="mume-header" id="%E6%9E%84%E5%BB%BA%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83">构建编译环境</h3>
<p>操作系统:Ubuntu 20.04 LTS<br>
开发板:JS9331开发板</p>
<p>可以先更换国内源。</p>
<p>安装依赖:</p>
<pre data-role="codeBlock" data-info class="language-"><code>sudo apt-get install subversion g++ zlib1g-dev build-essential git python rsync man-db
sudo apt-get install libncurses5-dev gawk gettext unzip file libssl-dev wget zip time
</code></pre><h3 class="mume-header" id="%E8%8E%B7%E5%8F%96openwrt%E7%9A%84%E6%BA%90%E4%BB%A3%E7%A0%81">获取OpenWrt的源代码</h3>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">git</span> clone https://git.openwrt.org/openwrt/openwrt.git/
<span class="token builtin class-name">cd</span> openwrt
</pre><ul>
<li>如果无法从地址<code>git clone</code>,可以访问镜像:</li>
</ul>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">git</span> clone https://github.com/openwrt/openwrt.git
</pre><p>切换到指定的版本,首先查看可用的版本号:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">git</span> tag
</pre><p>切换到<code>v17.01.7</code></p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">git</span> checkout v17.01.7
</pre><h3 class="mume-header" id="%E6%9B%B4%E6%96%B0%E5%92%8C%E5%AE%89%E8%A3%85feeds">更新和安装<code>feeds</code></h3>
<p>切换到<code>./scripts</code>文件夹下:</p>
<pre data-role="codeBlock" data-info class="language-"><code>cd scripts
./feeds update -a
./feeds install -a
</code></pre><blockquote>
<p><code>feeds install</code>使得下载的包在<code>make menuconfig</code>中可用。</p>
</blockquote>
<ul>
<li>在<code>./feeds update -a</code>时遇到端口443拒接访问:</li>
</ul>
<pre data-role="codeBlock" data-info="bash" class="language-bash">fatal: 无法访问 <span class="token string">'https://git.lede-project.org/feed/packages.git/'</span>:
Failed to connect to git.lede-project.org port <span class="token number">443</span>: 拒绝连接
</pre><p>可以在根目录的<code>feeds.conf.default</code>中把所有的<code>https://</code>改为<code>git://</code>。</p>
<h3 class="mume-header" id="%E8%AE%BE%E7%BD%AE%E7%BC%96%E8%AF%91%E9%80%89%E9%A1%B9">设置编译选项</h3>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">make</span> menuconfig
</pre><h4 class="mume-header" id="%E5%AE%89%E8%A3%85luci">安装LuCI</h4>
<p>如果上一步<code>./feeds install -a</code>正确,那<code>menuconfig</code>里应该会多出<code>LuCI</code>的选项。</p>
<p>在<code>LuCI -> 1. Collections</code>里勾选:</p>
<ul>
<li>luci</li>
<li>luci-ssl-openssl</li>
</ul>
<p>要添加中文支持,在<code>LuCI -> 2. Modules -> Translation</code>里勾选:</p>
<ul>
<li>Chinese</li>
</ul>
<blockquote>
<p>发现这么设置后无论是否勾选<code>3. Applications -> luci-app-uhttpd</code>最终生成的固件中都会包含<code>httpd</code></p>
</blockquote>
<h3 class="mume-header" id="%E7%BC%96%E8%AF%91">编译</h3>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">make</span>
</pre><p>如果报错了,使用</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">make</span> -j1 <span class="token assign-left variable">V</span><span class="token operator">=</span>s
</pre><p>查看具体报错信息。</p>
<p>生成的<code>.bin</code>文件位于<code>.bin/targets/</code>中,对应<code>menuconfig</code>里面选择的target。</p>
<p>如果出现了各种:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">check_data_file_clashes: Package libustream-openssl20150806 wants to <span class="token function">install</span> <span class="token function">file</span> /home/b/Desktop/test_openwrt/openwrt/build_dir/target-i386_pentium4_musl/root-x86/lib/libustream-ssl.so
But that <span class="token function">file</span> is already provided by package * libustream-mbedtls20150806
</pre><p>可以检查<code>menuconfig</code>的选项,往往是配置有问题,实在不行只能删掉配置、清空文件然后重新编译。由于第一次编译时间实在是太长了,我们不喜欢这么干。</p>
<pre data-role="codeBlock" data-info class="language-"><code>mv .config .config.bak
make clean
</code></pre><h3 class="mume-header" id="%E7%83%A7%E5%86%99">烧写</h3>
<h4 class="mume-header" id="%E4%BD%BF%E7%94%A8u-boot-tftp%E7%83%A7%E5%86%99">使用u-boot + TFTP烧写</h4>
<h3 class="mume-header" id="%E5%B7%B2%E7%9F%A5%E9%97%AE%E9%A2%98">已知问题</h3>
<p><code>OpenWrt 19.07.7</code>编译会报警告</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">WARNING: Makefile <span class="token string">'package/feeds/packages/ksmbd/Makefile'</span> has a dependency on <span class="token string">'kmod-crypto-arc4'</span>, <span class="token function">which</span> does not exist
</pre><p>但是实际不影响编译和使用,原因未知。</p>
<h2 class="mume-header" id="%E5%86%99%E4%B8%80%E4%B8%AAinitd%E8%84%9A%E6%9C%AC">写一个init.d脚本</h2>
<h3 class="mume-header" id="sysv%E9%A3%8E%E6%A0%BCsysv_styleopenwrt_doc_initscripts">sysv风格<sup class="footnote-ref"><a href="#fn2" id="fnref2">[2]</a></sup><sup class="footnote-ref"><a href="#fn3" id="fnref3">[3]</a></sup></h3>
<p>在<code>/etc/init.d/</code>下的脚本在系统启动时会被调用,脚本的格式如下</p>
<p>假设文件为<code>/etc/init.d/example</code>,内容是:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token shebang important">#!/bin/sh /etc/rc.common</span>
<span class="token comment"># Example script</span>
<span class="token assign-left variable">START</span><span class="token operator">=</span><span class="token number">10</span>
<span class="token assign-left variable">STOP</span><span class="token operator">=</span><span class="token number">15</span>
<span class="token function-name function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token builtin class-name">echo</span> start
<span class="token comment"># commands to launch application</span>
<span class="token punctuation">}</span>
<span class="token function-name function">stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token builtin class-name">echo</span> stop
<span class="token comment"># commands to kill application </span>
<span class="token punctuation">}</span>
</pre><p>第一行指明使用<code>/bin/sh /etc/rc.common</code>作为脚本解释器并在执行脚本前调用<code>main</code>和检查脚本。</p>
<blockquote>
<p><code>#!</code>称作“Shebang”,用于指明执行这个脚本文件的解释器:</p>
<ol>
<li>如果脚本文件中没有#!这一行,那么执行时会默认采用当前Shell去解释这个脚本(即:$SHELL环境变量)。</li>
<li>如果<code>#!</code>之后的解释程序是一个可执行文件,那么执行这个脚本时,它就会把文件名及其参数一起作为参数传给那个解释程序去执行。</li>
<li>如果<code>#!</code>指定的解释程序没有可执行权限,则会报错<code>bad interpreter: Permission denied</code>。如果<code>#!</code>指定的解释程序不是一个可执行文件,那么指定的解释程序会被忽略,转而交给当前的SHELL去执行这个脚本。</li>
<li>如果<code>#!</code>指定的解释程序不存在,那么会报错<code>bad interpreter: No such file or directory</code>。注意:<code>#!</code>之后的解释程序,需要写其绝对路径(如:<code>#!/bin/bash</code>),它是不会自动到<code>$PATH</code>中寻找解释器的。</li>
<li>当然,如果你使用类似于<code>bash test.sh</code>这样的命令来执行脚本,那么<code>#!</code>这一行将会被忽略掉,解释器当然是用命令行中显式指定的bash。</li>
<li>脚本文件必须拥有可执行权限。</li>
</ol>
</blockquote>
<p><code>START=10</code>和<code>STOP=15</code>分别指定脚本启动时的次序,分别对应<code>/etc/rc.d</code>中软链接的名字,比如这里会对应<code>/etc/rc.d/S10example</code>和<code>/etc/rc.d/K15example</code>。</p>
<p><code>start()</code>和<code>stop()</code>是脚本的方法,公用的 init script 方法有:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">start <span class="token comment"># 启动服务</span>
stop <span class="token comment"># 停止服务</span>
restart <span class="token comment"># 重启服务</span>
reload <span class="token comment"># 重新载入配置文件, 如果失败则重启</span>
<span class="token builtin class-name">enable</span> <span class="token comment"># 启用开机自启动, 实际上是在/etc/rc.d/下创建S??和K??开头的软链</span>
disable <span class="token comment"># 禁用开机自启动, 实际上是删除/etc/rc.d/下对应的软链</span>
</pre><p>其中<code>start()</code>和<code>stop()</code>是必须的,缺少了会导致不工作。</p>
<h3 class="mume-header" id="procd%E9%A3%8E%E6%A0%BCprocd_styleopenwrt_wiki_procd_style">procd风格<sup class="footnote-ref"><a href="#fn4" id="fnref4">[4]</a></sup><sup class="footnote-ref"><a href="#fn5" id="fnref5">[5]</a></sup></h3>
<p>Procd:Openwrt的进程管理守护进程(process management daemon),它与初始化脚本(/etc/init.d/*)保持联系,当进程相关联的配置文件更改时,会触发procd去重启或启动脚本。Procd代替了原本的Hotplug2。</p>
<p>主要区别是:</p>
<ol>
<li>
<p>shebang不同:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token shebang important">#!/bin/sh /etc/rc.common</span>
</pre></li>
<li>
<p>需要指明<code>USE_PROCD=1</code>:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token assign-left variable">USE_PROCD</span><span class="token operator">=</span><span class="token number">1</span>
</pre></li>
<li>
<p>主要包含<code>start_service()</code>和<code>stop_service()</code>函数</p>
</li>
</ol>
<h3 class="mume-header" id="%E5%9C%A8%E7%BC%96%E8%AF%91%E6%97%B6%E5%8C%85%E5%90%AB%E8%84%9A%E6%9C%AC">在编译时包含脚本</h3>
<p>在<code>./target/linux/<arch_name>/base-files/</code>下的文件会包含在生成的固件中的对应位置,只要将写好的脚本置于此处,OpenWrt会在构建中(使用<code>enable()</code>及<code>disable()</code>)使其生效<sup class="footnote-ref"><a href="#fn3" id="fnref3:1">[3:1]</a></sup>,在编译后生成的固件就能自动调用启动脚本。</p>
<blockquote>
<p>可能需要<code>chmod 777</code></p>
</blockquote>
<h2 class="mume-header" id="openwrt%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">OpenWrt目录结构</h2>
<p><img src="openwrt/dir_structure.png" alt></p>
<p>如图,第一行是原始目录(由<code>Git</code>管理的 + 下载的),第二行是编译生成的目录,作用分别是:</p>
<table>
<thead>
<tr>
<th style="text-align:center">目录名</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">tools</td>
<td style="text-align:center">编译时需要一些工具, tools里包含了获取和编译这些工具的命令。里面是一些Makefile,有的可能还有patch。每个Makefile里都有一句 <code>$(eval $(call HostBuild))</code>,表示编译这个工具是为了在主机上使用的。</td>
</tr>
<tr>
<td style="text-align:center">toolchain</td>
<td style="text-align:center">包含一些命令去获取kernel headers, C library, bin-utils, compiler, debugger</td>
</tr>
<tr>
<td style="text-align:center">target</td>
<td style="text-align:center">各平台在这个目录里定义了firmware和kernel的编译过程。</td>
</tr>
<tr>
<td style="text-align:center">package</td>
<td style="text-align:center">包含针对各个软件包的Makefile。openwrt定义了一套Makefile模板,各软件参照这个模板定义了自己的信息,如软件包的版本、下载地址、编译方式、安装地址等。</td>
</tr>
<tr>
<td style="text-align:center">include</td>
<td style="text-align:center">openwrt的Makefile都存放在这里。</td>
</tr>
<tr>
<td style="text-align:center">scripts</td>
<td style="text-align:center">一些perl脚本,用于软件包管理。</td>
</tr>
<tr>
<td style="text-align:center">dl</td>
<td style="text-align:center">软件包下载后都放到这个目录里</td>
</tr>
<tr>
<td style="text-align:center">build_dir</td>
<td style="text-align:center">软件包都解压到build_dir/里,然后在此编译</td>
</tr>
<tr>
<td style="text-align:center">staging_dir</td>
<td style="text-align:center">最终安装目录。tools, toolchain被安装到这里,rootfs也会放到这里。</td>
</tr>
<tr>
<td style="text-align:center">feeds</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">bin</td>
<td style="text-align:center">编译完成之后,firmware和各ipk会放到此目录下。</td>
</tr>
</tbody>
</table>
<h2 class="mume-header" id="uci">UCI</h2>
<h3 class="mume-header" id="uci%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6">UCI配置文件</h3>
<p>UCI(Unified Configuration Interface)是OpenWrt中所有系统的统一配置接口,当应用提供UCI接口时,用户只需通过UCI配置文件进行配置,而不用处理各应用各不相同的分散的配置文件。UCI配置文件位于<code>/etc/config/</code>下,用户通过文本编辑器直接编辑,或者使用C/Lua/Shell的UCI工具配置,之后执行<code>/etc/init.d/</code>下的对应脚本即可完成一个配置。</p>
<p>比如Samba/CIFS程序,其原配置文件是在<code>/etc/samba/smb.conf</code>,而对应的UCI文件是<code>/etc/config/samba</code>,当<code>/etc/config/samba文</code>件被修改了之后,需要运行一次</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">/etc/init.d/samba start
</pre><h3 class="mume-header" id="uci%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%B7%A5%E5%85%B7">UCI命令行工具</h3>
<p>以<code>/etc/config/network</code>为例,其原内容为:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">config interface <span class="token string">'loopback'</span>
option ifname <span class="token string">'lo'</span>
option proto <span class="token string">'static'</span>
option ipaddr <span class="token string">'127.0.0.1'</span>
option netmask <span class="token string">'255.0.0.0'</span>
config globals <span class="token string">'globals'</span>
option ula_prefix <span class="token string">'fd89:5984:253b::/48'</span>
config interface <span class="token string">'lan'</span>
option <span class="token builtin class-name">type</span> <span class="token string">'bridge'</span>
option ifname <span class="token string">'eth1'</span>
option proto <span class="token string">'static'</span>
option ipaddr <span class="token string">'192.168.1.1'</span>
option netmask <span class="token string">'255.255.255.0'</span>
option ip6assign <span class="token string">'60'</span>
config interface <span class="token string">'wan'</span>
option ifname <span class="token string">'eth0'</span>
option proto <span class="token string">'dhcp'</span>
config interface <span class="token string">'wan6'</span>
option ifname <span class="token string">'eth0'</span>
option proto <span class="token string">'dhcpv6'</span>
</pre><p>可以发现一个<strong>config</strong>文件由若干个<strong>section</strong>组成,每个<strong>section</strong>又含有若干不同值的<strong>option</strong>。</p>
<p>大致结构为:</p>
<ul>
<li>config
<ul>
<li>section / list[section]
<ul>
<li>option / list[option]</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>如果我们想要添加的内容是:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">config route <span class="token string">'custom_static_route'</span>
option target <span class="token string">'1.1.1.1'</span>
option gateway <span class="token string">'1.1.1.1'</span>
option interface <span class="token string">'lan'</span>
</pre><p>可以使用</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">uci <span class="token builtin class-name">set</span> network.custom_static_route<span class="token operator">=</span>route
uci <span class="token builtin class-name">set</span> network.custom_static_route.target<span class="token operator">=</span><span class="token number">1.1</span>.1.1
uci <span class="token builtin class-name">set</span> network.custom_static_route.gateway<span class="token operator">=</span><span class="token number">1.1</span>.1.1
uci <span class="token builtin class-name">set</span> network.custom_static_route.interface<span class="token operator">=</span>lan
uci commit
</pre><p>所有<code>uci set</code>和<code>uci add</code>只会暂存更改,使用<code>uci commit</code>后才会正式提交到UCI文件中。特别注意,当创建section和创建option时参数的含义不同:</p>
<ul>
<li>创建section时,等号前为section的自定义名称,等号后为section的类型</li>
<li>创建option时,等号前是option的属性名,等号后为值</li>
</ul>
<blockquote>
<p>通过LuCI页面创建的section往往无命名,在UCI文件里会形如:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">config route
option target <span class="token string">'1.1.1.1'</span>
option gateway <span class="token string">'1.1.1.1'</span>
option interface <span class="token string">'lan'</span>
config route
option target <span class="token string">'2.2.2.2'</span>
option gateway <span class="token string">'2.2.2.2'</span>
option interface <span class="token string">'lan'</span>
</pre><p>这个时候得用list方式分别访问这两个section,<code>uci show network</code>得到的相关部分内容会是:</p>
<pre data-role="codeBlock" data-info class="language-"><code>network.@route[0]=route
network.@route[0].target='1.1.1.1'
network.@route[0].gateway='1.1.1.1'
network.@route[0].interface='lan'
network.@route[1]=route
network.@route[1].target='2.2.2.2'
network.@route[1].gateway='2.2.2.2'
network.@route[1].interface='lan'
</code></pre><p>这里的<code>@foo[i]</code>是访问list形式的section或者option的方法。</p>
</blockquote>
<h2 class="mume-header" id="luci">LuCI</h2>
<p>UCI(Unified Configuration Interface)是OpenWrt中所有系统的统一配置接口,LuCI(Lua Configuration Interface)是使用LuCI开发的供用户使用的一套配置界面</p>
<h3 class="mume-header" id="%E5%85%B3%E4%BA%8E%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83">关于开发环境</h3>
<p>根据官方文档<sup class="footnote-ref"><a href="#fn6" id="fnref6">[6]</a></sup>,LuCI的开发方式主要有两种:</p>
<ol>
<li>部署环境开发</li>
<li>开发环境开发</li>
</ol>
<p>前者即直接在烧写好了OpenWrt的板上进行代码编写,写完刷新一下即可在网页看到效果。</p>
<blockquote>
<p>凡是修改<code>controller/</code>文件夹中的配置,都需要重启板子或把<code>/tmp/</code>目录下<code>luci-indexcache</code>、<code>luci-modulecache/luci-sessions/</code>删除才能生效,其他几个文件夹修改可不用,刷新一下网页即可。</p>
</blockquote>
<p>后者是在LuCI的git仓库中进行代码编写,写完需要编译固件并烧写到板上才能看到效果。</p>
<p>两种开发方法大同小异,各有优劣。本文推荐的方法是先在本地按项目结构开发,需要预览的时候通过SCP直接拷入板内即可。直接板上开发过于麻烦。</p>
<h3 class="mume-header" id="luci-app%E5%BC%80%E5%8F%91helloworldluci_module_helloworld">LuCI app开发Helloworld<sup class="footnote-ref"><a href="#fn7" id="fnref7">[7]</a></sup></h3>
<h4 class="mume-header" id="%E5%88%9B%E5%BB%BA%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">创建目录结构</h4>
<p>在<code>luci</code>目录的<code>application</code>文件夹下新建如下结构:</p>
<ul>
<li>luci-app-myapplication
<ul>
<li>luasrc
<ul>
<li>controller
<ul>
<li>myapp
<ul>
<li>new_tab.lua</li>
</ul>
</li>
</ul>
</li>
<li>model
<ul>
<li>cbi
<ul>
<li>myapp-mymodule
<ul>
<li>gateway_sn.lua</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>view
<ul>
<li>myapp-mymodule
<ul>
<li>helloworld.htm</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>特别注意要确保行尾符是LF,如果是在windows下开发,推荐用vscode选择行尾符为LF。</p>
<p><code>new_tab.lua</code>的内容为:</p>
<pre data-role="codeBlock" data-info="lua" class="language-lua"><span class="token function">module</span><span class="token punctuation">(</span><span class="token string">"luci.controller.myapp.new_tab"</span><span class="token punctuation">,</span> package<span class="token punctuation">.</span>seeall<span class="token punctuation">)</span>
<span class="token keyword">function</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">entry</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"admin"</span><span class="token punctuation">,</span> <span class="token string">"new_tab"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">firstchild</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">translate</span><span class="token punctuation">(</span><span class="token string">"cfg"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>dependent<span class="token operator">=</span><span class="token keyword">false</span><span class="token punctuation">)</span>
<span class="token function">entry</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"admin"</span><span class="token punctuation">,</span> <span class="token string">"new_tab"</span><span class="token punctuation">,</span> <span class="token string">"sn"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">cbi</span><span class="token punctuation">(</span><span class="token string">"myapp-mymodule/gateway_sn"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">translate</span><span class="token punctuation">(</span><span class="token string">"sn"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token function">entry</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"admin"</span><span class="token punctuation">,</span> <span class="token string">"new_tab"</span><span class="token punctuation">,</span> <span class="token string">"hellworld"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">template</span><span class="token punctuation">(</span><span class="token string">"myapp-mymodule/helloworld"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">_</span><span class="token punctuation">(</span><span class="token string">"HelloWorld"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
<span class="token keyword">end</span>
</pre><p><code>gateway_sn.lua</code>的内容为:</p>
<pre data-role="codeBlock" data-info="lua" class="language-lua">m <span class="token operator">=</span> <span class="token function">Map</span><span class="token punctuation">(</span><span class="token string">"sn_file"</span><span class="token punctuation">,</span> <span class="token function">translate</span><span class="token punctuation">(</span><span class="token string">"产品序列号"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">-- cbi_file is the config file in /etc/config</span>
d <span class="token operator">=</span> m<span class="token punctuation">:</span><span class="token function">section</span><span class="token punctuation">(</span>TypedSection<span class="token punctuation">,</span> <span class="token string">"gateway_sn"</span><span class="token punctuation">)</span> <span class="token comment">-- info is the section called info in cbi_file</span>
a <span class="token operator">=</span> d<span class="token punctuation">:</span><span class="token function">option</span><span class="token punctuation">(</span>Value<span class="token punctuation">,</span> <span class="token string">"sn"</span><span class="token punctuation">,</span> <span class="token function">translate</span><span class="token punctuation">(</span><span class="token string">"序列号"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
a<span class="token punctuation">.</span>optional<span class="token operator">=</span><span class="token keyword">false</span><span class="token punctuation">;</span>
a<span class="token punctuation">.</span>rmempty <span class="token operator">=</span> <span class="token keyword">false</span><span class="token punctuation">;</span> <span class="token comment">-- name is the option in the cbi_file</span>
<span class="token keyword">return</span> m
</pre><p><code>helloworld.htm</code>的内容为:</p>
<pre data-role="codeBlock" data-info="html" class="language-html"><%+header%>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span><%: HelloWorld %><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span>
<%+footer%>
</pre><p>整个目录内的内容拷贝到板上<code>/usr/lib/lua/luci</code>,<code>reboot</code>后重新进入LuCI即可看到效果。</p>
<h3 class="mume-header" id="dispatching-tree">dispatching tree</h3>
<p>LuCI使用dispatching tree加载每个controller中的<code>index()</code>函数。LuCI页面整个呈一个树形结构,使用<code>luci.dispatcher</code>中<code>entry()</code>来进行注册:</p>
<pre data-role="codeBlock" data-info="lua" class="language-lua"><span class="token function">entry</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> target<span class="token punctuation">,</span> title<span class="token operator">=</span><span class="token keyword">nil</span><span class="token punctuation">,</span> order<span class="token operator">=</span><span class="token keyword">nil</span><span class="token punctuation">)</span>
</pre><ul>
<li>
<p>path: path描述了dispatching tree中自身的位置,比如网页中的<code>/cgi-bin/luci/foo/bar/baz</code>在register时写<code>foo.bar.baz</code>。</p>
</li>
<li>
<p>target: 有三类,<code>call</code>、<code>template</code>和<code>cbi</code>:</p>
</li>
</ul>
<h3 class="mume-header" id="%E7%BB%93%E6%9E%84">结构</h3>
<h4 class="mume-header" id="%E4%BB%93%E5%BA%93%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">仓库目录结构</h4>
<table>
<thead>
<tr>
<th style="text-align:center">目录名</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">applications</td>
<td style="text-align:center">针对其他模块或者应用的单独的一个应用或者插件,可理解为功能模块</td>
</tr>
<tr>
<td style="text-align:center">i18n</td>
<td style="text-align:center">国际化</td>
</tr>
<tr>
<td style="text-align:center">modules</td>
<td style="text-align:center">应用集 ?包含主要的用户文件、核心文件</td>
</tr>
<tr>
<td style="text-align:center">theme</td>
<td style="text-align:center">主题</td>
</tr>
<tr>
<td style="text-align:center">build</td>
<td style="text-align:center">编译过程的脚本</td>
</tr>
<tr>
<td style="text-align:center">host</td>
<td style="text-align:center">编译产生的:将语言打包为二进制格式</td>
</tr>
<tr>
<td style="text-align:center">contrib</td>
<td style="text-align:center">/</td>
</tr>
<tr>
<td style="text-align:center">ipkg-ramips_24kec</td>
<td style="text-align:center">编译产生的:封装make menuconfig阶段选中的页面和功能</td>
</tr>
<tr>
<td style="text-align:center">libs</td>
<td style="text-align:center">独立的库函数:控件、接口、cbi.js、</td>
</tr>
<tr>
<td style="text-align:center">po</td>
<td style="text-align:center">语言文件</td>
</tr>
</tbody>
</table>
<p><code>modules</code>下的<code>luci-base</code>和<code>luci-mod-admin-full</code>包含了网页的基本功能。</p>
<h4 class="mume-header" id="module%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">module目录结构</h4>
<table>
<thead>
<tr>
<th style="text-align:center">文件/目录名</th>
<th style="text-align:center">子目录名</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">htdocs</td>
<td style="text-align:center">/</td>
<td style="text-align:center">html+docs,此目录存放HTML相关文件,主要包含以下两个目录,当烧录到硬件设备后,将拷贝到/www根目录下</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:center">cgi-bin</td>
<td style="text-align:center">存放luci启动脚本</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:center">luci-static</td>
<td style="text-align:center">存放HTML相关文件,包含CSS、JS及网页图片等文件</td>
</tr>
<tr>
<td style="text-align:center">luasrc</td>
<td style="text-align:center">/</td>
<td style="text-align:center">lua+src,此目录存放系统功能的LUA文件及M(model)、V(view)、C(controller)文件夹,当烧录到硬件设备后,将拷贝到/usr/lib/lua/luci目录下</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:center">controller</td>
<td style="text-align:center">控制器,生成页面的菜单栏并定义各个页面的调用方法</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:center">model</td>
<td style="text-align:center">数据模型,根据底层UCI配置文件生成页面</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:center">view</td>
<td style="text-align:center">视图,HTML页面</td>
</tr>
<tr>
<td style="text-align:center">po</td>
<td style="text-align:center">/</td>
<td style="text-align:center">定义页面的语言风格</td>
</tr>
<tr>
<td style="text-align:center">root</td>
<td style="text-align:center">/</td>
<td style="text-align:center">存放配置文件,该目录下的所有文件将拷贝到硬件设备根目录下</td>
</tr>
<tr>
<td style="text-align:center">src</td>
<td style="text-align:center">/</td>
<td style="text-align:center">生成所需要的库文件及LUA脚本</td>
</tr>
<tr>
<td style="text-align:center">Makefile</td>
<td style="text-align:center">/</td>
<td style="text-align:center">定义模块的编译方法</td>
</tr>
</tbody>
</table>
<h4 class="mume-header" id="%E6%9D%BF%E4%B8%8A%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">板上目录结构</h4>
<ol>
<li><code>/www</code>下</li>
</ol>
<table>
<thead>
<tr>
<th style="text-align:center">文件/目录名</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">cgi-bin</td>
<td style="text-align:center">此文件从luci-base下拷贝过来的,存放luci启动脚本</td>
</tr>
<tr>
<td style="text-align:center">index.html</td>
<td style="text-align:center">http请求的主页面,默认是/www/index.html,这个文件里显示了登录时常看见的那句话“LuCI - Lua Configuration Interface”,同时也指定了href链接/cgi-bin/luci</td>
</tr>
<tr>
<td style="text-align:center">luci-static</td>
<td style="text-align:center">存放HTML相关文件,包含CSS、JS及网页图片等文件。不同主题的htdocs/luci-static都将拷贝到这个目录下</td>
</tr>
</tbody>
</table>
<ol start="2">
<li><code>/usr/lib/lua</code>下</li>
</ol>
<p>顾名思义,存放了与LUA相关的文件,在LUA脚本中,通过require命令引用的脚本及函数,起始路径都是该目录。同时,不同模型及主题的luasrc文件夹都拷贝到/usr/lib/lua/luci目录下,通过/etc/config/luci中的mediaurlbase字段决定当前使用的主题及语言。</p>
<h2 class="mume-header" id="%E6%90%AD%E5%BB%BAvmware%E8%BF%90%E8%A1%8C%E7%9A%84x86-openwrt">搭建VMware运行的x86 OpenWrt</h2>
<h3 class="mume-header" id="%E7%BC%96%E8%AF%91%E5%9B%BA%E4%BB%B6">编译固件</h3>
<p>执行</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">make</span> menuconfig
</pre><p><code>Target System</code>选择<code>x86</code>,<code>Subtarget</code>选择<code>x86_64</code>,<code>Target Profile</code>默认为<code>Generic</code>。</p>
<p>在<code>Target Images</code>中勾选<code>Build Vmware image files (VMDK)</code>,这样会生成<code>.vmdk</code>格式的镜像,可以直接供VMware使用。</p>
<p>在<code>LuCI -> 1. Collections</code>里勾选<code>luci</code>和<code>luci-ssl-openssl</code>,之后</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">make</span>
</pre><p>之后生成的固件在<code>./bin/targets/x86/64</code>下,文件名如<code>openwrt-x86-64-combined-ext4.vmdk</code>。</p>
<h3 class="mume-header" id="%E8%AE%BE%E7%BD%AE%E8%99%9A%E6%8B%9F%E6%9C%BA">设置虚拟机</h3>
<p>本文使用的VMware版本为<code>VMware Workstation Pro 15</code>,下面记录在虚拟机中安装上文编译的OpenWrt镜像的步骤。</p>
<ol>
<li>
<p>打开<code>文件 -> 新建虚拟机</code></p>
</li>
<li>
<p>选择<code>自定义(高级)</code></p>
</li>
<li>
<p>硬件兼容性可以保留默认<code>Workstation 15.x</code></p>
</li>
<li>
<p>选择<code>稍后安装操作系统</code></p>
</li>
<li>
<p>客户机操作系统:</p>
<p>选择<code>Linux</code>;由于我编译的<code>OpenWrt</code>中内核源码版本为<code>4.14</code>,故版本选择<code>其他Linux 4.x 64位</code></p>
</li>
<li>
<p>设置名称和路径、处理器数量、内存大小等,合适即可</p>
</li>
<li>
<p>网络连接、I/O控制器类型、虚拟磁盘类型可以选择默认值</p>
</li>
<li>
<p>磁盘选择<code>使用现有虚拟磁盘</code>,选择上一步编译生成的<code>.vmdk</code>文件</p>
<p>如果弹出<code>将现有虚拟磁盘转换为更新的格式?</code>,可以选择<code>转换</code></p>
</li>
<li>
<p><strong>配置网卡</strong>,选择<code>自定义硬件</code>:</p>
<p>OpenWrt需要一个WAN口和一个LAN口,所以至少需要两个<code>网络适配器</code>,其中用于WAN口的选择<code>桥接模式</code>,用于LAN口的可以选择<code>自定义</code>中任意一个未被使用的虚拟网络。我使用的配置是:</p>
<table>
<thead>
<tr>
<th style="text-align:center">设备</th>
<th style="text-align:center">摘要</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">网络适配器</td>
<td style="text-align:center">自定义(VMnet2)</td>
</tr>
<tr>
<td style="text-align:center">网络适配器2</td>
<td style="text-align:center">桥接模式(自动)</td>
</tr>
</tbody>
</table>
</li>
<li>
<p>至此虚拟机建立完成,启动虚拟机</p>
</li>
<li>
<p><strong>设置网络参数</strong>:</p>
<p>进入OpenWrt系统,敲一下回车进入命令行,然后修改网络配置文件:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">vim</span> /etc/config/network
</pre><p>主要是保证<code>'lan'</code>和<code>'wan'</code>这两个配置没问题,这里需要打开VMWare中<code>编辑 -> 虚拟网络编辑器</code>查看VMnet的网段。</p>
<p>这里我的<code>VMnet2</code>的子网地址是<code>192.168.233.0</code>,并且我的宿主机为<code>192.168.233.1</code>,因此只需要将<code>'lan'</code>下的<code>option ipaddr</code>修改到同一网段下的另一个IP即可。</p>
<p><code>wan</code>我这里保持的默认<code>dhcp</code>没有出现问题。</p>
<p>给出一个我的参考:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">config interface <span class="token string">'lan'</span>
option <span class="token builtin class-name">type</span> <span class="token string">'bridge'</span>
option ifname <span class="token string">'eth0'</span>
option proto <span class="token string">'static'</span>
option ipaddr <span class="token string">'192.168.233.10'</span>
option netmask <span class="token string">'255.255.255.0'</span>
option ip6assign <span class="token string">'60'</span>
config interface <span class="token string">'wan'</span>
option ifname <span class="token string">'eth1'</span>
option proto <span class="token string">'dhcp'</span>
</pre><p>之后使用</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">service</span> network restart
</pre><p>如果WAN口配置没有问题,那在OpenWrt虚拟机中应该可以直接ping通外网,如</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">ping</span> www.baidu.com
</pre><p>如果LAN口没有问题,那宿主机和OpenWrt应该可以互相ping通,比如在宿主机ping虚拟机:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">ping</span> <span class="token number">192.168</span>.233.10
</pre></li>
</ol>
<h2 class="mume-header" id="%E4%BD%BF%E7%94%A8docker%E5%9C%A8%E9%AB%98%E7%89%88%E6%9C%ACubuntu%E4%B8%8B%E7%BC%96%E8%AF%91%E6%97%A7%E7%89%88%E6%9C%ACopenwrt">使用docker在高版本Ubuntu下编译旧版本OpenWrt</h2>
<p>由于高版本的gnulib与低版本不兼容的问题,在高版本Ubuntu下编译一个依赖低版本gnulib的OpenWrt版本可能会遇到<code>freadahead.c</code>,<code>fseterr.c</code>相关的报错问题,比如:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash">freadahead.c:91:3: error: <span class="token comment">#error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."</span>
</pre><p>解决方法是降低本系统内gnuilb版本,或者手动修改受影响的源代码,或者直接换一个旧版本Ubuntu。百度的方法绝大多数都如这种:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token builtin class-name">cd</span> /opt/p2/openwrt/build_dir/host/m4-1.4.17/
<span class="token function">sed</span> -i <span class="token string">'s/IO_ftrylockfile/IO_EOF_SEEN/'</span> lib/*.c
<span class="token builtin class-name">echo</span> <span class="token string">"#define _IO_IN_BACKUP 0x100"</span> <span class="token operator">>></span> lib/stdio-impl.h
</pre><p>是直接修改了受影响的代码。但是这些都很麻烦,受影响的地方多且琐碎,如果我们仍想用高版本的Ubuntu如20.04,可以使用docker来进行编译,使用一个Ubuntu 16.04的docker镜像就可以了。</p>
<h3 class="mume-header" id="%E5%AE%89%E8%A3%85docker">安装docker</h3>
<p>Docker本身是跨平台的,在Windows或者Mac OS上运行docker需要安装<a href="https://docs.docker.com/desktop/">Docker Desktop</a>,大体上Docker Desktop运行了一个Linux虚拟机,在虚拟机中再运行docker。本文是在Ubuntu 20.04下安装<a href="https://docs.docker.com/engine/">Docker Engine</a>的流程<sup class="footnote-ref"><a href="#fn8" id="fnref8">[8]</a></sup>。</p>
<p>首先移除系统中可能存在的旧版本docker:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">sudo</span> <span class="token function">apt-get</span> remove docker docker-engine docker.io containerd runc
</pre><p>添加docker源,先安装让apt可以用HTTPS源的依赖:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">sudo</span> <span class="token function">apt-get</span> update
<span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> <span class="token punctuation">\</span>
apt-transport-https <span class="token punctuation">\</span>
ca-certificates <span class="token punctuation">\</span>
<span class="token function">curl</span> <span class="token punctuation">\</span>
gnupg <span class="token punctuation">\</span>
lsb-release
</pre><p>添加docker官方的GPG key:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">curl</span> -fsSL https://download.docker.com/linux/ubuntu/gpg <span class="token operator">|</span> <span class="token function">sudo</span> gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
</pre><p>设置稳定版的源:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token builtin class-name">echo</span> <span class="token punctuation">\</span>
<span class="token string">"deb [arch=<span class="token variable"><span class="token variable">$(</span>dpkg --print-architecture<span class="token variable">)</span></span> signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
<span class="token variable"><span class="token variable">$(</span>lsb_release -cs<span class="token variable">)</span></span> stable"</span> <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token function">tee</span> /etc/apt/sources.list.d/docker.list <span class="token operator">></span> /dev/null
</pre><p>安装Docker Engine:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">sudo</span> <span class="token function">apt-get</span> update
<span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> docker-ce docker-ce-cli containerd.io
</pre><p>如果安装没有问题,可以用<code>docker run hello-world</code>进行验证,会自动从Docker Hub上拉取hello-world镜像并且运行。</p>
<h3 class="mume-header" id="%E9%85%8D%E7%BD%AE%E9%95%9C%E5%83%8F%E5%92%8C%E5%85%B6%E4%B8%AD%E7%9A%84%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83">配置镜像和其中的编译环境</h3>
<p>拉取一个Ubuntu 16.04的镜像:</p>
<pre data-role="codeBlock" data-info="bash" class="language-bash"><span class="token function">sudo</span> docker pull ubuntu:16.04