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
|
/*
* Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
* Copyright (C) 2014 Karel Zak <kzak@redhat.com>
*
* This program is free software. You can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation: either Version 1
* or (at your option) any later version.
*
* A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
* patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
* leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
* 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
* This program had (head,sector,cylinder) as basic unit, and was
* (therefore) broken in several ways for the use on larger disks -
* for example, my last patch (from 2.0d to 2.0e) was required
* to allow a partition to cross cylinder 8064, and to write an
* extended partition past the 4GB mark.
*
* Karel Zak wrote new sfdisk based on libfdisk from util-linux
* in 2014.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <libsmartcols.h>
#ifdef HAVE_LIBREADLINE
# define _FUNCTION_DEF
# include <readline/readline.h>
#endif
#include <libgen.h>
#include <sys/time.h>
#include "c.h"
#include "xalloc.h"
#include "nls.h"
#include "debug.h"
#include "strutils.h"
#include "closestream.h"
#include "colors.h"
#include "blkdev.h"
#include "all-io.h"
#include "rpmatch.h"
#include "optutils.h"
#include "ttyutils.h"
#include "libfdisk.h"
#include "fdisk-list.h"
/*
* sfdisk debug stuff (see fdisk.h and include/debug.h)
*/
static UL_DEBUG_DEFINE_MASK(sfdisk);
UL_DEBUG_DEFINE_MASKNAMES(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
#define SFDISKPROG_DEBUG_INIT (1 << 1)
#define SFDISKPROG_DEBUG_PARSE (1 << 2)
#define SFDISKPROG_DEBUG_MISC (1 << 3)
#define SFDISKPROG_DEBUG_ASK (1 << 4)
#define SFDISKPROG_DEBUG_ALL 0xFFFF
#define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
#define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
enum {
ACT_FDISK = 1,
ACT_ACTIVATE,
ACT_CHANGE_ID,
ACT_DUMP,
ACT_LIST,
ACT_LIST_FREE,
ACT_LIST_TYPES,
ACT_REORDER,
ACT_RELOCATE,
ACT_SHOW_SIZE,
ACT_SHOW_GEOM,
ACT_VERIFY,
ACT_PARTTYPE,
ACT_PARTUUID,
ACT_PARTLABEL,
ACT_PARTATTRS,
ACT_DISCARD_FREE,
ACT_DISKID,
ACT_DELETE,
ACT_BACKUP_SECTORS,
};
struct sfdisk {
int act; /* ACT_* */
int partno; /* -N <partno>, default -1 */
int wipemode; /* remove foreign signatures from disk */
int pwipemode; /* remove foreign signatures from partitions */
const char *lockmode; /* as specified by --lock */
const char *label; /* --label <label> */
const char *label_nested; /* --label-nested <label> */
const char *backup_file; /* -O <path> */
const char *move_typescript; /* --movedata <typescript> */
char *prompt;
struct fdisk_context *cxt; /* libfdisk context */
struct fdisk_partition *orig_pa; /* -N <partno> before the change */
unsigned int verify : 1, /* call fdisk_verify_disklabel() */
quiet : 1, /* suppress extra messages */
interactive : 1, /* running on tty */
noreread : 1, /* don't check device is in use */
force : 1, /* do also stupid things */
backup : 1, /* backup sectors before write PT */
container : 1, /* PT contains container (MBR extended) partitions */
unused : 1, /* PT contains unused partition */
append : 1, /* don't create new PT, append partitions only */
json : 1, /* JSON dump */
movedata: 1, /* move data after resize */
movefsync: 1, /* use fsync() after each write() */
notell : 1, /* don't tell kernel about new PT */
noact : 1; /* do not write to device */
};
#define SFDISK_PROMPT ">>> "
static void sfdiskprog_init_debug(void)
{
__UL_INIT_DEBUG_FROM_ENV(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
}
static int get_user_reply(const char *prompt, char *buf, size_t bufsz)
{
char *p;
size_t sz;
#ifdef HAVE_LIBREADLINE
if (isatty(STDIN_FILENO)) {
p = readline(prompt);
if (!p)
return 1;
xstrncpy(buf, p, bufsz);
free(p);
} else
#endif
{
fputs(prompt, stdout);
fflush(stdout);
if (!fgets(buf, bufsz, stdin))
return 1;
}
for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */
if (p > buf)
memmove(buf, p, p - buf); /* remove blank space */
sz = strlen(buf);
if (sz && *(buf + sz - 1) == '\n')
*(buf + sz - 1) = '\0';
DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf));
return 0;
}
static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
struct fdisk_ask *ask,
void *data)
{
struct sfdisk *sf = (struct sfdisk *) data;
int rc = 0;
assert(ask);
switch(fdisk_ask_get_type(ask)) {
case FDISK_ASKTYPE_INFO:
if (sf->quiet)
break;
fputs(fdisk_ask_print_get_mesg(ask), stdout);
fputc('\n', stdout);
break;
case FDISK_ASKTYPE_WARNX:
fflush(stdout);
color_scheme_fenable("warn", UL_COLOR_RED, stderr);
fputs(fdisk_ask_print_get_mesg(ask), stderr);
color_fdisable(stderr);
fputc('\n', stderr);
break;
case FDISK_ASKTYPE_WARN:
fflush(stdout);
color_scheme_fenable("warn", UL_COLOR_RED, stderr);
fputs(fdisk_ask_print_get_mesg(ask), stderr);
errno = fdisk_ask_print_get_errno(ask);
fprintf(stderr, ": %m\n");
color_fdisable(stderr);
break;
case FDISK_ASKTYPE_YESNO:
{
char buf[BUFSIZ] = { '\0' };
fputc('\n', stdout);
do {
int x;
fputs(fdisk_ask_get_query(ask), stdout);
rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf));
if (rc)
break;
x = rpmatch(buf);
if (x == RPMATCH_YES || x == RPMATCH_NO) {
fdisk_ask_yesno_set_result(ask, x);
break;
}
} while(1);
DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc));
break;
}
default:
break;
}
return rc;
}
static void sfdisk_init(struct sfdisk *sf)
{
fdisk_init_debug(0);
scols_init_debug(0);
sfdiskprog_init_debug();
sf->cxt = fdisk_new_context();
if (!sf->cxt)
err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
fdisk_set_ask(sf->cxt, ask_callback, (void *) sf);
if (sf->wipemode != WIPEMODE_ALWAYS)
fdisk_enable_bootbits_protection(sf->cxt, 1);
if (sf->label_nested) {
struct fdisk_context *x = fdisk_new_nested_context(sf->cxt,
sf->label_nested);
if (!x)
err(EXIT_FAILURE, _("failed to allocate nested libfdisk context"));
/* the original context is available by fdisk_get_parent() */
sf->cxt = x;
}
}
static int sfdisk_deinit(struct sfdisk *sf)
{
struct fdisk_context *parent;
assert(sf);
assert(sf->cxt);
parent = fdisk_get_parent(sf->cxt);
if (parent) {
fdisk_unref_context(sf->cxt);
sf->cxt = parent;
}
fdisk_unref_context(sf->cxt);
free(sf->prompt);
memset(sf, 0, sizeof(*sf));
return 0;
}
static struct fdisk_partition *get_partition(struct fdisk_context *cxt, size_t partno)
{
struct fdisk_table *tb = NULL;
struct fdisk_partition *pa;
if (fdisk_get_partitions(cxt, &tb) != 0)
return NULL;
pa = fdisk_table_get_partition_by_partno(tb, partno);
if (pa)
fdisk_ref_partition(pa);
fdisk_unref_table(tb);
return pa;
}
static void backup_sectors(struct sfdisk *sf,
const char *tpl,
const char *name,
const char *devname,
uint64_t offset, size_t size)
{
char *fname;
int fd, devfd;
devfd = fdisk_get_devfd(sf->cxt);
assert(devfd >= 0);
xasprintf(&fname, "%s0x%08"PRIx64".bak", tpl, offset);
fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd < 0)
goto fail;
if (lseek(devfd, (off_t) offset, SEEK_SET) == (off_t) -1) {
fdisk_warn(sf->cxt, _("cannot seek %s"), devname);
goto fail;
} else {
unsigned char *buf = xmalloc(size);
if (read_all(devfd, (char *) buf, size) != (ssize_t) size) {
fdisk_warn(sf->cxt, _("cannot read %s"), devname);
free(buf);
goto fail;
}
if (write_all(fd, buf, size) != 0) {
fdisk_warn(sf->cxt, _("cannot write %s"), fname);
free(buf);
goto fail;
}
free(buf);
}
fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"),
name, (uintmax_t) offset, (uintmax_t) size, fname);
close(fd);
free(fname);
return;
fail:
errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname);
}
static char *mk_backup_filename_tpl(const char *filename, const char *devname, const char *suffix)
{
char *tpl = NULL;
char *name, *buf = xstrdup(devname);
name = basename(buf);
if (!filename || strcmp(filename, "@default") == 0) {
const char *home = getenv ("HOME");
if (!home)
errx(EXIT_FAILURE, _("failed to create a backup file, $HOME undefined"));
xasprintf(&tpl, "%s/sfdisk-%s%s", home, name, suffix);
} else
xasprintf(&tpl, "%s-%s%s", filename, name, suffix);
free(buf);
return tpl;
}
static void backup_partition_table(struct sfdisk *sf, const char *devname)
{
const char *name;
char *tpl;
uint64_t offset = 0;
size_t size = 0;
int i = 0;
assert(sf);
if (!fdisk_has_label(sf->cxt))
return;
tpl = mk_backup_filename_tpl(sf->backup_file, devname, "-");
color_scheme_enable("header", UL_COLOR_BOLD);
fdisk_info(sf->cxt, _("Backup files:"));
color_disable();
while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size)
backup_sectors(sf, tpl, name, devname, offset, size);
if (!sf->quiet)
fputc('\n', stdout);
free(tpl);
}
static int assign_device(struct sfdisk *sf, const char *devname, int rdonly)
{
struct fdisk_context *cxt = sf->cxt;
if (fdisk_assign_device(cxt, devname, rdonly) != 0)
err(EXIT_FAILURE, _("cannot open %s"), devname);
if (!fdisk_is_readonly(cxt)) {
if (blkdev_lock(fdisk_get_devfd(cxt), devname, sf->lockmode) != 0) {
fdisk_deassign_device(cxt, 1);
exit(EXIT_FAILURE);
}
if (sf->backup)
backup_partition_table(sf, devname);
}
return 0;
}
static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_partition *orig_pa)
{
struct fdisk_partition *pa = get_partition(sf->cxt, partno);
char *devname = NULL, *typescript = NULL, *buf = NULL;
FILE *f = NULL;
int ok = 0, fd, backward = 0;
fdisk_sector_t nsectors, from, to, step, i, prev;
size_t io, ss, step_bytes, cc, ioerr = 0;
uintmax_t src, dst, nbytes;
int progress = 0, rc = 0;
struct timeval prev_time;
uint64_t bytes_per_sec = 0;
assert(sf->movedata);
if (!pa)
warnx(_("failed to read new partition from device; ignoring --move-data"));
else if (!fdisk_partition_has_size(pa))
warnx(_("failed to get size of the new partition; ignoring --move-data"));
else if (!fdisk_partition_has_start(pa))
warnx(_("failed to get start of the new partition; ignoring --move-data"));
else if (!fdisk_partition_has_size(orig_pa))
warnx(_("failed to get size of the old partition; ignoring --move-data"));
else if (!fdisk_partition_has_start(orig_pa))
warnx(_("failed to get start of the old partition; ignoring --move-data"));
else if (fdisk_partition_get_start(pa) == fdisk_partition_get_start(orig_pa))
warnx(_("start of the partition has not been moved; ignoring --move-data"));
else if (fdisk_partition_get_size(orig_pa) > fdisk_partition_get_size(pa))
warnx(_("new partition is smaller than original; ignoring --move-data"));
else
ok = 1;
if (!ok)
return -EINVAL;
DBG(MISC, ul_debug("moving data"));
fd = fdisk_get_devfd(sf->cxt);
/* set move direction and overlay */
nsectors = fdisk_partition_get_size(orig_pa);
from = fdisk_partition_get_start(orig_pa);
to = fdisk_partition_get_start(pa);
if ((to >= from && from + nsectors >= to) ||
(from >= to && to + nsectors >= from)) {
/* source and target overlay, check if we need to copy
* backwardly from end of the source */
DBG(MISC, ul_debug("overlay between source and target"));
backward = from < to;
DBG(MISC, ul_debug(" copy order: %s", backward ? "backward" : "forward"));
}
/* set optimal step size -- nearest to 1MiB aligned to optimal I/O */
io = fdisk_get_optimal_iosize(sf->cxt);
ss = fdisk_get_sector_size(sf->cxt);
if (!io)
io = ss;
if (io < 1024 * 1024)
step_bytes = ((1024 * 1024) + io/2) / io * io;
else
step_bytes = io;
step = step_bytes / ss;
nbytes = nsectors * ss;
DBG(MISC, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step, step_bytes));
#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
if (!backward)
ignore_result( posix_fadvise(fd, from * ss,
nsectors * ss, POSIX_FADV_SEQUENTIAL) );
#endif
devname = fdisk_partname(fdisk_get_devname(sf->cxt), partno+1);
if (sf->move_typescript)
typescript = mk_backup_filename_tpl(sf->move_typescript, devname, ".move");
if (!sf->quiet) {
fdisk_info(sf->cxt, "%s", "");
color_scheme_enable("header", UL_COLOR_BOLD);
fdisk_info(sf->cxt, sf->noact ? _("Data move: (--no-act)") : _("Data move:"));
color_disable();
if (typescript)
fdisk_info(sf->cxt, _(" typescript file: %s"), typescript);
printf(_(" start sector: (from/to) %ju / %ju\n"), (uintmax_t) from, (uintmax_t) to);
printf(_(" sectors: %ju\n"), (uintmax_t) nsectors);
printf(_(" step size: %zu bytes\n"), step_bytes);
putchar('\n');
fflush(stdout);
if (isatty(fileno(stdout)))
progress = 1;
}
if (sf->interactive) {
int yes = 0;
fdisk_ask_yesno(sf->cxt, _("Do you want to move partition data?"), &yes);
if (!yes) {
fdisk_info(sf->cxt, _("Leaving."));
free(devname);
return 0;
}
}
if (typescript) {
f = fopen(typescript, "w");
if (!f) {
rc = -errno;
fdisk_warn(sf->cxt, _("cannot open %s"), typescript);
goto done;
}
/* don't translate */
fprintf(f, "# sfdisk: " PACKAGE_STRING "\n");
fprintf(f, "# Disk: %s\n", devname);
fprintf(f, "# Partition: %zu\n", partno + 1);
fprintf(f, "# Operation: move data\n");
fprintf(f, "# Sector size: %zu\n", ss);
fprintf(f, "# Original start offset (sectors/bytes): %ju/%ju\n",
(uintmax_t)from, (uintmax_t)from * ss);
fprintf(f, "# New start offset (sectors/bytes): %ju/%ju\n",
(uintmax_t)to, (uintmax_t)to * ss);
fprintf(f, "# Area size (sectors/bytes): %ju/%ju\n",
(uintmax_t)nsectors, (uintmax_t)nsectors * ss);
fprintf(f, "# Step size (sectors/bytes): %" PRIu64 "/%zu\n", step, step_bytes);
fprintf(f, "# Steps: %ju\n", ((uintmax_t) nsectors / step) + 1);
fprintf(f, "#\n");
fprintf(f, "# <step>: <from> <to> (step offsets in bytes)\n");
}
src = (backward ? from + nsectors : from) * ss;
dst = (backward ? to + nsectors : to) * ss;
buf = xmalloc(step_bytes);
DBG(MISC, ul_debug(" initial: src=%ju dst=%ju", src, dst));
gettimeofday(&prev_time, NULL);
prev = 0;
for (cc = 1, i = 0; i < nsectors && nbytes > 0; i += step, cc++) {
if (nbytes < step_bytes) {
DBG(MISC, ul_debug("aligning step #%05zu from %zu to %ju",
cc, step_bytes, nbytes));
step_bytes = nbytes;
}
nbytes -= step_bytes;
if (backward)
src -= step_bytes, dst -= step_bytes;
DBG(MISC, ul_debug("#%05zu: src=%ju dst=%ju", cc, src, dst));
if (!sf->noact) {
/* read source */
if (lseek(fd, src, SEEK_SET) == (off_t) -1 ||
read_all(fd, buf, step_bytes) != (ssize_t) step_bytes) {
if (f)
fprintf(f, "%05zu: read error %12ju %12ju\n", cc, src, dst);
fdisk_warn(sf->cxt,
_("cannot read at offset: %ju; continue"), src);
ioerr++;
goto next;
}
/* write target */
if (lseek(fd, dst, SEEK_SET) == (off_t) -1 ||
write_all(fd, buf, step_bytes) != 0) {
if (f)
fprintf(f, "%05zu: write error %12ju %12ju\n", cc, src, dst);
fdisk_warn(sf->cxt,
_("cannot write at offset: %ju; continue"), dst);
ioerr++;
goto next;
}
if (sf->movefsync && fsync(fd) != 0)
fdisk_warn(sf->cxt,
_("cannot fsync at offset: %ju; continue"), dst);
}
/* write log */
if (f)
fprintf(f, "%05zu: %12ju %12ju\n", cc, src, dst);
if (progress && i % 10 == 0) {
unsigned int elapsed = 0; /* usec */
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
if (cur_time.tv_sec - prev_time.tv_sec > 1) {
elapsed = ((cur_time.tv_sec - prev_time.tv_sec) * 1000000) +
(cur_time.tv_usec - prev_time.tv_usec);
bytes_per_sec = ((i - prev) * ss) / elapsed; /* per usec */
bytes_per_sec *= 1000000; /* per sec */
prev_time = cur_time;
prev = i;
}
if (bytes_per_sec)
fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%, %.1f MiB/s)."),
i + 1, nsectors,
100.0 / ((double) nsectors/(i+1)),
(double) (bytes_per_sec / (1024 * 1024)));
else
fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%)."),
i + 1, nsectors,
100.0 / ((double) nsectors/(i+1)));
fflush(stdout);
fputc('\r', stdout);
}
next:
if (!backward)
src += step_bytes, dst += step_bytes;
}
if (progress && nsectors) {
int x = get_terminal_width(80);
for (; x > 0; x--)
fputc(' ', stdout);
fflush(stdout);
fputc('\r', stdout);
if (i > nsectors)
/* see for() above; @i has to be greater than @nsectors
* on success due to i += step */
i = nsectors;
fprintf(stdout, _("Moved %ju from %ju sectors (%.0f%%)."),
i, nsectors,
100.0 / ((double) nsectors/(i+1)));
fputc('\n', stdout);
}
rc = 0;
done:
if (f)
fclose(f);
free(buf);
free(typescript);
if (sf->noact)
fdisk_info(sf->cxt, _("Your data has not been moved (--no-act)."));
if (ioerr) {
fdisk_info(sf->cxt, _("%zu I/O errors detected!"), ioerr);
rc = -EIO;
} else if (rc)
warn(_("%s: failed to move data"), devname);
free(devname);
return rc;
}
static int write_changes(struct sfdisk *sf)
{
int rc = 0;
if (sf->noact)
fdisk_info(sf->cxt, _("The partition table is unchanged (--no-act)."));
else
rc = fdisk_write_disklabel(sf->cxt);
if (rc == 0 && sf->movedata && sf->orig_pa)
rc = move_partition_data(sf, sf->partno, sf->orig_pa);
if (!sf->noact && !rc) {
fdisk_info(sf->cxt, _("\nThe partition table has been altered."));
if (!sf->notell) {
/* Let's wait a little bit. It's possible that our
* system is still busy with a previous re-read
* ioctl (on sfdisk start) or with another task
* related to the write to the device.
*/
xusleep(250000);
fdisk_reread_partition_table(sf->cxt);
}
}
if (!rc)
rc = fdisk_deassign_device(sf->cxt,
sf->noact || sf->notell); /* no-sync */
return rc;
}
/*
* sfdisk --list [<device ..]
*/
static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
{
int fail = 0;
fdisk_enable_listonly(sf->cxt, 1);
if (argc) {
int i;
for (i = 0; i < argc; i++)
if (print_device_pt(sf->cxt, argv[i], 1, sf->verify, i) != 0)
fail++;
} else
print_all_devices_pt(sf->cxt, sf->verify);
return fail;
}
/*
* sfdisk --list-free [<device ..]
*/
static int command_list_freespace(struct sfdisk *sf, int argc, char **argv)
{
int fail = 0;
fdisk_enable_listonly(sf->cxt, 1);
if (argc) {
int i;
for (i = 0; i < argc; i++)
if (print_device_freespace(sf->cxt, argv[i], 1, i) != 0)
fail++;
} else
print_all_devices_freespace(sf->cxt);
return fail;
}
/*
* sfdisk --list-types
*/
static int command_list_types(struct sfdisk *sf)
{
const struct fdisk_parttype *t;
struct fdisk_label *lb;
const char *name;
size_t i = 0;
int codes;
assert(sf);
assert(sf->cxt);
name = sf->label ? sf->label : "dos";
lb = fdisk_get_label(sf->cxt, name);
if (!lb)
errx(EXIT_FAILURE, _("unsupported label '%s'"), name);
codes = fdisk_label_has_code_parttypes(lb);
fputs(_("Id Name\n\n"), stdout);
while ((t = fdisk_label_get_parttype(lb, i++))) {
if (codes)
printf("%2x %s\n", fdisk_parttype_get_code(t),
fdisk_parttype_get_name(t));
else
printf("%s %s\n", fdisk_parttype_get_string(t),
fdisk_parttype_get_name(t));
}
return 0;
}
static int verify_device(struct sfdisk *sf, const char *devname)
{
int rc = 1;
fdisk_enable_listonly(sf->cxt, 1);
assign_device(sf, devname, 1);
color_scheme_enable("header", UL_COLOR_BOLD);
fdisk_info(sf->cxt, "%s:", devname);
color_disable();
if (!fdisk_has_label(sf->cxt))
fdisk_info(sf->cxt, _("unrecognized partition table type"));
else
rc = fdisk_verify_disklabel(sf->cxt);
fdisk_deassign_device(sf->cxt, 1);
return rc;
}
/*
* sfdisk --verify [<device ..]
*/
static int command_verify(struct sfdisk *sf, int argc, char **argv)
{
int nfails = 0, ct = 0;
if (argc) {
int i;
for (i = 0; i < argc; i++) {
if (i)
fdisk_info(sf->cxt, " ");
if (verify_device(sf, argv[i]) < 0)
nfails++;
}
} else {
FILE *f = NULL;
char *dev;
while ((dev = next_proc_partition(&f))) {
if (ct)
fdisk_info(sf->cxt, " ");
if (verify_device(sf, dev) < 0)
nfails++;
free(dev);
ct++;
}
}
return nfails;
}
static int get_size(const char *dev, int silent, uintmax_t *sz)
{
int fd, rc = 0;
fd = open(dev, O_RDONLY);
if (fd < 0) {
if (!silent)
warn(_("cannot open %s"), dev);
return -errno;
}
if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) {
if (!silent)
warn(_("Cannot get size of %s"), dev);
rc = -errno;
}
close(fd);
return rc;
}
/*
* sfdisk --show-size [<device ..]
*
* (silly, but just for backward compatibility)
*/
static int command_show_size(struct sfdisk *sf __attribute__((__unused__)),
int argc, char **argv)
{
uintmax_t sz;
if (argc) {
int i;
for (i = 0; i < argc; i++) {
if (get_size(argv[i], 0, &sz) == 0)
printf("%ju\n", sz / 2);
}
} else {
FILE *f = NULL;
uintmax_t total = 0;
char *dev;
while ((dev = next_proc_partition(&f))) {
if (get_size(dev, 1, &sz) == 0) {
printf("%s: %9ju\n", dev, sz / 2);
total += sz / 2;
}
free(dev);
}
if (total)
printf(_("total: %ju blocks\n"), total);
}
return 0;
}
static int print_geom(struct sfdisk *sf, const char *devname)
{
fdisk_enable_listonly(sf->cxt, 1);
assign_device(sf, devname, 1);
fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track",
devname,
(uintmax_t) fdisk_get_geom_cylinders(sf->cxt),
(uintmax_t) fdisk_get_geom_heads(sf->cxt),
(uintmax_t) fdisk_get_geom_sectors(sf->cxt));
fdisk_deassign_device(sf->cxt, 1);
return 0;
}
/*
* sfdisk --show-geometry [<device ..]
*/
static int command_show_geometry(struct sfdisk *sf, int argc, char **argv)
{
int nfails = 0;
if (argc) {
int i;
for (i = 0; i < argc; i++) {
if (print_geom(sf, argv[i]) < 0)
nfails++;
}
} else {
FILE *f = NULL;
char *dev;
while ((dev = next_proc_partition(&f))) {
if (print_geom(sf, dev) < 0)
nfails++;
free(dev);
}
}
return nfails;
}
/*
* sfdisk --activate <device> [<partno> ...]
*/
static int command_activate(struct sfdisk *sf, int argc, char **argv)
{
int rc, nparts, i, listonly;
struct fdisk_partition *pa = NULL;
const char *devname = NULL;
if (argc < 1)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
/* --activate <device> */
listonly = argc == 1;
assign_device(sf, devname, listonly);
if (fdisk_is_label(sf->cxt, GPT)) {
if (fdisk_gpt_is_hybrid(sf->cxt))
errx(EXIT_FAILURE, _("toggle boot flags is unsupported for Hybrid GPT/MBR"));
/* Switch from GPT to PMBR */
sf->cxt = fdisk_new_nested_context(sf->cxt, "dos");
if (!sf->cxt)
err(EXIT_FAILURE, _("cannot switch to PMBR"));
fdisk_info(sf->cxt, _("Activation is unsupported for GPT -- entering nested PMBR."));
} else if (!fdisk_is_label(sf->cxt, DOS))
errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR or PMBR only"));
nparts = fdisk_get_npartitions(sf->cxt);
for (i = 0; i < nparts; i++) {
char *data = NULL;
/* note that fdisk_get_partition() reuses the @pa pointer, you
* don't have to (re)allocate it */
if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
continue;
/* sfdisk --activate list bootable partitions */
if (listonly) {
if (!fdisk_partition_is_bootable(pa))
continue;
if (fdisk_partition_to_string(pa, sf->cxt,
FDISK_FIELD_DEVICE, &data) == 0) {
printf("%s\n", data);
free(data);
}
/* deactivate all active partitions */
} else if (fdisk_partition_is_bootable(pa))
fdisk_toggle_partition_flag(sf->cxt, i, DOS_FLAG_ACTIVE);
}
/* sfdisk --activate <partno> [..] */
for (i = 1; i < argc; i++) {
int n;
if (i == 1 && strcmp(argv[1], "-") == 0)
break;
n = strtou32_or_err(argv[i], _("failed to parse partition number"));
rc = fdisk_toggle_partition_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE);
if (rc)
errx(EXIT_FAILURE,
_("%s: partition %d: failed to toggle bootable flag"),
devname, i + 1);
}
fdisk_unref_partition(pa);
if (listonly)
rc = fdisk_deassign_device(sf->cxt, 1);
else
rc = write_changes(sf);
return rc;
}
/*
* sfdisk --delete <device> [<partno> ...]
*/
static int command_delete(struct sfdisk *sf, int argc, char **argv)
{
size_t i;
const char *devname = NULL;
if (argc < 1)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
assign_device(sf, devname, 0);
/* delete all */
if (argc == 1) {
size_t nparts = fdisk_get_npartitions(sf->cxt);
for (i = 0; i < nparts; i++) {
if (fdisk_is_partition_used(sf->cxt, i) &&
fdisk_delete_partition(sf->cxt, i) != 0)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, i + 1);
}
/* delete specified */
} else {
for (i = 1; i < (size_t) argc; i++) {
size_t n = strtou32_or_err(argv[i], _("failed to parse partition number"));
if (n == 0)
errx(EXIT_FAILURE, _("partition number must be a positive number"));
if (fdisk_delete_partition(sf->cxt, n - 1) != 0)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, n);
}
}
return write_changes(sf);
}
/*
* sfdisk --reorder <device>
*/
static int command_reorder(struct sfdisk *sf, int argc, char **argv)
{
const char *devname = NULL;
int rc;
if (argc)
devname = argv[0];
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
assign_device(sf, devname, 0); /* read-write */
if (fdisk_reorder_partitions(sf->cxt) == 1) /* unchanged */
rc = fdisk_deassign_device(sf->cxt, 1);
else
rc = write_changes(sf);
return rc;
}
/*
* sfdisk --dump <device>
*/
static int command_dump(struct sfdisk *sf, int argc, char **argv)
{
const char *devname = NULL;
struct fdisk_script *dp;
int rc;
if (argc)
devname = argv[0];
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
assign_device(sf, devname, 1); /* read-only */
if (!fdisk_has_label(sf->cxt))
errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname);
dp = fdisk_new_script(sf->cxt);
if (!dp)
err(EXIT_FAILURE, _("failed to allocate dump struct"));
rc = fdisk_script_read_context(dp, NULL);
if (rc)
errx(EXIT_FAILURE, _("%s: failed to dump partition table"), devname);
if (sf->json)
fdisk_script_enable_json(dp, 1);
fdisk_script_write_file(dp, stdout);
fdisk_unref_script(dp);
fdisk_deassign_device(sf->cxt, 1); /* no-sync() */
return 0;
}
/*
* sfdisk --backup-pt-sectors <device>
*/
static int command_backup_sectors(struct sfdisk *sf, int argc, char **argv)
{
const char *devname = NULL;
if (argc)
devname = argv[0];
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
assign_device(sf, devname, 1); /* read-only */
if (!fdisk_has_label(sf->cxt))
errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname);
backup_partition_table(sf, devname);
fdisk_deassign_device(sf->cxt, 1); /* no-sync() */
return 0;
}
static void assign_device_partition(struct sfdisk *sf,
const char *devname,
size_t partno,
int rdonly)
{
int rc;
size_t n;
struct fdisk_label *lb = NULL;
assert(sf);
assert(devname);
/* read-only when a new <type> undefined */
rc = fdisk_assign_device(sf->cxt, devname, rdonly);
if (rc)
err(EXIT_FAILURE, _("cannot open %s"), devname);
if (!fdisk_is_readonly(sf->cxt)
&& blkdev_lock(fdisk_get_devfd(sf->cxt), devname, sf->lockmode) != 0) {
fdisk_deassign_device(sf->cxt, 1);
return;
}
lb = fdisk_get_label(sf->cxt, NULL);
if (!lb)
errx(EXIT_FAILURE, _("%s: no partition table found"), devname);
n = fdisk_get_npartitions(sf->cxt);
if (partno > n)
errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains "
"only %zu partitions"), devname, partno, n);
if (!fdisk_is_partition_used(sf->cxt, partno - 1))
errx(EXIT_FAILURE, _("%s: partition %zu: partition is unused"),
devname, partno);
}
/*
* sfdisk --part-type <device> <partno> [<type>]
*/
static int command_parttype(struct sfdisk *sf, int argc, char **argv)
{
size_t partno;
struct fdisk_parttype *type = NULL;
struct fdisk_label *lb;
const char *devname = NULL, *typestr = NULL;
if (!argc)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
if (argc < 2)
errx(EXIT_FAILURE, _("no partition number specified"));
partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
if (argc == 3)
typestr = argv[2];
else if (argc > 3)
errx(EXIT_FAILURE, _("unexpected arguments"));
/* read-only when a new <type> undefined */
assign_device_partition(sf, devname, partno, !typestr);
lb = fdisk_get_label(sf->cxt, NULL);
/* print partition type */
if (!typestr) {
const struct fdisk_parttype *t = NULL;
struct fdisk_partition *pa = NULL;
if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
t = fdisk_partition_get_type(pa);
if (!t)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"),
devname, partno);
if (fdisk_label_has_code_parttypes(lb))
printf("%2x\n", fdisk_parttype_get_code(t));
else
printf("%s\n", fdisk_parttype_get_string(t));
fdisk_unref_partition(pa);
fdisk_deassign_device(sf->cxt, 1);
return 0;
}
if (sf->backup)
backup_partition_table(sf, devname);
/* parse <type> and apply to PT */
type = fdisk_label_advparse_parttype(lb, typestr,
FDISK_PARTTYPE_PARSE_DATA
| FDISK_PARTTYPE_PARSE_ALIAS
| FDISK_PARTTYPE_PARSE_NAME
| FDISK_PARTTYPE_PARSE_SHORTCUT);
if (!type)
errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"),
fdisk_label_get_name(lb), typestr);
else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"),
devname, partno);
fdisk_unref_parttype(type);
return write_changes(sf);
}
/*
* sfdisk --part-uuid <device> <partno> [<uuid>]
*/
static int command_partuuid(struct sfdisk *sf, int argc, char **argv)
{
size_t partno;
struct fdisk_partition *pa = NULL;
const char *devname = NULL, *uuid = NULL;
if (!argc)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
if (argc < 2)
errx(EXIT_FAILURE, _("no partition number specified"));
partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
if (argc == 3)
uuid = argv[2];
else if (argc > 3)
errx(EXIT_FAILURE, _("unexpected arguments"));
/* read-only if uuid not given */
assign_device_partition(sf, devname, partno, !uuid);
/* print partition uuid */
if (!uuid) {
const char *str = NULL;
if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
str = fdisk_partition_get_uuid(pa);
if (!str)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition UUID"),
devname, partno);
printf("%s\n", str);
fdisk_unref_partition(pa);
fdisk_deassign_device(sf->cxt, 1);
return 0;
}
if (sf->backup)
backup_partition_table(sf, devname);
pa = fdisk_new_partition();
if (!pa)
err(EXIT_FAILURE, _("failed to allocate partition object"));
if (fdisk_partition_set_uuid(pa, uuid) != 0 ||
fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition UUID"),
devname, partno);
fdisk_unref_partition(pa);
return write_changes(sf);
}
/*
* sfdisk --part-label <device> <partno> [<label>]
*/
static int command_partlabel(struct sfdisk *sf, int argc, char **argv)
{
size_t partno;
struct fdisk_partition *pa = NULL;
const char *devname = NULL, *name = NULL;
if (!argc)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
if (argc < 2)
errx(EXIT_FAILURE, _("no partition number specified"));
partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
if (argc == 3)
name = argv[2];
else if (argc > 3)
errx(EXIT_FAILURE, _("unexpected arguments"));
/* read-only if name not given */
assign_device_partition(sf, devname, partno, !name);
/* print partition name */
if (!name) {
const char *str = NULL;
if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
str = fdisk_partition_get_name(pa);
if (!str)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition name"),
devname, partno);
printf("%s\n", str);
fdisk_unref_partition(pa);
fdisk_deassign_device(sf->cxt, 1);
return 0;
}
if (sf->backup)
backup_partition_table(sf, devname);
pa = fdisk_new_partition();
if (!pa)
err(EXIT_FAILURE, _("failed to allocate partition object"));
if (fdisk_partition_set_name(pa, name) != 0 ||
fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition name"),
devname, partno);
fdisk_unref_partition(pa);
return write_changes(sf);
}
/*
* sfdisk --part-attrs <device> <partno> [<attrs>]
*/
static int command_partattrs(struct sfdisk *sf, int argc, char **argv)
{
size_t partno;
struct fdisk_partition *pa = NULL;
const char *devname = NULL, *attrs = NULL;
if (!argc)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
if (argc < 2)
errx(EXIT_FAILURE, _("no partition number specified"));
partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
if (argc == 3)
attrs = argv[2];
else if (argc > 3)
errx(EXIT_FAILURE, _("unexpected arguments"));
/* read-only if name not given */
assign_device_partition(sf, devname, partno, !attrs);
/* print partition name */
if (!attrs) {
const char *str = NULL;
if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
str = fdisk_partition_get_attrs(pa);
if (str)
printf("%s\n", str);
fdisk_unref_partition(pa);
fdisk_deassign_device(sf->cxt, 1);
return 0;
}
if (sf->backup)
backup_partition_table(sf, devname);
pa = fdisk_new_partition();
if (!pa)
err(EXIT_FAILURE, _("failed to allocate partition object"));
if (fdisk_partition_set_attrs(pa, attrs) != 0 ||
fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"),
devname, partno);
fdisk_unref_partition(pa);
return write_changes(sf);
}
#ifdef BLKDISCARD
/*
* sfdisk --discard-free <device>
*/
static int command_discard_free(struct sfdisk *sf, int argc, char **argv)
{
struct fdisk_table *tb = NULL;
struct fdisk_iter *itr = NULL;
struct fdisk_partition *pa = NULL;
const char *devname = NULL;
uint64_t ss;
int rc;
if (!argc)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
if (argc > 1)
errx(EXIT_FAILURE, _("unexpected arguments"));
itr = fdisk_new_iter(FDISK_ITER_FORWARD);
if (!itr)
err(EXIT_FAILURE, _("failed to allocate iterator"));
assign_device(sf, devname, 0);
ss = fdisk_get_sector_size(sf->cxt);
rc = fdisk_get_freespaces(sf->cxt, &tb);
if (rc) {
fdisk_warn(sf->cxt, _("failed to gather unpartitioned space"));
goto done;
}
while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
uint64_t range[2];
if (!fdisk_partition_has_size(pa) ||
!fdisk_partition_has_start(pa))
continue;
range[0] = (uint64_t) fdisk_partition_get_start(pa);
range[1] = (uint64_t) fdisk_partition_get_size(pa);
fdisk_info(sf->cxt, _("Discarding region %"PRIu64
"-%"PRIu64""),
range[0], range[0] + range[1] - 1);
range[0] *= ss;
range[1] *= ss;
errno = 0;
if (ioctl(fdisk_get_devfd(sf->cxt), BLKDISCARD, &range)) {
rc = -errno;
fdisk_warn(sf->cxt, _("BLKDISCARD ioctl failed"));
break;
}
}
done:
fdisk_free_iter(itr);
fdisk_unref_table(tb);
return rc;
}
#else /* BLKDISCARD */
static int command_discard_free(struct sfdisk *sf, int argc, char **argv)
{
fdisk_warnx(sf->cxt, _("Discard unsupported on your system."));
}
#endif /* BLKDISCARD */
/*
* sfdisk --disk-id <device> [<str>]
*/
static int command_diskid(struct sfdisk *sf, int argc, char **argv)
{
const char *devname = NULL;
char *str = NULL;
if (!argc)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
if (argc == 2)
str = argv[1];
else if (argc > 2)
errx(EXIT_FAILURE, _("unexpected arguments"));
assign_device(sf, devname, !str);
/* print */
if (!str) {
fdisk_get_disklabel_id(sf->cxt, &str);
if (str)
printf("%s\n", str);
free(str);
fdisk_deassign_device(sf->cxt, 1);
return 0;
}
if (fdisk_set_disklabel_id_from_string(sf->cxt, str) != 0)
errx(EXIT_FAILURE, _("%s: failed to set disklabel ID"), devname);
return write_changes(sf);
}
/*
* sfdisk --relocate <mode> <device>
*/
static int command_relocate(struct sfdisk *sf, int argc, char **argv)
{
const char *devname = NULL;
const char *oper = NULL;
struct fdisk_label *lb;
if (!argc)
errx(EXIT_FAILURE, _("no relocate operation specified"));
if (argc < 2)
errx(EXIT_FAILURE, _("no disk device specified"));
if (argc > 2)
errx(EXIT_FAILURE, _("unexpected arguments"));
oper = argv[0];
devname = argv[1];
lb = fdisk_get_label(sf->cxt, "gpt");
if (strcmp(oper, "gpt-bak-mini") == 0)
fdisk_gpt_enable_minimize(lb, 1);
else if (strcmp(oper, "gpt-bak-std") != 0)
errx(EXIT_FAILURE, _("unsupported relocation operation"));
assign_device(sf, devname, 0);
fdisk_label_set_changed(lb, 1);
return write_changes(sf);
}
static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
{
struct fdisk_partition *pa = NULL;
char *data;
assert(sf);
if (sf->quiet)
return;
if (fdisk_get_partition(sf->cxt, n, &pa) != 0)
return;
fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data);
printf("%12s : ", data);
fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data);
printf("%12s ", data);
fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data);
printf("%12s ", data);
fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data);
printf("(%s) ", data);
fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data);
printf("%s\n", data);
fdisk_unref_partition(pa);
}
static void command_fdisk_help(void)
{
fputs(_("\nHelp:\n"), stdout);
fputc('\n', stdout);
color_scheme_enable("help-title", UL_COLOR_BOLD);
fputs(_(" Commands:\n"), stdout);
color_disable();
fputs(_(" write write table to disk and exit\n"), stdout);
fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout);
fputs(_(" abort exit sfdisk shell\n"), stdout);
fputs(_(" print display the partition table\n"), stdout);
fputs(_(" help show this help text\n"), stdout);
fputc('\n', stdout);
fputs(_(" Ctrl-D the same as 'quit'\n"), stdout);
fputc('\n', stdout);
color_scheme_enable("help-title", UL_COLOR_BOLD);
fputs(_(" Input format:\n"), stdout);
color_disable();
fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout);
fputc('\n', stdout);
fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
" specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
" The default is the first free space.\n"), stdout);
fputc('\n', stdout);
fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
" specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
" The default is all available space.\n"), stdout);
fputc('\n', stdout);
fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout);
fputs(_(" MBR: hex or L,S,Ex,X,U,R,V shortcuts.\n"), stdout);
fputs(_(" GPT: UUID or L,S,H,U,R,V shortcuts.\n"), stdout);
fputc('\n', stdout);
fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout);
fputc('\n', stdout);
color_scheme_enable("help-title", UL_COLOR_BOLD);
fputs(_(" Example:\n"), stdout);
color_disable();
fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout);
fputc('\n', stdout);
}
enum {
SFDISK_DONE_NONE = 0,
SFDISK_DONE_EOF,
SFDISK_DONE_ABORT,
SFDISK_DONE_WRITE,
SFDISK_DONE_ASK
};
/* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
static int loop_control_commands(struct sfdisk *sf,
struct fdisk_script *dp,
char *buf)
{
const char *p = skip_blank(buf);
int rc = SFDISK_DONE_NONE;
if (strcmp(p, "print") == 0)
list_disklabel(sf->cxt);
else if (strcmp(p, "help") == 0)
command_fdisk_help();
else if (strcmp(p, "quit") == 0)
rc = SFDISK_DONE_ASK;
else if (strcmp(p, "write") == 0)
rc = SFDISK_DONE_WRITE;
else if (strcmp(p, "abort") == 0)
rc = SFDISK_DONE_ABORT;
else {
if (sf->interactive)
fdisk_warnx(sf->cxt, _("unsupported command"));
else {
fdisk_warnx(sf->cxt, _("line %d: unsupported command"),
fdisk_script_get_nlines(dp));
rc = -EINVAL;
}
}
return rc;
}
static int has_container_or_unused(struct sfdisk *sf)
{
size_t i, nparts;
struct fdisk_partition *pa = NULL;
if (sf->container || sf->unused)
return 1;
nparts = fdisk_get_npartitions(sf->cxt);
for (i = 0; i < nparts; i++) {
if (!fdisk_is_partition_used(sf->cxt, i)) {
sf->unused = 1;
continue;
}
if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
continue;
if (fdisk_partition_is_container(pa))
sf->container = 1;
}
fdisk_unref_partition(pa);
return sf->container || sf->unused;
}
static size_t last_pt_partno(struct sfdisk *sf)
{
size_t i, nparts, partno = 0;
struct fdisk_partition *pa = NULL;
nparts = fdisk_get_npartitions(sf->cxt);
for (i = 0; i < nparts; i++) {
size_t x;
if (fdisk_get_partition(sf->cxt, i, &pa) != 0 ||
!fdisk_partition_is_used(pa))
continue;
x = fdisk_partition_get_partno(pa);
if (x > partno)
partno = x;
}
fdisk_unref_partition(pa);
return partno;
}
#ifdef HAVE_LIBREADLINE
static char *sfdisk_fgets(struct fdisk_script *dp,
char *buf, size_t bufsz, FILE *f)
{
struct sfdisk *sf = (struct sfdisk *) fdisk_script_get_userdata(dp);
assert(dp);
assert(buf);
assert(bufsz > 2);
if (sf->interactive) {
char *p = readline(sf->prompt);
size_t len;
if (!p)
return NULL;
len = strlen(p);
if (len > bufsz - 2)
len = bufsz - 2;
memcpy(buf, p, len);
buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */
buf[len + 1] = '\0';
free(p);
fflush(stdout);
return buf;
}
return fgets(buf, bufsz, f);
}
#endif
static int ignore_partition(struct fdisk_partition *pa)
{
/* incomplete partition setting */
if (!fdisk_partition_has_start(pa) && !fdisk_partition_start_is_default(pa))
return 1;
if (!fdisk_partition_has_size(pa) && !fdisk_partition_end_is_default(pa))
return 1;
/* probably dump from old sfdisk with start=0 size=0 */
if (fdisk_partition_has_start(pa) && fdisk_partition_get_start(pa) == 0 &&
fdisk_partition_has_size(pa) && fdisk_partition_get_size(pa) == 0)
return 1;
return 0;
}
static void follow_wipe_mode(struct sfdisk *sf)
{
int dowipe = sf->wipemode == WIPEMODE_ALWAYS ? 1 : 0;
if (sf->interactive && sf->wipemode == WIPEMODE_AUTO)
dowipe = 1; /* do it in interactive mode */
if (fdisk_is_ptcollision(sf->cxt) && sf->wipemode != WIPEMODE_NEVER)
dowipe = 1; /* always wipe old PT */
fdisk_enable_wipe(sf->cxt, dowipe);
if (sf->quiet)
return;
if (dowipe) {
if (!fdisk_is_ptcollision(sf->cxt)) {
fdisk_warnx(sf->cxt, _(
"The device contains '%s' signature and it may be removed by a write command. "
"See sfdisk(8) man page and --wipe option for more details."),
fdisk_get_collision(sf->cxt));
fputc('\n', stdout);
}
} else {
fdisk_warnx(sf->cxt, _(
"The device contains '%s' signature and it may remain on the device. "
"It is recommended to wipe the device with wipefs(8) or "
"sfdisk --wipe, in order to avoid possible collisions."),
fdisk_get_collision(sf->cxt));
fputc('\n', stderr);
}
}
static int wipe_partition(struct sfdisk *sf, size_t partno)
{
int rc, yes = 0;
char *fstype = NULL;
struct fdisk_partition *tmp = NULL;
DBG(MISC, ul_debug("checking for signature"));
rc = fdisk_get_partition(sf->cxt, partno, &tmp);
if (rc)
goto done;
rc = fdisk_partition_to_string(tmp, sf->cxt, FDISK_FIELD_FSTYPE, &fstype);
if (rc || fstype == NULL)
goto done;
fdisk_warnx(sf->cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
if (sf->pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
fdisk_ask_yesno(sf->cxt, _("Do you want to remove the signature?"), &yes);
else if (sf->pwipemode == WIPEMODE_ALWAYS)
yes = 1;
if (yes) {
fdisk_info(sf->cxt, _("The signature will be removed by a write command."));
rc = fdisk_wipe_partition(sf->cxt, partno, TRUE);
}
done:
fdisk_unref_partition(tmp);
free(fstype);
DBG(MISC, ul_debug("partition wipe check end [rc=%d]", rc));
return rc;
}
static void refresh_prompt_buffer(struct sfdisk *sf, const char *devname,
size_t next_partno, int created)
{
if (created) {
char *partname = fdisk_partname(devname, next_partno + 1);
if (!partname)
err(EXIT_FAILURE, _("failed to allocate partition name"));
if (!sf->prompt || !startswith(sf->prompt, partname)) {
free(sf->prompt);
xasprintf(&sf->prompt,"%s: ", partname);
}
free(partname);
} else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) {
free(sf->prompt);
sf->prompt = xstrdup(SFDISK_PROMPT);
}
}
/*
* sfdisk <device> [[-N] <partno>]
*
* Note that the option -N is there for backward compatibility only.
*/
static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
{
int rc = 0, partno = sf->partno, created = 0, unused = 0, ignored = 0;
struct fdisk_script *dp;
struct fdisk_table *tb = NULL;
const char *devname = NULL, *label;
char buf[BUFSIZ];
size_t next_partno = (size_t) -1;
if (argc)
devname = argv[0];
if (partno < 0 && argc > 1)
partno = strtou32_or_err(argv[1],
_("failed to parse partition number"));
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
assign_device(sf, devname, 0);
dp = fdisk_new_script(sf->cxt);
if (!dp)
err(EXIT_FAILURE, _("failed to allocate script handler"));
fdisk_set_script(sf->cxt, dp);
#ifdef HAVE_LIBREADLINE
fdisk_script_set_fgets(dp, sfdisk_fgets);
#endif
fdisk_script_set_userdata(dp, (void *) sf);
/*
* Don't create a new disklabel when [-N] <partno> specified. In this
* case reuse already specified disklabel. Let's check that the disk
* really contains the partition.
*/
if (partno >= 0) {
size_t n;
if (!fdisk_has_label(sf->cxt))
errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
"no partition table was found"),
devname, partno + 1);
n = fdisk_get_npartitions(sf->cxt);
if ((size_t) partno > n)
errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
"partition table contains only %zu "
"partitions"),
devname, partno + 1, n);
if (!fdisk_is_partition_used(sf->cxt, partno)) {
fdisk_warnx(sf->cxt, _("warning: %s: partition %d is not defined yet"),
devname, partno + 1);
unused = 1;
}
created = 1;
next_partno = partno;
if (sf->movedata)
sf->orig_pa = get_partition(sf->cxt, partno);
}
if (sf->append) {
created = 1;
next_partno = last_pt_partno(sf) + 1;
}
if (!sf->quiet && sf->interactive) {
color_scheme_enable("welcome", UL_COLOR_GREEN);
fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING);
color_disable();
fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n"
"Be careful before using the write command.\n"));
}
if (!sf->noact && !sf->noreread) {
if (!sf->quiet)
fputs(_("Checking that no-one is using this disk right now ..."), stdout);
if (fdisk_device_is_used(sf->cxt)) {
if (!sf->quiet)
fputs(_(" FAILED\n\n"), stdout);
fdisk_warnx(sf->cxt, _(
"This disk is currently in use - repartitioning is probably a bad idea.\n"
"Umount all file systems, and swapoff all swap partitions on this disk.\n"
"Use the --no-reread flag to suppress this check.\n"));
if (!sf->force)
errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks."));
} else if (!sf->quiet)
fputs(_(" OK\n\n"), stdout);
}
if (fdisk_get_collision(sf->cxt))
follow_wipe_mode(sf);
if (!sf->quiet) {
list_disk_geometry(sf->cxt);
if (fdisk_has_label(sf->cxt)) {
fdisk_info(sf->cxt, _("\nOld situation:"));
list_disklabel(sf->cxt);
}
}
if (sf->label)
label = sf->label;
else if (fdisk_has_label(sf->cxt))
label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL));
else
label = "dos"; /* just for backward compatibility */
if (fdisk_script_set_header(dp, "label", label) != 0)
errx(EXIT_FAILURE, _("failed to set script header"));
if (!sf->quiet && sf->interactive) {
if (!fdisk_has_label(sf->cxt) && !sf->label)
fdisk_info(sf->cxt,
_("\nsfdisk is going to create a new '%s' disk label.\n"
"Use 'label: <name>' before you define a first partition\n"
"to override the default."), label);
fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n"));
} else if (!sf->quiet)
fputc('\n', stdout);
tb = fdisk_script_get_table(dp);
assert(tb);
do {
size_t nparts;
DBG(PARSE, ul_debug("<---next-line--->"));
if (next_partno == (size_t) -1)
next_partno = fdisk_table_get_nents(tb);
if (created
&& partno < 0
&& next_partno == fdisk_get_npartitions(sf->cxt)
&& !has_container_or_unused(sf)) {
fdisk_info(sf->cxt, _("All partitions used."));
rc = SFDISK_DONE_ASK;
break;
}
refresh_prompt_buffer(sf, devname, next_partno, created);
if (sf->prompt && (sf->interactive || !sf->quiet)) {
#ifndef HAVE_LIBREADLINE
fputs(sf->prompt, stdout);
#else
if (!sf->interactive)
fputs(sf->prompt, stdout);
#endif
}
rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
if (rc == -ENOTSUP) {
buf[sizeof(buf) - 1] = '\0';
fdisk_warnx(sf->cxt, _("Unknown script header '%s' -- ignore."), buf);
continue;
}
if (rc < 0) {
DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands"));
buf[sizeof(buf) - 1] = '\0';
rc = loop_control_commands(sf, dp, buf);
if (rc)
break;
continue;
}
if (rc == 1) {
rc = SFDISK_DONE_EOF;
if (!sf->quiet)
fputs(_("Done.\n"), stdout);
break;
}
nparts = fdisk_table_get_nents(tb);
if (nparts) {
size_t cur_partno = (size_t) -1;
struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1);
assert(pa);
if (ignore_partition(pa)) {
fdisk_info(sf->cxt, _("Ignoring partition."));
next_partno++;
ignored++;
continue;
}
if (!created) {
/* ignore "last-lba" and use default if --force specified */
if (sf->force && fdisk_script_get_header(dp, "last-lba")) {
fdisk_info(sf->cxt, _("Ignoring last-lba script header."));
fdisk_script_set_header(dp, "last-lba", NULL);
}
/* create a new disklabel */
rc = fdisk_apply_script_headers(sf->cxt, dp);
created = !rc;
if (rc) {
errno = -rc;
fdisk_warn(sf->cxt, _(
"Failed to apply script headers, disk label not created"));
}
if (rc == 0 && fdisk_get_collision(sf->cxt))
follow_wipe_mode(sf);
}
if (!rc && partno >= 0) { /* -N <partno>, modify partition */
rc = fdisk_set_partition(sf->cxt, partno, pa);
rc = rc == 0 ? SFDISK_DONE_ASK : SFDISK_DONE_ABORT;
break;
}
if (!rc) { /* add partition */
if (!sf->interactive && !sf->quiet &&
(!sf->prompt || startswith(sf->prompt, SFDISK_PROMPT))) {
refresh_prompt_buffer(sf, devname, next_partno, created);
fputs(sf->prompt, stdout);
}
rc = fdisk_add_partition(sf->cxt, pa, &cur_partno);
if (rc) {
errno = -rc;
fdisk_warn(sf->cxt, _("Failed to add #%zu partition"), next_partno + 1);
}
}
/* wipe partition on success
*
* Note that unused=1 means -N <partno> for unused,
* otherwise we wipe only newly created partitions.
*/
if (rc == 0 && (unused || partno < 0)) {
rc = wipe_partition(sf, unused ? (size_t) partno : cur_partno);
if (rc)
errno = -rc;
}
if (!rc) {
/* success print result */
if (sf->interactive)
sfdisk_print_partition(sf, cur_partno);
next_partno = cur_partno + 1;
} else if (pa) /* error, drop partition from script */
fdisk_table_remove_partition(tb, pa);
} else
fdisk_info(sf->cxt, _("Script header accepted."));
if (rc && !sf->interactive) {
rc = SFDISK_DONE_ABORT;
break;
}
} while (1);
/* create empty disk label if label, but no partition specified */
if ((rc == SFDISK_DONE_EOF || rc == SFDISK_DONE_WRITE) && created == 0
&& fdisk_script_has_force_label(dp) == 1
&& fdisk_table_get_nents(tb) == (size_t) ignored
&& fdisk_script_get_header(dp, "label")) {
int xrc = fdisk_apply_script_headers(sf->cxt, dp);
if (xrc) {
fdisk_warnx(sf->cxt, _(
"Failed to apply script headers, "
"disk label not created."));
rc = SFDISK_DONE_ABORT;
}
}
if (!sf->quiet && rc != SFDISK_DONE_ABORT) {
fdisk_info(sf->cxt, _("\nNew situation:"));
list_disk_identifier(sf->cxt);
list_disklabel(sf->cxt);
}
switch (rc) {
case SFDISK_DONE_ASK:
case SFDISK_DONE_EOF:
if (sf->interactive) {
int yes = 0;
fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes);
if (!yes) {
fdisk_info(sf->cxt, _("Leaving."));
rc = 0;
break;
}
}
/* fallthrough */
case SFDISK_DONE_WRITE:
rc = write_changes(sf);
break;
case SFDISK_DONE_ABORT:
default: /* rc < 0 on error */
fdisk_info(sf->cxt, _("Leaving.\n"));
break;
}
fdisk_set_script(sf->cxt, NULL);
fdisk_unref_script(dp);
return rc;
}
static void __attribute__((__noreturn__)) usage(void)
{
FILE *out = stdout;
fputs(USAGE_HEADER, out);
fprintf(out,
_(" %1$s [options] <dev> [[-N] <part>]\n"
" %1$s [options] <command>\n"), program_invocation_short_name);
fputs(USAGE_SEPARATOR, out);
fputs(_("Display or manipulate a disk partition table.\n"), out);
fputs(USAGE_COMMANDS, out);
fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out);
fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out);
fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out);
fputs(_(" -B, --backup-pt-sectors <dev> binary partition table backup (see -b and -O)\n"), out);
fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out);
fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out);
fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out);
fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out);
fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out);
fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out);
fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out);
fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out);
fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out);
fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out);
fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(_(" --discard-free <dev> discard (trim) unpartitioned areas\n"), out);
fputs(_(" --disk-id <dev> [<str>] print or change disk label ID (UUID)\n"), out);
fputs(_(" --relocate <oper> <dev> move partition header\n"), out);
fputs(USAGE_ARGUMENTS, out);
fputs(_(" <dev> device (usually disk) path\n"), out);
fputs(_(" <part> partition number\n"), out);
fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out);
fputs(USAGE_OPTIONS, out);
fputs(_(" -a, --append append partitions to existing partition table\n"), out);
fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out);
fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out);
fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out);
fputs(_(" --move-use-fsync use fsync after each write when move data\n"), out);
fputs(_(" -f, --force disable all consistency checking\n"), out);
fprintf(out,
_(" --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
fprintf(out,
" %s\n", USAGE_COLORS_DEFAULT);
fputs(_(" --sector-size <size> physical and logical sector size\n"), out);
fprintf(out,
_(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
fputs(_(" -N, --partno <num> specify partition number\n"), out);
fputs(_(" -n, --no-act do everything except write to device\n"), out);
fputs(_(" --no-reread do not check whether the device is in use\n"), out);
fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out);
fputs(_(" -O, --backup-file <path> override default backup file name\n"), out);
fputs(_(" -o, --output <list> output columns\n"), out);
fputs(_(" -q, --quiet suppress extra info messages\n"), out);
fprintf(out,
_(" -w, --wipe <mode> wipe signatures (%s, %s or %s)\n"), "auto", "always", "never");
fprintf(out,
_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (%s, %s or %s)\n"), "auto", "always", "never");
fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out);
fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out);
fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out);
fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out);
fputs(USAGE_SEPARATOR, out);
fprintf(out, " -h, --help %s\n", USAGE_OPTSTR_HELP);
fprintf(out, " -v, --version %s\n", USAGE_OPTSTR_VERSION);
list_available_columns(out);
fprintf(out, USAGE_MAN_TAIL("sfdisk(8)"));
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
const char *outarg = NULL;
int rc = -EINVAL, c, longidx = -1, bytes = 0;
int colormode = UL_COLORMODE_UNDEF;
size_t user_ss = 0;
struct sfdisk _sf = {
.partno = -1,
.wipemode = WIPEMODE_AUTO,
.pwipemode = WIPEMODE_AUTO,
.interactive = isatty(STDIN_FILENO) ? 1 : 0,
}, *sf = &_sf;
enum {
OPT_CHANGE_ID = CHAR_MAX + 1,
OPT_PRINT_ID,
OPT_ID,
OPT_NOREREAD,
OPT_PARTUUID,
OPT_PARTLABEL,
OPT_PARTTYPE,
OPT_PARTATTRS,
OPT_DISCARDFREE,
OPT_DISKID,
OPT_BYTES,
OPT_COLOR,
OPT_MOVEDATA,
OPT_MOVEFSYNC,
OPT_DELETE,
OPT_NOTELL,
OPT_RELOCATE,
OPT_LOCK,
OPT_SECTORSIZE
};
static const struct option longopts[] = {
{ "activate",no_argument, NULL, 'A' },
{ "append", no_argument, NULL, 'a' },
{ "backup-pt-sectors", no_argument, NULL, 'B' },
{ "backup", no_argument, NULL, 'b' },
{ "backup-file", required_argument, NULL, 'O' },
{ "bytes", no_argument, NULL, OPT_BYTES },
{ "color", optional_argument, NULL, OPT_COLOR },
{ "lock", optional_argument, NULL, OPT_LOCK },
{ "delete", no_argument, NULL, OPT_DELETE },
{ "dump", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "force", no_argument, NULL, 'f' },
{ "json", no_argument, NULL, 'J' },
{ "label", required_argument, NULL, 'X' },
{ "label-nested", required_argument, NULL, 'Y' },
{ "list", no_argument, NULL, 'l' },
{ "list-free", no_argument, NULL, 'F' },
{ "list-types", no_argument, NULL, 'T' },
{ "no-act", no_argument, NULL, 'n' },
{ "no-reread", no_argument, NULL, OPT_NOREREAD },
{ "no-tell-kernel", no_argument, NULL, OPT_NOTELL },
{ "move-data", optional_argument, NULL, OPT_MOVEDATA },
{ "move-use-fsync", no_argument, NULL, OPT_MOVEFSYNC },
{ "output", required_argument, NULL, 'o' },
{ "partno", required_argument, NULL, 'N' },
{ "reorder", no_argument, NULL, 'r' },
{ "sector-size", required_argument, NULL, OPT_SECTORSIZE },
{ "show-geometry", no_argument, NULL, 'g' },
{ "quiet", no_argument, NULL, 'q' },
{ "verify", no_argument, NULL, 'V' },
{ "version", no_argument, NULL, 'v' },
{ "wipe", required_argument, NULL, 'w' },
{ "wipe-partitions", required_argument, NULL, 'W' },
{ "relocate", no_argument, NULL, OPT_RELOCATE },
{ "part-uuid", no_argument, NULL, OPT_PARTUUID },
{ "part-label", no_argument, NULL, OPT_PARTLABEL },
{ "part-type", no_argument, NULL, OPT_PARTTYPE },
{ "part-attrs", no_argument, NULL, OPT_PARTATTRS },
{ "discard-free", no_argument, NULL, OPT_DISCARDFREE },
{ "disk-id", no_argument, NULL, OPT_DISKID },
{ "show-pt-geometry", no_argument, NULL, 'G' }, /* deprecated */
{ "unit", required_argument, NULL, 'u' }, /* deprecated */
{ "Linux", no_argument, NULL, 'L' }, /* deprecated */
{ "show-size", no_argument, NULL, 's' }, /* deprecated */
{ "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */
{ "id", no_argument, NULL, 'c' }, /* deprecated */
{ "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */
{ NULL, 0, NULL, 0 },
};
static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
{ 'F','d'}, /* --list-free --dump */
{ 'F','J'}, /* --list-free --json */
{ 's','u'}, /* --show-size --unit */
{ 0 }
};
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
close_stdout_atexit();
while ((c = getopt_long(argc, argv, "aAbBcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
longopts, &longidx)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
switch(c) {
case 'A':
sf->act = ACT_ACTIVATE;
break;
case 'a':
sf->append = 1;
break;
case 'b':
sf->backup = 1;
break;
case 'B':
sf->act = ACT_BACKUP_SECTORS;
break;
case OPT_CHANGE_ID:
case OPT_PRINT_ID:
case OPT_ID:
warnx(_("%s is deprecated in favour of --part-type"),
longopts[longidx].name);
sf->act = ACT_PARTTYPE;
break;
case 'c':
warnx(_("--id is deprecated in favour of --part-type"));
sf->act = ACT_PARTTYPE;
break;
case 'J':
sf->json = 1;
/* fallthrough */
case 'd':
sf->act = ACT_DUMP;
break;
case 'F':
sf->act = ACT_LIST_FREE;
break;
case 'f':
sf->force = 1;
break;
case 'G':
warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
/* fallthrough */
case 'g':
sf->act = ACT_SHOW_GEOM;
break;
case 'h':
usage();
break;
case 'l':
sf->act = ACT_LIST;
break;
case 'L':
warnx(_("--Linux option is unnecessary and deprecated"));
break;
case 'o':
outarg = optarg;
break;
case 'O':
sf->backup = 1;
sf->backup_file = optarg;
break;
case 'n':
sf->noact = 1;
break;
case 'N':
sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1;
break;
case 'q':
sf->quiet = 1;
break;
case 'r':
sf->act = ACT_REORDER;
break;
case 's':
sf->act = ACT_SHOW_SIZE;
break;
case 'T':
sf->act = ACT_LIST_TYPES;
break;
case 'u':
if (*optarg != 'S')
errx(EXIT_FAILURE, _("unsupported unit '%c'"), *optarg);
break;
case 'v':
print_version(EXIT_SUCCESS);
case 'V':
sf->verify = 1;
break;
case 'w':
sf->wipemode = wipemode_from_string(optarg);
if (sf->wipemode < 0)
errx(EXIT_FAILURE, _("unsupported wipe mode"));
break;
case 'W':
sf->pwipemode = wipemode_from_string(optarg);
if (sf->pwipemode < 0)
errx(EXIT_FAILURE, _("unsupported wipe mode"));
break;
case 'X':
sf->label = optarg;
break;
case 'Y':
sf->label_nested = optarg;
break;
case OPT_PARTUUID:
sf->act = ACT_PARTUUID;
break;
case OPT_PARTTYPE:
sf->act = ACT_PARTTYPE;
break;
case OPT_PARTLABEL:
sf->act = ACT_PARTLABEL;
break;
case OPT_PARTATTRS:
sf->act = ACT_PARTATTRS;
break;
case OPT_DISCARDFREE:
sf->act = ACT_DISCARD_FREE;
break;
case OPT_DISKID:
sf->act = ACT_DISKID;
break;
case OPT_NOREREAD:
sf->noreread = 1;
break;
case OPT_BYTES:
bytes = 1;
break;
case OPT_COLOR:
colormode = UL_COLORMODE_AUTO;
if (optarg)
colormode = colormode_or_err(optarg,
_("unsupported color mode"));
break;
case OPT_MOVEDATA:
sf->movedata = 1;
sf->move_typescript = optarg;
break;
case OPT_MOVEFSYNC:
sf->movefsync = 1;
break;
case OPT_DELETE:
sf->act = ACT_DELETE;
break;
case OPT_NOTELL:
sf->notell = 1;
break;
case OPT_RELOCATE:
sf->act = ACT_RELOCATE;
break;
case OPT_LOCK:
sf->lockmode = "1";
if (optarg) {
if (*optarg == '=')
optarg++;
sf->lockmode = optarg;
}
break;
case OPT_SECTORSIZE:
user_ss = strtou32_or_err(optarg,
_("invalid sector size argument"));
if (user_ss != 512 && user_ss != 1024 &&
user_ss != 2048 && user_ss != 4096)
errx(EXIT_FAILURE, _("invalid sector size argument"));
break;
default:
errtryhelp(EXIT_FAILURE);
}
}
colors_init(colormode, "sfdisk");
sfdisk_init(sf);
if (bytes)
fdisk_set_size_unit(sf->cxt, FDISK_SIZEUNIT_BYTES);
if (user_ss)
fdisk_save_user_sector_size(sf->cxt, user_ss, user_ss);
if (outarg)
init_fields(NULL, outarg, NULL);
if (sf->verify && !sf->act)
sf->act = ACT_VERIFY; /* --verify make be used with --list too */
else if (!sf->act)
sf->act = ACT_FDISK; /* default */
if (sf->movedata && !(sf->act == ACT_FDISK && sf->partno >= 0))
errx(EXIT_FAILURE, _("--movedata requires -N"));
switch (sf->act) {
case ACT_ACTIVATE:
rc = command_activate(sf, argc - optind, argv + optind);
break;
case ACT_BACKUP_SECTORS:
rc = command_backup_sectors(sf, argc - optind, argv + optind);
break;
case ACT_DELETE:
rc = command_delete(sf, argc - optind, argv + optind);
break;
case ACT_LIST:
rc = command_list_partitions(sf, argc - optind, argv + optind);
break;
case ACT_LIST_TYPES:
rc = command_list_types(sf);
break;
case ACT_LIST_FREE:
rc = command_list_freespace(sf, argc - optind, argv + optind);
break;
case ACT_FDISK:
rc = command_fdisk(sf, argc - optind, argv + optind);
break;
case ACT_DUMP:
rc = command_dump(sf, argc - optind, argv + optind);
break;
case ACT_SHOW_SIZE:
rc = command_show_size(sf, argc - optind, argv + optind);
break;
case ACT_SHOW_GEOM:
rc = command_show_geometry(sf, argc - optind, argv + optind);
break;
case ACT_VERIFY:
rc = command_verify(sf, argc - optind, argv + optind);
break;
case ACT_PARTTYPE:
rc = command_parttype(sf, argc - optind, argv + optind);
break;
case ACT_PARTUUID:
rc = command_partuuid(sf, argc - optind, argv + optind);
break;
case ACT_PARTLABEL:
rc = command_partlabel(sf, argc - optind, argv + optind);
break;
case ACT_PARTATTRS:
rc = command_partattrs(sf, argc - optind, argv + optind);
break;
case ACT_DISCARD_FREE:
rc = command_discard_free(sf, argc - optind, argv + optind);
break;
case ACT_DISKID:
rc = command_diskid(sf, argc - optind, argv + optind);
break;
case ACT_REORDER:
rc = command_reorder(sf, argc - optind, argv + optind);
break;
case ACT_RELOCATE:
rc = command_relocate(sf, argc - optind, argv + optind);
break;
}
sfdisk_deinit(sf);
DBG(MISC, ul_debug("bye! [rc=%d]", rc));
return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
|