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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#if HAVE_VALGRIND_MEMCHECK_H
#include <valgrind/memcheck.h>
#endif
#include <fcntl.h>
#include <getopt.h>
#include <linux/fs.h>
#include <linux/loop.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include "sd-device.h"
#include "sd-id128.h"
#include "alloc-util.h"
#include "blkid-util.h"
#include "blockdev-util.h"
#include "btrfs-util.h"
#include "chase-symlinks.h"
#include "conf-files.h"
#include "conf-parser.h"
#include "cryptsetup-util.h"
#include "def.h"
#include "device-util.h"
#include "devnum-util.h"
#include "dirent-util.h"
#include "efivars.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fdisk-util.h"
#include "fileio.h"
#include "format-table.h"
#include "format-util.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "gpt.h"
#include "hexdecoct.h"
#include "hmac.h"
#include "id128-util.h"
#include "json.h"
#include "list.h"
#include "loop-util.h"
#include "main-func.h"
#include "mkdir.h"
#include "mkfs-util.h"
#include "mount-util.h"
#include "mountpoint-util.h"
#include "parse-argument.h"
#include "parse-helpers.h"
#include "pretty-print.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "random-util.h"
#include "resize-fs.h"
#include "sort-util.h"
#include "specifier.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "sync-util.h"
#include "terminal-util.h"
#include "tpm-pcr.h"
#include "tpm2-util.h"
#include "user-util.h"
#include "utf8.h"
/* If not configured otherwise use a minimal partition size of 10M */
#define DEFAULT_MIN_SIZE (10*1024*1024)
/* Hard lower limit for new partition sizes */
#define HARD_MIN_SIZE 4096
/* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
#define GPT_METADATA_SIZE (1044*1024)
/* LUKS2 takes off 16M of the partition size with its metadata by default */
#define LUKS2_METADATA_SIZE (16*1024*1024)
/* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
* are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
* sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
* waste 3K per partition, which is probably fine. */
static enum {
EMPTY_REFUSE, /* refuse empty disks, never create a partition table */
EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */
EMPTY_REQUIRE, /* require an empty disk, create a partition table */
EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */
EMPTY_CREATE, /* create disk as loopback file, create a partition table always */
} arg_empty = EMPTY_REFUSE;
static bool arg_dry_run = true;
static const char *arg_node = NULL;
static char *arg_root = NULL;
static char *arg_image = NULL;
static char **arg_definitions = NULL;
static bool arg_discard = true;
static bool arg_can_factory_reset = false;
static int arg_factory_reset = -1;
static sd_id128_t arg_seed = SD_ID128_NULL;
static bool arg_randomize = false;
static int arg_pretty = -1;
static uint64_t arg_size = UINT64_MAX;
static bool arg_size_auto = false;
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
static PagerFlags arg_pager_flags = 0;
static bool arg_legend = true;
static void *arg_key = NULL;
static size_t arg_key_size = 0;
static char *arg_tpm2_device = NULL;
static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
static char *arg_tpm2_public_key = NULL;
static uint32_t arg_tpm2_public_key_pcr_mask = UINT32_MAX;
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
STATIC_DESTRUCTOR_REGISTER(arg_definitions, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_public_key, freep);
typedef struct Partition Partition;
typedef struct FreeArea FreeArea;
typedef struct Context Context;
typedef enum EncryptMode {
ENCRYPT_OFF,
ENCRYPT_KEY_FILE,
ENCRYPT_TPM2,
ENCRYPT_KEY_FILE_TPM2,
_ENCRYPT_MODE_MAX,
_ENCRYPT_MODE_INVALID = -EINVAL,
} EncryptMode;
typedef enum VerityMode {
VERITY_OFF,
VERITY_DATA,
VERITY_HASH,
_VERITY_MODE_MAX,
_VERITY_MODE_INVALID = -EINVAL,
} VerityMode;
struct Partition {
char *definition_path;
char **drop_in_files;
sd_id128_t type_uuid;
sd_id128_t current_uuid, new_uuid;
bool new_uuid_is_set;
char *current_label, *new_label;
bool dropped;
bool factory_reset;
int32_t priority;
uint32_t weight, padding_weight;
uint64_t current_size, new_size;
uint64_t size_min, size_max;
uint64_t current_padding, new_padding;
uint64_t padding_min, padding_max;
uint64_t partno;
uint64_t offset;
struct fdisk_partition *current_partition;
struct fdisk_partition *new_partition;
FreeArea *padding_area;
FreeArea *allocated_to_area;
char *copy_blocks_path;
bool copy_blocks_auto;
int copy_blocks_fd;
uint64_t copy_blocks_size;
char *format;
char **copy_files;
char **make_directories;
EncryptMode encrypt;
VerityMode verity;
char *verity_match_key;
uint64_t gpt_flags;
int no_auto;
int read_only;
int growfs;
uint8_t *roothash;
size_t roothash_size;
Partition *siblings[_VERITY_MODE_MAX];
LIST_FIELDS(Partition, partitions);
};
#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
#define PARTITION_EXISTS(p) (!!(p)->current_partition)
struct FreeArea {
Partition *after;
uint64_t size;
uint64_t allocated;
};
struct Context {
LIST_HEAD(Partition, partitions);
size_t n_partitions;
FreeArea **free_areas;
size_t n_free_areas;
uint64_t start, end, total;
struct fdisk_context *fdisk_context;
uint64_t sector_size;
uint64_t grain_size;
sd_id128_t seed;
};
static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = {
[ENCRYPT_OFF] = "off",
[ENCRYPT_KEY_FILE] = "key-file",
[ENCRYPT_TPM2] = "tpm2",
[ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2",
};
static const char *verity_mode_table[_VERITY_MODE_MAX] = {
[VERITY_OFF] = "off",
[VERITY_DATA] = "data",
[VERITY_HASH] = "hash",
};
#if HAVE_LIBCRYPTSETUP
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(verity_mode, VerityMode);
#else
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(verity_mode, VerityMode);
#endif
static uint64_t round_down_size(uint64_t v, uint64_t p) {
return (v / p) * p;
}
static uint64_t round_up_size(uint64_t v, uint64_t p) {
v = DIV_ROUND_UP(v, p);
if (v > UINT64_MAX / p)
return UINT64_MAX; /* overflow */
return v * p;
}
static Partition *partition_new(void) {
Partition *p;
p = new(Partition, 1);
if (!p)
return NULL;
*p = (Partition) {
.weight = 1000,
.padding_weight = 0,
.current_size = UINT64_MAX,
.new_size = UINT64_MAX,
.size_min = UINT64_MAX,
.size_max = UINT64_MAX,
.current_padding = UINT64_MAX,
.new_padding = UINT64_MAX,
.padding_min = UINT64_MAX,
.padding_max = UINT64_MAX,
.partno = UINT64_MAX,
.offset = UINT64_MAX,
.copy_blocks_fd = -1,
.copy_blocks_size = UINT64_MAX,
.no_auto = -1,
.read_only = -1,
.growfs = -1,
};
return p;
}
static Partition* partition_free(Partition *p) {
if (!p)
return NULL;
free(p->current_label);
free(p->new_label);
free(p->definition_path);
strv_free(p->drop_in_files);
if (p->current_partition)
fdisk_unref_partition(p->current_partition);
if (p->new_partition)
fdisk_unref_partition(p->new_partition);
free(p->copy_blocks_path);
safe_close(p->copy_blocks_fd);
free(p->format);
strv_free(p->copy_files);
strv_free(p->make_directories);
free(p->verity_match_key);
free(p->roothash);
return mfree(p);
}
static void partition_foreignize(Partition *p) {
assert(p);
assert(PARTITION_EXISTS(p));
/* Reset several parameters set through definition file to make the partition foreign. */
p->new_label = mfree(p->new_label);
p->definition_path = mfree(p->definition_path);
p->drop_in_files = strv_free(p->drop_in_files);
p->copy_blocks_path = mfree(p->copy_blocks_path);
p->copy_blocks_fd = safe_close(p->copy_blocks_fd);
p->format = mfree(p->format);
p->copy_files = strv_free(p->copy_files);
p->make_directories = strv_free(p->make_directories);
p->verity_match_key = mfree(p->verity_match_key);
p->new_uuid = SD_ID128_NULL;
p->new_uuid_is_set = false;
p->priority = 0;
p->weight = 1000;
p->padding_weight = 0;
p->size_min = UINT64_MAX;
p->size_max = UINT64_MAX;
p->padding_min = UINT64_MAX;
p->padding_max = UINT64_MAX;
p->no_auto = -1;
p->read_only = -1;
p->growfs = -1;
p->verity = VERITY_OFF;
}
static Partition* partition_unlink_and_free(Context *context, Partition *p) {
if (!p)
return NULL;
LIST_REMOVE(partitions, context->partitions, p);
assert(context->n_partitions > 0);
context->n_partitions--;
return partition_free(p);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free);
static Context *context_new(sd_id128_t seed) {
Context *context;
context = new(Context, 1);
if (!context)
return NULL;
*context = (Context) {
.start = UINT64_MAX,
.end = UINT64_MAX,
.total = UINT64_MAX,
.seed = seed,
};
return context;
}
static void context_free_free_areas(Context *context) {
assert(context);
for (size_t i = 0; i < context->n_free_areas; i++)
free(context->free_areas[i]);
context->free_areas = mfree(context->free_areas);
context->n_free_areas = 0;
}
static Context *context_free(Context *context) {
if (!context)
return NULL;
while (context->partitions)
partition_unlink_and_free(context, context->partitions);
assert(context->n_partitions == 0);
context_free_free_areas(context);
if (context->fdisk_context)
fdisk_unref_context(context->fdisk_context);
return mfree(context);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
static int context_add_free_area(
Context *context,
uint64_t size,
Partition *after) {
FreeArea *a;
assert(context);
assert(!after || !after->padding_area);
if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1))
return -ENOMEM;
a = new(FreeArea, 1);
if (!a)
return -ENOMEM;
*a = (FreeArea) {
.size = size,
.after = after,
};
context->free_areas[context->n_free_areas++] = a;
if (after)
after->padding_area = a;
return 0;
}
static void partition_drop_or_foreignize(Partition *p) {
if (!p || p->dropped || PARTITION_IS_FOREIGN(p))
return;
if (PARTITION_EXISTS(p)) {
log_info("Can't grow existing partition %s of priority %" PRIi32 ", ignoring.",
strna(p->current_label ?: p->new_label), p->priority);
/* Handle the partition as foreign. Do not set dropped flag. */
partition_foreignize(p);
} else {
log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.",
p->definition_path, p->priority);
p->dropped = true;
p->allocated_to_area = NULL;
}
}
static bool context_drop_or_foreignize_one_priority(Context *context) {
int32_t priority = 0;
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
priority = MAX(priority, p->priority);
}
/* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
* least one existing priority */
if (priority <= 0)
return false;
LIST_FOREACH(partitions, p, context->partitions) {
if (p->priority < priority)
continue;
partition_drop_or_foreignize(p);
/* We ensure that all verity sibling partitions have the same priority, so it's safe
* to drop all siblings here as well. */
for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++)
partition_drop_or_foreignize(p->siblings[mode]);
}
return true;
}
static uint64_t partition_min_size(const Context *context, const Partition *p) {
uint64_t sz;
assert(context);
assert(p);
/* Calculate the disk space we really need at minimum for this partition. If the partition already
* exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
* than 4K.
*
* DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
if (PARTITION_IS_FOREIGN(p)) {
/* Don't allow changing size of partitions not managed by us */
assert(p->current_size != UINT64_MAX);
return p->current_size;
}
sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE;
if (!PARTITION_EXISTS(p)) {
uint64_t d = 0;
if (p->encrypt != ENCRYPT_OFF)
d += round_up_size(LUKS2_METADATA_SIZE, context->grain_size);
if (p->copy_blocks_size != UINT64_MAX)
d += round_up_size(p->copy_blocks_size, context->grain_size);
else if (p->format || p->encrypt != ENCRYPT_OFF) {
uint64_t f;
/* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
f = p->format ? round_up_size(minimal_size_by_fs_name(p->format), context->grain_size) : UINT64_MAX;
d += f == UINT64_MAX ? context->grain_size : f;
}
if (d > sz)
sz = d;
}
return MAX(round_up_size(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, context->grain_size), sz);
}
static uint64_t partition_max_size(const Context *context, const Partition *p) {
uint64_t sm;
/* Calculate how large the partition may become at max. This is generally the configured maximum
* size, except when it already exists and is larger than that. In that case it's the existing size,
* since we never want to shrink partitions. */
assert(context);
assert(p);
if (PARTITION_IS_FOREIGN(p)) {
/* Don't allow changing size of partitions not managed by us */
assert(p->current_size != UINT64_MAX);
return p->current_size;
}
if (p->size_max == UINT64_MAX)
return UINT64_MAX;
sm = round_down_size(p->size_max, context->grain_size);
if (p->current_size != UINT64_MAX)
sm = MAX(p->current_size, sm);
return MAX(partition_min_size(context, p), sm);
}
static uint64_t partition_min_padding(const Partition *p) {
assert(p);
return p->padding_min != UINT64_MAX ? p->padding_min : 0;
}
static uint64_t partition_max_padding(const Partition *p) {
assert(p);
return p->padding_max;
}
static uint64_t partition_min_size_with_padding(Context *context, const Partition *p) {
uint64_t sz;
/* Calculate the disk space we need for this partition plus any free space coming after it. This
* takes user configured padding into account as well as any additional whitespace needed to align
* the next partition to 4K again. */
assert(context);
assert(p);
sz = partition_min_size(context, p) + partition_min_padding(p);
if (PARTITION_EXISTS(p)) {
/* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
assert(p->offset != UINT64_MAX);
return round_up_size(p->offset + sz, context->grain_size) - p->offset;
}
/* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
return round_up_size(sz, context->grain_size);
}
static uint64_t free_area_available(const FreeArea *a) {
assert(a);
/* Determines how much of this free area is not allocated yet */
assert(a->size >= a->allocated);
return a->size - a->allocated;
}
static uint64_t free_area_current_end(Context *context, const FreeArea *a) {
assert(context);
assert(a);
if (!a->after)
return free_area_available(a);
assert(a->after->offset != UINT64_MAX);
assert(a->after->current_size != UINT64_MAX);
/* Calculate where the free area ends, based on the offset of the partition preceding it. */
return round_up_size(a->after->offset + a->after->current_size, context->grain_size) + free_area_available(a);
}
static uint64_t free_area_min_end(Context *context, const FreeArea *a) {
assert(context);
assert(a);
if (!a->after)
return 0;
assert(a->after->offset != UINT64_MAX);
assert(a->after->current_size != UINT64_MAX);
/* Calculate where the partition would end when we give it as much as it needs. */
return round_up_size(a->after->offset + partition_min_size_with_padding(context, a->after), context->grain_size);
}
static uint64_t free_area_available_for_new_partitions(Context *context, const FreeArea *a) {
assert(context);
assert(a);
/* Similar to free_area_available(), but takes into account that the required size and padding of the
* preceding partition is honoured. */
return LESS_BY(free_area_current_end(context, a), free_area_min_end(context, a));
}
static int free_area_compare(FreeArea *const *a, FreeArea *const*b, Context *context) {
assert(context);
return CMP(free_area_available_for_new_partitions(context, *a),
free_area_available_for_new_partitions(context, *b));
}
static uint64_t charge_size(Context *context, uint64_t total, uint64_t amount) {
assert(context);
/* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
assert(amount <= total);
return LESS_BY(total, round_up_size(amount, context->grain_size));
}
static uint64_t charge_weight(uint64_t total, uint64_t amount) {
assert(amount <= total);
return total - amount;
}
static bool context_allocate_partitions(Context *context, uint64_t *ret_largest_free_area) {
assert(context);
/* This may be called multiple times. Reset previous assignments. */
for (size_t i = 0; i < context->n_free_areas; i++)
context->free_areas[i]->allocated = 0;
/* Sort free areas by size, putting smallest first */
typesafe_qsort_r(context->free_areas, context->n_free_areas, free_area_compare, context);
/* In any case return size of the largest free area (i.e. not the size of all free areas
* combined!) */
if (ret_largest_free_area)
*ret_largest_free_area =
context->n_free_areas == 0 ? 0 :
free_area_available_for_new_partitions(context, context->free_areas[context->n_free_areas-1]);
/* Check that each existing partition can fit its area. */
for (size_t i = 0; i < context->n_free_areas; i++)
if (free_area_current_end(context, context->free_areas[i]) <
free_area_min_end(context, context->free_areas[i]))
return false;
/* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */
LIST_FOREACH(partitions, p, context->partitions) {
bool fits = false;
uint64_t required;
FreeArea *a = NULL;
/* Skip partitions we already dropped or that already exist */
if (p->dropped || PARTITION_EXISTS(p))
continue;
/* How much do we need to fit? */
required = partition_min_size_with_padding(context, p);
assert(required % context->grain_size == 0);
for (size_t i = 0; i < context->n_free_areas; i++) {
a = context->free_areas[i];
if (free_area_available_for_new_partitions(context, a) >= required) {
fits = true;
break;
}
}
if (!fits)
return false; /* 😢 Oh no! We can't fit this partition into any free area! */
/* Assign the partition to this free area */
p->allocated_to_area = a;
/* Budget the minimal partition size */
a->allocated += required;
}
return true;
}
static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) {
uint64_t weight_sum = 0;
assert(context);
assert(a);
assert(ret);
/* Determine the sum of the weights of all partitions placed in or before the specified free area */
LIST_FOREACH(partitions, p, context->partitions) {
if (p->padding_area != a && p->allocated_to_area != a)
continue;
if (p->weight > UINT64_MAX - weight_sum)
goto overflow_sum;
weight_sum += p->weight;
if (p->padding_weight > UINT64_MAX - weight_sum)
goto overflow_sum;
weight_sum += p->padding_weight;
}
*ret = weight_sum;
return 0;
overflow_sum:
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
}
static uint64_t scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum) {
assert(weight_sum >= weight);
for (;;) {
if (weight == 0)
return 0;
if (weight == weight_sum)
return value;
if (value <= UINT64_MAX / weight)
return value * weight / weight_sum;
/* Rescale weight and weight_sum to make not the calculation overflow. To satisfy the
* following conditions, 'weight_sum' is rounded up but 'weight' is rounded down:
* - the sum of scale_by_weight() for all weights must not be larger than the input value,
* - scale_by_weight() must not be larger than the ideal value (i.e. calculated with uint128_t). */
weight_sum = DIV_ROUND_UP(weight_sum, 2);
weight /= 2;
}
}
typedef enum GrowPartitionPhase {
/* The zeroth phase: do not touch foreign partitions (i.e. those we don't manage). */
PHASE_FOREIGN,
/* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
PHASE_OVERCHARGE,
/* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
PHASE_UNDERCHARGE,
/* The third phase: we distribute what remains among the remaining partitions, according to the weights */
PHASE_DISTRIBUTE,
_GROW_PARTITION_PHASE_MAX,
} GrowPartitionPhase;
static bool context_grow_partitions_phase(
Context *context,
FreeArea *a,
GrowPartitionPhase phase,
uint64_t *span,
uint64_t *weight_sum) {
bool try_again = false;
assert(context);
assert(a);
assert(span);
assert(weight_sum);
/* Now let's look at the intended weights and adjust them taking the minimum space assignments into
* account. i.e. if a partition has a small weight but a high minimum space value set it should not
* get any additional room from the left-overs. Similar, if two partitions have the same weight they
* should get the same space if possible, even if one has a smaller minimum size than the other. */
LIST_FOREACH(partitions, p, context->partitions) {
/* Look only at partitions associated with this free area, i.e. immediately
* preceding it, or allocated into it */
if (p->allocated_to_area != a && p->padding_area != a)
continue;
if (p->new_size == UINT64_MAX) {
uint64_t share, rsz, xsz;
bool charge = false;
/* Calculate how much this space this partition needs if everyone would get
* the weight based share */
share = scale_by_weight(*span, p->weight, *weight_sum);
rsz = partition_min_size(context, p);
xsz = partition_max_size(context, p);
if (phase == PHASE_FOREIGN && PARTITION_IS_FOREIGN(p)) {
/* Never change of foreign partitions (i.e. those we don't manage) */
p->new_size = p->current_size;
charge = true;
} else if (phase == PHASE_OVERCHARGE && rsz > share) {
/* This partition needs more than its calculated share. Let's assign
* it that, and take this partition out of all calculations and start
* again. */
p->new_size = rsz;
charge = try_again = true;
} else if (phase == PHASE_UNDERCHARGE && xsz < share) {
/* This partition accepts less than its calculated
* share. Let's assign it that, and take this partition out
* of all calculations and start again. */
p->new_size = xsz;
charge = try_again = true;
} else if (phase == PHASE_DISTRIBUTE) {
/* This partition can accept its calculated share. Let's
* assign it. There's no need to restart things here since
* assigning this shouldn't impact the shares of the other
* partitions. */
assert(share >= rsz);
p->new_size = CLAMP(round_down_size(share, context->grain_size), rsz, xsz);
charge = true;
}
if (charge) {
*span = charge_size(context, *span, p->new_size);
*weight_sum = charge_weight(*weight_sum, p->weight);
}
}
if (p->new_padding == UINT64_MAX) {
uint64_t share, rsz, xsz;
bool charge = false;
share = scale_by_weight(*span, p->padding_weight, *weight_sum);
rsz = partition_min_padding(p);
xsz = partition_max_padding(p);
if (phase == PHASE_OVERCHARGE && rsz > share) {
p->new_padding = rsz;
charge = try_again = true;
} else if (phase == PHASE_UNDERCHARGE && xsz < share) {
p->new_padding = xsz;
charge = try_again = true;
} else if (phase == PHASE_DISTRIBUTE) {
assert(share >= rsz);
p->new_padding = CLAMP(round_down_size(share, context->grain_size), rsz, xsz);
charge = true;
}
if (charge) {
*span = charge_size(context, *span, p->new_padding);
*weight_sum = charge_weight(*weight_sum, p->padding_weight);
}
}
}
return !try_again;
}
static void context_grow_partition_one(Context *context, FreeArea *a, Partition *p, uint64_t *span) {
uint64_t m;
assert(context);
assert(a);
assert(p);
assert(span);
if (*span == 0)
return;
if (p->allocated_to_area != a)
return;
if (PARTITION_IS_FOREIGN(p))
return;
assert(p->new_size != UINT64_MAX);
/* Calculate new size and align. */
m = round_down_size(p->new_size + *span, context->grain_size);
/* But ensure this doesn't shrink the size. */
m = MAX(m, p->new_size);
/* And ensure this doesn't exceed the maximum size. */
m = MIN(m, partition_max_size(context, p));
assert(m >= p->new_size);
*span = charge_size(context, *span, m - p->new_size);
p->new_size = m;
}
static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) {
uint64_t weight_sum = 0, span;
int r;
assert(context);
assert(a);
r = context_sum_weights(context, a, &weight_sum);
if (r < 0)
return r;
/* Let's calculate the total area covered by this free area and the partition before it */
span = a->size;
if (a->after) {
assert(a->after->offset != UINT64_MAX);
assert(a->after->current_size != UINT64_MAX);
span += round_up_size(a->after->offset + a->after->current_size, context->grain_size) - a->after->offset;
}
for (GrowPartitionPhase phase = 0; phase < _GROW_PARTITION_PHASE_MAX;)
if (context_grow_partitions_phase(context, a, phase, &span, &weight_sum))
phase++; /* go to the next phase */
/* We still have space left over? Donate to preceding partition if we have one */
if (span > 0 && a->after)
context_grow_partition_one(context, a, a->after, &span);
/* What? Even still some space left (maybe because there was no preceding partition, or it had a
* size limit), then let's donate it to whoever wants it. */
if (span > 0)
LIST_FOREACH(partitions, p, context->partitions) {
context_grow_partition_one(context, a, p, &span);
if (span == 0)
break;
}
/* Yuck, still no one? Then make it padding */
if (span > 0 && a->after) {
assert(a->after->new_padding != UINT64_MAX);
a->after->new_padding += span;
}
return 0;
}
static int context_grow_partitions(Context *context) {
int r;
assert(context);
for (size_t i = 0; i < context->n_free_areas; i++) {
r = context_grow_partitions_on_free_area(context, context->free_areas[i]);
if (r < 0)
return r;
}
/* All existing partitions that have no free space after them can't change size */
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
if (!PARTITION_EXISTS(p) || p->padding_area) {
/* The algorithm above must have initialized this already */
assert(p->new_size != UINT64_MAX);
continue;
}
assert(p->new_size == UINT64_MAX);
p->new_size = p->current_size;
assert(p->new_padding == UINT64_MAX);
p->new_padding = p->current_padding;
}
return 0;
}
static void context_place_partitions(Context *context) {
uint64_t partno = 0;
assert(context);
/* Determine next partition number to assign */
LIST_FOREACH(partitions, p, context->partitions) {
if (!PARTITION_EXISTS(p))
continue;
assert(p->partno != UINT64_MAX);
if (p->partno >= partno)
partno = p->partno + 1;
}
for (size_t i = 0; i < context->n_free_areas; i++) {
FreeArea *a = context->free_areas[i];
_unused_ uint64_t left;
uint64_t start;
if (a->after) {
assert(a->after->offset != UINT64_MAX);
assert(a->after->new_size != UINT64_MAX);
assert(a->after->new_padding != UINT64_MAX);
start = a->after->offset + a->after->new_size + a->after->new_padding;
} else
start = context->start;
start = round_up_size(start, context->grain_size);
left = a->size;
LIST_FOREACH(partitions, p, context->partitions) {
if (p->allocated_to_area != a)
continue;
p->offset = start;
p->partno = partno++;
assert(left >= p->new_size);
start += p->new_size;
left -= p->new_size;
assert(left >= p->new_padding);
start += p->new_padding;
left -= p->new_padding;
}
}
}
static int config_parse_type(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
sd_id128_t *type_uuid = data;
int r;
assert(rvalue);
assert(type_uuid);
r = gpt_partition_type_uuid_from_string(rvalue, type_uuid);
if (r < 0)
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue);
return 0;
}
static int config_parse_label(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *resolved = NULL;
char **label = data;
int r;
assert(rvalue);
assert(label);
/* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of
* assigning the empty string to reset to default here, but really accept it as label to set. */
r = specifier_printf(rvalue, GPT_LABEL_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in Label=, ignoring: %s", rvalue);
return 0;
}
if (!utf8_is_valid(resolved)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Partition label not valid UTF-8, ignoring: %s", rvalue);
return 0;
}
r = gpt_partition_label_valid(resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
resolved, rvalue);
return 0;
}
if (!r) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
resolved, rvalue);
return 0;
}
free_and_replace(*label, resolved);
return 0;
}
static int config_parse_weight(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
uint32_t *w = ASSERT_PTR(data), v;
int r;
assert(rvalue);
r = safe_atou32(rvalue, &v);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse weight value, ignoring: %s", rvalue);
return 0;
}
if (v > 1000U*1000U) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v);
return 0;
}
*w = v;
return 0;
}
static int config_parse_size4096(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
uint64_t *sz = data, parsed;
int r;
assert(rvalue);
assert(data);
r = parse_size(rvalue, 1024, &parsed);
if (r < 0)
return log_syntax(unit, LOG_ERR, filename, line, r,
"Failed to parse size value: %s", rvalue);
if (ltype > 0)
*sz = round_up_size(parsed, 4096);
else if (ltype < 0)
*sz = round_down_size(parsed, 4096);
else
*sz = parsed;
if (*sz != parsed)
log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " %s %" PRIu64 ", a multiple of 4096.",
lvalue, parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), *sz);
return 0;
}
static int config_parse_fstype(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char **fstype = data;
assert(rvalue);
assert(data);
if (!filename_is_valid(rvalue))
return log_syntax(unit, LOG_ERR, filename, line, 0,
"File system type is not valid, refusing: %s", rvalue);
return free_and_strdup_warn(fstype, rvalue);
}
static int config_parse_copy_files(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL;
const char *p = rvalue, *target;
Partition *partition = data;
int r;
assert(rvalue);
assert(partition);
r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
if (r < 0)
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue);
if (r == 0) {
log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue);
return 0;
}
r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
if (r < 0)
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue);
if (r == 0)
target = source; /* No target, then it's the same as the source */
else
target = buffer;
if (!isempty(p))
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue);
r = specifier_printf(source, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_source);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue);
return 0;
}
r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
if (r < 0)
return 0;
r = specifier_printf(target, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_target);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target);
return 0;
}
r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
if (r < 0)
return 0;
r = strv_consume_pair(&partition->copy_files, TAKE_PTR(resolved_source), TAKE_PTR(resolved_target));
if (r < 0)
return log_oom();
return 0;
}
static int config_parse_copy_blocks(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *d = NULL;
Partition *partition = data;
int r;
assert(rvalue);
assert(partition);
if (isempty(rvalue)) {
partition->copy_blocks_path = mfree(partition->copy_blocks_path);
partition->copy_blocks_auto = false;
return 0;
}
if (streq(rvalue, "auto")) {
partition->copy_blocks_path = mfree(partition->copy_blocks_path);
partition->copy_blocks_auto = true;
return 0;
}
r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue);
return 0;
}
r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
if (r < 0)
return 0;
free_and_replace(partition->copy_blocks_path, d);
partition->copy_blocks_auto = false;
return 0;
}
static int config_parse_make_dirs(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Partition *partition = data;
const char *p = rvalue;
int r;
assert(rvalue);
assert(partition);
for (;;) {
_cleanup_free_ char *word = NULL, *d = NULL;
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
return 0;
}
if (r == 0)
return 0;
r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word);
continue;
}
r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
if (r < 0)
continue;
r = strv_consume(&partition->make_directories, TAKE_PTR(d));
if (r < 0)
return log_oom();
}
}
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF, "Invalid encryption mode");
static int config_parse_gpt_flags(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
uint64_t *gpt_flags = data;
int r;
assert(rvalue);
assert(gpt_flags);
r = safe_atou64(rvalue, gpt_flags);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse Flags= value, ignoring: %s", rvalue);
return 0;
}
return 0;
}
static int config_parse_uuid(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Partition *partition = ASSERT_PTR(data);
int r;
if (isempty(rvalue)) {
partition->new_uuid = SD_ID128_NULL;
partition->new_uuid_is_set = false;
return 0;
}
if (streq(rvalue, "null")) {
partition->new_uuid = SD_ID128_NULL;
partition->new_uuid_is_set = true;
return 0;
}
r = sd_id128_from_string(rvalue, &partition->new_uuid);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128bit ID/UUID, ignoring: %s", rvalue);
return 0;
}
partition->new_uuid_is_set = true;
return 0;
}
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_verity, verity_mode, VerityMode, VERITY_OFF, "Invalid verity mode");
static int partition_read_definition(Partition *p, const char *path, const char *const *conf_file_dirs) {
ConfigTableItem table[] = {
{ "Partition", "Type", config_parse_type, 0, &p->type_uuid },
{ "Partition", "Label", config_parse_label, 0, &p->new_label },
{ "Partition", "UUID", config_parse_uuid, 0, p },
{ "Partition", "Priority", config_parse_int32, 0, &p->priority },
{ "Partition", "Weight", config_parse_weight, 0, &p->weight },
{ "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight },
{ "Partition", "SizeMinBytes", config_parse_size4096, 1, &p->size_min },
{ "Partition", "SizeMaxBytes", config_parse_size4096, -1, &p->size_max },
{ "Partition", "PaddingMinBytes", config_parse_size4096, 1, &p->padding_min },
{ "Partition", "PaddingMaxBytes", config_parse_size4096, -1, &p->padding_max },
{ "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset },
{ "Partition", "CopyBlocks", config_parse_copy_blocks, 0, p },
{ "Partition", "Format", config_parse_fstype, 0, &p->format },
{ "Partition", "CopyFiles", config_parse_copy_files, 0, p },
{ "Partition", "MakeDirectories", config_parse_make_dirs, 0, p },
{ "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt },
{ "Partition", "Verity", config_parse_verity, 0, &p->verity },
{ "Partition", "VerityMatchKey", config_parse_string, 0, &p->verity_match_key },
{ "Partition", "Flags", config_parse_gpt_flags, 0, &p->gpt_flags },
{ "Partition", "ReadOnly", config_parse_tristate, 0, &p->read_only },
{ "Partition", "NoAuto", config_parse_tristate, 0, &p->no_auto },
{ "Partition", "GrowFileSystem", config_parse_tristate, 0, &p->growfs },
{}
};
int r;
_cleanup_free_ char *filename = NULL;
const char* dropin_dirname;
r = path_extract_filename(path, &filename);
if (r < 0)
return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);;
dropin_dirname = strjoina(filename, ".d");
r = config_parse_many(
STRV_MAKE_CONST(path),
conf_file_dirs,
dropin_dirname,
"Partition\0",
config_item_table_lookup, table,
CONFIG_PARSE_WARN,
p,
NULL,
&p->drop_in_files);
if (r < 0)
return r;
if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"SizeMinBytes= larger than SizeMaxBytes=, refusing.");
if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
if (sd_id128_is_null(p->type_uuid))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Type= not defined, refusing.");
if ((p->copy_blocks_path || p->copy_blocks_auto) &&
(p->format || !strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing.");
if ((!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)) && streq_ptr(p->format, "swap"))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Format=swap and CopyFiles= cannot be combined, refusing.");
if (!p->format && (!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories) || (p->encrypt != ENCRYPT_OFF && !(p->copy_blocks_path || p->copy_blocks_auto)))) {
/* Pick "ext4" as file system if we are configured to copy files or encrypt the device */
p->format = strdup("ext4");
if (!p->format)
return log_oom();
}
if (p->verity != VERITY_OFF || p->encrypt != ENCRYPT_OFF) {
r = dlopen_cryptsetup();
if (r < 0)
return log_syntax(NULL, LOG_ERR, path, 1, r,
"libcryptsetup not found, Verity=/Encrypt= are not supported: %m");
}
if (p->verity != VERITY_OFF && !p->verity_match_key)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"VerityMatchKey= must be set if Verity=%s", verity_mode_to_string(p->verity));
if (p->verity == VERITY_OFF && p->verity_match_key)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"VerityMatchKey= can only be set if Verity= is not \"%s\"",
verity_mode_to_string(p->verity));
if (p->verity == VERITY_HASH && (p->copy_files || p->copy_blocks_path || p->copy_blocks_auto || p->format || p->make_directories))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"CopyBlocks=/CopyFiles=/Format=/MakeDirectories= cannot be used with Verity=%s",
verity_mode_to_string(p->verity));
if (p->verity != VERITY_OFF && p->encrypt != ENCRYPT_OFF)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Encrypting verity hash/data partitions is not supported");
/* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */
if ((gpt_partition_type_is_root_verity(p->type_uuid) ||
gpt_partition_type_is_usr_verity(p->type_uuid)) &&
p->read_only < 0)
p->read_only = true;
/* Default to "growfs" on, unless read-only */
if (gpt_partition_type_knows_growfs(p->type_uuid) &&
p->read_only <= 0)
p->growfs = true;
return 0;
}
static int find_verity_sibling(Context *context, Partition *p, VerityMode mode, Partition **ret) {
Partition *s = NULL;
assert(p);
assert(p->verity != VERITY_OFF);
assert(p->verity_match_key);
assert(mode != VERITY_OFF);
assert(p->verity != mode);
assert(ret);
/* Try to find the matching sibling partition of the given type for a verity partition. For a data
* partition, this is the corresponding hash partiton with the same verity name (and vice versa for
* the hash partition).
*/
LIST_FOREACH(partitions, q, context->partitions) {
if (p == q)
continue;
if (q->verity != mode)
continue;
assert(q->verity_match_key);
if (!streq(p->verity_match_key, q->verity_match_key))
continue;
if (s)
return -ENOTUNIQ;
s = q;
}
if (!s)
return -ENXIO;
*ret = s;
return 0;
}
static int context_read_definitions(
Context *context,
char **directories,
const char *root) {
_cleanup_strv_free_ char **files = NULL;
Partition *last = NULL;
int r;
const char *const *dirs;
assert(context);
dirs = (const char* const*) (directories ?: CONF_PATHS_STRV("repart.d"));
r = conf_files_list_strv(&files, ".conf", directories ? NULL : root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
if (r < 0)
return log_error_errno(r, "Failed to enumerate *.conf files: %m");
STRV_FOREACH(f, files) {
_cleanup_(partition_freep) Partition *p = NULL;
p = partition_new();
if (!p)
return log_oom();
p->definition_path = strdup(*f);
if (!p->definition_path)
return log_oom();
r = partition_read_definition(p, *f, dirs);
if (r < 0)
return r;
LIST_INSERT_AFTER(partitions, context->partitions, last, p);
last = TAKE_PTR(p);
context->n_partitions++;
}
/* Check that each configured verity hash/data partition has a matching verity data/hash partition. */
LIST_FOREACH(partitions, p, context->partitions) {
if (p->verity == VERITY_OFF)
continue;
for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++) {
Partition *q;
if (p->verity == mode)
continue;
if (p->siblings[mode])
continue;
r = find_verity_sibling(context, p, mode, &q);
if (r == -ENXIO)
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
"Missing verity %s partition for verity %s partition with VerityMatchKey=%s",
verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
if (r == -ENOTUNIQ)
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
"Multiple verity %s partitions found for verity %s partition with VerityMatchKey=%s",
verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
if (r < 0)
return r;
if (q->priority != p->priority)
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
"Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s",
p->priority, q->priority, p->verity_match_key);
p->siblings[mode] = q;
}
}
return 0;
}
static int determine_current_padding(
struct fdisk_context *c,
struct fdisk_table *t,
struct fdisk_partition *p,
uint64_t secsz,
uint64_t grainsz,
uint64_t *ret) {
size_t n_partitions;
uint64_t offset, next = UINT64_MAX;
assert(c);
assert(t);
assert(p);
if (!fdisk_partition_has_end(p))
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!");
offset = fdisk_partition_get_end(p);
assert(offset < UINT64_MAX / secsz);
offset *= secsz;
n_partitions = fdisk_table_get_nents(t);
for (size_t i = 0; i < n_partitions; i++) {
struct fdisk_partition *q;
uint64_t start;
q = fdisk_table_get_partition(t, i);
if (!q)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
if (fdisk_partition_is_used(q) <= 0)
continue;
if (!fdisk_partition_has_start(q))
continue;
start = fdisk_partition_get_start(q);
assert(start < UINT64_MAX / secsz);
start *= secsz;
if (start >= offset && (next == UINT64_MAX || next > start))
next = start;
}
if (next == UINT64_MAX) {
/* No later partition? In that case check the end of the usable area */
next = fdisk_get_last_lba(c);
assert(next < UINT64_MAX);
next++; /* The last LBA is one sector before the end */
assert(next < UINT64_MAX / secsz);
next *= secsz;
if (offset > next)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
}
assert(next >= offset);
offset = round_up_size(offset, grainsz);
next = round_down_size(next, grainsz);
*ret = LESS_BY(next, offset); /* Saturated subtraction, rounding might have fucked things up */
return 0;
}
static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) {
_cleanup_free_ char *ids = NULL;
int r;
if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
return -EINVAL;
ids = new(char, SD_ID128_UUID_STRING_MAX);
if (!ids)
return -ENOMEM;
r = fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) data, ids));
if (r < 0)
return r;
TAKE_PTR(ids);
return 0;
}
static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) {
int r;
r = fdisk_set_ask(c, fdisk_ask_cb, &id);
if (r < 0)
return r;
r = fdisk_set_disklabel_id(c);
if (r < 0)
return r;
return fdisk_set_ask(c, NULL, NULL);
}
static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) {
union {
uint8_t md[SHA256_DIGEST_SIZE];
sd_id128_t id;
} result;
assert(token);
assert(ret);
/* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
* calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
* machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
* the machine ID we don't want to leak. */
hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), result.md);
/* Take the first half, mark it as v4 UUID */
assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
*ret = id128_make_v4_uuid(result.id);
return 0;
}
static int context_load_partition_table(
Context *context,
const char *node,
int *backing_fd) {
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
_cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors;
_cleanup_free_ char *disk_uuid_string = NULL;
bool from_scratch = false;
sd_id128_t disk_uuid;
size_t n_partitions;
unsigned long secsz;
uint64_t grainsz;
int r;
assert(context);
assert(node);
assert(backing_fd);
assert(!context->fdisk_context);
assert(!context->free_areas);
assert(context->start == UINT64_MAX);
assert(context->end == UINT64_MAX);
assert(context->total == UINT64_MAX);
c = fdisk_new_context();
if (!c)
return log_oom();
/* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
* /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
if (*backing_fd < 0)
r = fdisk_assign_device(c, node, arg_dry_run);
else
r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(*backing_fd), arg_dry_run);
if (r == -EINVAL && arg_size_auto) {
struct stat st;
/* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
* it if automatic sizing is requested. */
if (*backing_fd < 0)
r = stat(node, &st);
else
r = fstat(*backing_fd, &st);
if (r < 0)
return log_error_errno(errno, "Failed to stat block device '%s': %m", node);
if (S_ISREG(st.st_mode) && st.st_size == 0) {
/* User the fallback values if we have no better idea */
context->sector_size = 512;
context->grain_size = 4096;
return /* from_scratch = */ true;
}
r = -EINVAL;
}
if (r < 0)
return log_error_errno(r, "Failed to open device '%s': %m", node);
if (*backing_fd < 0) {
/* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
*backing_fd = fd_reopen(fdisk_get_devfd(c), O_RDONLY|O_CLOEXEC);
if (*backing_fd < 0)
return log_error_errno(*backing_fd, "Failed to duplicate fdisk fd: %m");
/* Tell udev not to interfere while we are processing the device */
if (flock(*backing_fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
return log_error_errno(errno, "Failed to lock block device: %m");
}
/* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the
* device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use
* it for all our needs. Note that the values we use ourselves always are in bytes though, thus mean
* the same thing universally. Also note that regardless what kind of sector size is in use we'll
* place partitions at multiples of 4K. */
secsz = fdisk_get_sector_size(c);
/* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
if (secsz < 512 || !ISPOWEROF2(secsz))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz);
/* Use at least 4K, and ensure it's a multiple of the sector size, regardless if that is smaller or
* larger */
grainsz = secsz < 4096 ? 4096 : secsz;
log_debug("Sector size of device is %lu bytes. Using grain size of %" PRIu64 ".", secsz, grainsz);
switch (arg_empty) {
case EMPTY_REFUSE:
/* Refuse empty disks, insist on an existing GPT partition table */
if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", node);
break;
case EMPTY_REQUIRE:
/* Require an empty disk, refuse any existing partition table */
r = fdisk_has_label(c);
if (r < 0)
return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
if (r > 0)
return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", node);
from_scratch = true;
break;
case EMPTY_ALLOW:
/* Allow both an empty disk and an existing partition table, but only GPT */
r = fdisk_has_label(c);
if (r < 0)
return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
if (r > 0) {
if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", node);
} else
from_scratch = true;
break;
case EMPTY_FORCE:
case EMPTY_CREATE:
/* Always reinitiaize the disk, don't consider what there was on the disk before */
from_scratch = true;
break;
}
if (from_scratch) {
r = fdisk_create_disklabel(c, "gpt");
if (r < 0)
return log_error_errno(r, "Failed to create GPT disk label: %m");
r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to set GPT disk label: %m");
goto add_initial_free_area;
}
r = fdisk_get_disklabel_id(c, &disk_uuid_string);
if (r < 0)
return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
r = sd_id128_from_string(disk_uuid_string, &disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m");
if (sd_id128_is_null(disk_uuid)) {
r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
r = fdisk_set_disklabel_id(c);
if (r < 0)
return log_error_errno(r, "Failed to set GPT disk label: %m");
}
r = fdisk_get_partitions(c, &t);
if (r < 0)
return log_error_errno(r, "Failed to acquire partition table: %m");
n_partitions = fdisk_table_get_nents(t);
for (size_t i = 0; i < n_partitions; i++) {
_cleanup_free_ char *label_copy = NULL;
Partition *last = NULL;
struct fdisk_partition *p;
struct fdisk_parttype *pt;
const char *pts, *ids, *label;
uint64_t sz, start;
bool found = false;
sd_id128_t ptid, id;
size_t partno;
p = fdisk_table_get_partition(t, i);
if (!p)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
if (fdisk_partition_is_used(p) <= 0)
continue;
if (fdisk_partition_has_start(p) <= 0 ||
fdisk_partition_has_size(p) <= 0 ||
fdisk_partition_has_partno(p) <= 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
pt = fdisk_partition_get_type(p);
if (!pt)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m");
pts = fdisk_parttype_get_string(pt);
if (!pts)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m");
r = sd_id128_from_string(pts, &ptid);
if (r < 0)
return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts);
ids = fdisk_partition_get_uuid(p);
if (!ids)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID.");
r = sd_id128_from_string(ids, &id);
if (r < 0)
return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids);
label = fdisk_partition_get_name(p);
if (!isempty(label)) {
label_copy = strdup(label);
if (!label_copy)
return log_oom();
}
sz = fdisk_partition_get_size(p);
assert(sz <= UINT64_MAX/secsz);
sz *= secsz;
start = fdisk_partition_get_start(p);
assert(start <= UINT64_MAX/secsz);
start *= secsz;
partno = fdisk_partition_get_partno(p);
if (left_boundary == UINT64_MAX || left_boundary > start)
left_boundary = start;
/* Assign this existing partition to the first partition of the right type that doesn't have
* an existing one assigned yet. */
LIST_FOREACH(partitions, pp, context->partitions) {
last = pp;
if (!sd_id128_equal(pp->type_uuid, ptid))
continue;
if (!pp->current_partition) {
pp->current_uuid = id;
pp->current_size = sz;
pp->offset = start;
pp->partno = partno;
pp->current_label = TAKE_PTR(label_copy);
pp->current_partition = p;
fdisk_ref_partition(p);
r = determine_current_padding(c, t, p, secsz, grainsz, &pp->current_padding);
if (r < 0)
return r;
if (pp->current_padding > 0) {
r = context_add_free_area(context, pp->current_padding, pp);
if (r < 0)
return r;
}
found = true;
break;
}
}
/* If we have no matching definition, create a new one. */
if (!found) {
_cleanup_(partition_freep) Partition *np = NULL;
np = partition_new();
if (!np)
return log_oom();
np->current_uuid = id;
np->type_uuid = ptid;
np->current_size = sz;
np->offset = start;
np->partno = partno;
np->current_label = TAKE_PTR(label_copy);
np->current_partition = p;
fdisk_ref_partition(p);
r = determine_current_padding(c, t, p, secsz, grainsz, &np->current_padding);
if (r < 0)
return r;
if (np->current_padding > 0) {
r = context_add_free_area(context, np->current_padding, np);
if (r < 0)
return r;
}
LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
context->n_partitions++;
}
}
add_initial_free_area:
nsectors = fdisk_get_nsectors(c);
assert(nsectors <= UINT64_MAX/secsz);
nsectors *= secsz;
first_lba = fdisk_get_first_lba(c);
assert(first_lba <= UINT64_MAX/secsz);
first_lba *= secsz;
last_lba = fdisk_get_last_lba(c);
assert(last_lba < UINT64_MAX);
last_lba++;
assert(last_lba <= UINT64_MAX/secsz);
last_lba *= secsz;
assert(last_lba >= first_lba);
if (left_boundary == UINT64_MAX) {
/* No partitions at all? Then the whole disk is up for grabs. */
first_lba = round_up_size(first_lba, grainsz);
last_lba = round_down_size(last_lba, grainsz);
if (last_lba > first_lba) {
r = context_add_free_area(context, last_lba - first_lba, NULL);
if (r < 0)
return r;
}
} else {
/* Add space left of first partition */
assert(left_boundary >= first_lba);
first_lba = round_up_size(first_lba, grainsz);
left_boundary = round_down_size(left_boundary, grainsz);
last_lba = round_down_size(last_lba, grainsz);
if (left_boundary > first_lba) {
r = context_add_free_area(context, left_boundary - first_lba, NULL);
if (r < 0)
return r;
}
}
context->start = first_lba;
context->end = last_lba;
context->total = nsectors;
context->sector_size = secsz;
context->grain_size = grainsz;
context->fdisk_context = TAKE_PTR(c);
return from_scratch;
}
static void context_unload_partition_table(Context *context) {
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
/* Entirely remove partitions that have no configuration */
if (PARTITION_IS_FOREIGN(p)) {
partition_unlink_and_free(context, p);
continue;
}
/* Otherwise drop all data we read off the block device and everything we might have
* calculated based on it */
p->dropped = false;
p->current_size = UINT64_MAX;
p->new_size = UINT64_MAX;
p->current_padding = UINT64_MAX;
p->new_padding = UINT64_MAX;
p->partno = UINT64_MAX;
p->offset = UINT64_MAX;
if (p->current_partition) {
fdisk_unref_partition(p->current_partition);
p->current_partition = NULL;
}
if (p->new_partition) {
fdisk_unref_partition(p->new_partition);
p->new_partition = NULL;
}
p->padding_area = NULL;
p->allocated_to_area = NULL;
p->current_uuid = SD_ID128_NULL;
p->current_label = mfree(p->current_label);
}
context->start = UINT64_MAX;
context->end = UINT64_MAX;
context->total = UINT64_MAX;
if (context->fdisk_context) {
fdisk_unref_context(context->fdisk_context);
context->fdisk_context = NULL;
}
context_free_free_areas(context);
}
static int format_size_change(uint64_t from, uint64_t to, char **ret) {
char *t;
if (from != UINT64_MAX) {
if (from == to || to == UINT64_MAX)
t = strdup(FORMAT_BYTES(from));
else
t = strjoin(FORMAT_BYTES(from), " ", special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
} else if (to != UINT64_MAX)
t = strjoin(special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
else {
*ret = NULL;
return 0;
}
if (!t)
return log_oom();
*ret = t;
return 1;
}
static const char *partition_label(const Partition *p) {
assert(p);
if (p->new_label)
return p->new_label;
if (p->current_label)
return p->current_label;
return gpt_partition_type_uuid_to_string(p->type_uuid);
}
static int context_dump_partitions(Context *context, const char *node) {
_cleanup_(table_unrefp) Table *t = NULL;
uint64_t sum_padding = 0, sum_size = 0;
int r;
const size_t roothash_col = 13, dropin_files_col = 14;
bool has_roothash = false, has_dropin_files = false;
if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) {
log_info("Empty partition table.");
return 0;
}
t = table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity", "roothash", "drop-in files");
if (!t)
return log_oom();
if (!DEBUG_LOGGING) {
if (arg_json_format_flags & JSON_FORMAT_OFF)
(void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
(size_t) 8, (size_t) 11, roothash_col, dropin_files_col);
else
(void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
(size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10,
(size_t) 12, roothash_col, dropin_files_col);
}
(void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 9), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 10), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100);
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL, *rh = NULL;
char uuid_buffer[SD_ID128_UUID_STRING_MAX];
const char *label, *activity = NULL;
if (p->dropped)
continue;
if (p->current_size == UINT64_MAX)
activity = "create";
else if (p->current_size != p->new_size)
activity = "resize";
label = partition_label(p);
partname = p->partno != UINT64_MAX ? fdisk_partname(node, p->partno+1) : NULL;
r = format_size_change(p->current_size, p->new_size, &size_change);
if (r < 0)
return r;
r = format_size_change(p->current_padding, p->new_padding, &padding_change);
if (r < 0)
return r;
if (p->new_size != UINT64_MAX)
sum_size += p->new_size;
if (p->new_padding != UINT64_MAX)
sum_padding += p->new_padding;
if (p->verity == VERITY_HASH) {
rh = p->roothash ? hexmem(p->roothash, p->roothash_size) : strdup("TBD");
if (!rh)
return log_oom();
}
r = table_add_many(
t,
TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type_uuid, uuid_buffer),
TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
TABLE_UINT64, p->offset,
TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
TABLE_UINT64, p->new_size,
TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL,
TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding,
TABLE_UINT64, p->new_padding,
TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL,
TABLE_STRING, activity ?: "unchanged",
TABLE_STRING, rh,
TABLE_STRV, p->drop_in_files);
if (r < 0)
return table_log_add_error(r);
has_roothash = has_roothash || !isempty(rh);
has_dropin_files = has_dropin_files || !strv_isempty(p->drop_in_files);
}
if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) {
const char *a, *b;
a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size));
b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding));
r = table_add_many(
t,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_STRING, a,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_STRING, b,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY);
if (r < 0)
return table_log_add_error(r);
}
if (!has_roothash) {
r = table_hide_column_from_display(t, roothash_col);
if (r < 0)
return log_error_errno(r, "Failed to set columns to display: %m");
}
if (!has_dropin_files) {
r = table_hide_column_from_display(t, dropin_files_col);
if (r < 0)
return log_error_errno(r, "Failed to set columns to display: %m");
}
return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
}
static void context_bar_char_process_partition(
Context *context,
Partition *bar[],
size_t n,
Partition *p,
size_t *ret_start) {
uint64_t from, to, total;
size_t x, y;
assert(context);
assert(bar);
assert(n > 0);
assert(p);
if (p->dropped)
return;
assert(p->offset != UINT64_MAX);
assert(p->new_size != UINT64_MAX);
from = p->offset;
to = from + p->new_size;
assert(context->total > 0);
total = context->total;
assert(from <= total);
x = from * n / total;
assert(to <= total);
y = to * n / total;
assert(x <= y);
assert(y <= n);
for (size_t i = x; i < y; i++)
bar[i] = p;
*ret_start = x;
}
static int partition_hint(const Partition *p, const char *node, char **ret) {
_cleanup_free_ char *buf = NULL;
const char *label;
sd_id128_t id;
/* Tries really hard to find a suitable description for this partition */
if (p->definition_path) {
buf = strdup(basename(p->definition_path));
goto done;
}
label = partition_label(p);
if (!isempty(label)) {
buf = strdup(label);
goto done;
}
if (p->partno != UINT64_MAX) {
buf = fdisk_partname(node, p->partno+1);
goto done;
}
if (p->new_uuid_is_set)
id = p->new_uuid;
else if (!sd_id128_is_null(p->current_uuid))
id = p->current_uuid;
else
id = p->type_uuid;
buf = strdup(SD_ID128_TO_UUID_STRING(id));
done:
if (!buf)
return -ENOMEM;
*ret = TAKE_PTR(buf);
return 0;
}
static int context_dump_partition_bar(Context *context, const char *node) {
_cleanup_free_ Partition **bar = NULL;
_cleanup_free_ size_t *start_array = NULL;
Partition *last = NULL;
bool z = false;
size_t c, j = 0;
assert_se((c = columns()) >= 2);
c -= 2; /* We do not use the leftmost and rightmost character cell */
bar = new0(Partition*, c);
if (!bar)
return log_oom();
start_array = new(size_t, context->n_partitions);
if (!start_array)
return log_oom();
LIST_FOREACH(partitions, p, context->partitions)
context_bar_char_process_partition(context, bar, c, p, start_array + j++);
putc(' ', stdout);
for (size_t i = 0; i < c; i++) {
if (bar[i]) {
if (last != bar[i])
z = !z;
fputs(z ? ansi_green() : ansi_yellow(), stdout);
fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout);
} else {
fputs(ansi_normal(), stdout);
fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout);
}
last = bar[i];
}
fputs(ansi_normal(), stdout);
putc('\n', stdout);
for (size_t i = 0; i < context->n_partitions; i++) {
_cleanup_free_ char **line = NULL;
line = new0(char*, c);
if (!line)
return log_oom();
j = 0;
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_free_ char *d = NULL;
j++;
if (i < context->n_partitions - j) {
if (line[start_array[j-1]]) {
const char *e;
/* Upgrade final corner to the right with a branch to the right */
e = startswith(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT));
if (e) {
d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e);
if (!d)
return log_oom();
}
}
if (!d) {
d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
if (!d)
return log_oom();
}
} else if (i == context->n_partitions - j) {
_cleanup_free_ char *hint = NULL;
(void) partition_hint(p, node, &hint);
if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)))
d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint));
else
d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT), " ", strna(hint));
if (!d)
return log_oom();
}
if (d)
free_and_replace(line[start_array[j-1]], d);
}
putc(' ', stdout);
j = 0;
while (j < c) {
if (line[j]) {
fputs(line[j], stdout);
j += utf8_console_width(line[j]);
} else {
putc(' ', stdout);
j++;
}
}
putc('\n', stdout);
for (j = 0; j < c; j++)
free(line[j]);
}
return 0;
}
static bool context_has_roothash(Context *context) {
LIST_FOREACH(partitions, p, context->partitions)
if (p->roothash)
return true;
return false;
}
static int context_dump(Context *context, const char *node, bool late) {
int r;
assert(context);
assert(node);
if (arg_pretty == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
return 0;
/* If we're outputting JSON, only dump after doing all operations so we can include the roothashes
* in the output. */
if (!late && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
return 0;
/* If we're not outputting JSON, only dump again after doing all operations if there are any
* roothashes that we need to communicate to the user. */
if (late && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !context_has_roothash(context))
return 0;
r = context_dump_partitions(context, node);
if (r < 0)
return r;
/* Make sure we only write the partition bar once, even if we're writing the partition table twice to
* communicate roothashes. */
if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !late) {
putc('\n', stdout);
r = context_dump_partition_bar(context, node);
if (r < 0)
return r;
putc('\n', stdout);
}
fflush(stdout);
return 0;
}
static bool context_changed(const Context *context) {
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
if (p->allocated_to_area)
return true;
if (p->new_size != p->current_size)
return true;
}
return false;
}
static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) {
_cleanup_(blkid_free_probep) blkid_probe probe = NULL;
int r;
assert(context);
assert(offset != UINT64_MAX);
assert(size != UINT64_MAX);
probe = blkid_new_probe();
if (!probe)
return log_oom();
errno = 0;
r = blkid_probe_set_device(probe, fdisk_get_devfd(context->fdisk_context), offset, size);
if (r < 0)
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping.");
errno = 0;
if (blkid_probe_enable_superblocks(probe, true) < 0 ||
blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
blkid_probe_enable_partitions(probe, true) < 0 ||
blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing.");
for (;;) {
errno = 0;
r = blkid_do_probe(probe);
if (r < 0)
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems.");
if (r > 0)
break;
errno = 0;
if (blkid_do_wipe(probe, false) < 0)
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature.");
}
return 0;
}
static int context_wipe_partition(Context *context, Partition *p) {
int r;
assert(context);
assert(p);
assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
assert(p->offset != UINT64_MAX);
assert(p->new_size != UINT64_MAX);
r = context_wipe_range(context, p->offset, p->new_size);
if (r < 0)
return r;
log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno);
return 0;
}
static int context_discard_range(
Context *context,
uint64_t offset,
uint64_t size) {
struct stat st;
int fd;
assert(context);
assert(offset != UINT64_MAX);
assert(size != UINT64_MAX);
if (size <= 0)
return 0;
assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
if (fstat(fd, &st) < 0)
return -errno;
if (S_ISREG(st.st_mode)) {
if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
if (ERRNO_IS_NOT_SUPPORTED(errno))
return -EOPNOTSUPP;
return -errno;
}
return 1;
}
if (S_ISBLK(st.st_mode)) {
uint64_t range[2], end;
range[0] = round_up_size(offset, context->sector_size);
if (offset > UINT64_MAX - size)
return -ERANGE;
end = offset + size;
if (end <= range[0])
return 0;
range[1] = round_down_size(end - range[0], context->sector_size);
if (range[1] <= 0)
return 0;
if (ioctl(fd, BLKDISCARD, range) < 0) {
if (ERRNO_IS_NOT_SUPPORTED(errno))
return -EOPNOTSUPP;
return -errno;
}
return 1;
}
return -EOPNOTSUPP;
}
static int context_discard_partition(Context *context, Partition *p) {
int r;
assert(context);
assert(p);
assert(p->offset != UINT64_MAX);
assert(p->new_size != UINT64_MAX);
assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
if (!arg_discard)
return 0;
r = context_discard_range(context, p->offset, p->new_size);
if (r == -EOPNOTSUPP) {
log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno);
return 0;
}
if (r == -EBUSY) {
/* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
log_info("Block device is busy, not discarding partition %" PRIu64 " because it probably is mounted.", p->partno);
return 0;
}
if (r == 0) {
log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno);
log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno);
return 1;
}
static int context_discard_gap_after(Context *context, Partition *p) {
uint64_t gap, next = UINT64_MAX;
int r;
assert(context);
assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
if (p)
gap = p->offset + p->new_size;
else
gap = context->start;
LIST_FOREACH(partitions, q, context->partitions) {
if (q->dropped)
continue;
assert(q->offset != UINT64_MAX);
assert(q->new_size != UINT64_MAX);
if (q->offset < gap)
continue;
if (next == UINT64_MAX || q->offset < next)
next = q->offset;
}
if (next == UINT64_MAX) {
next = context->end;
if (gap > next)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
}
assert(next >= gap);
r = context_discard_range(context, gap, next - gap);
if (r == -EOPNOTSUPP) {
if (p)
log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno);
else
log_info("Storage does not support discard, not discarding gap at beginning of disk.");
return 0;
}
if (r == 0) /* Too short */
return 0;
if (r < 0) {
if (p)
return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno);
else
return log_error_errno(r, "Failed to discard gap at beginning of disk.");
}
if (p)
log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno);
else
log_info("Successfully discarded gap at beginning of disk.");
return 0;
}
static int context_wipe_and_discard(Context *context, bool from_scratch) {
int r;
assert(context);
/* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
* we were supposed to start from scratch anyway, as in that case we just discard the whole block
* device in one go early on. */
LIST_FOREACH(partitions, p, context->partitions) {
if (!p->allocated_to_area)
continue;
r = context_wipe_partition(context, p);
if (r < 0)
return r;
if (!from_scratch) {
r = context_discard_partition(context, p);
if (r < 0)
return r;
r = context_discard_gap_after(context, p);
if (r < 0)
return r;
}
}
if (!from_scratch) {
r = context_discard_gap_after(context, NULL);
if (r < 0)
return r;
}
return 0;
}
static int partition_encrypt(
Context *context,
Partition *p,
const char *node,
struct crypt_device **ret_cd,
char **ret_volume,
int *ret_fd) {
#if HAVE_LIBCRYPTSETUP
_cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
_cleanup_(erase_and_freep) void *volume_key = NULL;
_cleanup_free_ char *dm_name = NULL, *vol = NULL;
size_t volume_key_size = 256 / 8;
sd_id128_t uuid;
int r;
assert(context);
assert(p);
assert(p->encrypt != ENCRYPT_OFF);
log_debug("Encryption mode for partition %" PRIu64 ": %s", p->partno, encrypt_mode_to_string(p->encrypt));
r = dlopen_cryptsetup();
if (r < 0)
return log_error_errno(r, "libcryptsetup not found, cannot encrypt: %m");
if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0)
return log_oom();
if (ret_volume) {
vol = path_join("/dev/mapper/", dm_name);
if (!vol)
return log_oom();
}
r = derive_uuid(p->new_uuid, "luks-uuid", &uuid);
if (r < 0)
return r;
log_info("Encrypting future partition %" PRIu64 "...", p->partno);
volume_key = malloc(volume_key_size);
if (!volume_key)
return log_oom();
r = crypto_random_bytes(volume_key, volume_key_size);
if (r < 0)
return log_error_errno(r, "Failed to generate volume key: %m");
r = sym_crypt_init(&cd, node);
if (r < 0)
return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
cryptsetup_enable_logging(cd);
r = sym_crypt_format(cd,
CRYPT_LUKS2,
"aes",
"xts-plain64",
SD_ID128_TO_UUID_STRING(uuid),
volume_key,
volume_key_size,
&(struct crypt_params_luks2) {
.label = strempty(p->new_label),
.sector_size = context->sector_size,
});
if (r < 0)
return log_error_errno(r, "Failed to LUKS2 format future partition: %m");
if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) {
r = sym_crypt_keyslot_add_by_volume_key(
cd,
CRYPT_ANY_SLOT,
volume_key,
volume_key_size,
strempty(arg_key),
arg_key_size);
if (r < 0)
return log_error_errno(r, "Failed to add LUKS2 key: %m");
}
if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) {
#if HAVE_TPM2
_cleanup_(erase_and_freep) char *base64_encoded = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
_cleanup_(erase_and_freep) void *secret = NULL;
_cleanup_free_ void *pubkey = NULL;
_cleanup_free_ void *blob = NULL, *hash = NULL;
size_t secret_size, blob_size, hash_size, pubkey_size = 0;
uint16_t pcr_bank, primary_alg;
int keyslot;
if (arg_tpm2_public_key_pcr_mask != 0) {
r = tpm2_load_pcr_public_key(arg_tpm2_public_key, &pubkey, &pubkey_size);
if (r < 0) {
if (arg_tpm2_public_key || r != -ENOENT)
return log_error_errno(r, "Failed read TPM PCR public key: %m");
log_debug_errno(r, "Failed to read TPM2 PCR public key, proceeding without: %m");
arg_tpm2_public_key_pcr_mask = 0;
}
}
r = tpm2_seal(arg_tpm2_device,
arg_tpm2_pcr_mask,
pubkey, pubkey_size,
arg_tpm2_public_key_pcr_mask,
/* pin= */ NULL,
&secret, &secret_size,
&blob, &blob_size,
&hash, &hash_size,
&pcr_bank,
&primary_alg);
if (r < 0)
return log_error_errno(r, "Failed to seal to TPM2: %m");
r = base64mem(secret, secret_size, &base64_encoded);
if (r < 0)
return log_error_errno(r, "Failed to base64 encode secret key: %m");
r = cryptsetup_set_minimal_pbkdf(cd);
if (r < 0)
return log_error_errno(r, "Failed to set minimal PBKDF: %m");
keyslot = sym_crypt_keyslot_add_by_volume_key(
cd,
CRYPT_ANY_SLOT,
volume_key,
volume_key_size,
base64_encoded,
strlen(base64_encoded));
if (keyslot < 0)
return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
r = tpm2_make_luks2_json(
keyslot,
arg_tpm2_pcr_mask,
pcr_bank,
pubkey, pubkey_size,
arg_tpm2_public_key_pcr_mask,
primary_alg,
blob, blob_size,
hash, hash_size,
0,
&v);
if (r < 0)
return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
r = cryptsetup_add_token_json(cd, v);
if (r < 0)
return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m");
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Support for TPM2 enrollment not enabled.");
#endif
}
r = sym_crypt_activate_by_volume_key(
cd,
dm_name,
volume_key,
volume_key_size,
arg_discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
if (r < 0)
return log_error_errno(r, "Failed to activate LUKS superblock: %m");
log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno);
if (ret_fd) {
_cleanup_close_ int dev_fd = -1;
dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY);
if (dev_fd < 0)
return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol);
*ret_fd = TAKE_FD(dev_fd);
}
if (ret_cd)
*ret_cd = TAKE_PTR(cd);
if (ret_volume)
*ret_volume = TAKE_PTR(vol);
return 0;
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot encrypt: %m");
#endif
}
static int deactivate_luks(struct crypt_device *cd, const char *node) {
#if HAVE_LIBCRYPTSETUP
int r;
if (!cd)
return 0;
assert(node);
/* udev or so might access out block device in the background while we are done. Let's hence force
* detach the volume. We sync'ed before, hence this should be safe. */
r = sym_crypt_deactivate_by_name(cd, basename(node), CRYPT_DEACTIVATE_FORCE);
if (r < 0)
return log_error_errno(r, "Failed to deactivate LUKS device: %m");
return 1;
#else
return 0;
#endif
}
static int context_copy_blocks(Context *context) {
int whole_fd = -1, r;
assert(context);
/* Copy in file systems on the block level */
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
_cleanup_free_ char *encrypted = NULL;
_cleanup_close_ int encrypted_dev_fd = -1;
int target_fd;
if (p->copy_blocks_fd < 0)
continue;
if (p->dropped)
continue;
if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
continue;
assert(p->new_size != UINT64_MAX);
assert(p->copy_blocks_size != UINT64_MAX);
assert(p->new_size >= p->copy_blocks_size);
if (whole_fd < 0)
assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
if (p->encrypt != ENCRYPT_OFF) {
r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
if (r < 0)
return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
if (r < 0)
return log_error_errno(r, "Failed to encrypt device: %m");
if (flock(encrypted_dev_fd, LOCK_EX) < 0)
return log_error_errno(errno, "Failed to lock LUKS device: %m");
target_fd = encrypted_dev_fd;
} else {
if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
return log_error_errno(errno, "Failed to seek to partition offset: %m");
target_fd = whole_fd;
}
log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".",
p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno);
r = copy_bytes_full(p->copy_blocks_fd, target_fd, p->copy_blocks_size, 0, NULL, NULL, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
if (fsync(target_fd) < 0)
return log_error_errno(errno, "Failed to synchronize copied data blocks: %m");
if (p->encrypt != ENCRYPT_OFF) {
encrypted_dev_fd = safe_close(encrypted_dev_fd);
r = deactivate_luks(cd, encrypted);
if (r < 0)
return r;
sym_crypt_free(cd);
cd = NULL;
r = loop_device_sync(d);
if (r < 0)
return log_error_errno(r, "Failed to sync loopback device: %m");
}
log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
}
return 0;
}
static int do_copy_files(Partition *p, const char *fs) {
int r;
assert(p);
assert(fs);
STRV_FOREACH_PAIR(source, target, p->copy_files) {
_cleanup_close_ int sfd = -1, pfd = -1, tfd = -1;
sfd = chase_symlinks_and_open(*source, arg_root, CHASE_PREFIX_ROOT|CHASE_WARN, O_CLOEXEC|O_NOCTTY, NULL);
if (sfd < 0)
return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_root), *source);
r = fd_verify_regular(sfd);
if (r < 0) {
if (r != -EISDIR)
return log_error_errno(r, "Failed to check type of source file '%s': %m", *source);
/* We are looking at a directory */
tfd = chase_symlinks_and_open(*target, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
if (tfd < 0) {
_cleanup_free_ char *dn = NULL, *fn = NULL;
if (tfd != -ENOENT)
return log_error_errno(tfd, "Failed to open target directory '%s': %m", *target);
r = path_extract_filename(*target, &fn);
if (r < 0)
return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
r = path_extract_directory(*target, &dn);
if (r < 0)
return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
if (r < 0)
return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
if (pfd < 0)
return log_error_errno(pfd, "Failed to open parent directory of target: %m");
r = copy_tree_at(
sfd, ".",
pfd, fn,
UID_INVALID, GID_INVALID,
COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS);
} else
r = copy_tree_at(
sfd, ".",
tfd, ".",
UID_INVALID, GID_INVALID,
COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS);
if (r < 0)
return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
} else {
_cleanup_free_ char *dn = NULL, *fn = NULL;
/* We are looking at a regular file */
r = path_extract_filename(*target, &fn);
if (r == -EADDRNOTAVAIL || r == O_DIRECTORY)
return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
"Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", *target, *source);
if (r < 0)
return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
r = path_extract_directory(*target, &dn);
if (r < 0)
return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
if (r < 0)
return log_error_errno(r, "Failed to create parent directory: %m");
pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
if (pfd < 0)
return log_error_errno(pfd, "Failed to open parent directory of target: %m");
tfd = openat(pfd, fn, O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0700);
if (tfd < 0)
return log_error_errno(errno, "Failed to create target file '%s': %m", *target);
r = copy_bytes(sfd, tfd, UINT64_MAX, COPY_REFLINK|COPY_SIGINT);
if (r < 0)
return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
(void) copy_xattr(sfd, tfd, COPY_ALL_XATTRS);
(void) copy_access(sfd, tfd);
(void) copy_times(sfd, tfd, 0);
}
}
return 0;
}
static int do_make_directories(Partition *p, const char *fs) {
int r;
assert(p);
assert(fs);
STRV_FOREACH(d, p->make_directories) {
r = mkdir_p_root(fs, *d, UID_INVALID, GID_INVALID, 0755);
if (r < 0)
return log_error_errno(r, "Failed to create directory '%s' in file system: %m", *d);
}
return 0;
}
static int partition_populate(Partition *p, const char *node) {
int r;
assert(p);
assert(node);
if (strv_isempty(p->copy_files) && strv_isempty(p->make_directories))
return 0;
log_info("Populating partition %" PRIu64 " with files.", p->partno);
/* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
* appear in the host namespace. Hence we fork a child that has its own file system namespace and
* detached mount propagation. */
r = safe_fork("(sd-copy)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
if (r < 0)
return r;
if (r == 0) {
static const char fs[] = "/run/systemd/mount-root";
/* This is a child process with its own mount namespace and propagation to host turned off */
r = mkdir_p(fs, 0700);
if (r < 0) {
log_error_errno(r, "Failed to create mount point: %m");
_exit(EXIT_FAILURE);
}
if (mount_nofollow_verbose(LOG_ERR, node, fs, p->format, MS_NOATIME|MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL) < 0)
_exit(EXIT_FAILURE);
if (do_copy_files(p, fs) < 0)
_exit(EXIT_FAILURE);
if (do_make_directories(p, fs) < 0)
_exit(EXIT_FAILURE);
r = syncfs_path(AT_FDCWD, fs);
if (r < 0) {
log_error_errno(r, "Failed to synchronize written files: %m");
_exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
}
log_info("Successfully populated partition %" PRIu64 " with files.", p->partno);
return 0;
}
static int context_mkfs(Context *context) {
int fd = -1, r;
assert(context);
/* Make a file system */
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
_cleanup_free_ char *encrypted = NULL;
_cleanup_close_ int encrypted_dev_fd = -1;
const char *fsdev;
sd_id128_t fs_uuid;
if (p->dropped)
continue;
if (PARTITION_EXISTS(p)) /* Never format existing partitions */
continue;
if (!p->format)
continue;
assert(p->offset != UINT64_MAX);
assert(p->new_size != UINT64_MAX);
if (fd < 0)
assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
/* Loopback block devices are not only useful to turn regular files into block devices, but
* also to cut out sections of block devices into new block devices. */
r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
if (r < 0)
return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
if (p->encrypt != ENCRYPT_OFF) {
r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
if (r < 0)
return log_error_errno(r, "Failed to encrypt device: %m");
if (flock(encrypted_dev_fd, LOCK_EX) < 0)
return log_error_errno(errno, "Failed to lock LUKS device: %m");
fsdev = encrypted;
} else
fsdev = d->node;
log_info("Formatting future partition %" PRIu64 ".", p->partno);
/* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
* keyed off the partition UUID. */
r = derive_uuid(p->new_uuid, "file-system-uuid", &fs_uuid);
if (r < 0)
return r;
r = make_filesystem(fsdev, p->format, strempty(p->new_label), fs_uuid, arg_discard);
if (r < 0) {
encrypted_dev_fd = safe_close(encrypted_dev_fd);
(void) deactivate_luks(cd, encrypted);
return r;
}
log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
/* The file system is now created, no need to delay udev further */
if (p->encrypt != ENCRYPT_OFF)
if (flock(encrypted_dev_fd, LOCK_UN) < 0)
return log_error_errno(errno, "Failed to unlock LUKS device: %m");
r = partition_populate(p, fsdev);
if (r < 0) {
encrypted_dev_fd = safe_close(encrypted_dev_fd);
(void) deactivate_luks(cd, encrypted);
return r;
}
/* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
* if we don't sync before detaching a block device the in-flight sectors possibly won't hit
* the disk. */
if (p->encrypt != ENCRYPT_OFF) {
if (fsync(encrypted_dev_fd) < 0)
return log_error_errno(errno, "Failed to synchronize LUKS volume: %m");
encrypted_dev_fd = safe_close(encrypted_dev_fd);
r = deactivate_luks(cd, encrypted);
if (r < 0)
return r;
sym_crypt_free(cd);
cd = NULL;
}
r = loop_device_sync(d);
if (r < 0)
return log_error_errno(r, "Failed to sync loopback device: %m");
}
return 0;
}
static int do_verity_format(
LoopDevice *data_device,
LoopDevice *hash_device,
uint64_t sector_size,
uint8_t **ret_roothash,
size_t *ret_roothash_size) {
#if HAVE_LIBCRYPTSETUP
_cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
_cleanup_free_ uint8_t *rh = NULL;
size_t rhs;
int r;
assert(data_device);
assert(hash_device);
assert(sector_size > 0);
assert(ret_roothash);
assert(ret_roothash_size);
r = dlopen_cryptsetup();
if (r < 0)
return log_error_errno(r, "libcryptsetup not found, cannot setup verity: %m");
r = sym_crypt_init(&cd, hash_device->node);
if (r < 0)
return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
r = sym_crypt_format(
cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0,
&(struct crypt_params_verity){
.data_device = data_device->node,
.flags = CRYPT_VERITY_CREATE_HASH,
.hash_name = "sha256",
.hash_type = 1,
.data_block_size = sector_size,
.hash_block_size = sector_size,
.salt_size = 32,
});
if (r < 0)
return log_error_errno(r, "Failed to setup verity hash data: %m");
r = sym_crypt_get_volume_key_size(cd);
if (r < 0)
return log_error_errno(r, "Failed to determine verity root hash size: %m");
rhs = (size_t) r;
rh = malloc(rhs);
if (!rh)
return log_oom();
r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, (char *) rh, &rhs, NULL, 0);
if (r < 0)
return log_error_errno(r, "Failed to get verity root hash: %m");
*ret_roothash = TAKE_PTR(rh);
*ret_roothash_size = rhs;
return 0;
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot setup verity: %m");
#endif
}
static int context_verity(Context *context) {
int fd = -1, r;
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
Partition *dp;
_cleanup_(loop_device_unrefp) LoopDevice *hash_device = NULL, *data_device = NULL;
_cleanup_free_ uint8_t *rh = NULL;
size_t rhs = 0; /* Initialize to work around for GCC false positive. */
if (p->dropped)
continue;
if (PARTITION_EXISTS(p)) /* Never format existing partitions */
continue;
if (p->verity != VERITY_HASH)
continue;
assert_se(dp = p->siblings[VERITY_DATA]);
assert(!dp->dropped);
if (fd < 0)
assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
r = loop_device_make(fd, O_RDONLY, dp->offset, dp->new_size, 0, LOCK_EX, &data_device);
if (r < 0)
return log_error_errno(r,
"Failed to make loopback device of verity data partition %" PRIu64 ": %m",
p->partno);
r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &hash_device);
if (r < 0)
return log_error_errno(r,
"Failed to make loopback device of verity hash partition %" PRIu64 ": %m",
p->partno);
r = do_verity_format(data_device, hash_device, context->sector_size, &rh, &rhs);
if (r < 0)
return r;
assert(rhs >= sizeof(sd_id128_t) * 2);
if (!dp->new_uuid_is_set) {
memcpy_safe(dp->new_uuid.bytes, rh, sizeof(sd_id128_t));
dp->new_uuid_is_set = true;
}
if (!p->new_uuid_is_set) {
memcpy_safe(p->new_uuid.bytes, rh + rhs - sizeof(sd_id128_t), sizeof(sd_id128_t));
p->new_uuid_is_set = true;
}
p->roothash = TAKE_PTR(rh);
p->roothash_size = rhs;
}
return 0;
}
static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) {
struct {
sd_id128_t type_uuid;
uint64_t counter;
} _packed_ plaintext = {};
union {
uint8_t md[SHA256_DIGEST_SIZE];
sd_id128_t id;
} result;
uint64_t k = 0;
int r;
assert(context);
assert(p);
assert(ret);
/* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
* hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
* HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
* installation we are processing, but if random behaviour is desired can be random, too. We use the
* seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
* and the partition type as plaintext. The partition type is suffixed with a counter (only for the
* second and later partition of the same type) if we have more than one partition of the same
* time. Or in other words:
*
* With:
* SEED := /etc/machine-id
*
* If first partition instance of type TYPE_UUID:
* PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
*
* For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
* PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
*/
LIST_FOREACH(partitions, q, context->partitions) {
if (p == q)
break;
if (!sd_id128_equal(p->type_uuid, q->type_uuid))
continue;
k++;
}
plaintext.type_uuid = p->type_uuid;
plaintext.counter = htole64(k);
hmac_sha256(context->seed.bytes, sizeof(context->seed.bytes),
&plaintext,
k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext),
result.md);
/* Take the first half, mark it as v4 UUID */
assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
result.id = id128_make_v4_uuid(result.id);
/* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
LIST_FOREACH(partitions, q, context->partitions) {
if (p == q)
continue;
if (sd_id128_in_set(result.id, q->current_uuid, q->new_uuid)) {
log_warning("Partition UUID calculated from seed for partition %" PRIu64 " already used, reverting to randomized UUID.", p->partno);
r = sd_id128_randomize(&result.id);
if (r < 0)
return log_error_errno(r, "Failed to generate randomized UUID: %m");
break;
}
}
*ret = result.id;
return 0;
}
static int partition_acquire_label(Context *context, Partition *p, char **ret) {
_cleanup_free_ char *label = NULL;
const char *prefix;
unsigned k = 1;
assert(context);
assert(p);
assert(ret);
prefix = gpt_partition_type_uuid_to_string(p->type_uuid);
if (!prefix)
prefix = "linux";
for (;;) {
const char *ll = label ?: prefix;
bool retry = false;
LIST_FOREACH(partitions, q, context->partitions) {
if (p == q)
break;
if (streq_ptr(ll, q->current_label) ||
streq_ptr(ll, q->new_label)) {
retry = true;
break;
}
}
if (!retry)
break;
label = mfree(label);
if (asprintf(&label, "%s-%u", prefix, ++k) < 0)
return log_oom();
}
if (!label) {
label = strdup(prefix);
if (!label)
return log_oom();
}
*ret = TAKE_PTR(label);
return 0;
}
static int context_acquire_partition_uuids_and_labels(Context *context) {
int r;
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
/* Never touch foreign partitions */
if (PARTITION_IS_FOREIGN(p)) {
p->new_uuid = p->current_uuid;
if (p->current_label) {
r = free_and_strdup_warn(&p->new_label, strempty(p->current_label));
if (r < 0)
return r;
}
continue;
}
if (!sd_id128_is_null(p->current_uuid))
p->new_uuid = p->current_uuid; /* Never change initialized UUIDs */
else if (!p->new_uuid_is_set && p->verity == VERITY_OFF) {
/* Not explicitly set by user! */
r = partition_acquire_uuid(context, p, &p->new_uuid);
if (r < 0)
return r;
p->new_uuid_is_set = true;
}
if (!isempty(p->current_label)) {
/* never change initialized labels */
r = free_and_strdup_warn(&p->new_label, p->current_label);
if (r < 0)
return r;
} else if (!p->new_label) {
/* Not explicitly set by user! */
r = partition_acquire_label(context, p, &p->new_label);
if (r < 0)
return r;
}
}
return 0;
}
static int set_gpt_flags(struct fdisk_partition *q, uint64_t flags) {
_cleanup_free_ char *a = NULL;
for (unsigned i = 0; i < sizeof(flags) * 8; i++) {
uint64_t bit = UINT64_C(1) << i;
char buf[DECIMAL_STR_MAX(unsigned)+1];
if (!FLAGS_SET(flags, bit))
continue;
xsprintf(buf, "%u", i);
if (!strextend_with_separator(&a, ",", buf))
return -ENOMEM;
}
return fdisk_partition_set_attrs(q, a);
}
static uint64_t partition_merge_flags(Partition *p) {
uint64_t f;
assert(p);
f = p->gpt_flags;
if (p->no_auto >= 0) {
if (gpt_partition_type_knows_no_auto(p->type_uuid))
SET_FLAG(f, GPT_FLAG_NO_AUTO, p->no_auto);
else {
char buffer[SD_ID128_UUID_STRING_MAX];
log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
yes_no(p->no_auto),
gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
}
}
if (p->read_only >= 0) {
if (gpt_partition_type_knows_read_only(p->type_uuid))
SET_FLAG(f, GPT_FLAG_READ_ONLY, p->read_only);
else {
char buffer[SD_ID128_UUID_STRING_MAX];
log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
yes_no(p->read_only),
gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
}
}
if (p->growfs >= 0) {
if (gpt_partition_type_knows_growfs(p->type_uuid))
SET_FLAG(f, GPT_FLAG_GROWFS, p->growfs);
else {
char buffer[SD_ID128_UUID_STRING_MAX];
log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
yes_no(p->growfs),
gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
}
}
return f;
}
static int context_mangle_partitions(Context *context) {
int r;
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
assert(p->new_size != UINT64_MAX);
assert(p->offset != UINT64_MAX);
assert(p->partno != UINT64_MAX);
if (PARTITION_EXISTS(p)) {
bool changed = false;
assert(p->current_partition);
if (p->new_size != p->current_size) {
assert(p->new_size >= p->current_size);
assert(p->new_size % context->sector_size == 0);
r = fdisk_partition_size_explicit(p->current_partition, true);
if (r < 0)
return log_error_errno(r, "Failed to enable explicit sizing: %m");
r = fdisk_partition_set_size(p->current_partition, p->new_size / context->sector_size);
if (r < 0)
return log_error_errno(r, "Failed to grow partition: %m");
log_info("Growing existing partition %" PRIu64 ".", p->partno);
changed = true;
}
if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
r = fdisk_partition_set_uuid(p->current_partition, SD_ID128_TO_UUID_STRING(p->new_uuid));
if (r < 0)
return log_error_errno(r, "Failed to set partition UUID: %m");
log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno);
changed = true;
}
if (!streq_ptr(p->new_label, p->current_label)) {
r = fdisk_partition_set_name(p->current_partition, strempty(p->new_label));
if (r < 0)
return log_error_errno(r, "Failed to set partition label: %m");
log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno);
changed = true;
}
if (changed) {
assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */
r = fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition);
if (r < 0)
return log_error_errno(r, "Failed to update partition: %m");
}
} else {
_cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL;
_cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
assert(!p->new_partition);
assert(p->offset % context->sector_size == 0);
assert(p->new_size % context->sector_size == 0);
assert(p->new_label);
t = fdisk_new_parttype();
if (!t)
return log_oom();
r = fdisk_parttype_set_typestr(t, SD_ID128_TO_UUID_STRING(p->type_uuid));
if (r < 0)
return log_error_errno(r, "Failed to initialize partition type: %m");
q = fdisk_new_partition();
if (!q)
return log_oom();
r = fdisk_partition_set_type(q, t);
if (r < 0)
return log_error_errno(r, "Failed to set partition type: %m");
r = fdisk_partition_size_explicit(q, true);
if (r < 0)
return log_error_errno(r, "Failed to enable explicit sizing: %m");
r = fdisk_partition_set_start(q, p->offset / context->sector_size);
if (r < 0)
return log_error_errno(r, "Failed to position partition: %m");
r = fdisk_partition_set_size(q, p->new_size / context->sector_size);
if (r < 0)
return log_error_errno(r, "Failed to grow partition: %m");
r = fdisk_partition_set_partno(q, p->partno);
if (r < 0)
return log_error_errno(r, "Failed to set partition number: %m");
r = fdisk_partition_set_uuid(q, SD_ID128_TO_UUID_STRING(p->new_uuid));
if (r < 0)
return log_error_errno(r, "Failed to set partition UUID: %m");
r = fdisk_partition_set_name(q, strempty(p->new_label));
if (r < 0)
return log_error_errno(r, "Failed to set partition label: %m");
/* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */
r = set_gpt_flags(q, partition_merge_flags(p));
if (r < 0)
return log_error_errno(r, "Failed to set GPT partition flags: %m");
log_info("Adding new partition %" PRIu64 " to partition table.", p->partno);
r = fdisk_add_partition(context->fdisk_context, q, NULL);
if (r < 0)
return log_error_errno(r, "Failed to add partition: %m");
assert(!p->new_partition);
p->new_partition = TAKE_PTR(q);
}
}
return 0;
}
static int context_write_partition_table(
Context *context,
const char *node,
bool from_scratch) {
_cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL;
int capable, r;
assert(context);
if (!from_scratch && !context_changed(context)) {
log_info("No changes.");
return 0;
}
if (arg_dry_run) {
log_notice("Refusing to repartition, please re-run with --dry-run=no.");
return 0;
}
log_info("Applying changes.");
if (from_scratch) {
r = context_wipe_range(context, 0, context->total);
if (r < 0)
return r;
log_info("Wiped block device.");
r = context_discard_range(context, 0, context->total);
if (r == -EOPNOTSUPP)
log_info("Storage does not support discard, not discarding entire block device data.");
else if (r < 0)
return log_error_errno(r, "Failed to discard entire block device: %m");
else if (r > 0)
log_info("Discarded entire block device.");
}
r = fdisk_get_partitions(context->fdisk_context, &original_table);
if (r < 0)
return log_error_errno(r, "Failed to acquire partition table: %m");
/* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
* gaps between partitions, just to be sure. */
r = context_wipe_and_discard(context, from_scratch);
if (r < 0)
return r;
r = context_copy_blocks(context);
if (r < 0)
return r;
r = context_mkfs(context);
if (r < 0)
return r;
r = context_verity(context);
if (r < 0)
return r;
r = context_mangle_partitions(context);
if (r < 0)
return r;
log_info("Writing new partition table.");
r = fdisk_write_disklabel(context->fdisk_context);
if (r < 0)
return log_error_errno(r, "Failed to write partition table: %m");
capable = blockdev_partscan_enabled(fdisk_get_devfd(context->fdisk_context));
if (capable == -ENOTBLK)
log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
else if (capable < 0)
return log_error_errno(capable, "Failed to check if block device supports partition scanning: %m");
else if (capable > 0) {
log_info("Telling kernel to reread partition table.");
if (from_scratch)
r = fdisk_reread_partition_table(context->fdisk_context);
else
r = fdisk_reread_changes(context->fdisk_context, original_table);
if (r < 0)
return log_error_errno(r, "Failed to reread partition table: %m");
} else
log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
log_info("All done.");
return 0;
}
static int context_read_seed(Context *context, const char *root) {
int r;
assert(context);
if (!sd_id128_is_null(context->seed))
return 0;
if (!arg_randomize) {
_cleanup_close_ int fd = -1;
fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, NULL);
if (fd == -ENOENT)
log_info("No machine ID set, using randomized partition UUIDs.");
else if (fd < 0)
return log_error_errno(fd, "Failed to determine machine ID of image: %m");
else {
r = id128_read_fd(fd, ID128_PLAIN_OR_UNINIT, &context->seed);
if (r == -ENOMEDIUM)
log_info("No machine ID set, using randomized partition UUIDs.");
else if (r < 0)
return log_error_errno(r, "Failed to parse machine ID of image: %m");
return 0;
}
}
r = sd_id128_randomize(&context->seed);
if (r < 0)
return log_error_errno(r, "Failed to generate randomized seed: %m");
return 0;
}
static int context_factory_reset(Context *context, bool from_scratch) {
size_t n = 0;
int r;
assert(context);
if (arg_factory_reset <= 0)
return 0;
if (from_scratch) /* Nothing to reset if we start from scratch */
return 0;
if (arg_dry_run) {
log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
return 0;
}
log_info("Applying factory reset.");
LIST_FOREACH(partitions, p, context->partitions) {
if (!p->factory_reset || !PARTITION_EXISTS(p))
continue;
assert(p->partno != UINT64_MAX);
log_info("Removing partition %" PRIu64 " for factory reset.", p->partno);
r = fdisk_delete_partition(context->fdisk_context, p->partno);
if (r < 0)
return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno);
n++;
}
if (n == 0) {
log_info("Factory reset requested, but no partitions to delete found.");
return 0;
}
r = fdisk_write_disklabel(context->fdisk_context);
if (r < 0)
return log_error_errno(r, "Failed to write disk label: %m");
log_info("Successfully deleted %zu partitions.", n);
return 1;
}
static int context_can_factory_reset(Context *context) {
assert(context);
LIST_FOREACH(partitions, p, context->partitions)
if (p->factory_reset && PARTITION_EXISTS(p))
return true;
return false;
}
static int resolve_copy_blocks_auto_candidate(
dev_t partition_devno,
sd_id128_t partition_type_uuid,
dev_t restrict_devno,
sd_id128_t *ret_uuid) {
_cleanup_(blkid_free_probep) blkid_probe b = NULL;
_cleanup_close_ int fd = -1;
_cleanup_free_ char *p = NULL;
const char *pttype, *t;
sd_id128_t pt_parsed, u;
blkid_partition pp;
dev_t whole_devno;
blkid_partlist pl;
int r;
/* Checks if the specified partition has the specified GPT type UUID, and is located on the specified
* 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is
* backed by two separate partitions: the data and the hash partitions, and we need to find the right
* one of the two. */
r = block_get_whole_disk(partition_devno, &whole_devno);
if (r < 0)
return log_error_errno(
r,
"Unable to determine containing block device of partition %u:%u: %m",
major(partition_devno), minor(partition_devno));
if (restrict_devno != (dev_t) -1 &&
restrict_devno != whole_devno)
return log_error_errno(
SYNTHETIC_ERRNO(EPERM),
"Partition %u:%u is located outside of block device %u:%u, refusing.",
major(partition_devno), minor(partition_devno),
major(restrict_devno), minor(restrict_devno));
fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p);
if (r < 0)
return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(whole_devno));
b = blkid_new_probe();
if (!b)
return log_oom();
errno = 0;
r = blkid_probe_set_device(b, fd, 0, 0);
if (r != 0)
return log_error_errno(errno_or_else(ENOMEM), "Failed to open block device '%s': %m", p);
(void) blkid_probe_enable_partitions(b, 1);
(void) blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
errno = 0;
r = blkid_do_safeprobe(b);
if (IN_SET(r, -2, 1)) { /* nothing found or ambiguous result */
log_debug("Didn't find partition table on block device '%s'.", p);
return false;
}
if (r != 0)
return log_error_errno(errno_or_else(EIO), "Unable to probe for partition table of '%s': %m", p);
(void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
if (!streq_ptr(pttype, "gpt")) {
log_debug("Didn't find a GPT partition table on '%s'.", p);
return false;
}
errno = 0;
pl = blkid_probe_get_partitions(b);
if (!pl)
return log_error_errno(errno_or_else(EIO), "Unable read partition table of '%s': %m", p);
errno = 0;
pp = blkid_partlist_devno_to_partition(pl, partition_devno);
if (!pp) {
log_debug("Partition %u:%u has no matching partition table entry on '%s'.",
major(partition_devno), minor(partition_devno), p);
return false;
}
t = blkid_partition_get_type_string(pp);
if (isempty(t)) {
log_debug("Partition %u:%u has no type on '%s'.",
major(partition_devno), minor(partition_devno), p);
return false;
}
r = sd_id128_from_string(t, &pt_parsed);
if (r < 0) {
log_debug_errno(r, "Failed to parse partition type \"%s\": %m", t);
return false;
}
if (!sd_id128_equal(pt_parsed, partition_type_uuid)) {
log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR " (needed: " SD_ID128_FORMAT_STR "), ignoring.",
major(partition_devno), minor(partition_devno),
SD_ID128_FORMAT_VAL(pt_parsed), SD_ID128_FORMAT_VAL(partition_type_uuid));
return false;
}
t = blkid_partition_get_uuid(pp);
if (isempty(t)) {
log_debug("Partition %u:%u has no UUID.",
major(partition_devno), minor(partition_devno));
return false;
}
r = sd_id128_from_string(t, &u);
if (r < 0) {
log_debug_errno(r, "Failed to parse partition UUID \"%s\": %m", t);
return false;
}
log_debug("Automatically found partition %u:%u of right type " SD_ID128_FORMAT_STR ".",
major(partition_devno), minor(partition_devno),
SD_ID128_FORMAT_VAL(pt_parsed));
if (ret_uuid)
*ret_uuid = u;
return true;
}
static int find_backing_devno(
const char *path,
const char *root,
dev_t *ret) {
_cleanup_free_ char *resolved = NULL;
int r;
assert(path);
r = chase_symlinks(path, root, CHASE_PREFIX_ROOT, &resolved, NULL);
if (r < 0)
return r;
r = path_is_mount_point(resolved, NULL, 0);
if (r < 0)
return r;
if (r == 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
return -ENOENT;
r = get_block_device(resolved, ret);
if (r < 0)
return r;
if (r == 0) /* Not backed by physical file system, we can't use this */
return -ENOENT;
return 0;
}
static int resolve_copy_blocks_auto(
sd_id128_t type_uuid,
const char *root,
dev_t restrict_devno,
dev_t *ret_devno,
sd_id128_t *ret_uuid) {
const char *try1 = NULL, *try2 = NULL;
char p[SYS_BLOCK_PATH_MAX("/slaves")];
_cleanup_(closedirp) DIR *d = NULL;
sd_id128_t found_uuid = SD_ID128_NULL;
dev_t devno, found = 0;
int r;
/* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the
* --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all,
* and restrict block device references in the --image= case to loopback block device we set up.
*
* restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and
* thus declares which device (and its partition subdevices) we shall limit access to. If
* restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if
* it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */
if (restrict_devno == 0)
return log_error_errno(SYNTHETIC_ERRNO(EPERM),
"Automatic discovery of backing block devices not permitted in --root= mode, refusing.");
/* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching
* partitions in the host, using the appropriate directory as key and ensuring that the partition
* type matches. */
if (gpt_partition_type_is_root(type_uuid))
try1 = "/";
else if (gpt_partition_type_is_usr(type_uuid))
try1 = "/usr/";
else if (gpt_partition_type_is_root_verity(type_uuid))
try1 = "/";
else if (gpt_partition_type_is_usr_verity(type_uuid))
try1 = "/usr/";
else if (sd_id128_equal(type_uuid, GPT_ESP)) {
try1 = "/efi/";
try2 = "/boot/";
} else if (sd_id128_equal(type_uuid, GPT_XBOOTLDR))
try1 = "/boot/";
else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Partition type " SD_ID128_FORMAT_STR " not supported from automatic source block device discovery.",
SD_ID128_FORMAT_VAL(type_uuid));
r = find_backing_devno(try1, root, &devno);
if (r == -ENOENT && try2)
r = find_backing_devno(try2, root, &devno);
if (r < 0)
return log_error_errno(r, "Failed to resolve automatic CopyBlocks= path for partition type " SD_ID128_FORMAT_STR ", sorry: %m",
SD_ID128_FORMAT_VAL(type_uuid));
xsprintf_sys_block_path(p, "/slaves", devno);
d = opendir(p);
if (d) {
struct dirent *de;
for (;;) {
_cleanup_free_ char *q = NULL, *t = NULL;
sd_id128_t u;
dev_t sl;
errno = 0;
de = readdir_no_dot(d);
if (!de) {
if (errno != 0)
return log_error_errno(errno, "Failed to read directory '%s': %m", p);
break;
}
if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
continue;
q = path_join(p, de->d_name, "/dev");
if (!q)
return log_oom();
r = read_one_line_file(q, &t);
if (r < 0)
return log_error_errno(r, "Failed to read %s: %m", q);
r = parse_devnum(t, &sl);
if (r < 0) {
log_debug_errno(r, "Failed to parse %s, ignoring: %m", q);
continue;
}
if (major(sl) == 0) {
log_debug_errno(r, "Device backing %s is special, ignoring: %m", q);
continue;
}
r = resolve_copy_blocks_auto_candidate(sl, type_uuid, restrict_devno, &u);
if (r < 0)
return r;
if (r > 0) {
/* We found a matching one! */
if (found != 0)
return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
"Multiple matching partitions found, refusing.");
found = sl;
found_uuid = u;
}
}
} else if (errno != ENOENT)
return log_error_errno(errno, "Failed open %s: %m", p);
else {
r = resolve_copy_blocks_auto_candidate(devno, type_uuid, restrict_devno, &found_uuid);
if (r < 0)
return r;
if (r > 0)
found = devno;
}
if (found == 0)
return log_error_errno(SYNTHETIC_ERRNO(ENXIO),
"Unable to automatically discover suitable partition to copy blocks from.");
if (ret_devno)
*ret_devno = found;
if (ret_uuid)
*ret_uuid = found_uuid;
return 0;
}
static int context_open_copy_block_paths(
Context *context,
const char *root,
dev_t restrict_devno) {
int r;
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_close_ int source_fd = -1;
_cleanup_free_ char *opened = NULL;
sd_id128_t uuid = SD_ID128_NULL;
uint64_t size;
struct stat st;
assert(p->copy_blocks_fd < 0);
assert(p->copy_blocks_size == UINT64_MAX);
if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */
continue;
if (p->copy_blocks_path) {
source_fd = chase_symlinks_and_open(p->copy_blocks_path, root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
if (source_fd < 0)
return log_error_errno(source_fd, "Failed to open '%s': %m", p->copy_blocks_path);
if (fstat(source_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
if (!S_ISREG(st.st_mode) && restrict_devno != (dev_t) -1)
return log_error_errno(SYNTHETIC_ERRNO(EPERM),
"Copying from block device node is not permitted in --image=/--root= mode, refusing.");
} else if (p->copy_blocks_auto) {
dev_t devno;
r = resolve_copy_blocks_auto(p->type_uuid, root, restrict_devno, &devno, &uuid);
if (r < 0)
return r;
source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
if (r < 0)
return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
if (fstat(source_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
} else
continue;
if (S_ISDIR(st.st_mode)) {
_cleanup_free_ char *bdev = NULL;
dev_t devt;
/* If the file is a directory, automatically find the backing block device */
if (major(st.st_dev) != 0)
devt = st.st_dev;
else {
/* Special support for btrfs */
r = btrfs_get_block_device_fd(source_fd, &devt);
if (r == -EUCLEAN)
return btrfs_log_dev_root(LOG_ERR, r, opened);
if (r < 0)
return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened);
}
safe_close(source_fd);
source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev);
if (r < 0)
return log_error_errno(r, "Failed to open block device backing '%s': %m", opened);
if (fstat(source_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
}
if (S_ISREG(st.st_mode))
size = st.st_size;
else if (S_ISBLK(st.st_mode)) {
if (ioctl(source_fd, BLKGETSIZE64, &size) != 0)
return log_error_errno(errno, "Failed to determine size of block device to copy from: %m");
} else
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing: %m", opened);
if (size <= 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", opened);
if (size % 512 != 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", opened);
p->copy_blocks_fd = TAKE_FD(source_fd);
p->copy_blocks_size = size;
free_and_replace(p->copy_blocks_path, opened);
/* When copying from an existing partition copy that partitions UUID if none is configured explicitly */
if (!p->new_uuid_is_set && !sd_id128_is_null(uuid)) {
p->new_uuid = uuid;
p->new_uuid_is_set = true;
}
}
return 0;
}
static int help(void) {
_cleanup_free_ char *link = NULL;
int r;
r = terminal_urlify_man("systemd-repart", "1", &link);
if (r < 0)
return log_oom();
printf("%s [OPTIONS...] [DEVICE]\n"
"\n%sGrow and add partitions to partition table.%s\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" --no-legend Do not show the headers and footers\n"
" --dry-run=BOOL Whether to run dry-run operation\n"
" --empty=MODE One of refuse, allow, require, force, create; controls\n"
" how to handle empty disks lacking partition tables\n"
" --discard=BOOL Whether to discard backing blocks for new partitions\n"
" --pretty=BOOL Whether to show pretty summary before doing changes\n"
" --factory-reset=BOOL Whether to remove data partitions before recreating\n"
" them\n"
" --can-factory-reset Test whether factory reset is defined\n"
" --root=PATH Operate relative to root path\n"
" --image=PATH Operate relative to image file\n"
" --definitions=DIR Find partition definitions in specified directory\n"
" --key-file=PATH Key to use when encrypting partitions\n"
" --tpm2-device=PATH Path to TPM2 device node to use\n"
" --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
" TPM2 PCR indexes to use for TPM2 enrollment\n"
" --tpm2-public-key=PATH\n"
" Enroll signed TPM2 PCR policy against PEM public key\n"
" --tpm2-public-key-pcrs=PCR1+PCR2+PCR3+…\n"
" Enroll signed TPM2 PCR policy for specified TPM2 PCRs\n"
" --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
" --size=BYTES Grow loopback file to specified size\n"
" --json=pretty|short|off\n"
" Generate JSON output\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
ansi_highlight(),
ansi_normal(),
link);
return 0;
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_NO_PAGER,
ARG_NO_LEGEND,
ARG_DRY_RUN,
ARG_EMPTY,
ARG_DISCARD,
ARG_FACTORY_RESET,
ARG_CAN_FACTORY_RESET,
ARG_ROOT,
ARG_IMAGE,
ARG_SEED,
ARG_PRETTY,
ARG_DEFINITIONS,
ARG_SIZE,
ARG_JSON,
ARG_KEY_FILE,
ARG_TPM2_DEVICE,
ARG_TPM2_PCRS,
ARG_TPM2_PUBLIC_KEY,
ARG_TPM2_PUBLIC_KEY_PCRS,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
{ "dry-run", required_argument, NULL, ARG_DRY_RUN },
{ "empty", required_argument, NULL, ARG_EMPTY },
{ "discard", required_argument, NULL, ARG_DISCARD },
{ "factory-reset", required_argument, NULL, ARG_FACTORY_RESET },
{ "can-factory-reset", no_argument, NULL, ARG_CAN_FACTORY_RESET },
{ "root", required_argument, NULL, ARG_ROOT },
{ "image", required_argument, NULL, ARG_IMAGE },
{ "seed", required_argument, NULL, ARG_SEED },
{ "pretty", required_argument, NULL, ARG_PRETTY },
{ "definitions", required_argument, NULL, ARG_DEFINITIONS },
{ "size", required_argument, NULL, ARG_SIZE },
{ "json", required_argument, NULL, ARG_JSON },
{ "key-file", required_argument, NULL, ARG_KEY_FILE },
{ "tpm2-device", required_argument, NULL, ARG_TPM2_DEVICE },
{ "tpm2-pcrs", required_argument, NULL, ARG_TPM2_PCRS },
{ "tpm2-public-key", required_argument, NULL, ARG_TPM2_PUBLIC_KEY },
{ "tpm2-public-key-pcrs", required_argument, NULL, ARG_TPM2_PUBLIC_KEY_PCRS },
{}
};
int c, r, dry_run = -1;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
switch (c) {
case 'h':
return help();
case ARG_VERSION:
return version();
case ARG_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
case ARG_NO_LEGEND:
arg_legend = false;
break;
case ARG_DRY_RUN:
r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run);
if (r < 0)
return r;
break;
case ARG_EMPTY:
if (isempty(optarg) || streq(optarg, "refuse"))
arg_empty = EMPTY_REFUSE;
else if (streq(optarg, "allow"))
arg_empty = EMPTY_ALLOW;
else if (streq(optarg, "require"))
arg_empty = EMPTY_REQUIRE;
else if (streq(optarg, "force"))
arg_empty = EMPTY_FORCE;
else if (streq(optarg, "create")) {
arg_empty = EMPTY_CREATE;
if (dry_run < 0)
dry_run = false; /* Imply --dry-run=no if we create the loopback file
* anew. After all we cannot really break anyone's
* partition tables that way. */
} else
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to parse --empty= parameter: %s", optarg);
break;
case ARG_DISCARD:
r = parse_boolean_argument("--discard=", optarg, &arg_discard);
if (r < 0)
return r;
break;
case ARG_FACTORY_RESET:
r = parse_boolean_argument("--factory-reset=", optarg, NULL);
if (r < 0)
return r;
arg_factory_reset = r;
break;
case ARG_CAN_FACTORY_RESET:
arg_can_factory_reset = true;
break;
case ARG_ROOT:
r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
if (r < 0)
return r;
break;
case ARG_IMAGE:
r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
if (r < 0)
return r;
break;
case ARG_SEED:
if (isempty(optarg)) {
arg_seed = SD_ID128_NULL;
arg_randomize = false;
} else if (streq(optarg, "random"))
arg_randomize = true;
else {
r = sd_id128_from_string(optarg, &arg_seed);
if (r < 0)
return log_error_errno(r, "Failed to parse seed: %s", optarg);
arg_randomize = false;
}
break;
case ARG_PRETTY:
r = parse_boolean_argument("--pretty=", optarg, NULL);
if (r < 0)
return r;
arg_pretty = r;
break;
case ARG_DEFINITIONS: {
_cleanup_free_ char *path = NULL;
r = parse_path_argument(optarg, false, &path);
if (r < 0)
return r;
if (strv_consume(&arg_definitions, TAKE_PTR(path)) < 0)
return log_oom();
break;
}
case ARG_SIZE: {
uint64_t parsed, rounded;
if (streq(optarg, "auto")) {
arg_size = UINT64_MAX;
arg_size_auto = true;
break;
}
r = parse_size(optarg, 1024, &parsed);
if (r < 0)
return log_error_errno(r, "Failed to parse --size= parameter: %s", optarg);
rounded = round_up_size(parsed, 4096);
if (rounded == 0)
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing.");
if (rounded == UINT64_MAX)
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing.");
if (rounded != parsed)
log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " %s %" PRIu64 ")",
parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), rounded);
arg_size = rounded;
arg_size_auto = false;
break;
}
case ARG_JSON:
r = parse_json_argument(optarg, &arg_json_format_flags);
if (r <= 0)
return r;
break;
case ARG_KEY_FILE: {
_cleanup_(erase_and_freep) char *k = NULL;
size_t n = 0;
r = read_full_file_full(
AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
NULL,
&k, &n);
if (r < 0)
return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
erase_and_free(arg_key);
arg_key = TAKE_PTR(k);
arg_key_size = n;
break;
}
case ARG_TPM2_DEVICE: {
_cleanup_free_ char *device = NULL;
if (streq(optarg, "list"))
return tpm2_list_devices();
if (!streq(optarg, "auto")) {
device = strdup(optarg);
if (!device)
return log_oom();
}
free(arg_tpm2_device);
arg_tpm2_device = TAKE_PTR(device);
break;
}
case ARG_TPM2_PCRS:
r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask);
if (r < 0)
return r;
break;
case ARG_TPM2_PUBLIC_KEY:
r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_tpm2_public_key);
if (r < 0)
return r;
break;
case ARG_TPM2_PUBLIC_KEY_PCRS:
r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_public_key_pcr_mask);
if (r < 0)
return r;
break;
case '?':
return -EINVAL;
default:
assert_not_reached();
}
if (argc - optind > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Expected at most one argument, the path to the block device.");
if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
if (arg_can_factory_reset)
arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence
* non-dry-run mode makes no sense. Thus, imply dry run mode so that we
* open things strictly read-only. */
else if (dry_run >= 0)
arg_dry_run = dry_run;
if (arg_empty == EMPTY_CREATE && (arg_size == UINT64_MAX && !arg_size_auto))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"If --empty=create is specified, --size= must be specified, too.");
if (arg_image && arg_root)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
else if (!arg_image && !arg_root && in_initrd()) {
/* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
* former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
* is vendor-supplied but the root fs formatted on first boot. */
r = path_is_mount_point("/sysusr/usr", NULL, 0);
if (r <= 0) {
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");
arg_root = strdup("/sysroot");
} else
arg_root = strdup("/sysusr");
if (!arg_root)
return log_oom();
}
arg_node = argc > optind ? argv[optind] : NULL;
if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node && !arg_image)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
if (arg_tpm2_pcr_mask == UINT32_MAX)
arg_tpm2_pcr_mask = TPM2_PCR_MASK_DEFAULT;
if (arg_tpm2_public_key_pcr_mask == UINT32_MAX)
arg_tpm2_public_key_pcr_mask = UINT32_C(1) << TPM_PCR_INDEX_KERNEL_IMAGE;
if (arg_pretty < 0 && isatty(STDOUT_FILENO))
arg_pretty = true;
return 1;
}
static int parse_proc_cmdline_factory_reset(void) {
bool b;
int r;
if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
return 0;
if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
return 0;
r = proc_cmdline_get_bool("systemd.factory_reset", &b);
if (r < 0)
return log_error_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m");
if (r > 0) {
arg_factory_reset = b;
if (b)
log_notice("Honouring factory reset requested via kernel command line.");
}
return 0;
}
static int parse_efi_variable_factory_reset(void) {
_cleanup_free_ char *value = NULL;
int r;
if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
return 0;
if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
return 0;
r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset), &value);
if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
return 0;
if (r < 0)
return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
r = parse_boolean(value);
if (r < 0)
return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m");
arg_factory_reset = r;
if (r)
log_notice("Factory reset requested via EFI variable FactoryReset.");
return 0;
}
static int remove_efi_variable_factory_reset(void) {
int r;
r = efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset), NULL, 0);
if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
return 0;
if (r < 0)
return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
log_info("Successfully unset EFI variable FactoryReset.");
return 0;
}
static int acquire_root_devno(
const char *p,
const char *root,
int mode,
char **ret,
int *ret_fd) {
_cleanup_free_ char *found_path = NULL;
dev_t devno, fd_devno = MODE_INVALID;
_cleanup_close_ int fd = -1;
struct stat st;
int r;
assert(p);
assert(ret);
assert(ret_fd);
fd = chase_symlinks_and_open(p, root, CHASE_PREFIX_ROOT, mode, &found_path);
if (fd < 0)
return fd;
if (fstat(fd, &st) < 0)
return -errno;
if (S_ISREG(st.st_mode)) {
*ret = TAKE_PTR(found_path);
*ret_fd = TAKE_FD(fd);
return 0;
}
if (S_ISBLK(st.st_mode)) {
/* Refuse referencing explicit block devices if a root dir is specified, after all we should
* not be able to leave the image the root path constrains us to. */
if (root)
return -EPERM;
fd_devno = devno = st.st_rdev;
} else if (S_ISDIR(st.st_mode)) {
devno = st.st_dev;
if (major(devno) == 0) {
r = btrfs_get_block_device_fd(fd, &devno);
if (r == -ENOTTY) /* not btrfs */
return -ENODEV;
if (r < 0)
return r;
}
} else
return -ENOTBLK;
/* From dm-crypt to backing partition */
r = block_get_originating(devno, &devno);
if (r == -ENOENT)
log_debug_errno(r, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p);
else if (r < 0)
log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p);
/* From partition to whole disk containing it */
r = block_get_whole_disk(devno, &devno);
if (r < 0)
log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
r = devname_from_devnum(S_IFBLK, devno, ret);
if (r < 0)
return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
/* Only if we still look at the same block device we can reuse the fd. Otherwise return an
* invalidated fd. */
*ret_fd = fd_devno != MODE_INVALID && fd_devno == devno ? TAKE_FD(fd) : -1;
return 0;
}
static int find_root(char **ret, int *ret_fd) {
_cleanup_free_ char *device = NULL;
int r;
assert(ret);
assert(ret_fd);
if (arg_node) {
if (arg_empty == EMPTY_CREATE) {
_cleanup_close_ int fd = -1;
_cleanup_free_ char *s = NULL;
s = strdup(arg_node);
if (!s)
return log_oom();
fd = open(arg_node, O_RDONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, 0666);
if (fd < 0)
return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
*ret = TAKE_PTR(s);
*ret_fd = TAKE_FD(fd);
return 0;
}
/* Note that we don't specify a root argument here: if the user explicitly configured a node
* we'll take it relative to the host, not the image */
r = acquire_root_devno(arg_node, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
if (r == -EUCLEAN)
return btrfs_log_dev_root(LOG_ERR, r, arg_node);
if (r < 0)
return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", arg_node);
return 0;
}
assert(IN_SET(arg_empty, EMPTY_REFUSE, EMPTY_ALLOW));
/* If the root mount has been replaced by some form of volatile file system (overlayfs), the
* original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
* here. */
r = readlink_malloc("/run/systemd/volatile-root", &device);
if (r == -ENOENT) { /* volatile-root not found */
/* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
* latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
* (think: volatile setups) */
FOREACH_STRING(p, "/", "/usr") {
r = acquire_root_devno(p, arg_root, O_RDONLY|O_DIRECTORY|O_CLOEXEC, ret, ret_fd);
if (r < 0) {
if (r == -EUCLEAN)
return btrfs_log_dev_root(LOG_ERR, r, p);
if (r != -ENODEV)
return log_error_errno(r, "Failed to determine backing device of %s: %m", p);
} else
return 0;
}
} else if (r < 0)
return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m");
else {
r = acquire_root_devno(device, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
if (r == -EUCLEAN)
return btrfs_log_dev_root(LOG_ERR, r, device);
if (r < 0)
return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", device);
return 0;
}
return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device.");
}
static int resize_pt(int fd) {
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
int r;
/* After resizing the backing file we need to resize the partition table itself too, so that it takes
* possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
* immediately write it again, with no changes. */
c = fdisk_new_context();
if (!c)
return log_oom();
r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), 0);
if (r < 0)
return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd));
r = fdisk_has_label(c);
if (r < 0)
return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd));
if (r == 0) {
log_debug("Not resizing partition table, as there currently is none.");
return 0;
}
r = fdisk_write_disklabel(c);
if (r < 0)
return log_error_errno(r, "Failed to write resized partition table: %m");
log_info("Resized partition table.");
return 1;
}
static int resize_backing_fd(
const char *node, /* The primary way we access the disk image to operate on */
int *fd, /* An O_RDONLY fd referring to that inode */
const char *backing_file, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
LoopDevice *loop_device) {
_cleanup_close_ int writable_fd = -1;
uint64_t current_size;
struct stat st;
int r;
assert(node);
assert(fd);
if (arg_size == UINT64_MAX) /* Nothing to do */
return 0;
if (*fd < 0) {
/* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
* keep a reference to the file we can pass around. */
*fd = open(node, O_RDONLY|O_CLOEXEC);
if (*fd < 0)
return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node);
}
if (fstat(*fd, &st) < 0)
return log_error_errno(errno, "Failed to stat '%s': %m", node);
if (S_ISBLK(st.st_mode)) {
if (!backing_file)
return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot resize block device '%s'.", node);
assert(loop_device);
if (ioctl(*fd, BLKGETSIZE64, ¤t_size) < 0)
return log_error_errno(errno, "Failed to determine size of block device %s: %m", node);
} else {
r = stat_verify_regular(&st);
if (r < 0)
return log_error_errno(r, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node);
assert(!backing_file);
assert(!loop_device);
current_size = st.st_size;
}
if (current_size >= arg_size) {
log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
return 0;
}
if (S_ISBLK(st.st_mode)) {
assert(backing_file);
/* This is a loopback device. We can't really grow those directly, but we can grow the
* backing file, hence let's do that. */
writable_fd = open(backing_file, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
if (writable_fd < 0)
return log_error_errno(errno, "Failed to open backing file '%s': %m", backing_file);
if (fstat(writable_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat() backing file '%s': %m", backing_file);
r = stat_verify_regular(&st);
if (r < 0)
return log_error_errno(r, "Backing file '%s' of block device is not a regular file: %m", backing_file);
if ((uint64_t) st.st_size != current_size)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
node, backing_file);
} else {
assert(S_ISREG(st.st_mode));
assert(!backing_file);
/* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
* reopen the file for that temporarily. We keep the writable fd only open for this operation though,
* as fdisk can't accept it anyway. */
writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC);
if (writable_fd < 0)
return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node);
}
if (!arg_discard) {
if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
if (!ERRNO_IS_NOT_SUPPORTED(errno))
return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
/* Fallback to truncation, if fallocate() is not supported. */
log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
} else {
if (current_size == 0) /* Likely regular file just created by us */
log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size), node);
else
log_info("File '%s' grown from %s to %s by allocation.",
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
goto done;
}
}
if (ftruncate(writable_fd, arg_size) < 0)
return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
if (current_size == 0) /* Likely regular file just created by us */
log_info("Sized '%s' to %s.", node, FORMAT_BYTES(arg_size));
else
log_info("File '%s' grown from %s to %s by truncation.",
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
done:
r = resize_pt(writable_fd);
if (r < 0)
return r;
if (loop_device) {
r = loop_device_refresh_size(loop_device, UINT64_MAX, arg_size);
if (r < 0)
return log_error_errno(r, "Failed to update loop device size: %m");
}
return 1;
}
static int determine_auto_size(Context *c) {
uint64_t sum;
assert(c);
sum = round_up_size(GPT_METADATA_SIZE, 4096);
LIST_FOREACH(partitions, p, c->partitions) {
uint64_t m;
if (p->dropped)
continue;
m = partition_min_size_with_padding(c, p);
if (m > UINT64_MAX - sum)
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Image would grow too large, refusing.");
sum += m;
}
if (c->total != UINT64_MAX)
/* Image already allocated? Then show its size. */
log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
FORMAT_BYTES(sum), FORMAT_BYTES(c->total));
else
/* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
log_info("Automatically determined minimal disk image size as %s.",
FORMAT_BYTES(sum));
arg_size = sum;
return 0;
}
static int run(int argc, char *argv[]) {
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
_cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
_cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
_cleanup_(context_freep) Context* context = NULL;
_cleanup_free_ char *node = NULL;
_cleanup_close_ int backing_fd = -1;
bool from_scratch, node_is_our_loop = false;
int r;
log_show_color(true);
log_parse_environment();
log_open();
r = parse_argv(argc, argv);
if (r <= 0)
return r;
r = parse_proc_cmdline_factory_reset();
if (r < 0)
return r;
r = parse_efi_variable_factory_reset();
if (r < 0)
return r;
#if HAVE_LIBCRYPTSETUP
cryptsetup_enable_logging(NULL);
#endif
if (arg_image) {
assert(!arg_root);
/* Mount this strictly read-only: we shall modify the partition table, not the file
* systems */
r = mount_image_privately_interactively(
arg_image,
DISSECT_IMAGE_MOUNT_READ_ONLY |
(arg_node ? DISSECT_IMAGE_DEVICE_READ_ONLY : 0) | /* If a different node to make changes to is specified let's open the device in read-only mode) */
DISSECT_IMAGE_GPT_ONLY |
DISSECT_IMAGE_RELAX_VAR_CHECK |
DISSECT_IMAGE_USR_NO_ROOT |
DISSECT_IMAGE_REQUIRE_ROOT,
&mounted_dir,
&loop_device,
&decrypted_image);
if (r < 0)
return r;
arg_root = strdup(mounted_dir);
if (!arg_root)
return log_oom();
if (!arg_node) {
arg_node = strdup(loop_device->node);
if (!arg_node)
return log_oom();
/* Remember that the device we are about to manipulate is actually the one we
* allocated here, and thus to increase its backing file we know what to do */
node_is_our_loop = true;
}
}
context = context_new(arg_seed);
if (!context)
return log_oom();
strv_uniq(arg_definitions);
r = context_read_definitions(context, arg_definitions, arg_root);
if (r < 0)
return r;
if (context->n_partitions <= 0 && arg_empty == EMPTY_REFUSE) {
log_info("Didn't find any partition definition files, nothing to do.");
return 0;
}
r = find_root(&node, &backing_fd);
if (r < 0)
return r;
if (arg_size != UINT64_MAX) {
r = resize_backing_fd(
node,
&backing_fd,
node_is_our_loop ? arg_image : NULL,
node_is_our_loop ? loop_device : NULL);
if (r < 0)
return r;
}
r = context_load_partition_table(context, node, &backing_fd);
if (r == -EHWPOISON)
return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
* really an error when called at boot. */
if (r < 0)
return r;
from_scratch = r > 0; /* Starting from scratch */
if (arg_can_factory_reset) {
r = context_can_factory_reset(context);
if (r < 0)
return r;
if (r == 0)
return EXIT_FAILURE;
return 0;
}
r = context_factory_reset(context, from_scratch);
if (r < 0)
return r;
if (r > 0) {
/* We actually did a factory reset! */
r = remove_efi_variable_factory_reset();
if (r < 0)
return r;
/* Reload the reduced partition table */
context_unload_partition_table(context);
r = context_load_partition_table(context, node, &backing_fd);
if (r < 0)
return r;
}
#if 0
(void) context_dump_partitions(context, node);
putchar('\n');
#endif
r = context_read_seed(context, arg_root);
if (r < 0)
return r;
/* Open all files to copy blocks from now, since we want to take their size into consideration */
r = context_open_copy_block_paths(
context,
arg_root,
loop_device ? loop_device->devno : /* if --image= is specified, only allow partitions on the loopback device */
arg_root && !arg_image ? 0 : /* if --root= is specified, don't accept any block device */
(dev_t) -1); /* if neither is specified, make no restrictions */
if (r < 0)
return r;
if (arg_size_auto) {
r = determine_auto_size(context);
if (r < 0)
return r;
/* Flush out everything again, and let's grow the file first, then start fresh */
context_unload_partition_table(context);
assert(arg_size != UINT64_MAX);
r = resize_backing_fd(
node,
&backing_fd,
node_is_our_loop ? arg_image : NULL,
node_is_our_loop ? loop_device : NULL);
if (r < 0)
return r;
r = context_load_partition_table(context, node, &backing_fd);
if (r < 0)
return r;
}
/* First try to fit new partitions in, dropping by priority until it fits */
for (;;) {
uint64_t largest_free_area;
if (context_allocate_partitions(context, &largest_free_area))
break; /* Success! */
if (!context_drop_or_foreignize_one_priority(context)) {
r = log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
"Can't fit requested partitions into available free space (%s), refusing.",
FORMAT_BYTES(largest_free_area));
determine_auto_size(context);
return r;
}
}
/* Now assign free space according to the weight logic */
r = context_grow_partitions(context);
if (r < 0)
return r;
/* Now calculate where each new partition gets placed */
context_place_partitions(context);
/* Make sure each partition has a unique UUID and unique label */
r = context_acquire_partition_uuids_and_labels(context);
if (r < 0)
return r;
(void) context_dump(context, node, /*late=*/ false);
r = context_write_partition_table(context, node, from_scratch);
if (r < 0)
return r;
(void) context_dump(context, node, /*late=*/ true);
return 0;
}
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
|