include 167 KB
Newer Older
Rafael Monnerat's avatar
Rafael Monnerat committed
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 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977
#================
# FILE          : KIWILinuxRC.sh
#----------------
# PROJECT       : OpenSUSE Build-Service
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH, Germany
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This module contains common used functions
#               : for the suse linuxrc and preinit boot image
#               : files
#               : 
#               :
# STATUS        : Development
#----------------
#======================================
# Exports (General)
#--------------------------------------
export ELOG_FILE=/var/log/boot.kiwi
export TRANSFER_ERRORS_FILE=/tmp/transfer.errors
export UFONT=/usr/share/fbiterm/fonts/b16.pcf.gz
export HYBRID_PERSISTENT_FS=ext3
export HYBRID_PERSISTENT_ID=83
export HYBRID_PERSISTENT_DIR=/read-write
export UTIMER_INFO=/dev/utimer

#======================================
# Exports (General)
#--------------------------------------
test -z "$ELOG_CONSOLE"       && export ELOG_CONSOLE=/dev/tty3
test -z "$ELOG_BOOTSHELL"     && export ELOG_BOOTSHELL=/dev/tty2
test -z "$ELOG_EXCEPTION"     && export ELOG_EXCEPTION=/dev/tty1
test -z "$KLOG_CONSOLE"       && export KLOG_CONSOLE=4
test -z "$KLOG_DEFAULT"       && export KLOG_DEFAULT=1
test -z "$ELOG_STOPPED"       && export ELOG_STOPPED=0
test -z "$PARTITIONER"        && export PARTITIONER=parted
test -z "$DEFAULT_VGA"        && export DEFAULT_VGA=0x314
test -z "$HAVE_MODULES_ORDER" && export HAVE_MODULES_ORDER=1
test -z "$DIALOG_LANG"        && export DIALOG_LANG=ask
test -z "$TERM"               && export TERM=linux
test -z "$LANG"               && export LANG=en_US.utf8
test -z "$UTIMER"             && export UTIMER=0
test -z "$VGROUP"             && export VGROUP=kiwiVG
test -z "$PARTED_HAVE_ALIGN"  && export PARTED_HAVE_ALIGN=0
test -z "$PARTED_HAVE_MACHINE"&& export PARTED_HAVE_MACHINE=0
test -z "$DHCPCD_HAVE_PERSIST"&& export DHCPCD_HAVE_PERSIST=1
if [ -x /sbin/blogd ];then
	test -z "$CONSOLE"            && export CONSOLE=/dev/console
	test -z "$REDIRECT"           && export REDIRECT=/dev/tty1
fi
if parted -h | grep -q '\-\-align';then
	export PARTED_HAVE_ALIGN=1
fi
if parted -h | grep -q '\-\-machine';then
	export PARTED_HAVE_MACHINE=1
fi
if [ $PARTED_HAVE_MACHINE -eq 0 ];then
	export PARTITIONER=sfdisk
fi
if dhcpcd -p 2>&1 | grep -q 'Usage';then
	export DHCPCD_HAVE_PERSIST=0
fi

#======================================
# Dialog
#--------------------------------------
function Dialog {
	local code=1
	export DIALOG_CANCEL=1
	if [ -e /dev/fb0 ];then
		cat > /tmp/fbcode <<- EOF
			dialog \
				--ok-label "$TEXT_OK" \
				--cancel-label "$TEXT_CANCEL" \
				--yes-label "$TEXT_YES" \
				--no-label "$TEXT_NO" \
				--exit-label "$TEXT_EXIT" \
				$@
			echo \$? > /tmp/fbcode
		EOF
		fbiterm -m $UFONT -- bash /tmp/fbcode
		code=$(cat /tmp/fbcode)
	else
		eval dialog \
			--ok-label "$TEXT_OK" \
			--cancel-label "$TEXT_CANCEL" \
			--yes-label "$TEXT_YES" \
			--no-label "$TEXT_NO" \
			--exit-label "$TEXT_EXIT" \
			$@
		code=$?
	fi
	return $code
}
#======================================
# Debug
#--------------------------------------
function Debug {
	# /.../
	# print message if variable DEBUG is set to 1
	# -----
	if test "$DEBUG" = 1;then
		echo "+++++> $1"
	fi
}
#======================================
# Echo
#--------------------------------------
function Echo {
	# /.../
	# print a message to the controling terminal
	# ----
	if [ $ELOG_STOPPED = 0 ];then
		set +x
	fi
	if [ ! $UTIMER = 0 ] && kill -0 $UTIMER &>/dev/null;then
		kill -HUP $UTIMER
		local prefix=$(cat $UTIMER_INFO)
	else
		local prefix="===>"
	fi
	local option=""
	local optn=""
	local opte=""
	while getopts "bne" option;do
		case $option in
			b) prefix="    " ;;
			n) optn="-n" ;;
			e) opte="-e" ;;
			*) echo "Invalid argument: $option" ;;
		esac
	done
	shift $(($OPTIND - 1))
	if [ $ELOG_STOPPED = 0 ];then
		set -x
	fi
	echo $optn $opte "$prefix $1"
	if [ $ELOG_STOPPED = 0 ];then
		set +x
	fi
	OPTIND=1
	if [ $ELOG_STOPPED = 0 ];then
		set -x
	fi
}
#======================================
# WaitKey
#--------------------------------------
function WaitKey {
	# /.../
	# if DEBUG is set wait for ENTER to continue
	# ----
	if test "$DEBUG" = 1;then
		Echo -n "Press ENTER to continue..."
		read
	fi
}
#======================================
# closeKernelConsole
#--------------------------------------
function closeKernelConsole {
	# /.../
	# close the kernel console, set level to 1
	# ----
	if [ -x /usr/sbin/klogconsole ];then
		klogconsole -l 1
	fi
}
#======================================
# openKernelConsole
#--------------------------------------
function openKernelConsole {
	# /.../
	# move the kernel console to terminal 3 as you can't see the messages
	# now directly it looks like the kernel console is switched off
	# but it isn't really. If DEBUG is set the logging remains on
	# the first console
	# ----
	if [ ! -x /usr/sbin/klogconsole ];then
		return
	fi
	if test "$DEBUG" = 0;then
		Echo "Kernel logging enabled on: /dev/tty$KLOG_CONSOLE"
		setctsid /dev/tty$KLOG_CONSOLE \
			klogconsole -l 7 -r$KLOG_CONSOLE
	fi
}
#======================================
# reopenKernelConsole
#--------------------------------------
function reopenKernelConsole {
	# /.../
	# reopen kernel console to be able to see kernel messages
	# while the system is booting
	# ----
	if [ ! -x /usr/sbin/klogconsole ];then
		return
	fi
	Echo "Kernel logging enabled on: /dev/tty$KLOG_DEFAULT"
	klogconsole -l 7 -r$KLOG_DEFAULT
}
#======================================
# importFile
#--------------------------------------
function importFile {
	# /.../
	# import the config.<MAC> style format. the function
	# will export each entry of the file as variable into
	# the current shell environment
	# ----
	IFS="
	"
	while read line;do
		echo $line | grep -qi "^#" && continue
		key=`echo "$line" | cut -d '=' -f1`
		item=`echo "$line" | cut -d '=' -f2-`
		if [ -z "$key" ] || [ -z "$item" ];then
			continue
		fi
		if ! echo $item | grep -E -q "^(\"|')";then
			item="'"$item"'"
		fi
		Debug "$key=$item"
		eval export "$key\=$item"
	done
	if [ ! -z "$ERROR_INTERRUPT" ];then
		Echo -e "$ERROR_INTERRUPT"
		systemException "*** interrupted ****" "shell"
	fi
	IFS=$IFS_ORIG
}
#======================================
# systemException
#--------------------------------------
function systemException {
	# /.../
	# print a message to the controling terminal followed
	# by an action. Possible actions are reboot, wait
	# and opening a shell
	# ----
	set +x
	local what=$2
	local nuldev=/dev/null
	local ttydev=$ELOG_EXCEPTION
	if [ ! -e $nuldev ];then
		nuldev=/mnt/$nuldev
	fi
	if [ ! -e $ttydev ];then
		ttydev=/mnt/$ttydev
	fi
	test -e /proc/splash && echo verbose > /proc/splash
	if [ $what = "reboot" ];then
		if cat /proc/cmdline | grep -qi "kiwidebug=1";then
			what="shell"
		fi
	fi
	Echo "$1"
	case "$what" in
	"reboot")
		Echo "rebootException: error consoles at Alt-F3/F4"
		Echo "rebootException: reboot in 120 sec..."; sleep 120
		/sbin/reboot -f -i >$nuldev
	;;
	"wait")
		Echo "waitException: waiting for ever..."
		while true;do sleep 100;done
	;;
	"shell")
		Echo "shellException: providing shell..."
		setctsid $ttydev /bin/bash -i
	;;
	"user_reboot")
		Echo "reboot triggered by user: consoles at Alt-F3/F4"
		Echo "reboot in 30 sec..."; sleep 30
		/sbin/reboot -f -i >$nuldev
	;;
	*)
		Echo "unknownException..."
	;;
	esac
}
#======================================
# copyDevices
#--------------------------------------
function copyDeviceNodes {
	local search=$1
	local prefix=$2
	local dtype
	local major
	local minor
	local perms
	if [ -z "$search" ];then
		search=/dev
	fi
	pushd $search >/dev/null
	for i in *;do
		if [ -e $prefix/$i ];then
			continue
		fi
		if [ -b $i ];then
			dtype=b
		elif [ -c $i ];then
			dtype=c
		elif [ -p $i ];then
			dtype=p
		else
			continue
		fi
		info=`stat $i -c "0%a:0x%t:0x%T"`
		major=`echo $info | cut -f2 -d:`
		minor=`echo $info | cut -f3 -d:`
		perms=`echo $info | cut -f1 -d:`
		if [ $dtype = "p" ];then
			mknod -m $perms $prefix/$i $dtype
		else
			mknod -m $perms $prefix/$i $dtype $major $minor
		fi
	done
	popd >/dev/null
}
#======================================
# copyDevices
#--------------------------------------
function createInitialDevices {
	local prefix=$1
	mkdir -p $prefix
	if [ ! -d $prefix ];then
		return
	fi
	if [ -e $prefix/null ];then
		rm -f $prefix/null
	fi
	test -c $prefix/tty      || mknod -m 0666 $prefix/tty      c 5 0
	test -c $prefix/tty1     || mknod -m 0666 $prefix/tty1     c 4 1
	test -c $prefix/tty2     || mknod -m 0666 $prefix/tty2     c 4 2
	test -c $prefix/tty3     || mknod -m 0666 $prefix/tty3     c 4 3
	test -c $prefix/tty4     || mknod -m 0666 $prefix/tty4     c 4 4
	test -c $prefix/console  || mknod -m 0600 $prefix/console  c 5 1
	test -c $prefix/ptmx     || mknod -m 0666 $prefix/ptmx     c 5 2
	exec < $prefix/console > $prefix/console
	test -c $prefix/null     || mknod -m 0666 $prefix/null     c 1 3
	test -c $prefix/kmsg     || mknod -m 0600 $prefix/kmsg     c 1 11
	test -c $prefix/snapshot || mknod -m 0660 $prefix/snapshot c 10 231
	test -c $prefix/random   || mknod -m 0666 $prefix/random   c 1 8
	test -c $prefix/urandom  || mknod -m 0644 $prefix/urandom  c 1 9
	test -b $prefix/loop0    || mknod -m 0640 $prefix/loop0    b 7 0
	test -b $prefix/loop1    || mknod -m 0640 $prefix/loop1    b 7 1
	test -b $prefix/loop2    || mknod -m 0640 $prefix/loop2    b 7 2
	mkdir -p -m 0755 $prefix/pts
	mkdir -p -m 1777 $prefix/shm
	test -L $prefix/fd     || ln -s /proc/self/fd $prefix/fd
	test -L $prefix/stdin  || ln -s fd/0 $prefix/stdin
	test -L $prefix/stdout || ln -s fd/1 $prefix/stdout
	test -L $prefix/stderr || ln -s fd/2 $prefix/stderr
}
#======================================
# mountSystemFilesystems
#--------------------------------------
function mountSystemFilesystems {
	mount -t proc  proc   /proc
	mount -t sysfs sysfs  /sys
}
#======================================
# umountSystemFilesystems
#--------------------------------------
function umountSystemFilesystems {
	umount /dev/pts &>/dev/null
	umount /sys     &>/dev/null
	umount /proc    &>/dev/null
}
#======================================
# createFramebufferDevices
#--------------------------------------
function createFramebufferDevices {
	if [ -f /proc/fb ]; then
		Echo "Creating framebuffer devices"
		while read fbnum fbtype; do
			if [ $(($fbnum < 32)) ] ; then
				[ -c /dev/fb$fbnum ] || mknod -m 0660 /dev/fb$fbnum c 29 $fbnum
			fi
		done < /proc/fb
	fi
}
#======================================
# errorLogStop
#--------------------------------------
function errorLogStop {
	set +x
	export ELOG_STOPPED=1
	exec 2>$ELOG_EXCEPTION
}
#======================================
# errorLogContinue
#--------------------------------------
function errorLogContinue {
	exec 2>>$ELOG_FILE
	export ELOG_STOPPED=0
	set -x
}
#======================================
# errorLogStart
#--------------------------------------
function errorLogStart {
	# /.../
	# Log all errors up to now to the terminal specified
	# by ELOG_CONSOLE
	# ----
	if [ ! -f $ELOG_FILE ];then
		#======================================
		# Header for main stage log
		#--------------------------------------
		echo "KIWI Log:" >> $ELOG_FILE
	else
		#======================================
		# Header for pre-init stage log
		#--------------------------------------
		startUtimer
		echo "KIWI PreInit Log" >> $ELOG_FILE
		cat /iprocs | grep -v TAIL_PID > /iprocs
	fi
	echo "Boot-Logging enabled on $ELOG_CONSOLE"
	setctsid -f $ELOG_CONSOLE tail -f $ELOG_FILE &
	exec 2>>$ELOG_FILE
	if [ -f .profile ];then
		echo "KIWI .profile contents:" 1>&2
		cat .profile 1>&2
	fi
	set -x 1>&2
	local DTYPE=`stat -f -c "%T" /proc 2>/dev/null`
	if test "$DTYPE" != "proc" ; then
		mount -t proc proc /proc
	fi
	TAIL_PID=$(fuser $ELOG_CONSOLE | tr -d " ")
	echo TAIL_PID=$TAIL_PID >> /iprocs
}
#======================================
# udevPending
#--------------------------------------
function udevPending {
	local timeout=30
	if [ -x /sbin/udevadm ];then
		/sbin/udevadm trigger
		/sbin/udevadm settle --timeout=$timeout
	else
		/sbin/udevtrigger
		/sbin/udevsettle --timeout=$timeout
	fi
}
#======================================
# udevSystemStart
#--------------------------------------
function udevSystemStart {
	# /.../
	# start udev while in pre-init phase. This means we can
	# run udev from the standard runlevel script
	# ----
	/etc/init.d/boot.udev start
	echo
}
#======================================
# udevSystemStop
#--------------------------------------
function udevSystemStop {
	# /.../
	# stop udev while in pre-init phase.
	# ----
	/etc/init.d/boot.udev stop
	echo
}
#======================================
# udevStart
#--------------------------------------
function udevStart {
	# /.../
	# start the udev daemon.
	# ----
	echo "Creating device nodes with udev"
	# disable hotplug helper, udevd listens to netlink
	if [ -e /proc/sys/kernel/hotplug ];then
		echo "" > /proc/sys/kernel/hotplug
	fi
	if ! ls /lib/modules/*/modules.order &>/dev/null;then
		# /.../
		# without modules.order in place we prevent udev from loading
		# the storage modules because it does not make a propper
		# choice if there are multiple possible modules available.
		# Example:
		# udev prefers ata_generic over ata_piix but the hwinfo
		# order is ata_piix first which also seems to make more
		# sense.
		# -----
		rm -f /etc/udev/rules.d/*-drivers.rules
		rm -f /lib/udev/rules.d/*-drivers.rules
		HAVE_MODULES_ORDER=0
	fi
	# nodes in a tmpfs
	mount -t tmpfs -o mode=0755 udev /dev
	# static nodes
	createInitialDevices /dev
	# terminal devices
	mount -t devpts devpts /dev/pts
	# start the udev daemon
	udevd udev_log="debug" &
	echo UDEVD_PID=$! >> /iprocs
	# wait for pending triggered udev events.
	udevPending
	# start splashy if configured
	startSplashy
}
#======================================
# udevKill
#--------------------------------------
function udevKill {
	. /iprocs ; kill $UDEVD_PID
}
#======================================
# startSplashy
#--------------------------------------
function startSplashy {
	if [ -x /usr/sbin/splashy ];then
		splashy boot
	fi
}
#======================================
# startBlogD
#--------------------------------------
function startBlogD {
	if test -n "$REDIRECT" ; then
		mkdir -p /var/log
		> /dev/shm/initrd.msg
		ln -sf /dev/shm/initrd.msg /var/log/boot.msg
		mkdir -p /var/run
		/sbin/blogd $REDIRECT
		BLOGD_PID=$(cat /var/run/blogd.pid)
		echo BLOGD_PID=$BLOGD_PID >> /iprocs
	fi
}
#======================================
# killBlogD
#--------------------------------------
function killBlogD {
	# /.../
	# kill blogd on /dev/console
	# ----
	if test -n "$REDIRECT" ; then
		local umountProc=0
		if [ ! -e /proc/mounts ];then
			mount -t proc proc /proc
			umountProc=1
		fi
		Echo "Stopping boot logging"
		. /iprocs ; kill $BLOGD_PID
		if [ $umountProc -eq 1 ];then
			umount /proc
		fi
	fi
}
#======================================
# installBootLoader
#--------------------------------------
function installBootLoader {
	# /.../
	# generic function to install the boot loader.
	# The selection of the bootloader happens according to
	# the architecture of the system
	# ----
	local arch=`uname -m`
	if [ -z "$loader" ];then
		loader="grub"
	fi
	case $arch-$loader in
		i*86-grub)       installBootLoaderGrub ;;
		x86_64-grub)     installBootLoaderGrub ;;
		ppc*)            installBootLoaderLilo ;;
		i*86-syslinux)   installBootLoaderSyslinux ;;
		x86_64-syslinux) installBootLoaderSyslinux ;;
		i*86-extlinux)   installBootLoaderSyslinux ;;
		x86_64-extlinux) installBootLoaderSyslinux ;;
		s390-zipl)       installBootLoaderS390 ;;
		s390x-zipl)      installBootLoaderS390 ;;
		*)
		systemException \
			"*** boot loader install for $arch-$loader not implemented ***" \
		"reboot"
	esac
}
#======================================
# installBootLoaderRecovery
#--------------------------------------
function installBootLoaderRecovery {
	# /.../
	# generic function to install the boot loader into
	# the recovery partition. The selection of the bootloader
	# happens according to the architecture of the system
	# ----
	local arch=`uname -m`
	if [ -z "$loader" ];then
		loader="grub"
	fi
	case $arch-$loader in
		i*86-grub)       installBootLoaderGrubRecovery ;;
		x86_64-grub)     installBootLoaderGrubRecovery ;;
		i*86-syslinux)   installBootLoaderSyslinuxRecovery ;;
		x86_64-syslinux) installBootLoaderSyslinuxRecovery ;;
		i*86-extlinux)   installBootLoaderSyslinuxRecovery ;;
		x86_64-extlinux) installBootLoaderSyslinuxRecovery ;;
		s390-zipl)       installBootLoaderS390Recovery ;;
		s390x-zipl)      installBootLoaderS390Recovery ;;
		*)
		systemException \
			"*** boot loader setup for $arch-$loader not implemented ***" \
		"reboot"
	esac
}
#======================================
# installBootLoaderS390
#--------------------------------------
function installBootLoaderS390 {
	if [ -x /sbin/zipl ];then
		Echo "Installing boot loader..."
		zipl -c /etc/zipl.conf 1>&2
		if [ ! $? = 0 ];then
			Echo "Failed to install boot loader"
		fi
	else
		Echo "Image doesn't have zipl installed"
		Echo "Can't install boot loader"
	fi
}
#======================================
# installBootLoaderSyslinux
#--------------------------------------
function installBootLoaderSyslinux {
	local syslmbr=/usr/share/syslinux/mbr.bin
	if [ -e $syslmbr ];then
		Echo "Installing boot loader..."
		if [ $loader = "syslinux" ];then
			syslinux $imageBootDevice
		else
			extlinux --install /boot/syslinux
		fi
		dd if=$syslmbr of=$imageDiskDevice bs=512 count=1 conv=notrunc
	else
		Echo "Image doesn't have syslinux (mbr.bin) installed"
		Echo "Can't install boot loader"
	fi
}
#======================================
# installBootLoaderGrub
#--------------------------------------
function installBootLoaderGrub {
	# /.../
	# install the grub according to the contents of
	# /etc/grub.conf and /boot/grub/menu.lst
	# ----
	if [ -x /usr/sbin/grub ];then
		Echo "Installing boot loader..."
		/usr/sbin/grub --batch --no-floppy < /etc/grub.conf 1>&2
		if [ ! $? = 0 ];then
			Echo "Failed to install boot loader"
		fi
	else
		Echo "Image doesn't have grub installed"
		Echo "Can't install boot loader"
	fi
}
#======================================
# installBootLoaderLilo
#--------------------------------------
function installBootLoaderLilo {
	# /.../
	# install the lilo according to the contents of
	# /etc/lilo.conf
	# ----
	if [ -x /sbin/lilo ];then
		Echo "Installing boot loader..."
		/sbin/lilo 1>&2
		if [ ! $? = 0 ];then
			Echo "Failed to install boot loader"
		fi
	else
		Echo "Image doesn't have lilo installed"
		Echo "Can't install boot loader"
	fi
}
#======================================
# installBootLoaderS390Recovery
#--------------------------------------
function installBootLoaderS390Recovery {
	systemException \
		"*** zipl: recovery boot not implemented ***" \
	"reboot"
}
#======================================
# installBootLoaderSyslinuxRecovery
#--------------------------------------
function installBootLoaderSyslinuxRecovery {
	local syslmbr=/usr/share/syslinux/mbr.bin
	if [ -e $syslmbr ];then
		if [ $loader = "syslinux" ];then
			syslinux $imageRecoveryDevice
		else
			extlinux --install /reco-save/boot/syslinux
		fi
	else
		Echo "Image doesn't have syslinux (mbr.bin) installed"
		Echo "Can't install boot loader"
	fi
}
#======================================
# installBootLoaderGrubRecovery
#--------------------------------------
function installBootLoaderGrubRecovery {
	# /.../
	# install the grub into the recovery partition.
	# By design the recovery partition is always the
	# fourth primary partition of the disk
	# ----
	local input=/grub.input
	local gdevreco=$(expr $recoid - 1)
	echo "device (hd0) $imageDiskDevice" > $input
	echo "root (hd0,$gdevreco)"  >> $input
	echo "setup (hd0,$gdevreco)" >> $input
	echo "quit"          >> $input
	if [ -x /usr/sbin/grub ];then
		/usr/sbin/grub --batch < $input 1>&2
		rm -f $input
	else
		Echo "Image doesn't have grub installed"
		Echo "Can't install boot loader"
		systemException \
			"recovery grub setup failed" \
		"reboot"
	fi
}
#======================================
# setupSUSEInitrd
#--------------------------------------
function setupSUSEInitrd {
	# /.../
	# call mkinitrd on suse systems to create the distro initrd
	# based on /etc/sysconfig/kernel
	# ----
	bootLoaderOK=1
	local umountProc=0
	local umountSys=0
	local systemMap=0
	local haveVMX=0
	local params
	local running
	local rlinux
	local rinitrd
	for i in `find /boot/ -name "System.map*"`;do
		systemMap=1
	done
	if [ $systemMap -eq 1 ];then
		if [ ! -e /proc/mounts ];then
			mount -t proc proc /proc
			umountProc=1
		fi
		if [ ! -e /sys/block ];then
			mount -t sysfs sysfs /sys
			umountSys=1
		fi
		if [ -f /etc/init.d/boot.device-mapper ];then
			/etc/init.d/boot.device-mapper start
		fi
		if grep -qi param_B /sbin/mkinitrd;then
			params="-B"
		fi
		if [ $bootLoaderOK = "1" ];then
			if [ -f /boot/initrd.vmx ];then
				rm -f /boot/initrd.vmx
				rm -f /boot/linux.vmx
				haveVMX=1
			fi
		fi
		if ! mkinitrd $params;then
			Echo "Can't create initrd"
			systemIntegrity=unknown
			bootLoaderOK=0
		fi
		if [ -f /etc/init.d/boot.device-mapper ];then
			/etc/init.d/boot.device-mapper stop
		fi
		if [ $bootLoaderOK = "1" ] && [ $haveVMX = "1" ];then
			running=$(uname -r)
			rlinux=vmlinuz-$running
			rinitrd=initrd-$running
			ln -s $rlinux  /boot/linux.vmx
			ln -s $rinitrd /boot/initrd.vmx
		fi
		if [ $umountSys -eq 1 ];then
			umount /sys
		fi
		if [ $umountProc -eq 1 ];then
			umount /proc
		fi
	else
		Echo "Image doesn't include kernel system map"
		Echo "Can't create initrd"
		systemIntegrity=unknown
		bootLoaderOK=0
	fi
}
#======================================
# callSUSEInitrdScripts
#--------------------------------------
function callSUSEInitrdScripts {
	# /.../
	# create initrd with mkinitrd and extract the run_all.sh script
	# after that call the script in /lib/mkinitrd. The mkinitrd
	# package must be installed in the system image to do that
	# ----
	local prefix=$1
	if [ ! -d $prefix/lib/mkinitrd ];then
		Echo "No mkinitrd package installed"
		Echo "Can't call initrd scripts"
		return
	fi
	mkinitrd >/dev/null
	if [ ! -f $prefix/boot/initrd ];then
		Echo "No initrd file found"
		Echo "Can't call initrd scripts"
		return
	fi
	mkdir $prefix/tmp/suse
	cd $prefix/tmp/suse && gzip -cd $prefix/boot/initrd | cpio -i
	if [ ! -f $prefix/tmp/suse/run_all.sh ];then
		Echo "No run_all.sh script in initrd"
		Echo "Can't call initrd scripts"
		return
	fi
	Echo "Calling SUSE initrd scripts"
	chroot . bash ./run_all.sh
}
#======================================
# setupBootLoader
#--------------------------------------
function setupBootLoader {
	# /.../
	# generic function to setup the boot loader configuration.
	# The selection of the bootloader happens according to
	# the architecture of the system
	# ----
	local arch=`uname -m`
	local para=""
	while [ $# -gt 0 ];do
		para="$para \"$1\""
		shift
	done
	if [ -z "$loader" ];then
		loader="grub"
	fi
	case $arch-$loader in
		i*86-grub)       eval setupBootLoaderGrub $para ;;
		x86_64-grub)     eval setupBootLoaderGrub $para ;;
		i*86-syslinux)   eval setupBootLoaderSyslinux $para ;;
		x86_64-syslinux) eval setupBootLoaderSyslinux $para ;;
		i*86-extlinux)   eval setupBootLoaderSyslinux $para ;;
		x86_64-extlinux) eval setupBootLoaderSyslinux $para ;;
		s390-zipl)       eval setupBootLoaderS390 $para ;;
		s390x-zipl)      eval setupBootLoaderS390 $para ;;
		ppc*)            eval setupBootLoaderLilo $para ;;
		*)
		systemException \
			"*** boot loader setup for $arch-$loader not implemented ***" \
		"reboot"
	esac
}
#======================================
# setupBootLoaderRecovery
#--------------------------------------
function setupBootLoaderRecovery {
	# /.../
	# generic function to setup the boot loader configuration
	# for the recovery partition. The selection of the bootloader
	# happens according to the architecture of the system
	# ----
	local arch=`uname -m`
	local para=""
	while [ $# -gt 0 ];do
		para="$para \"$1\""
		shift
	done
	if [ -z "$loader" ];then
		loader="grub"
	fi
	case $arch-$loader in
		i*86-grub)       eval setupBootLoaderGrubRecovery $para ;;
		x86_64-grub)     eval setupBootLoaderGrubRecovery $para ;;
		i*86-syslinux)   eval setupBootLoaderSyslinuxRecovery $para ;;
		x86_64-syslinux) eval setupBootLoaderSyslinuxRecovery $para ;;
		i*86-extlinux)   eval setupBootLoaderSyslinuxRecovery $para ;;
		x86_64-extlinux) eval setupBootLoaderSyslinuxRecovery $para ;;
		s390-zipl)       eval setupBootLoaderS390Recovery $para ;;
		s390x-zipl)      eval setupBootLoaderS390Recovery $para ;;
		*)
		systemException \
			"*** boot loader setup for $arch-$loader not implemented ***" \
		"reboot"
	esac
}
#======================================
# setupBootLoaderS390Recovery
#--------------------------------------
function setupBootLoaderS390Recovery {
	systemException \
		"*** zipl: recovery boot not implemented ***" \
	"reboot"
}
#======================================
# setupBootLoaderSyslinuxRecovery
#--------------------------------------
function setupBootLoaderSyslinuxRecovery {
	# /.../
	# create syslinux configuration for the recovery boot system
	# ----
	local mountPrefix=$1  # mount path of the image
	local destsPrefix=$2  # base dir for the config files
	local gnum=$3         # boot partition ID
	local rdev=$4         # root partition
	local gfix=$5         # syslinux title postfix
	local swap=$6         # optional swap partition
	local conf=$destsPrefix/boot/syslinux/syslinux.cfg
	local kernel=""
	local initrd=""
	local fbmode=$vga
	if [ -z "$fbmode" ];then
		fbmode=$DEFAULT_VGA
	fi
	#======================================
	# import syslinux into recovery
	#--------------------------------------
	cp -a $mountPrefix/boot/syslinux $destsPrefix/boot
	#======================================
	# setup config file name
	#--------------------------------------
	if [ $loader = "extlinux" ];then
		conf=$destsPrefix/boot/syslinux/extlinux.conf
	fi
	#======================================
	# create syslinux.cfg file
	#--------------------------------------
	echo "implicit 1"                   > $conf
	echo "prompt   1"                  >> $conf
	echo "TIMEOUT $KIWI_BOOT_TIMEOUT"  >> $conf
	if \
		[ -f "$destsPrefix/boot/syslinux/gfxboot.com" ] || \
		[ -f "$destsPrefix/boot/syslinux/gfxboot.c32" ]
	then
		echo "ui gfxboot bootlogo isolinux.msg" >> $conf
	else
		echo "gfxboot  bootlogo"                >> $conf
		echo "display  isolinux.msg"            >> $conf
	fi
	kernel=linux.vmx   # this is a copy of the kiwi linux.vmx file
	initrd=initrd.vmx  # this is a copy of the kiwi initrd.vmx file
	#======================================
	# create recovery entry
	#--------------------------------------
	if [ ! -z "$OEM_RECOVERY" ];then
		#======================================
		# Reboot
		#--------------------------------------
		title=$(makeLabel "Cancel/Reboot")
		echo "DEFAULT $title"                              >> $conf
		echo "label $title"                                >> $conf
		echo " localboot 0x80"                             >> $conf
		#======================================
		# Recovery
		#--------------------------------------
		title=$(makeLabel "Recover/Repair System")
		echo "label $title"                                >> $conf
		if xenServer $kernel $mountPrefix;then
			systemException \
				"*** $loader: Xen dom0 boot not implemented ***" \
			"reboot"
		else
			echo "kernel /boot/$kernel"                    >> $conf
			echo -n "append initrd=/boot/$initrd"          >> $conf
			echo -n " root=$diskByID"                      >> $conf
			if [ ! -z "$imageDiskDevice" ];then
				echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
			fi
			echo -n " vga=$fbmode loader=$loader"          >> $conf
			echo -n " splash=silent"                       >> $conf
			echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
			echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
			if [ "$haveLVM" = "yes" ];then
				echo -n " VGROUP=$VGROUP"                  >> $conf
			fi
			echo " KIWI_RECOVERY=$recoid"                  >> $conf
			echo " showopts"                               >> $conf
		fi
		#======================================
		# Restore
		#--------------------------------------
		title=$(makeLabel "Restore Factory System")
		echo "label $title"                                >> $conf
		if xenServer $kernel $mountPrefix;then
			systemException \
				"*** $loader: Xen dom0 boot not implemented ***" \
			"reboot"
		else
			echo "kernel /boot/$kernel"                    >> $conf
			echo -n "append initrd=/boot/$initrd"          >> $conf
			echo -n " root=$diskByID"                      >> $conf
			if [ ! -z "$imageDiskDevice" ];then
				echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
			fi
			echo -n " vga=$fbmode loader=$loader"          >> $conf
			echo -n " splash=silent"                       >> $conf
			echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
			echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
			if [ "$haveLVM" = "yes" ];then
				echo -n " VGROUP=$VGROUP"                  >> $conf
			fi
			echo " KIWI_RECOVERY=$recoid RESTORE=1"        >> $conf
			echo " showopts"                               >> $conf
		fi
	fi
}
#======================================
# setupBootLoaderGrubRecovery
#--------------------------------------
function setupBootLoaderGrubRecovery {
	# /.../
	# create menu.lst file for the recovery boot system
	# ----
	local mountPrefix=$1  # mount path of the image
	local destsPrefix=$2  # base dir for the config files
	local gfix=$3         # grub title
	local menu=$destsPrefix/boot/grub/menu.lst
	local kernel=""
	local initrd=""
	local fbmode=$vga
	local gdevreco=$(expr $recoid - 1)
	if [ -z "$fbmode" ];then
		fbmode=$DEFAULT_VGA
	fi
	gdev_recovery="(hd0,$gdevreco)"
	rdev_recovery=$OEM_RECOVERY
	diskByID=`getDiskID $rdev_recovery`
	#======================================
	# import grub stages into recovery
	#--------------------------------------
	mkdir -p $destsPrefix/boot/grub
	cp $mountPrefix/boot/grub/stage1 $destsPrefix/boot/grub
	cp $mountPrefix/boot/grub/stage2 $destsPrefix/boot/grub
	#======================================
	# create recovery menu.lst
	#--------------------------------------
	echo "timeout 30" > $menu
	echo "gfxmenu $gdev_recovery/boot/message" >> $menu
	kernel=vmlinuz # this is a copy of the kiwi linux.vmx file
	initrd=initrd  # this is a copy of the kiwi initrd.vmx file
	#======================================
	# create recovery entry
	#--------------------------------------
	if [ ! -z "$OEM_RECOVERY" ];then
		#======================================
		# Make the cancel option default
		#--------------------------------------
		echo "default 0"                                  >> $menu
		#======================================
		# Reboot
		#--------------------------------------
		title=$(makeLabel "Cancel/Reboot")
		echo "title $title"                               >> $menu
		echo " reboot"                                    >> $menu
		#======================================
		# Recovery
		#--------------------------------------
		title=$(makeLabel "Recover/Repair System")
		echo "title $title"                               >> $menu
		if xenServer $kernel $mountPrefix;then
			echo " root $gdev_recovery"                   >> $menu
			echo " kernel /boot/xen.gz"                   >> $menu
			echo -n " module /boot/$kernel"               >> $menu
			echo -n " root=$diskByID $console"            >> $menu
			if [ ! -z "$imageDiskDevice" ];then
				echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
			fi
			echo -n " vga=$fbmode splash=silent"          >> $menu
			echo -n " $KIWI_INITRD_PARAMS"                >> $menu
			echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
			if [ "$haveLVM" = "yes" ];then
				echo -n " VGROUP=$VGROUP"                 >> $menu
			fi
			echo " KIWI_RECOVERY=$recoid showopts"        >> $menu
			echo " module /boot/$initrd"                  >> $menu
		else
			echo -n " kernel $gdev_recovery/boot/$kernel" >> $menu
			echo -n " root=$diskByID $console"            >> $menu
			if [ ! -z "$imageDiskDevice" ];then
				echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
			fi
			echo -n " vga=$fbmode splash=silent"          >> $menu
			echo -n " $KIWI_INITRD_PARAMS"                >> $menu
			echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
			if [ "$haveLVM" = "yes" ];then
				echo -n " VGROUP=$VGROUP"                 >> $menu
			fi
			echo " KIWI_RECOVERY=$recoid showopts"        >> $menu
			echo " initrd $gdev_recovery/boot/$initrd"    >> $menu
		fi
		#======================================
		# Restore
		#--------------------------------------
		title=$(makeLabel "Restore Factory System")
		echo "title $title"                               >> $menu
		if xenServer $kernel $mountPrefix;then
			echo " root $gdev_recovery"                   >> $menu
			echo " kernel /boot/xen.gz"                   >> $menu
			echo -n " module /boot/$kernel"               >> $menu
			echo -n " root=$diskByID $console"            >> $menu
			if [ ! -z "$imageDiskDevice" ];then
				echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
			fi
			echo -n " vga=$fbmode splash=silent"          >> $menu
			echo -n " $KIWI_INITRD_PARAMS"                >> $menu
			echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
			if [ "$haveLVM" = "yes" ];then
				echo -n " VGROUP=$VGROUP"                 >> $menu
			fi
			echo " KIWI_RECOVERY=$recoid showopts"        >> $menu
			echo " module /boot/$initrd"                  >> $menu
		else
			echo -n " kernel $gdev_recovery/boot/$kernel" >> $menu
			echo -n " root=$diskByID $console"            >> $menu
			if [ ! -z "$imageDiskDevice" ];then
				echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
			fi
			echo -n " vga=$fbmode splash=silent"          >> $menu
			echo -n " $KIWI_INITRD_PARAMS"                >> $menu
			echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
			if [ "$haveLVM" = "yes" ];then
				echo -n " VGROUP=$VGROUP"                 >> $menu
			fi
			echo -n " KIWI_RECOVERY=$recoid RESTORE=1"    >> $menu
			echo " showopts"                              >> $menu
			echo " initrd $gdev_recovery/boot/$initrd"    >> $menu
		fi
	fi
}
#======================================
# setupBootLoaderS390
#--------------------------------------
function setupBootLoaderS390 {
	# /.../
	# create /etc/zipl.conf used for the
	# zipl bootloader
	# ----
	local mountPrefix=$1  # mount path of the image
	local destsPrefix=$2  # base dir for the config files
	local znum=$3         # boot partition ID
	local rdev=$4         # root partition
	local zfix=$5         # zipl title postfix
	local swap=$6         # optional swap partition
	local conf=$destsPrefix/etc/zipl.conf
	local sysb=$destsPrefix/etc/sysconfig/bootloader
	local kname=""
	local kernel=""
	local initrd=""
	local title=""
	#======================================
	# check for device by ID
	#--------------------------------------
	local diskByID=`getDiskID $rdev`
	local swapByID=`getDiskID $swap`
	#======================================
	# check for boot image .profile
	#--------------------------------------
	if [ -f /.profile ];then
		importFile < /.profile
	fi
	#======================================
	# check for bootloader displayname
	#--------------------------------------
	if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
		kiwi_oemtitle=$kiwi_displayname
	fi
	#======================================
	# check for system image .profile
	#--------------------------------------
	if [ -f $mountPrefix/image/.profile ];then
		importFile < $mountPrefix/image/.profile
	fi
	#======================================
	# check for kernel options
	#--------------------------------------
	if [ ! -z "$kiwi_cmdline" ];then
		KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
	fi
	#======================================
	# check for syslinux title postfix
	#--------------------------------------
	if [ -z "$zfix" ];then
		zfix="unknown"
	fi
	#======================================
	# check for boot TIMEOUT
	#--------------------------------------
	if [ -z "$KIWI_BOOT_TIMEOUT" ];then
		KIWI_BOOT_TIMEOUT=100;
	fi
	#======================================
	# create directory structure
	#--------------------------------------
	for dir in $conf $sysb;do
		dir=`dirname $dir`; mkdir -p $dir
	done
	#======================================
	# create zipl.conf file
	#--------------------------------------
	local count
	local title_default
	local title_failsafe
	echo "[defaultboot]"                      > $conf
	echo "defaultmenu = menu"                >> $conf
	echo ":menu"                             >> $conf
	echo "    default = 1"                   >> $conf
	echo "    prompt = 1"                    >> $conf
	echo "    target = /boot/zipl"           >> $conf
	echo "    timeout = $KIWI_BOOT_TIMEOUT"  >> $conf
	count=1
	IFS="," ; for i in $KERNEL_LIST;do
		if test -z "$i";then
			continue
		fi
		kname=${KERNEL_NAME[$count]}
		if ! echo $zfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
			if [ "$count" = "1" ];then
				title_default=$(makeLabel "$zfix")
			else
				title_default=$(makeLabel "$kname ( $zfix )")
			fi
		elif [ -z "$kiwi_oemtitle" ];then
			title_default=$(makeLabel "$kname ( $zfix )")
		else
			if [ "$count" = "1" ];then
				title_default=$(makeLabel "$kiwi_oemtitle ( $zfix )")
			else
				title_default=$(makeLabel "$kiwi_oemtitle-$kname ( $zfix )")
			fi
		fi
		title_failsafe=$(makeLabel "Failsafe -- $title_default")
		echo "    $count = $title_default"  >> $conf
		count=`expr $count + 1`
		echo "    $count = $title_failsafe" >> $conf
		count=`expr $count + 1`
	done
	count=1
	IFS="," ; for i in $KERNEL_LIST;do
		if test -z "$i";then
			continue
		fi
		kernel=`echo $i | cut -f1 -d:`
		initrd=`echo $i | cut -f2 -d:`
		kname=${KERNEL_NAME[$count]}
		if ! echo $zfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
			if [ "$count" = "1" ];then
				title_default=$(makeLabel "$zfix")
			else
				title_default=$(makeLabel "$kname ( $zfix )")
			fi
		elif [ -z "$kiwi_oemtitle" ];then
			title_default=$(makeLabel "$kname ( $zfix )")
		else
			if [ "$count" = "1" ];then
				title_default=$(makeLabel "$kiwi_oemtitle ( $zfix )")
			else
				title_default=$(makeLabel "$kiwi_oemtitle-$kname ( $zfix )")
			fi
		fi
		title_failsafe=$(makeLabel "Failsafe -- $title_default")
		#======================================
		# create standard entry
		#--------------------------------------
		echo "[$title_default]"                  >> $conf
		echo "target  = /boot/zipl"              >> $conf
		echo "image   = /boot/$kernel"           >> $conf
		echo "ramdisk = /boot/$initrd,0x2000000" >> $conf
		echo -n "parameters = \"root=$diskByID"  >> $conf
		if [ ! -z "$imageDiskDevice" ];then
			echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
		fi
		if [ ! -z "$swap" ];then
			echo -n " resume=$swapByID" >> $conf
		fi
		if [ "$haveLVM" = "yes" ];then
			echo -n " VGROUP=$VGROUP" >> $conf
		fi
		echo -n " $KIWI_INITRD_PARAMS"  >> $conf
		echo -n " $KIWI_KERNEL_OPTIONS" >> $conf
		echo " loader=$loader\""        >> $conf
		#======================================
		# create failsafe entry
		#--------------------------------------
		echo "[$title_failsafe]"                 >> $conf
		echo "target  = /boot/zipl"              >> $conf
		echo "image   = /boot/$kernel"           >> $conf
		echo "ramdisk = /boot/$initrd,0x2000000" >> $conf
		echo -n "parameters = \"root=$diskByID"  >> $conf
		if [ ! -z "$imageDiskDevice" ];then
			echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
		fi
		if [ "$haveLVM" = "yes" ];then
			echo -n " VGROUP=$VGROUP" >> $conf
		fi
		echo -n " $KIWI_INITRD_PARAMS"       >> $conf
		echo -n " $KIWI_KERNEL_OPTIONS"      >> $conf
		echo " loader=$loader x11failsafe\"" >> $conf
		count=`expr $count + 1`
	done
	#======================================
	# create recovery entry
	#--------------------------------------
	if [ ! -z "$OEM_RECOVERY" ];then
		systemException \
			"*** zipl: recovery chain loading not implemented ***" \
		"reboot"
	fi
	#======================================
	# create sysconfig/bootloader
	#--------------------------------------
	echo "LOADER_TYPE=\"$loader\""                            > $sysb
	echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
	echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID"                          >> $sysb
	fi
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo " showopts\""                                       >> $sysb
	echo -n "FAILSAFE_APPEND=\"root=$diskByID"               >> $sysb
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo -n " x11failsafe noresume\""                        >> $sysb
}
#======================================
# setupBootLoaderSyslinux
#--------------------------------------
function setupBootLoaderSyslinux {
	# /.../
	# create syslinux.cfg used for the
	# syslinux bootloader
	# ----
	local mountPrefix=$1  # mount path of the image
	local destsPrefix=$2  # base dir for the config files
	local gnum=$3         # boot partition ID
	local rdev=$4         # root partition
	local gfix=$5         # syslinux title postfix
	local swap=$6         # optional swap partition
	local conf=$destsPrefix/boot/syslinux/syslinux.cfg
	local sysb=$destsPrefix/etc/sysconfig/bootloader
	local console=""
	local kname=""
	local kernel=""
	local initrd=""
	local title=""
	local fbmode=$vga
	if [ ! -z "$OEM_RECOVERY" ];then
		local gdevreco=$recoid
	fi
	if [ -z "$fbmode" ];then
		fbmode=$DEFAULT_VGA
	fi
	#======================================
	# check for device by ID
	#--------------------------------------
	local diskByID=`getDiskID $rdev`
	local swapByID=`getDiskID $swap`
	#======================================
	# check for boot image .profile
	#--------------------------------------
	if [ -f /.profile ];then
		importFile < /.profile
	fi
	#======================================
	# check for bootloader displayname
	#--------------------------------------
	if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
		kiwi_oemtitle=$kiwi_displayname
	fi
	#======================================
	# check for system image .profile
	#--------------------------------------
	if [ -f $mountPrefix/image/.profile ];then
		importFile < $mountPrefix/image/.profile
	fi
	#======================================
	# check for kernel options
	#--------------------------------------
	if [ ! -z "$kiwi_cmdline" ];then
		KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
	fi
	#======================================
	# setup config file name
	#--------------------------------------
	if [ $loader = "extlinux" ];then
		conf=$destsPrefix/boot/syslinux/extlinux.conf
	fi
	#======================================
	# check for syslinux title postfix
	#--------------------------------------
	if [ -z "$gfix" ];then
		gfix="unknown"
	fi
	#======================================
	# check for boot TIMEOUT
	#--------------------------------------
	if [ -z "$KIWI_BOOT_TIMEOUT" ];then
		KIWI_BOOT_TIMEOUT=100;
	fi
	#======================================
	# create directory structure
	#--------------------------------------
	for dir in $conf $sysb;do
		dir=`dirname $dir`; mkdir -p $dir
	done
	#======================================
	# create syslinux.cfg file
	#--------------------------------------
	echo "implicit 1"                   > $conf
	echo "prompt   1"                  >> $conf
	echo "TIMEOUT $KIWI_BOOT_TIMEOUT"  >> $conf
	if \
		[ -f "$destsPrefix/boot/syslinux/gfxboot.com" ] || \
		[ -f "$destsPrefix/boot/syslinux/gfxboot.c32" ]
	then
		echo "ui gfxboot bootlogo isolinux.msg" >> $conf
	else
		echo "gfxboot  bootlogo"                >> $conf
		echo "display  isolinux.msg"            >> $conf
	fi
	local count=1
	IFS="," ; for i in $KERNEL_LIST;do
		if test ! -z "$i";then
			#======================================
			# setup syslinux requirements
			#--------------------------------------
			kernel=`echo $i | cut -f1 -d:`
			initrd=`echo $i | cut -f2 -d:`
			kname=${KERNEL_NAME[$count]}
			#======================================
			# move to FAT requirements 8+3
			#--------------------------------------
			if [ "$loader" = "syslinux" ];then
				kernel="linux.$count"
				initrd="initrd.$count"
			fi
			if ! echo $gfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
				if [ "$count" = "1" ];then
					title=$(makeLabel "$gfix")
				else
					title=$(makeLabel "$kname [ $gfix ]")
				fi
			elif [ -z "$kiwi_oemtitle" ];then
				title=$(makeLabel "$kname [ $gfix ]")
			else
				if [ "$count" = "1" ];then
					title=$(makeLabel "$kiwi_oemtitle [ $gfix ]")
				else
					title=$(makeLabel "$kiwi_oemtitle-$kname [ $gfix ]")
				fi
			fi
			#======================================
			# create standard entry
			#--------------------------------------
			echo "DEFAULT $title"                              >> $conf
			echo "label $title"                                >> $conf
			if xenServer $kernel $mountPrefix;then
				systemException \
					"*** $loader: Xen dom0 boot not implemented ***" \
				"reboot"
			else
				echo "kernel /boot/$kernel"                    >> $conf
				echo -n "append initrd=/boot/$initrd"          >> $conf
				echo -n " root=$diskByID $console"             >> $conf
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
				fi
				echo -n " vga=$fbmode loader=$loader"          >> $conf
				echo -n " splash=silent"                       >> $conf
				if [ ! -z "$swap" ];then
					echo -n " resume=$swapByID"                >> $conf
				fi
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"         >> $conf
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"         >> $conf
				fi
				echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
				echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                  >> $conf
				fi
				echo " showopts"                               >> $conf
			fi
			#======================================
			# create Failsafe entry
			#--------------------------------------
			title=$(makeLabel "Failsafe -- $title")
			echo "label $title"                                >> $conf
			if xenServer $kernel $mountPrefix;then
				systemException \
					"*** $loader: Xen dom0 boot not implemented ***" \
				"reboot"
			else
				echo "kernel /boot/$kernel"                    >> $conf
				echo -n "append initrd=/boot/$initrd"          >> $conf
				echo -n " root=$diskByID $console"             >> $conf
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
				fi
				echo -n " vga=$fbmode loader=$loader"          >> $conf
				echo -n " splash=silent"                       >> $conf
				if [ ! -z "$swap" ];then
					echo -n " resume=$swapByID"                >> $conf
				fi
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"         >> $conf
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"         >> $conf
				fi
				echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
				echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                  >> $conf
				fi
				echo -n " showopts ide=nodma apm=off acpi=off" >> $conf
				echo -n " noresume selinux=0 nosmp"            >> $conf
				echo " noapic maxcpus=0 edd=off"               >> $conf
			fi
			count=`expr $count + 1`
		fi
	done
	#======================================
	# create recovery entry
	#--------------------------------------
	if [ ! -z "$OEM_RECOVERY" ];then
		echo "label Recovery"                             >> $conf
		echo "kernel chain"                               >> $conf
		echo "append hd0 $gdevreco"                       >> $conf
	fi
	#======================================
	# create sysconfig/bootloader
	#--------------------------------------
	echo "LOADER_TYPE=\"$loader\""                           > $sysb
	echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
	echo "DEFAULT_VGA=\"$fbmode\""                           >> $sysb 
	echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID"                          >> $sysb
	fi
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo " showopts\""                                       >> $sysb
	echo "FAILSAFE_VGA=\"$fbmode\""                          >> $sysb
	echo -n "FAILSAFE_APPEND=\"root=$diskByID splash=silent" >> $sysb
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID"                          >> $sysb
	fi
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo -n " showopts ide=nodma apm=off acpi=off noresume"  >> $sysb
	echo "selinux=0 nosmp noapic maxcpus=0 edd=off\""        >> $sysb
}
#======================================
# setupBootLoaderGrub
#--------------------------------------
function setupBootLoaderGrub {
	# /.../
	# create grub.conf and menu.lst file used for
	# installing the bootloader
	# ----
	local mountPrefix=$1  # mount path of the image
	local destsPrefix=$2  # base dir for the config files
	local gnum=$3         # grub boot partition ID
	local rdev=$4         # grub root partition
	local gfix=$5         # grub title postfix
	local swap=$6         # optional swap partition
	local menu=$destsPrefix/boot/grub/menu.lst
	local conf=$destsPrefix/etc/grub.conf
	local dmap=$destsPrefix/boot/grub/device.map
	local sysb=$destsPrefix/etc/sysconfig/bootloader
	local stage=/boot/grub/stage2
	local console=""
	local kname=""
	local kernel=""
	local initrd=""
	local title=""
	local rdisk=""
	local fbmode=$vga
	if [ ! -z "$OEM_RECOVERY" ];then
		local gdevreco=$(expr $recoid - 1)
	fi
	if [ -z "$fbmode" ];then
		fbmode=$DEFAULT_VGA
	fi
	#======================================
	# check for device by ID
	#--------------------------------------
	local diskByID=`getDiskID $rdev`
	local swapByID=`getDiskID $swap`
	#======================================
	# check for boot image .profile
	#--------------------------------------
	if [ -f /.profile ];then
		importFile < /.profile
	fi
	#======================================
	# check for system image .profile
	#--------------------------------------
	if [ -f $mountPrefix/image/.profile ];then
		importFile < $mountPrefix/image/.profile
	fi
	#======================================
	# check for bootloader displayname
	#--------------------------------------
	if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
		kiwi_oemtitle=$kiwi_displayname
	fi
	#======================================
	# check for kernel options
	#--------------------------------------
	if [ ! -z "$kiwi_cmdline" ];then
		KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
	fi
	#======================================
	# check for grub device
	#--------------------------------------
	if [ -z "$gnum" ];then
		gnum=1
	fi
	#======================================
	# check for grub title postfix
	#--------------------------------------
	if [ -z "$gfix" ];then
		gfix="unknown"
	fi
	#======================================
	# check for boot TIMEOUT
	#--------------------------------------
	if [ -z "$KIWI_BOOT_TIMEOUT" ];then
		KIWI_BOOT_TIMEOUT=10;
	fi
	#======================================
	# create directory structure
	#--------------------------------------
	for dir in $menu $conf $dmap $sysb;do
		dir=`dirname $dir`; mkdir -p $dir
	done
	#======================================
	# setup grub device node
	#--------------------------------------
	gdev="(hd0,$gnum)"
	#======================================
	# create menu.lst file
	#--------------------------------------
	echo "timeout $KIWI_BOOT_TIMEOUT"  > $menu
	if [ -f /image/loader/message ] || [ -f /boot/message ];then
		echo "gfxmenu $gdev/boot/message" >> $menu
	fi
	local count=1
	IFS="," ; for i in $KERNEL_LIST;do
		if test ! -z "$i";then
			#======================================
			# create grub requirements
			#--------------------------------------
			kernel=`echo $i | cut -f1 -d:`
			initrd=`echo $i | cut -f2 -d:`
			kname=${KERNEL_NAME[$count]}
			if ! echo $gfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
				if [ "$count" = "1" ];then
					title=$(makeLabel "$gfix")
				else
					title=$(makeLabel "$kname [ $gfix ]")
				fi
			elif [ -z "$kiwi_oemtitle" ];then
				title=$(makeLabel "$kname [ $gfix ]")
			else
				if [ "$count" = "1" ];then
					title=$(makeLabel "$kiwi_oemtitle [ $gfix ]")
				else
					title=$(makeLabel "$kiwi_oemtitle-$kname [ $gfix ]")
				fi
			fi
			#======================================
			# create standard entry
			#--------------------------------------
			echo "title $title"                                   >> $menu
			if xenServer $kname $mountPrefix;then
				echo " root $gdev"                                >> $menu
				echo " kernel /boot/xen.gz"                       >> $menu
				echo -n " module /boot/$kernel"                   >> $menu
				echo -n " root=$diskByID"                         >> $menu
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
				fi
				echo -n " $console vga=$fbmode splash=silent"     >> $menu
				if [ ! -z "$swap" ];then
					echo -n " resume=$swapByID"                   >> $menu
				fi
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"            >> $menu
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"            >> $menu
				fi
				echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
				echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                     >> $menu
				fi
				echo " showopts"                                  >> $menu
				echo " module /boot/$initrd"                      >> $menu
			else
				echo -n " kernel $gdev/boot/$kernel"              >> $menu
				echo -n " root=$diskByID"                         >> $menu
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
				fi
				echo -n " $console vga=$fbmode splash=silent"     >> $menu
				if [ ! -z "$swap" ];then
					echo -n " resume=$swapByID"                   >> $menu
				fi
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"            >> $menu
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"            >> $menu
				fi
				echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
				echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                     >> $menu
				fi
				echo " showopts"                                  >> $menu
				echo " initrd $gdev/boot/$initrd"                 >> $menu
			fi
			#======================================
			# create failsafe entry
			#--------------------------------------
			title=$(makeLabel "Failsafe -- $title")
			echo "title $title"                                   >> $menu
			if xenServer $kname $mountPrefix;then
				echo " root $gdev"                                >> $menu
				echo " kernel /boot/xen.gz"                       >> $menu
				echo -n " module /boot/$kernel"                   >> $menu
				echo -n " root=$diskByID"                         >> $menu
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
				fi
				echo -n " $console vga=$fbmode splash=silent"     >> $menu
				echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
				echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                     >> $menu
				fi
				echo -n " showopts ide=nodma apm=off acpi=off"    >> $menu
				echo -n " noresume selinux=0 nosmp"               >> $menu
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"            >> $menu
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"            >> $menu
				fi
				echo " noapic maxcpus=0 edd=off"                  >> $menu
				echo " module /boot/$initrd"                      >> $menu
			else
				echo -n " kernel $gdev/boot/$kernel"              >> $menu
				echo -n " root=$diskByID"                         >> $menu
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
				fi
				echo -n " $console vga=$fbmode splash=silent"     >> $menu
				echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
				echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                     >> $menu
				fi
				echo -n " showopts ide=nodma apm=off acpi=off"    >> $menu
				echo -n " noresume selinux=0 nosmp"               >> $menu
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"            >> $menu
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"            >> $menu
				fi
				echo " noapic maxcpus=0 edd=off"                  >> $menu
				echo " initrd $gdev/boot/$initrd"                 >> $menu
			fi
			count=`expr $count + 1`
		fi
	done
	#======================================
	# create recovery entry
	#--------------------------------------
	if [ ! -z "$OEM_RECOVERY" ];then
		echo "title Recovery"                             >> $menu
		echo " rootnoverify (hd0,$gdevreco)"              >> $menu
		echo " chainloader +1"                            >> $menu
	fi
	#======================================
	# create grub.conf file
	#--------------------------------------
	echo "root $gdev" > $conf
	if dd if=$rdev bs=1 count=512 | file - | grep -q Bootloader;then
		echo "setup --stage2=$stage $gdev" >> $conf
	else
		echo "setup --stage2=$stage (hd0)" >> $conf
	fi
	echo "quit" >> $conf
	#======================================
	# create grub device map
	#--------------------------------------
	rdisk=`echo $rdev | sed -e s"@[0-9]@@g"`
	if [ ! -z "$imageDiskDevice" ];then
		rdisk=$imageDiskDevice
	fi
	echo "(hd0) $rdisk" > $dmap
	#======================================
	# create sysconfig/bootloader
	#--------------------------------------
	echo "LOADER_TYPE=\"grub\""                               > $sysb
	echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
	echo "DEFAULT_VGA=\"$fbmode\""                           >> $sysb  
	echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID"                          >> $sysb
	fi
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo " showopts\""                                       >> $sysb
	echo "FAILSAFE_VGA=\"$fbmode\""                          >> $sysb
	echo -n "FAILSAFE_APPEND=\"root=$diskByID splash=silent" >> $sysb
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID"                          >> $sysb
	fi
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo -n " showopts ide=nodma apm=off acpi=off noresume"  >> $sysb
	echo "selinux=0 nosmp noapic maxcpus=0 edd=off\""        >> $sysb
}
#======================================
# setupBootLoaderLilo
#--------------------------------------
function setupBootLoaderLilo {
	# /.../
	# create lilo.conf file used for
	# installing the bootloader
	# ----
	local mountPrefix=$1  # mount path of the image
	local destsPrefix=$2  # base dir for the config files
	local lnum=$3         # lilo boot partition ID
	local rdev=$4         # lilo root partition
	local lfix=$5         # lilo title postfix
	local swap=$6         # optional swap partition
	local conf=$destsPrefix/etc/lilo.conf
	local sysb=$destsPrefix/etc/sysconfig/bootloader
	local console=""
	local kname=""
	local kernel=""
	local initrd=""
	local title=""
	local rdisk=""
	local fbmode=$vga
	if [ -z "$fbmode" ];then
		fbmode=$DEFAULT_VGA
	fi
	#======================================
	# check for device by ID
	#--------------------------------------
	local diskByID=`getDiskID $rdev`
	local swapByID=`getDiskID $swap`
	#======================================
	# check for boot image .profile
	#--------------------------------------
	if [ -f /.profile ];then
		importFile < /.profile
	fi
	#======================================
	# check for bootloader displayname
	#--------------------------------------
	if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
		kiwi_oemtitle=$kiwi_displayname
	fi
	#======================================
	# check for system image .profile
	#--------------------------------------
	if [ -f $mountPrefix/image/.profile ];then
		importFile < $mountPrefix/image/.profile
	fi
	#======================================
	# check for kernel options
	#--------------------------------------
	if [ ! -z "$kiwi_cmdline" ];then
		KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
	fi
	#======================================
	# check for lilo title postfix
	#--------------------------------------
	if [ -z "$lfix" ];then
		lfix="unknown"
	fi
	#======================================
	# check for boot TIMEOUT
	#--------------------------------------
	if [ -z "$KIWI_BOOT_TIMEOUT" ];then
		KIWI_BOOT_TIMEOUT=10;
	fi
	#======================================
	# setup lilo boot device
	#--------------------------------------
	rdisk=`echo $rdev | sed -e s"@[0-9]@@g"`
	if [ ! -z "$imageDiskDevice" ];then
		rdisk=$imageDiskDevice
	fi
	rdev=$rdisk$lnum
	#======================================
	# create directory structure
	#--------------------------------------
	for dir in $conf $sysb;do
		dir=`dirname $dir`; mkdir -p $dir
	done
	#======================================
	# create lilo.conf file
	#--------------------------------------
	echo "boot=$rdev"                                        >  $conf
	echo "activate"                                          >> $conf
	echo "timeout=`expr $KIWI_BOOT_TIMEOUT \* 10`"           >> $conf
	echo "default=kiwi$count"                                >> $conf
	local count=1
	IFS="," ; for i in $KERNEL_LIST;do
		if test ! -z "$i";then
			#======================================
			# create lilo requirements
			#--------------------------------------
			kernel=`echo $i | cut -f1 -d:`
			initrd=`echo $i | cut -f2 -d:`
			kname=${KERNEL_NAME[$count]}
			if ! echo $lfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
				if [ "$count" = "1" ];then
					title=$(makeLabel "$lfix")
				else
					title=$(makeLabel "$kname [ $lfix ]")
				fi
			elif [ -z "$kiwi_oemtitle" ];then
				title=$(makeLabel "$kname [ $lfix ]")
			else
				if [ "$count" = "1" ];then
					title=$(makeLabel "$kiwi_oemtitle [ $lfix ]")
				else
					title=$(makeLabel "$kiwi_oemtitle-$kname [ $lfix ]")
				fi
			fi
			#======================================
			# create standard entry
			#--------------------------------------
			echo "label=\"$title\""                           >> $conf
			if xenServer $kname $mountPrefix;then
				systemException \
					"*** lilo: Xen dom0 boot not implemented ***" \
				"reboot"
			else
				echo "image=/boot/$kernel"                    >> $conf
				echo "initrd=/boot/$initrd"                   >> $conf
				echo -n "append=\"quiet sysrq=1 panic=9"      >> $conf
				echo -n " root=$diskByID"                     >> $conf
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)" >> $conf
				fi
				echo -n " $console vga=$fbmode splash=silent" >> $conf
				if [ ! -z "$swap" ];then                     
					echo -n " resume=$swapByID"               >> $conf
				fi
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"        >> $conf
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"        >> $conf
				fi
				echo -n " $KIWI_INITRD_PARAMS"                >> $conf
				echo -n " $KIWI_KERNEL_OPTIONS"               >> $conf
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                 >> $conf
				fi
				echo " showopts\""                            >> $conf
			fi
			#======================================
			# create failsafe entry
			#--------------------------------------
			title=$(makeLabel "Failsafe -- $title")
			echo "label=\"$title\""                           >> $conf
			if xenServer $kname $mountPrefix;then
				systemException \
					"*** lilo: Xen dom0 boot not implemented ***" \
				"reboot"
			else
				echo "image=/boot/$kernel"                    >> $conf
				echo "initrd=/boot/$initrd"                   >> $conf
				echo -n "append=\"quiet sysrq=1 panic=9"      >> $conf
				echo -n " root=$diskByID"                     >> $conf
				if [ ! -z "$imageDiskDevice" ];then
					echo -n " disk=$(getDiskID $imageDiskDevice)" >> $conf
				fi
				echo -n " $console vga=$fbmode splash=silent" >> $conf
				if [ ! -z "$swap" ];then
					echo -n " resume=$swapByID"               >> $conf
				fi
				if [ -e /dev/xvc0 ];then
					echo -n " console=xvc console=tty"        >> $conf
				elif [ -e /dev/hvc0 ];then
					echo -n " console=hvc console=tty"        >> $conf
				fi
				echo -n " $KIWI_INITRD_PARAMS"                >> $conf
				echo -n " $KIWI_KERNEL_OPTIONS"               >> $conf
				if [ "$haveLVM" = "yes" ];then
					echo -n " VGROUP=$VGROUP"                 >> $conf
				fi
				echo -n " showopts ide=nodma apm=off"         >> $conf
				echo " acpi=off noresume selinux=0 nosmp\""   >> $conf
			fi
			count=`expr $count + 1`
		fi
	done
	#======================================
	# create recovery entry
	#--------------------------------------
	if [ ! -z "$OEM_RECOVERY" ];then
		systemException \
			"*** lilo: recovery chain loading not implemented ***" \
		"reboot"
	fi
	#======================================
	# create sysconfig/bootloader
	#--------------------------------------
	echo "LOADER_TYPE=\"lilo\""                               > $sysb
	echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
	echo "DEFAULT_VGA=\"$fbmode\""                           >> $sysb 
	echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID"                          >> $sysb
	fi
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo " showopts\""                                       >> $sysb
	echo "FAILSAFE_VGA=\"$fbmode\""                          >> $sysb
	echo -n "FAILSAFE_APPEND=\"root=$diskByID splash=silent" >> $sysb
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID"                          >> $sysb
	fi
	echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
	echo -n " showopts ide=nodma apm=off acpi=off noresume"  >> $sysb
	echo "selinux=0 nosmp noapic maxcpus=0 edd=off\""        >> $sysb
}
#======================================
# setupDefaultPXENetwork
#--------------------------------------
function setupDefaultPXENetwork {
	# /.../
	# create the /sysconfig/network file according to the PXE
	# boot interface.
	# ----
	local prefix=$1
	local niface=$prefix/etc/sysconfig/network/ifcfg-$PXE_IFACE
	mkdir -p $prefix/etc/sysconfig/network
	cat > $niface < /dev/null
	echo "BOOTPROTO='dhcp'"    >> $niface
	echo "STARTMODE='ifplugd'" >> $niface
	echo "USERCONTROL='no'"    >> $niface
}
#======================================
# setupDefaultFstab
#--------------------------------------
function setupDefaultFstab {
	# /.../
	# create a new /etc/fstab file with the default entries
	# ----
	local prefix=$1
	local nfstab=$prefix/etc/fstab
	mkdir -p $prefix/etc
	cat > $nfstab < /dev/null
	echo "devpts  /dev/pts   devpts  mode=0620,gid=5 0 0"   >> $nfstab
	echo "proc    /proc      proc    defaults 0 0"          >> $nfstab
	echo "sysfs   /sys       sysfs   noauto 0 0"            >> $nfstab
}
#======================================
# updateRootDeviceFstab
#--------------------------------------
function updateRootDeviceFstab {
	# /.../
	# add one line to the fstab file for the root device
	# ----
	IFS=$IFS_ORIG
	local prefix=$1
	local rdev=$2
	local nfstab=$prefix/etc/fstab
	local diskByID=`getDiskID $rdev`
	if [ ! -z "$NFSROOT" ];then
		local server=`echo $rdev | cut -f3 -d" "`
		local option=`echo $rdev | cut -f2 -d" "`
		echo "$server / nfs $option 0 0" >> $nfstab
		return
	fi
	#======================================
	# check for device by ID
	#--------------------------------------
	if [ -z "$UNIONFS_CONFIG" ]; then
		echo "$diskByID / $FSTYPE defaults 0 0" >> $nfstab
	else
		echo "/dev/root / defaults 0 0" >> $nfstab
	fi
	#======================================
	# check for LVM volume setup
	#--------------------------------------
	if [ "$haveLVM" = "yes" ];then
		for i in /dev/$VGROUP/LV*;do
			if [ ! -e $i ];then
				continue
			fi
			local volume=$(echo $i | cut -f4 -d/ | cut -c3-)
			local mpoint=$(echo $volume | tr _ /)
			if \
				[ ! $volume = "Root" ] && \
				[ ! $volume = "Comp" ] && \
				[ ! $volume = "Home" ] && \
				[ ! $volume = "Swap" ]
			then
				echo "/dev/$VGROUP/LV$volume /$mpoint $FSTYPE defaults 0 0" \
				>> $nfstab
			fi
		done
	fi
}
#======================================
# updateSwapDeviceFstab
#--------------------------------------
function updateSwapDeviceFstab {
	# /.../
	# add one line to the fstab file for the swap device
	# ----
	local prefix=$1
	local sdev=$2
	local diskByID=`getDiskID $sdev`
	local nfstab=$prefix/etc/fstab
	echo "$diskByID swap swap pri=42 0 0" >> $nfstab
}
#======================================
# updateLVMBootDeviceFstab
#--------------------------------------
function updateLVMBootDeviceFstab {
	# /.../
	# add one line to the fstab file for the /lvmboot
	# device partition
	# ----
	local prefix=$1
	local sdev=$2
	local mount=$3
	if [ -z "$mount" ];then
		mount=/lvmboot
	fi
	local diskByID=`getDiskID $sdev`
	local nfstab=$prefix/etc/fstab
	if [ ! -z "$FSTYPE" ];then
		FSTYPE_SAVE=$FSTYPE
	fi
	#======================================
	# probe filesystem
	#--------------------------------------
	probeFileSystem $sdev
	if [ -z "$FSTYPE" ] || [ "$FSTYPE" = "unknown" ];then
		FSTYPE="auto"
	fi
	echo "$diskByID $mount $FSTYPE defaults 0 0" >> $nfstab
	echo "$mount/boot /boot none bind 0 0" >> $nfstab
	if [ ! -z "$FSTYPE_SAVE" ];then
		FSTYPE=$FSTYPE_SAVE
	fi
}
#======================================
# updateClicBootDeviceFstab
#--------------------------------------
function updateClicBootDeviceFstab {
	updateLVMBootDeviceFstab $1 $2 "/clicboot"
}
#======================================
# updateLuksBootDeviceFstab
#--------------------------------------
function updateLuksBootDeviceFstab {
	updateLVMBootDeviceFstab $1 $2 "/luksboot"
}
#======================================
# updateBtrBootDeviceFstab
#--------------------------------------
function updateBtrBootDeviceFstab {
	updateLVMBootDeviceFstab $1 $2 "/btrboot"
}
#======================================
# updateSyslinuxBootDeviceFstab
#--------------------------------------
function updateSyslinuxBootDeviceFstab {
	# /.../
	# add one line to the fstab file for the /syslboot
	# device partition
	# ----
	local prefix=$1
	local sdev=$2
	local diskByID=`getDiskID $sdev`
	local nfstab=$prefix/etc/fstab
	if [ $loader = "syslinux" ];then
		echo "$diskByID /syslboot vfat defaults 0 0" >> $nfstab
	else
		echo "$diskByID /syslboot ext2 defaults 0 0" >> $nfstab
	fi
	echo "/syslboot/boot /boot none bind 0 0" >> $nfstab
}
#======================================
# updateOtherDeviceFstab
#--------------------------------------
function updateOtherDeviceFstab {
	# /.../
	# check the contents of the $PART variable and
	# add one line to the fstab file for each partition
	# which has a mount point defined.
	# ----
	local prefix=$1
	local nfstab=$prefix/etc/fstab
	local index=0
	local field=0
	local count=0
	local device
	local IFS=","
	for i in $PART;do
		field=0
		count=$((count + 1))
		IFS=";" ; for n in $i;do
		case $field in
			0) partSize=$n   ; field=1 ;;
			1) partID=$n     ; field=2 ;;
			2) partMount=$n;
		esac
		done
		if  [ ! -z "$partMount" ]    && \
			[ ! "$partMount" = "x" ] && \
			[ ! "$partMount" = "/" ]
		then
			device=$(ddn $DISK $count)
			probeFileSystem $device
			echo "$device $partMount $FSTYPE defaults 1 1" >> $nfstab
		fi
	done
}
#======================================
# setupKernelModules
#--------------------------------------
function setupKernelModules {
	# /.../
	# create sysconfig/kernel file which includes the
	# kernel modules to become integrated into the initrd
	# if created by the distro mkinitrd tool
	# ----
	local destprefix=$1
	local srcprefix=$2
	if [ -z "$srcprefix" ];then
		srcprefix=/mnt
	fi
	local ktempl=$srcprefix/var/adm/fillup-templates/sysconfig.kernel
	local syskernel=$destprefix/etc/sysconfig/kernel
	mkdir -p $destprefix/etc/sysconfig
	if [ ! -f $ktempl ];then
		systemException \
			"Can't find kernel sysconfig template in system image !" \
		"reboot"
	fi
	cp $ktempl $syskernel
	if [ ! -e $srcprefix/lib/mkinitrd/scripts/boot-usb.sh ];then
		# /.../
		# if boot-usb.sh does not exist we are based on an old
		# mkinitrd version which requires all modules as part of
		# sysconfig/kernel. Therefore we include all USB modules
		# required to support USB storage like USB sticks
		# ----
		local USB_MODULES="ehci-hcd ohci-hcd uhci-hcd usbcore usb-storage sd"
		INITRD_MODULES="$INITRD_MODULES $USB_MODULES"
	fi
	sed -i -e \
		s"@^INITRD_MODULES=.*@INITRD_MODULES=\"$INITRD_MODULES\"@" \
	$syskernel
	sed -i -e \
		s"@^DOMU_INITRD_MODULES=.*@DOMU_INITRD_MODULES=\"$DOMURD_MODULES\"@" \
	$syskernel
}
#======================================
# kernelCheck
#--------------------------------------
function kernelCheck {
	# /.../
	# Check this running kernel against the kernel
	# installed in the image. If the version does not 
	# match we need to reboot to activate the system
	# image kernel.
	# ----
	kactive=`uname -r`
	kreboot=1
	prefix=$1
	for i in $prefix/lib/modules/*;do
		if [ ! -d $i ];then
			continue
		fi
		kinstname=${i##*/}
		if [ $kinstname = $kactive ];then
			kreboot=0
			break
		fi
	done
	if [ $kreboot = 1 ];then
		Echo "Kernel versions do not match rebooting in 5 sec..."
		REBOOT_IMAGE="yes"
		sleep 5
	fi
}
#======================================
# probeFileSystem
#--------------------------------------
function probeFileSystem {
	# /.../
	# probe for the filesystem type. The function will
	# read 128 kB of the given device and check the
	# filesystem header data to detect the type of the
	# filesystem
	# ----
	FSTYPE=unknown
	FSTYPE=$(blkid $1 -s TYPE -o value)
	case $FSTYPE in
		btrfs)       FSTYPE=btrfs ;;
		ext4)        FSTYPE=ext4 ;;
		ext3)        FSTYPE=ext3 ;;
		ext2)        FSTYPE=ext2 ;;
		reiserfs)    FSTYPE=reiserfs ;;
		squashfs)    FSTYPE=squashfs ;;
		luks)        FSTYPE=luks ;;
		crypto_LUKS) FSTYPE=luks ;;
		vfat)        FSTYPE=vfat ;;
		clicfs)      FSTYPE=clicfs ;;
		*)
			FSTYPE=unknown
		;;
	esac
	if [ $FSTYPE = "unknown" ];then
		dd if=$1 of=/tmp/filesystem-$$ bs=128k count=1 >/dev/null
	fi
	if [ $FSTYPE = "unknown" ];then
		if grep -q ^CLIC /tmp/filesystem-$$;then
			FSTYPE=clicfs
		fi
		if grep -q ^hsqs /tmp/filesystem-$$;then
			FSTYPE=squashfs
		fi
	fi
	export FSTYPE
}
#======================================
# getSystemIntegrity
#--------------------------------------
function getSystemIntegrity {
	# /.../
	# check the variable SYSTEM_INTEGRITY which contains
	# information about the status of all image portions
	# per partition. If a number is given as parameter only
	# the information from the image assigned to this partition
	# is returned
	# ----
	if [ -z "$SYSTEM_INTEGRITY" ];then
		echo "clean"
	else
		echo $SYSTEM_INTEGRITY | cut -f$1 -d:
	fi
}
#======================================
# getSystemMD5Status
#--------------------------------------
function getSystemMD5Status {
	# /.../
	# return the md5 status of the given image number.
	# the function works similar to getSystemIntegrity
	# ----
	echo $SYSTEM_MD5STATUS | cut -f$1 -d:
}
#======================================
# waitForUSBDeviceScan
#--------------------------------------
function waitForUSBDeviceScan {
	local devices=0
	if [ ! "$HAVE_USB" = "yes" ];then
		return
	fi
	if [ ! "$SCAN_USB" = "complete" ];then
		Echo -n "Waiting for USB device scan to complete..."
		while \
			[ $(dmesg|grep -c 'usb-storage: device scan complete') -lt 1 ] && \
			[ $devices -lt 15 ]
		do
			echo -n .
			sleep 1
			devices=$(( $devices + 1 ))
		done
		echo
		udevPending
		SCAN_USB=complete
	fi
}
#======================================
# probeUSB
#--------------------------------------
function probeUSB {
	local module=""
	local stdevs=""
	local hwicmd="/usr/sbin/hwinfo"
	export HAVE_USB="no"
	export SCAN_USB="not-started"
	udevPending
	if [ $HAVE_MODULES_ORDER = 0 ];then
		#======================================
		# load host controller modules
		#--------------------------------------
		IFS="%"
		for i in \
			`$hwicmd --usb | grep "Driver [IA]" | 
			sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
		do
			if echo $i | grep -q "#0";then
				module=`echo $i | cut -f2 -d"\"" | tr -d " "`
				module=`echo $module | sed -es"@modprobe@@g"`
				IFS=";"
				for m in $module;do
					if ! echo $stdevs | grep -q $m;then
						stdevs="$stdevs $m"
					fi
				done
			fi
		done
		for i in \
			`$hwicmd --usb-ctrl | grep "Driver [IA]" | 
			sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
		do
			if echo $i | grep -q "#0";then
				module=`echo $i | cut -f2 -d"\"" | tr -d " "`
				module=`echo $module | sed -es"@modprobe@@g"`
				IFS=";"
				for m in $module;do
					if ! echo $stdevs | grep -q $m;then
						stdevs="$stdevs $m"
					fi
				done
			fi
		done
		IFS=$IFS_ORIG
		stdevs=`echo $stdevs`
		for module in $stdevs;do
			Echo "Probing module: $module"
			modprobe $module >/dev/null
		done
		#======================================
		# check load status for host controller
		#--------------------------------------
		if [ -z "$stdevs" ];then
			return
		fi
		#======================================
		# manually load storage/input drivers
		#--------------------------------------
		for i in usbhid usb-storage;do
			modprobe $i &>/dev/null
		done
	fi
	stdevs=$(ls -1 /sys/bus/usb/devices/ | wc -l)
	if [ $stdevs -gt 0 ];then
		export HAVE_USB="yes"
	fi
	waitForUSBDeviceScan
}
#======================================
# probeDevices
#--------------------------------------
function probeDevices {
	local skipUSB=$1
	#======================================
	# probe USB devices and load modules
	#--------------------------------------
	if [ -z "$skipUSB" ];then
		probeUSB
	fi
	#======================================
	# probe Disk devices and load modules
	#--------------------------------------
	if [ $HAVE_MODULES_ORDER = 0 ];then
		Echo "Including required kernel modules..."
		IFS="%"
		local module=""
		local stdevs=""
		local hwicmd="/usr/sbin/hwinfo"
		for i in \
			`$hwicmd --storage | grep "Driver [IA]" | 
			sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
		do
			if echo $i | grep -q "#0";then
				module=`echo $i | cut -f2 -d"\"" | tr -d " "`
				module=`echo $module | sed -es"@modprobe@@g"`
				IFS=";"
				for m in $module;do
					if ! echo $stdevs | grep -q $m;then
						stdevs="$stdevs $m"
					fi
				done
			fi
		done
		IFS=$IFS_ORIG
		stdevs=`echo $stdevs`
		if [ ! -z "$kiwikernelmodule" ];then
			for module in $kiwikernelmodule;do
				Echo "Probing module (cmdline): $module"
				INITRD_MODULES="$INITRD_MODULES $module"
				modprobe $module >/dev/null
			done
		fi
		for module in $stdevs;do
			loadok=1
			for broken in $kiwibrokenmodule;do
				if [ $broken = $module ];then
					Echo "Prevent loading module: $module"
					loadok=0; break
				fi
			done
			if [ $loadok = 1 ];then
				Echo "Probing module: $module"
				INITRD_MODULES="$INITRD_MODULES $module"
				modprobe $module >/dev/null
			fi
		done
		hwinfo --block &>/dev/null
		# /.../
		# older systems require ide-disk to be present at any time
		# for details on this crappy call see bug: #250241
		# ----
		modprobe ide-disk &>/dev/null
	else
		if [ ! -z "$kiwikernelmodule" ];then
			for module in $kiwikernelmodule;do
				Echo "Probing module (cmdline): $module"
				INITRD_MODULES="$INITRD_MODULES $module"
				modprobe $module >/dev/null
			done
		fi
	fi
	#======================================
	# Manual loading of modules
	#--------------------------------------
	for i in rd brd edd dm-mod xennet xenblk virtio_blk;do
		modprobe $i &>/dev/null
	done
	udevPending
}
#======================================
# CDDevice
#--------------------------------------
function CDDevice {
	# /.../
	# detect CD/DVD/USB device(s). The function use the information
	# from hwinfo --cdrom to search for the block device
	# ----
	IFS=$IFS_ORIG
	local count=0
	local h=/usr/sbin/hwinfo
	if [ $HAVE_MODULES_ORDER = 0 ];then
		for module in sg sd_mod sr_mod cdrom ide-cd BusLogic vfat; do
			/sbin/modprobe $module
		done
	fi
	Echo -n "Waiting for CD/DVD device(s) to appear..."
	while true;do
		cddevs=`$h --cdrom | grep "Device File:"|sed -e"s@(.*)@@" | cut -f2 -d:`
		cddevs=`echo $cddevs`
		for i in $cddevs;do
			if [ -b $i ];then
				test -z $cddev && cddev=$i || cddev=$cddev:$i
			fi
		done
		if [ ! -z "$cddev" ] || [ $count -eq 12 ]; then
			break
		else
			echo -n .
			sleep 1
		fi
		count=`expr $count + 1`
	done
	echo
	if [ -z "$cddev" ];then
		USBStickDevice
		if [ $stickFound = 0 ];then
			systemException \
				"Failed to detect CD/DVD or USB drive !" \
			"reboot"
		fi
		cddev=$stickDevice
	fi
}
#======================================
# USBStickDevice
#--------------------------------------
function USBStickDevice {
	stickFound=0
	local mode=$1
	#======================================
	# search for USB removable devices
	#--------------------------------------
	waitForUSBDeviceScan
	for device in /sys/bus/usb/drivers/usb-storage/*;do
		if [ ! -L $device ];then
			continue
		fi
		descriptions=$device/host*/target*/*/block*
		for description in $descriptions;do
			if [ ! -d $description ];then
				continue
			fi
			isremovable=$description/removable
			storageID=`echo $description | cut -f1 -d: | xargs basename`
			devicebID=`basename $description | cut -f2 -d:`
			if [ $devicebID = "block" ];then
				devicebID=`ls -1 $description`
				isremovable=$description/$devicebID/removable
			fi
			serial="/sys/bus/usb/devices/$storageID/serial"
			device="/dev/$devicebID"
			if [ ! -b $device ];then
				continue;
			fi
			if [ ! -f $isremovable ];then
				continue;
			fi
			if ! partitionSize $device >/dev/null;then
				continue;
			fi
			if [ ! -f $serial ];then
				serial="USB Stick (unknown type)"
			else
				serial=`cat $serial`
			fi
			removable=`cat $isremovable`
			# /.../
			# don't check the removable flag, it could be wrong
			# especially for USB hard disks connected via a
			# USB caddy, details in bug: 535113
			# ----
			removable=1
			if [ $removable -eq 1 ];then
				stickRoot=$device
				stickDevice=$(ddn $device 1)
				for devnr in 1 2;do
					dev=$(ddn $stickRoot $devnr)
					if ! kiwiMount "$dev" "/mnt" "-o ro";then
						continue
					fi
					if [ "$mode" = "install" ];then
						# /.../
						# USB stick search for install media
						# created with kiwi
						# ----
						if \
							[ ! -e /mnt/config.isoclient ] && \
							[ ! -e /mnt/config.usbclient ]
						then
							umountSystem
							continue
						fi
					elif [ "$mode" = "kexec" ];then
						# /.../
						# USB stick search for hotfix media
						# with kernel/initrd for later kexec
						# ----
						if \
							[ ! -e /mnt/linux.kexec ] && \
							[ ! -e /mnt/initrd.kexec ]
						then
							umountSystem
							continue
						fi
					else
						# /.../
						# USB stick search for Linux system tree
						# created with kiwi
						# ----
						if [ ! -e /mnt/etc/ImageVersion ]; then
							umountSystem
							continue
						fi
					fi
					stickFound=1
					umountSystem
					break
				done
				if [ "$stickFound" = 0 ];then
					continue
				fi
				stickSerial=$serial
				return
			fi
		done
	done
}
#======================================
# CDMountOption
#--------------------------------------
function CDMountOption {
	# /.../
	# checks for the ISO 9660 extension and prints the
	# mount option required to mount the device in full
	# speed mode
	# ----
	local id=$(blkid -o value -s TYPE $1)
	if [ "$id" = "iso9660" ];then
		echo "-t iso9660"
	fi
}
#======================================
# CDMount
#--------------------------------------
function CDMount {
	# /.../
	# search all CD/DVD drives and use the one we can find
	# the CD configuration on
	# ----
	local count=0
	local ecode=0
	local cdopt
	mkdir -p /cdrom
	if [ -f /.profile ];then
		importFile < /.profile
	fi
	#======================================
	# check for hybrid mbr ID
	#--------------------------------------
	if [ -z "$cdinst" ];then
		searchBIOSBootDevice
		ecode=$?
		if [ ! $ecode = 0 ];then
			if [ $ecode = 2 ];then
				systemException "$biosBootDevice" "reboot"
			fi
			unset kiwi_hybrid
		fi
	else
		unset kiwi_hybrid
	fi
	#======================================
	# walk through media
	#--------------------------------------
	if [ -z "$kiwi_hybrid" ];then
		#======================================
		# search for CD/DVD devices
		#--------------------------------------
		CDDevice
		Echo -n "Mounting live boot drive..."
		while true;do
			IFS=":"; for i in $cddev;do
				cdopt=$(CDMountOption $i)
				if [ -x /usr/bin/driveready ];then
					driveready $i&& eval mount $cdopt -o ro $i /cdrom >/dev/null
				else
					eval mount $cdopt -o ro $i /cdrom >/dev/null
				fi
				if [ -f $LIVECD_CONFIG ];then
					cddev=$i; echo
					#======================================
					# run mediacheck if requested and boot
					#--------------------------------------
					if [ "$mediacheck" = 1 ]; then
						test -e /proc/splash && echo verbose > /proc/splash
						checkmedia $cddev
						Echo -n "Press ENTER for reboot: "; read nope
						/sbin/reboot -f -i >/dev/null
					fi
					#======================================
					# device found go with it
					#--------------------------------------
					IFS=$IFS_ORIG
					return
				fi
				umount $i &>/dev/null
			done
			IFS=$IFS_ORIG
			if [ $count -eq 12 ]; then
				break
			else
				echo -n .
				sleep 1
			fi
			count=`expr $count + 1`
		done
	else
		#======================================
		# search for hybrid device
		#--------------------------------------
		# /.../
		# we have to use fdisk here for partition manipulation
		# because parted doesn't accept the partition table written
		# by the isohybrid tool :(
		# ----
		PARTITIONER=sfdisk
		if [ "x$kiwi_hybridpersistent" = "xyes" ]; then
			createHybridPersistent $biosBootDevice
		fi
		Echo -n "Mounting hybrid live boot drive..."
		cddev=$(ddn $biosBootDevice 1)
		kiwiMount "$cddev" "/cdrom" "-o ro"
		if [ -f $LIVECD_CONFIG ];then
			echo
			#======================================
			# run mediacheck if requested and boot
			#--------------------------------------
			if [ "$mediacheck" = 1 ]; then
				test -e /proc/splash && echo verbose > /proc/splash
				checkmedia $cddev
				Echo -n "Press ENTER for reboot: "; read nope
				/sbin/reboot -f -i >/dev/null
			fi
			#======================================
			# search hybrid for a write partition
			#--------------------------------------
			for disknr in 2 3 4;do
				id=`partitionID $biosBootDevice $disknr`
				if [ "$id" = "83" ];then
					export HYBRID_RW=$biosBootDevice$disknr
					break
				fi
			done
			#======================================
			# device found go with it
			#--------------------------------------
			PARTITIONER=parted
			return
		fi
		PARTITIONER=parted
		umount $cddev &>/dev/null
	fi
	echo
	systemException \
		"Couldn't find Live image configuration file" \
	"reboot"
}
#======================================
# CDUmount
#--------------------------------------
function CDUmount {
	# /.../
	# umount the CD device
	# ----
	umount $cddev
}
#======================================
# CDEject
#--------------------------------------
function CDEject {
	eject $cddev
}
#======================================
# searchBIOSBootDevice
#--------------------------------------
function searchBIOSBootDevice {
	# /.../
	# search for the BIOS boot device which is the device
	# with the BIOS id 0x80. The test may fail if the boot
	# device is a CD/DVD drive. If the test fails we search
	# for the MBR disk label and compare it with the kiwi
	# written mbrid file in /boot/grub/ of the system image
	# ----
	IFS=$IFS_ORIG
	local h=/usr/sbin/hwinfo
	local c="Device File:|BIOS id"
	local ddevs=`$h --disk|grep -E "$c"|sed -e"s@(.*)@@"|cut -f2 -d:|tr -d " "`
	local cmpd=/tmp/mbrids
	local ifix=0
	local matched
	local bios
	local file
	local pred
	#======================================
	# Store device with BIOS id 0x80
	#--------------------------------------
	for curd in $ddevs;do
		if [ $curd = "0x80" ];then
			bios=$pred; break
		fi
		pred=$curd
	done
	#======================================
	# Check for OEM ISO installation mode
	#--------------------------------------
	if [ ! -z "$cdinst" ];then
		CDMount
		umount $cddev
		curd=$cddev
		export biosBootDevice=$curd
		return 0
	fi
	#======================================
	# Search and copy all mbrid files 
	#--------------------------------------
	mkdir -p $cmpd
	for curd in $ddevs;do
		if [ ! $(echo $curd | cut -c 1) = "/" ];then
			continue
		fi
		for id in 1 2 3;do
			dev=$(ddn $curd $id)
			if ! mount -o ro $dev /mnt;then
				continue
			fi
			if [ -f /mnt/boot/grub/mbrid ];then
				cp -a /mnt/boot/grub/mbrid $cmpd/mbrid$ifix
				ifix=$(expr $ifix + 1)
				umount /mnt
				break
			fi
			umount /mnt
		done
	done
	#======================================
	# Read mbrid from the newest mbrid file 
	#--------------------------------------
	file=$(ls -1t $cmpd 2>/dev/null | head -n 1)
	if [ -z "$file" ];then
		export biosBootDevice="Failed to find MBR identifier !"
		return 1
	fi
	read mbrI < $cmpd/$file
	#======================================
	# Compare ID with MBR entry 
	#--------------------------------------
	ifix=0
	for curd in $ddevs;do
		if [ ! -b $curd ];then
			continue
		fi
		mbrML=`dd if=$curd bs=1 count=4 skip=$((0x1b8))|hexdump -n4 -e '"0x%x"'`
		mbrMB=`echo $mbrML | sed 's/^0x\(..\)\(..\)\(..\)\(..\)$/0x\4\3\2\1/'`
		if [ "$mbrML" = "$mbrI" ] || [ "$mbrMB" = "$mbrI" ];then
			ifix=1
			matched=$curd
			if [ "$curd" = "$bios" ];then
				export biosBootDevice=$curd
				return 0
			fi
		fi
	done
	if [ $ifix -eq 1 ];then
		export biosBootDevice=$matched
		return 0
	fi
	export biosBootDevice="No devices matches MBR identifier: $mbrI !"
	return 2
}
#======================================
# searchVolumeGroup
#--------------------------------------
function searchVolumeGroup {
	# /.../
	# search for a volume group named $VGROUP and if it can be
	# found activate it while creating appropriate device nodes:
	# /dev/$VGROUP/LVRoot and/or /dev/$VGROUP/LVComp
	# return zero on success
	# ----
	if vgscan 2>&1 | grep -q "$VGROUP"; then
		vgchange -a y $VGROUP
		return $?
	fi
	return 1
}
#======================================
# searchSwapSpace
#--------------------------------------
function searchSwapSpace {
	# /.../
	# search for a type=82 swap partition
	# ----
	if [ ! -z $kiwinoswapsearch ];then
		return
	fi
	local hwapp=/usr/sbin/hwinfo
	local diskdevs=`$hwapp --disk | grep "Device File:" | cut -f2 -d:`
	diskdevs=`echo $diskdevs | sed -e "s@(.*)@@"`
	for diskdev in $diskdevs;do
		for disknr in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15;do
			id=`partitionID $diskdev $disknr`
			if [ "$id" = "82" ];then
				echo $diskdev$disknr
				return
			fi
		done
	done
}
#======================================
# searchDiskSpace
#--------------------------------------
function searchDiskSpace {
	# /.../
	# search for a free non swap partition
	# ----
	if [ ! -z $kiwinoswapsearch ];then
		return
	fi
	local hwapp=/usr/sbin/hwinfo
	local diskdevs=`$hwapp --disk | grep "Device File:" | cut -f2 -d:`
	diskdevs=`echo $diskdevs | sed -e "s@(.*)@@"`
	for diskdev in $diskdevs;do
		for disknr in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15;do
			id=`partitionID $diskdev $disknr`
			if [ -z $id ];then
				id=0
			fi
			if [ "$id" -ne 82 ] && [ "$id" -ne 0 ];then
				echo $diskdev$disknr
				return
			fi
		done
	done
}
#======================================
# updateMTAB
#--------------------------------------
function updateMTAB {
	prefix=$1
	umount=0
	if [ ! -e /proc/mounts ];then
		mount -t proc proc /proc
		umount=1
	fi
	cat /proc/mounts > $prefix/etc/mtab
	if [ $umount -eq 1 ];then
		umount /proc
	fi
}
#======================================
# probeNetworkCard
#--------------------------------------
function probeNetworkCard {
	# /.../
	# use hwinfo to probe for all network devices. The
	# function will check for the driver which is needed
	# to support the card and returns the information in
	# the networkModule variable
	# ----
	IFS="%"
	local module=""
	local hwicmd="/usr/sbin/hwinfo"
	for i in \
		`$hwicmd --netcard | grep "Driver [IA]" | 
		sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
	do
		if echo $i | grep -q "#0";then
			module=`echo $i | cut -f2 -d"\"" | tr -d " "`
			module=`echo $module | sed -es"@modprobe@@g"`
			IFS=";"
			for m in $module;do
				if ! echo $networkModule | grep -q $m;then
					if [ ! -z "$networkModule" ];then
						networkModule="$networkModule:$m"
					else
						networkModule=$m
					fi
				fi
			done
		fi
	done
	IFS=$IFS_ORIG
	networkModule=`echo $networkModule`
}
#======================================
# setupNetwork
#--------------------------------------
function setupNetwork {
	# /.../
	# probe for the existing network interface names and
	# hardware addresses. Match the BOOTIF address from PXE
	# to the correct linux interface name. Setup the network
	# interface using a dhcp request. On success the dhcp
	# info file is imported into the current shell environment
	# and the nameserver information is written to
	# /etc/resolv.conf
	# ----
	IFS="
	"
	local MAC=0
	local DEV=0
	local mac_list=0
	local dev_list=0
	local index=0
	local hwicmd=/usr/sbin/hwinfo
	local iface=eth0
	local opts="--noipv4ll -p"
	for i in `$hwicmd --netcard`;do
		IFS=$IFS_ORIG
		if echo $i | grep -q "HW Address:";then
			MAC=`echo $i | sed -e s"@HW Address:@@"`
			MAC=`echo $MAC`
			mac_list[$index]=$MAC
			index=`expr $index + 1`
		fi
		if echo $i | grep -q "Device File:";then
			DEV=`echo $i | sed -e s"@Device File:@@"`
			DEV=`echo $DEV`
			dev_list[$index]=$DEV
		fi
	done
	if [ -z $BOOTIF ];then
		# /.../
		# there is no PXE boot interface information. We will use
		# the first detected interface as fallback solution
		# ----
		iface=${dev_list[0]}
	else
		# /.../
		# evaluate the information from the PXE boot interface
		# if we found the MAC in the list the appropriate interface
		# name is assigned. if not eth0 is used as fallback
		# ----
		index=0
		BOOTIF=`echo $BOOTIF | cut -f2- -d - | tr "-" ":"`
		for i in ${mac_list[*]};do
			if [ $i = $BOOTIF ];then
				iface=${dev_list[$index]}
			fi
			index=`expr $index + 1`
		done
	fi
	export PXE_IFACE=$iface
	if [ $DHCPCD_HAVE_PERSIST -eq 0 ];then
		# /.../
		# older version of dhcpd which doesn't have the
		# options we want to pass
		# ----
		unset opts
	fi
	if ! dhcpcd $opts $PXE_IFACE 1>&2;then
		systemException \
			"Failed to setup DHCP network interface !" \
		"reboot"
	fi
	for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;do
		if [ -s /var/lib/dhcpcd/dhcpcd-$PXE_IFACE.info ];then
			break
		fi
		sleep 2
	done
	if [ ! -s /var/lib/dhcpcd/dhcpcd-$PXE_IFACE.info ];then
		systemException \
			"Can't find DHCP info file: dhcpcd-$PXE_IFACE.info !" \
		"reboot"
	fi
	ifconfig lo 127.0.0.1 netmask 255.0.0.0 up
	importFile < /var/lib/dhcpcd/dhcpcd-$PXE_IFACE.info
	if [ -z "$DOMAIN" ] && [ -n "$DNSDOMAIN" ];then
		export DOMAIN=$DNSDOMAIN
	fi
	echo "search $DOMAIN" > /etc/resolv.conf
	if [ -z "$DNS" ] && [ -n "$DNSSERVERS" ];then
		export DNS=$DNSSERVERS
	fi
	IFS="," ; for i in $DNS;do
		echo "nameserver $i" >> /etc/resolv.conf
	done
	DHCPCHADDR=`echo $DHCPCHADDR | tr a-z A-Z`
}
#======================================
# updateNeeded
#--------------------------------------
function updateNeeded {
	# /.../
	# check the contents of the IMAGE key and compare the
	# image version file as well as the md5 sum of the installed
	# and the available image on the tftp server
	# ----
	SYSTEM_INTEGRITY=""
	SYSTEM_MD5STATUS=""
	local count=0
	IFS="," ; for i in $IMAGE;do
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3 ;;
			3) imageServer=$n ; field=4 ;;
			4) imageBlkSize=$n; field=5 ;;
			5) imageZipped=$n ;
		esac
		done
		atversion="$imageName-$imageVersion"
		versionFile="/mnt/etc/ImageVersion-$atversion"
		IFS=" "
		if [ -f "$versionFile" ];then
			read installed sum2 < $versionFile
		fi
		imageMD5s="image/$imageName-$imageVersion.md5"
		[ -z "$imageServer" ]  && imageServer=$SERVER
		[ -z "$imageBlkSize" ] && imageBlkSize=8192
		if [ ! -f /etc/image.md5 ];then
			fetchFile $imageMD5s /etc/image.md5 uncomp $imageServer
		fi
		read sum1 blocks blocksize zblocks zblocksize < /etc/image.md5
		if [ ! -z "$sum1" ];then
			SYSTEM_MD5STATUS="$SYSTEM_MD5STATUS:$sum1"
		else
			SYSTEM_MD5STATUS="$SYSTEM_MD5STATUS:none"
		fi
		if [ ! -z "$1" ];then
			continue
		fi
		if test "$count" = 1;then
		if test "$SYSTEM_INTEGRITY" = ":clean";then
			Echo "Main OS image update needed"
			Echo -b "Forcing download for multi image session"
			RELOAD_IMAGE="yes"
		fi
		fi
		count=$(($count + 1))
		Echo "Checking update status for image: $imageName"
		if test ! -z $RELOAD_IMAGE;then
			Echo -b "Update forced via RELOAD_IMAGE..."
			Echo -b "Update status: Clean"
			SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
			continue
		fi
		if test ! -f $versionFile;then
			Echo -b "Update forced: /etc/ImageVersion-$atversion not found"
			Echo -b "Update status: Clean"
			SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
			RELOAD_IMAGE="yes"
			continue
		fi
		Echo -b "Current: $atversion Installed: $installed"
		if test "$atversion" = "$installed";then
			if test "$sum1" = "$sum2";then
				Echo -b "Update status: Fine"
				SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:fine"
				continue
			fi
			Echo -b "Image Update for image [ $imageName ] needed"
			Echo -b "Image version equals but md5 checksum failed"
			Echo -b "This means the contents of the new image differ"
			RELOAD_IMAGE="yes"
		else
			Echo -b "Image Update for image [ $imageName ] needed"
			Echo -b "Name and/or image version differ"
			RELOAD_IMAGE="yes"
		fi
		Echo -b "Update status: Clean"
		SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
	done
	SYSTEM_INTEGRITY=`echo $SYSTEM_INTEGRITY | cut -f2- -d:`
	SYSTEM_MD5STATUS=`echo $SYSTEM_MD5STATUS | cut -f2- -d:`
}
#======================================
# cleanSweep
#--------------------------------------
function cleanSweep {
	# /.../
	# zero out a the given disk device
	# ----
	local diskDevice=$1
	dd if=/dev/zero of=$diskDevice bs=32M >/dev/null
}
#======================================
# sfdiskGetPartitionID
#--------------------------------------
function sfdiskGetPartitionID {
	# /.../
	# prints the partition ID for the given device and number
	# ----
	sfdisk -c $1 $2
}
#======================================
# partedGetPartitionID
#--------------------------------------
function partedGetPartitionID {
	# /.../
	# prints the partition ID for the given device and number
	# ----
	parted -m -s $1 print | grep ^$2: | cut -f2 -d, |\
		cut -f2 -d= | tr -d ";" | tr -d 0
}
#======================================
# partitionID
#--------------------------------------
function partitionID {
	local diskDevice=$1
	local diskNumber=$2
	if [ $PARTITIONER = "sfdisk" ];then
		sfdiskGetPartitionID $diskDevice $diskNumber
	else
		partedGetPartitionID $diskDevice $diskNumber
	fi
}
#======================================
# partitionSize
#--------------------------------------
function partitionSize {
	local diskDevice=$1
	if [ -z "$diskDevice" ] || [ ! -e "$diskDevice" ];then
		return 1
	fi
	expr $(blockdev --getsize64 $diskDevice) / 1024
}
#======================================
# linuxPartition
#--------------------------------------
function linuxPartition {
	# /.../
	# check for a linux partition on partition number 2
	# using the given disk device. On success return 0
	# ----
	local diskDevice=$1
	local diskPartitionType=`partitionID $diskDevice 2`
	if test "$diskPartitionType" = "83";then
		return 0
	fi
	return 1
}
#======================================
# kernelList
#--------------------------------------
function kernelList {
	# /.../
	# check for all installed kernels whether there are valid
	# links to the initrd and kernel files. The function will
	# save the valid linknames in the variable KERNEL_LIST
	# ----
	local prefix=$1
	local kcount=1
	local kname=""
	local kernel=""
	local initrd=""
	local kpair=""
	local krunning=`uname -r`
	KERNEL_LIST=""
	KERNEL_NAME=""
	KERNEL_PAIR=""
	#======================================
	# search running kernel first
	#--------------------------------------
	if [ -d $prefix/lib/modules/$krunning ];then
		for name in vmlinux vmlinuz image;do
			if [ -f $prefix/boot/$name-$krunning ];then
				kernel=$name-$krunning
				initrd=initrd-$krunning
				break
			fi
		done
		if [ ! -z "$kernel" ];then
			KERNEL_PAIR=$kernel:$initrd
			KERNEL_NAME[$kcount]=$krunning
			KERNEL_LIST=$KERNEL_PAIR
		fi
	fi
	#======================================
	# search for other kernels
	#--------------------------------------
	if [ ! -z "$KERNEL_LIST" ];then
		for i in $prefix/lib/modules/*;do
			if [ ! -d $i ];then
				continue
			fi
			unset kernel
			unset initrd
			kname=`basename $i`
			if [ "$kname" = $krunning ];then
				continue
			fi
			for name in vmlinux vmlinuz image;do
			for k in $prefix/boot/$name-${i##*/}; do
				if [ -f $k ];then
					kernel=${k##*/}
					initrd=initrd-${i##*/}
					break 2
				fi
			done
			done
			if [ -z "$kernel" ];then
				continue
			fi
			kcount=$((kcount+1))
			kpair=$kernel:$initrd
			KERNEL_NAME[$kcount]=$kname
			KERNEL_LIST=$KERNEL_LIST,$kpair
		done
	fi
	if [ -z "$KERNEL_LIST" ];then
		# /.../
		# the system image doesn't provide the kernel and initrd but
		# if there is a downloaded kernel and initrd from the KIWI_INITRD
		# setup. the kernelList function won't find initrds that gets
		# downloaded over tftp so make sure the vmlinu[zx]/initrd combo
		# gets added
		# ----
		if [ -e $prefix/boot/vmlinuz ];then
			KERNEL_LIST="vmlinuz:initrd"
			KERNEL_NAME[1]=vmlinuz
		fi
		if [ -e $prefix/boot/vmlinux ];then
			KERNEL_LIST="vmlinux:initrd"
			KERNEL_NAME[1]=vmlinux
		fi
	fi
	export KERNEL_LIST
	export KERNEL_NAME
	export KERNEL_PAIR
}
#======================================
# validateSize
#--------------------------------------
function validateSize {
	# /.../
	# check if the image fits into the requested partition.
	# An information about the sizes is printed out
	# ----
	haveBytes=`partitionSize $imageDevice`
	haveBytes=`expr $haveBytes \* 1024`
	haveMByte=`expr $haveBytes / 1048576`
	needBytes=`expr $blocks \* $blocksize`
	needMByte=`expr $needBytes / 1048576`
	Echo "Have size: $imageDevice -> $haveBytes Bytes [ $haveMByte MB ]"
	Echo "Need size: $needBytes Bytes [ $needMByte MB ]"
	if test $haveBytes -gt $needBytes;then
		return 0
	fi
	return 1
}
#======================================
# validateTarSize
#--------------------------------------
function validateTarSize {
	# /.../
	# this function requires a destination directory which
	# could be a tmpfs mount and a compressed tar source file.
	# The function will then check if the tar file could be
	# unpacked according to the size of the destination
	# ----
	local tsrc=$1
	local haveKByte=0
	local haveMByte=0
	local needBytes=0
	local needMByte=0
	haveKByte=`cat /proc/meminfo | grep MemFree | cut -f2 -d: | cut -f1 -dk`
	haveMByte=`expr $haveKByte / 1024`
	needBytes=`du --bytes $tsrc | cut -f1`
	needMByte=`expr $needBytes / 1048576`
	Echo "Have size: proc/meminfo -> $haveMByte MB"
	Echo "Need size: $tsrc -> $needMByte MB [ uncompressed ]"
	if [ "$haveMByte" -gt "$needMByte" ];then
		return 0
	fi
	return 1
}
#======================================
# validateBlockSize
#--------------------------------------
function validateBlockSize {
	# /.../
	# check the block size value. atftp limits to a maximum of
	# 65535 blocks, so the block size must be checked according
	# to the size of the image. The block size itself is also
	# limited to 65464 bytes
	# ----
	if [ -z "$zblocks" ] && [ -z "$blocks" ];then
		# md5 file not yet read in... skip
		return
	fi
	if [ ! -z "$zblocks" ];then
		isize=`expr $zblocks \* $zblocksize`
	else
		isize=`expr $blocks \* $blocksize`
	fi
	isize=`expr $isize / 65535`
	if [ $isize -gt $imageBlkSize ];then
		imageBlkSize=`expr $isize + 1024`
	fi
	if [ $imageBlkSize -gt 65464 ];then
		systemException \
			"Maximum blocksize for atftp protocol exceeded" \
		"reboot"
	fi
}
#======================================
# loadOK
#--------------------------------------
function loadOK {
	# /.../
	# check the output of the atftp command, unfortunately
	# there is no useful return code to check so we have to
	# check the output of the command
	# ----
	for i in "File not found" "aborting" "no option named" "unknown host" ; do
		if echo "$1" | grep -q  "$i" ; then
			return 1
		fi
	done
	return 0
}
#======================================
# includeKernelParameters
#--------------------------------------
function includeKernelParameters {
	# /.../
	# include the parameters from /proc/cmdline into
	# the current shell environment
	# ----
	IFS=$IFS_ORIG
	for i in `cat /proc/cmdline`;do
		if ! echo $i | grep -q "=";then
			continue
		fi
		kernelKey=`echo $i | cut -f1 -d=`
		kernelVal=`echo $i | cut -f2 -d=`
		eval export $kernelKey=$kernelVal
	done
	if [ ! -z "$kiwikernelmodule" ];then
		kiwikernelmodule=`echo $kiwikernelmodule | tr , " "`
	fi
	if [ ! -z "$kiwibrokenmodule" ];then
		kiwibrokenmodule=`echo $kiwibrokenmodule | tr , " "`
	fi
	if [ ! -z "$kiwistderr" ];then
		export ELOG_CONSOLE=$kiwistderr
		export ELOG_EXCEPTION=$kiwistderr
	fi
	if [ ! -z "$ramdisk_size" ];then
		local modfile=/etc/modprobe.conf.local
		if [ -f $modfile ];then
			sed -i -e s"@rd_size=.*@rd_size=$ramdisk_size@" $modfile
		fi
	fi
	if [ ! -z "$lang" ];then
		export DIALOG_LANG=$lang
	fi
}
#======================================
# checkServer
#--------------------------------------
function checkServer {
	# /.../
	# check the kernel commandline parameter kiwiserver.
	# If it exists its contents will be used as
	# server address stored in the SERVER variabe
	# ----
	if [ ! -z $kiwiserver ];then
		Echo "Found server in kernel cmdline"
		SERVER=$kiwiserver
	fi
	if [ ! -z $kiwiservertype ]; then
		Echo "Found server type in kernel cmdline"
		SERVERTYPE=$kiwiservertype
	else
		SERVERTYPE=tftp
	fi
}
#======================================
# umountSystem
#--------------------------------------
function umountSystem {
	local retval=0
	local OLDIFS=$IFS
	local mountList="/mnt /read-only /read-write"
	IFS=$IFS_ORIG
	#======================================
	# umount boot device
	#--------------------------------------
	if [ ! -z "$imageBootDevice" ];then
		umount $imageBootDevice 1>&2
	fi
	#======================================
	# umount mounted mountList paths
	#--------------------------------------
	for mpath in $(cat /proc/mounts | cut -f2 -d " ");do
		for umount in $mountList;do
			if [ "$mpath" = "$umount" ];then
				if ! umount $mpath >/dev/null;then
				if ! umount -l $mpath >/dev/null;then
					retval=1
				fi
				fi
			fi
		done
	done
	#======================================
	# remove mount points
	#--------------------------------------
	for dir in "/read-only" "/read-write" "/xino";do
		test -d $dir && rmdir $dir 1>&2
	done
	IFS=$OLDIFS
	return $retval
}
#======================================
# isFSTypeReadOnly
#--------------------------------------
function isFSTypeReadOnly {
	if [ "$FSTYPE" = "squashfs" ];then
		export unionFST=aufs
		return 0
	fi
	if [ "$FSTYPE" = "clicfs" ];then
		export unionFST=clicfs
		return 0
	fi
	return 1
}
#======================================
# kiwiMount
#--------------------------------------
function kiwiMount {
	local src=$1
	local dst=$2
	local opt=$3
	local lop=$4
	#======================================
	# load not autoloadable fs modules
	#--------------------------------------
	modprobe squashfs &>/dev/null
	#======================================
	# store old FSTYPE value
	#--------------------------------------
	if [ ! -z "$FSTYPE" ];then
		FSTYPE_SAVE=$FSTYPE
	fi
	#======================================
	# probe filesystem
	#--------------------------------------
	if [ ! "$FSTYPE" = "nfs" ];then
		if [ ! -z "$lop" ];then
			probeFileSystem $lop
		else
			probeFileSystem $src
		fi
	fi
	if [ -z "$FSTYPE" ] || [ "$FSTYPE" = "unknown" ];then
		FSTYPE="auto"
	fi
	#======================================
	# decide for a mount method
	#--------------------------------------
	if [ ! -z "$lop" ];then
		# /.../
		# if loop mount is requested a fixed loop1 device
		# was set as src parameter. Because this fixed loop
		# name is used later too we stick to this device name
		# ----
		losetup /dev/loop1 $lop
	fi
	if ! mount -t $FSTYPE $opt $src $dst >/dev/null;then
		return 1
	fi
	if [ ! -z "$FSTYPE_SAVE" ];then
		FSTYPE=$FSTYPE_SAVE
	fi
	return 0
}
#======================================
# setupReadWrite
#--------------------------------------
function setupReadWrite {
	# /.../
	# check/create read-write filesystem used for
	# overlay data
	# ----
	local rwDir=/read-write
	local rwDevice=`echo $UNIONFS_CONFIG | cut -d , -f 1`
	mkdir -p $rwDir
	if [ $LOCAL_BOOT = "no" ] && [ $systemIntegrity = "clean" ];then
		if [ "$RELOAD_IMAGE" = "yes" ] || \
			! mount -o ro $rwDevice $rwDir >/dev/null
		then
			#======================================
			# store old FSTYPE value
			#--------------------------------------
			if [ ! -z "$FSTYPE" ];then
				FSTYPE_SAVE=$FSTYPE
			fi
			#======================================
			# probe filesystem
			#--------------------------------------
			probeFileSystem $rwDevice
			if [ ! "$FSTYPE" = "unknown" ];then
				Echo "Checking filesystem for RW data on $rwDevice..."
				e2fsck -p $rwDevice
			fi
			#======================================
			# restore FSTYPE
			#--------------------------------------
			if [ ! -z "$FSTYPE_SAVE" ];then
				FSTYPE=$FSTYPE_SAVE
			fi
			if [ "$RELOAD_IMAGE" = "yes" ] || \
				! mount -o ro $rwDevice $rwDir >/dev/null
			then
				Echo "Creating filesystem for RW data on $rwDevice..."
				if ! mke2fs -F -T ext3 -j $rwDevice >/dev/null;then
					Echo "Failed to create ext3 filesystem"
					return 1
				fi
				e2fsck -p $rwDevice >/dev/null
			fi
		else
			umount $rwDevice
		fi
	fi
	return 0
}
#======================================
# mountSystemUnified
#--------------------------------------
function mountSystemUnified {
	local loopf=$1
	local roDir=/read-only
	local roDevice=`echo $UNIONFS_CONFIG | cut -d , -f 2`
	#======================================
	# create read only mount point
	#--------------------------------------
	mkdir -p $roDir
	#======================================
	# check read/only device location
	#--------------------------------------
	if [ ! -z "$NFSROOT" ];then
		roDevice="$imageRootDevice"
	fi
	#======================================
	# mount read only device
	#--------------------------------------
	if ! kiwiMount "$roDevice" "$roDir" "" $loopf;then
		Echo "Failed to mount read only filesystem"
		return 1
	fi
	#======================================
	# check union mount method
	#--------------------------------------
	mountSystemOverlay
}
#======================================
# mountSystemClicFS
#--------------------------------------
function mountSystemClicFS {
	local loopf=$1
	local roDir=/read-only
	local rwDevice=`echo $UNIONFS_CONFIG | cut -d , -f 1`
	local roDevice=`echo $UNIONFS_CONFIG | cut -d , -f 2`
	local clic_cmd=clicfs
	local resetReadWrite=0
	local haveBytes
	local haveKByte
	local haveMByte
	local wantCowFS
	local size
	#======================================
	# load fuse module
	#--------------------------------------
	modprobe fuse &>/dev/null
	#======================================
	# create read only mount point
	#--------------------------------------
	mkdir -p $roDir
	#======================================
	# check read/only device location
	#--------------------------------------
	if [ ! -z "$NFSROOT" ];then
		roDevice="$imageRootDevice"
	fi
	#======================================  
	# check kernel command line for log file  
	#--------------------------------------  
	if [ -n "$cliclog" ]; then  
		clic_cmd="$clic_cmd -l $cliclog"  
	fi  
	#======================================
	# check read/write device location
	#--------------------------------------
	getDiskDevice $rwDevice | grep -q ram
	if [ $? = 0 ];then
		haveKByte=`cat /proc/meminfo | grep MemFree | cut -f2 -d:| cut -f1 -dk`
		haveMByte=`expr $haveKByte / 1024`
		haveMByte=`expr $haveMByte \* 7 / 10`
		clic_cmd="$clic_cmd -m $haveMByte"
	else
		haveBytes=`blockdev --getsize64 $rwDevice`
		haveMByte=`expr $haveBytes / 1024 / 1024`
		wantCowFS=0
		if \
			[ "x$kiwi_hybrid" = "xyes" ] &&
			[ "x$kiwi_hybridpersistent" = "xyes" ]
		then
			# write into a cow file on a filesystem, for hybrid iso's
			wantCowFS=1
		fi
		if [ $wantCowFS = 1 ];then
			# write into a cow file on a filesystem
			mkdir -p $HYBRID_PERSISTENT_DIR
			if [ $LOCAL_BOOT = "no" ] && [ $systemIntegrity = "clean" ];then
				resetReadWrite=1
			elif ! mount $rwDevice $HYBRID_PERSISTENT_DIR;then
				resetReadWrite=1
			fi
			if [ $resetReadWrite = 1 ];then
				if ! setupReadWrite; then
					Echo "Failed to setup read-write filesystem"
					return 1
				fi
				if ! mount $rwDevice $HYBRID_PERSISTENT_DIR;then
					Echo "Failed to mount read/write filesystem"
					return 1
				fi
			fi
			clic_cmd="$clic_cmd -m $haveMByte"
			clic_cmd="$clic_cmd -c $HYBRID_PERSISTENT_DIR/.clicfs_COW"
			clic_cmd="$clic_cmd --ignore-cow-errors"
		else
			# write into a device directly
			clic_cmd="$clic_cmd -m $haveMByte -c $rwDevice --ignore-cow-errors"
		fi
	fi
	#======================================
	# mount/check clic file
	#--------------------------------------
	if [ ! -z "$NFSROOT" ];then
		#======================================
		# clic exported via NFS
		#--------------------------------------
		if ! kiwiMount "$roDevice" "$roDir" "" $loopf;then
			Echo "Failed to mount NFS filesystem"
			return 1
		fi
		if [ ! -e "$roDir/fsdata.ext3" ];then
			Echo "Can't find clic fsdata.ext3 in NFS export"
			return 1
		fi
	else
		#======================================
		# mount clic container
		#--------------------------------------
		if [ -z "$loopf" ];then
			loopf=$roDevice
		fi
		if ! $clic_cmd $loopf $roDir; then  
			Echo "Failed to mount clic filesystem"
			return 1
		fi 
	fi
	#======================================
	# mount root over clic
	#--------------------------------------
	size=`stat -c %s $roDir/fsdata.ext3`
	size=$((size/512))
	# we don't want reserved blocks...
	tune2fs -m 0 $roDir/fsdata.ext3 >/dev/null
	# we don't want automatic filesystem check...
	tune2fs -i 0 $roDir/fsdata.ext3 >/dev/null
	if [ ! $LOCAL_BOOT = "no" ];then
		e2fsck -p $roDir/fsdata.ext3
	fi
	if [ $LOCAL_BOOT = "no" ];then
		resize2fs $roDir/fsdata.ext3 $size"s"
	fi
	mount -o loop,noatime,nodiratime,errors=remount-ro,barrier=0 \
		$roDir/fsdata.ext3 /mnt
	if [ ! $? = 0 ];then
		Echo "Failed to mount ext3 clic container"
		return 1
	fi
	export haveClicFS=yes
	return 0
}
#======================================
# mountSystemOverlay
#--------------------------------------
function mountSystemOverlay {
	local roDir=/read-only
	local rwDir=/read-write
	local xiDir=/xino
	local rwDevice=`echo $UNIONFS_CONFIG | cut -d , -f 1`
	local roDevice=`echo $UNIONFS_CONFIG | cut -d , -f 2`
	local unionFST=`echo $UNIONFS_CONFIG | cut -d , -f 3`
	#======================================
	# create read write mount points
	#--------------------------------------
	for dir in $rwDir $xiDir;do
		mkdir -p $dir
	done
	#======================================
	# check read/write device location
	#--------------------------------------
	getDiskDevice $rwDevice | grep -q ram
	if [ $? = 0 ];then
		# /.../
		# write part is a ram location, use tmpfs for ram
		# disk data storage
		# ----
		if ! mount -t tmpfs tmpfs $rwDir >/dev/null;then
			return 1
		fi
	else
		# /.../
		# write part is not a ram disk, create/check ext3 filesystem
		# on it if not remote. Mount the filesystem
		# ----
		if [ "$roDevice" = "nfs" ];then
			rwDevice="-o nolock,rw $nfsRootServer:$rwDevice"
		fi
		if [ ! "$roDevice" = "nfs" ] && ! setupReadWrite; then
			return 1
		fi
		if ! mount $rwDevice $rwDir >/dev/null;then
			Echo "Failed to mount read/write filesystem"
			return 1
		fi
	fi
	#======================================
	# setup overlay mount
	#--------------------------------------
	if [ $unionFST = "aufs" ];then
		mount -t tmpfs tmpfs $xiDir >/dev/null || retval=1
		mount -t aufs \
			-o dirs=$rwDir=rw:$roDir=ro,xino=$xiDir/.aufs.xino aufs /mnt \
		>/dev/null || return 1
	else
		mount -t unionfs \
			-o dirs=$rwDir=rw:$roDir=ro unionfs /mnt
		>/dev/null || return 1
	fi
	usleep 500000
	return 0
}
#======================================
# mountSystemCombined
#--------------------------------------
function mountSystemCombined {
	local mountDevice=$1
	local loopf=$2
	local roDevice=$mountDevice
	if [ "$haveLVM" = "yes" ]; then
		local rwDevice="/dev/$VGROUP/LVRoot"
	elif [ "$haveLuks" = "yes" ]; then
		local rwDevice="/dev/mapper/luksReadWrite"
	else
		local rwDevice=`getNextPartition $mountDevice`
	fi
	mkdir /read-only >/dev/null
	# /.../
	# mount the read-only partition to /read-only and use
	# mount option -o ro for this filesystem
	# ----
	if ! kiwiMount "$roDevice" "/read-only" "" $loopf;then
		Echo "Failed to mount read only filesystem"
		return 1
	fi
	# /.../
	# mount a tmpfs as /mnt which will become the root fs (/) later on
	# and extract the rootfs tarball with the RAM data and the read-only
	# and read-write links into the tmpfs.
	# ----
	local rootfs=/read-only/rootfs.tar
	if [ ! -f $rootfs ];then
		Echo "Can't find rootfs tarball"
		umount "$roDevice" &>/dev/null
		return 1
	fi
	# /.../
	# count inode numbers for files in rootfs tarball
	# ----
	local inr=`tar -tf $rootfs | wc -l`
	inr=`expr $inr \* 11 / 10 / 1024`
	inr=$inr"k"
	# /.../
	# mount tmpfs, reserve max 512MB for the rootfs data
	# ----
	if ! mount -t tmpfs tmpfs -o size=512M,nr_inodes=$inr /mnt;then
		systemException \
			"Failed to mount root tmpfs" \
		"reboot"
	fi
	if ! validateTarSize $rootfs;then
		systemException \
			"Not enough RAM space available for temporary data" \
		"reboot"
	fi
	cd /mnt && tar xf $rootfs >/dev/null && cd /
	# /.../
	# create a /mnt/read-only mount point and move the /read-only
	# mount into the /mnt root tree. After that remove the /read-only
	# directory and create a link to /mnt/read-only instead
	# /read-only -> /mnt/read-only
	# ----
	mkdir /mnt/read-only >/dev/null
	mount --move /read-only /mnt/read-only >/dev/null
	rm -rf /read-only >/dev/null
	ln -s /mnt/read-only /read-only >/dev/null || return 1
	if ! echo $rwDevice | grep -q loop;then
		if partitionSize $rwDevice &>/dev/null;then
			# /.../
			# mount the read-write partition to /mnt/read-write and create
			# a link to it: /read-write -> /mnt/read-write 
			# ----
			mkdir /mnt/read-write >/dev/null
			mount $rwDevice /mnt/read-write >/dev/null
			rm -rf /read-write >/dev/null
			ln -s /mnt/read-write /read-write >/dev/null
		fi
	fi
}
#======================================
# mountSystemStandard
#--------------------------------------
function mountSystemStandard {
	local mountDevice=$1
	if [ "$FSTYPE" = "btrfs" ];then
		export haveBtrFS=yes
	fi
	if [ ! -z $FSTYPE ]          && 
	   [ ! $FSTYPE = "unknown" ] && 
	   [ ! $FSTYPE = "auto" ]
	then
		kiwiMount "$mountDevice" "/mnt"
	else
		mount $mountDevice /mnt >/dev/null
	fi
	if [ "$haveLVM" = "yes" ];then
		for i in /dev/$VGROUP/LV*;do
			local volume=$(echo $i | cut -f4 -d/ | cut -c3-)
			local mpoint=$(echo $volume | tr _ /)
			if \
				[ ! $volume = "Root" ] && \
				[ ! $volume = "Comp" ] && \
				[ ! $volume = "Swap" ]
			then
				mkdir -p /mnt/$mpoint
				mount /dev/$VGROUP/LV$volume /mnt/$mpoint
			fi
		done
	fi
	return $?
}
#======================================
# mountSystem
#--------------------------------------
function mountSystem {
	local retval=0
	local OLDIFS=$IFS
	IFS=$IFS_ORIG
	#======================================
	# set primary mount device
	#--------------------------------------
	local mountDevice="$imageRootDevice"
	if [ ! -z "$1" ];then
		mountDevice="$1"
	fi
	#======================================
	# wait for storage device to appear
	#--------------------------------------
	if ! echo $mountDevice | grep -qE "loop|nolock";then
		waitForStorageDevice $mountDevice
	fi
	#======================================
	# check root tree type
	#--------------------------------------
	if [ ! -z "$COMBINED_IMAGE" ];then
		mountSystemCombined "$mountDevice" $2
		retval=$?
	elif [ ! -z "$UNIONFS_CONFIG" ];then
		local unionFST=`echo $UNIONFS_CONFIG | cut -d , -f 3`
		if [ "$unionFST" = "clicfs" ];then
			mountSystemClicFS $2
		else
			mountSystemUnified $2
		fi
		retval=$?
	else
		mountSystemStandard "$mountDevice"
		retval=$?
	fi
	#======================================
	# setup boot partition
	#--------------------------------------
	if [ $retval = 0 ] && [ -z "$RESTORE" ];then
		setupBootPartition
	fi
	#======================================
	# reset mount counter
	#--------------------------------------
	resetMountCounter
	IFS=$OLDIFS
	return $retval
}
#======================================
# cleanDirectory
#--------------------------------------
function cleanDirectory {
	local directory=$1
	shift 1
	local save=$@
	local tmpdir=`mktemp -d`
	for saveItem in $save;do
		mv $directory/$saveItem $tmpdir >/dev/null
	done
	rm -rf $directory/*
	mv $tmpdir/* $directory
	rm -rf $tmpdir
}
#======================================
# cleanInitrd
#--------------------------------------
function cleanInitrd {
	cp /usr/bin/chroot /bin
	cp /sbin/halt /bin/reboot
	if [ -x /usr/sbin/klogconsole ];then
		cp /usr/sbin/klogconsole /bin
	fi
	for dir in /*;do
		case "$dir" in
			"/lib")   continue ;;
			"/lib64") continue ;;
			"/bin")   continue ;;
			"/mnt")   continue ;;
			"/read-only") continue ;;
			"/read-write") continue ;;
			"/xino")  continue ;;
			"/dev")   continue ;;
		esac
		rm -rf $dir/* &>/dev/null
	done
	if test -L /read-only;then
		rm -f /read-only
	fi
	if test -L /read-write;then
		rm -f /read-write
	fi
	# mount opens fstab so we give them one
	touch /etc/fstab
	hash -r
}
#======================================
# searchAlternativeConfig
#--------------------------------------
function searchAlternativeConfig {
	# Check config.IP in Hex (pxelinux style)
	localip=$IPADDR
	hexip1=`echo $localip | cut -f1 -d'.'`
	hexip2=`echo $localip | cut -f2 -d'.'`
	hexip3=`echo $localip | cut -f3 -d'.'`
	hexip4=`echo $localip | cut -f4 -d'.'`
	hexip=`printf "%02X" $hexip1 $hexip2 $hexip3 $hexip4`
	STEP=8
	while [ $STEP -gt 0 ]; do
		hexippart=`echo $hexip | cut -b -$STEP`
		Echo "Checking for config file: config.$hexippart"
		fetchFile KIWI/config.$hexippart $CONFIG
		if test -s $CONFIG;then
			break
		fi
		let STEP=STEP-1
	done
	# Check config.default if no hex config was found
	if test ! -s $CONFIG;then
		Echo "Checking for config file: config.default"
		fetchFile KIWI/config.default $CONFIG
	fi
}
#======================================
# runHook
#--------------------------------------
function runHook {
	HOOK="/hooks/$1.sh"
	if [ -e $HOOK ]; then
		. $HOOK
	fi
}
#======================================
# getNextPartition
#--------------------------------------
function getNextPartition {
	part=$1
	nextPart=`echo $part | sed -e "s/\(.*\)[0-9]/\1/"`
	nextPartNum=`echo $part | sed -e "s/.*\([0-9]\)/\1/"`
	nextPartNum=`expr $nextPartNum + 1`
	nextPart="${nextPart}${nextPartNum}"
	echo $nextPart
}
#======================================
# startShell
#--------------------------------------
function startShell {
	# /.../
	# start a debugging shell on ELOG_BOOTSHELL
	# ----
	if [ -z "$kiwistderr" ] && [ ! -z $kiwidebug ];then
		Echo "Starting boot shell on $ELOG_BOOTSHELL"
		setctsid -f $ELOG_BOOTSHELL /bin/bash -i
	fi
}
#======================================
# killShell
#--------------------------------------
function killShell {
	# /.../
	# kill debugging shell on ELOG_BOOTSHELL
	# ----
	local umountProc=0
	if [ ! -e /proc/mounts ];then
		mount -t proc proc /proc
		umountProc=1
	fi
	if [ -z "$kiwistderr" ];then
		Echo "Stopping boot shell"
		fuser -k $ELOG_BOOTSHELL >/dev/null
	fi
	if [ $umountProc -eq 1 ];then
		umount /proc
	fi
}
#======================================
# waitForStorageDevice
#--------------------------------------
function waitForStorageDevice {
	# /.../
	# function to check access on a storage device
	# which could be a whole disk or a partition.
	# the function will wait until the size of the
	# storage device could be obtained or the check
	# counter equals 4
	# ----
	local device=$1
	local check=0
	udevPending
	while true;do
		partitionSize $device &>/dev/null
		if [ $? = 0 ];then
			sleep 1; return 0
		fi
		if [ $check -eq 4 ];then
			return 1
		fi
		Echo "Waiting for device $device to settle..."
		check=`expr $check + 1`
		sleep 2
	done
}

#======================================
# waitForBlockDevice
#--------------------------------------
function waitForBlockDevice {
	# /.../
	# function to check if the given block device
	# exists. If not the function will wait until the
	# device appears or the check counter equals 4
	# ----
	local device=$1
	local check=0
	udevPending
	while true;do
		if [ -b $device ] || [ $check -eq 4 ];then
			break
		fi
		Echo "Waiting for device $device to settle..."
		check=`expr $check + 1`
		sleep 2
	done
}

#======================================
# fetchFile
#--------------------------------------
function fetchFile {
	# /.../
	# the generic fetcher which is able to use different protocols
	# tftp,ftp, http, https. fetchFile is used in the netboot linuxrc
	# and uses curl and atftp to download files from the network
	# ----
	local path=$1
	local dest=$2
	local izip=$3
	local host=$4
	local type=$5
	if test -z "$path"; then
		systemException "No path specified" "reboot"
	fi
	if test -z "$host"; then
		if test -z "$SERVER"; then
			systemException "No server specified" "reboot"
		fi
		host=$SERVER
	fi
	if test -z "$type"; then
		if test -z "$SERVERTYPE"; then
			type="tftp"
		else
			type="$SERVERTYPE"
		fi
	fi
	if test "$izip" = "compressed"; then
		path=$(echo $path | sed -e s@\\.gz@@)
		path="$path.gz"
	fi
	case "$type" in
		"http")
			if test "$izip" = "compressed"; then
				curl -f http://$host/$path 2>$TRANSFER_ERRORS_FILE |\
					gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE
			else
				curl -f http://$host/$path > $dest 2> $TRANSFER_ERRORS_FILE
			fi
			loadCode=$?
			;;
		"https")
			if test "$izip" = "compressed"; then
				curl -f -k https://$host/$path 2>$TRANSFER_ERRORS_FILE |\
					gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE
			else
				curl -f -k https://$host/$path > $dest 2> $TRANSFER_ERRORS_FILE
			fi
			loadCode=$?
			;;
		"ftp")
			if test "$izip" = "compressed"; then
				curl ftp://$host/$path 2>$TRANSFER_ERRORS_FILE |\
					gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE
			else
				curl ftp://$host/$path > $dest 2> $TRANSFER_ERRORS_FILE
			fi
			loadCode=$?
			;;
		"tftp")
			validateBlockSize
			# /.../
			# atftp activates multicast by '--option "multicast"'
			# and deactivates it again  by '--option "disable multicast"'
			# ----
			multicast_atftp="multicast"
			if test "$multicast" != "enable"; then 
				multicast_atftp="disable multicast"
			fi
			if test "$izip" = "compressed"; then
				# mutlicast is disabled because you can't seek in a pipe
				# atftp is disabled because it doesn't work with pipes
				busybox tftp \
					-b $imageBlkSize -g -r $path \
					-l >(gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE) \
					$host 2>>$TRANSFER_ERRORS_FILE
			else
				atftp \
					--option "$multicast_atftp"  \
					--option "blksize $imageBlkSize" \
					-g -r $path -l $dest $host &> $TRANSFER_ERRORS_FILE
			fi
			loadCode=$?
			;;
		*)
			systemException "Unknown download type: $type" "reboot"
			;;
	esac
	loadStatus=`cat $TRANSFER_ERRORS_FILE`
	return $loadCode
}

#======================================
# putFile
#--------------------------------------
function putFile {
	# /.../
	# the generic putFile function is used to upload boot data on
	# a server. Supported protocols are tftp, ftp, http, https
	# ----
	local path=$1
	local dest=$2
	local host=$3
	local type=$4
    if test -z "$path"; then
		systemException "No path specified" "reboot"
	fi
	if test -z "$host"; then
		if test -z "$SERVER"; then
			systemException "No server specified" "reboot"
		fi
		host=$SERVER
	fi
	if test -z "$type"; then
		if test -z "$SERVERTYPE"; then
			type="tftp"
		else
			type="$SERVERTYPE"
		fi
	fi
	case "$type" in
		"http")
			curl -f -T $path http://$host/$dest > $TRANSFER_ERRORS_FILE 2>&1
			return $?
			;;
		"https")
			curl -f -T $path https://$host/$dest > $TRANSFER_ERRORS_FILE 2>&1
			return $?
			;;
		"ftp")
			curl -T $path ftp://$host/$dest  > $TRANSFER_ERRORS_FILE 2>&1
			return $?
			;;
		"tftp")
            atftp -p -l $path -r $dest $host >/dev/null 2>&1
            return $?
			;;
		*)
			systemException "Unknown download type: $type" "reboot"
			;;
	esac
}

#======================================
# importBranding
#--------------------------------------
function importBranding {
	# /.../
	# include possible custom boot loader and bootsplash files
	# to the system to allow to use them persistently
	# ----
	if [ -f /image/loader/message ];then
		if ! canWrite /mnt/boot;then
			Echo "Can't write to boot, import of boot message skipped"
		else
			mv /image/loader/message /mnt/boot
		fi
	fi
	if [ -f /image/loader/branding/logo.mng ];then
	if [ -d /mnt/etc/bootsplash/themes ];then
		for theme in /mnt/etc/bootsplash/themes/*;do
			if [ -d $theme/images ];then
				if ! canWrite $theme;then
					Echo "Can't write to $theme, import of boot theme skipped"
					continue
				fi
				cp /image/loader/branding/logo.mng  $theme/images
				cp /image/loader/branding/logov.mng $theme/images
				cp /image/loader/branding/*.jpg $theme/images
				cp /image/loader/branding/*.cfg $theme/config
			fi
		done	
	fi
	fi
}

#======================================
# validateRootTree
#--------------------------------------
function validateRootTree {
	# /.../
	# after the root of the system image has been mounted we should
	# check whether that mount is a valid system tree or not. Therefore
	# some sanity checks are made here
	# ----
	if [ ! -x /mnt/sbin/init ];then
		systemException "/sbin/init no such file or not executable" "reboot"
	fi
}

#======================================
# getDiskID
#--------------------------------------
function getDiskID {
	# /.../
	# this function is able to turn a given standard device
	# name into the udev ID based representation
	# ----
	local device=$1
	if [ -z "$device" ];then
		return
	fi
	if echo $device | grep -q "$VGROUP"; then
		echo $device
		return
	fi
	for i in /dev/disk/by-id/*;do
		if echo $i | grep -q edd-;then
			continue
		fi
		local dev=`readlink $i`
		dev=/dev/`basename $dev`
		if [ $dev = $device ];then
			echo $i
			return
		fi
	done
	echo $device
}
#======================================
# getDiskDevice
#--------------------------------------
function getDiskDevice {
	# /.../
	# this function is able to turn the given udev disk
	# ID label into the /dev/ device name
	# ----
	local device=`readlink $1`
	if [ -z "$device" ];then
		echo $1
		return
	fi
	device=`basename $device`
	device=/dev/$device
	echo $device
}
#======================================
# getDiskModel
#--------------------------------------
function getDiskModels {
	# /.../
	# this function returns the disk identifier as
	# registered in the sysfs layer
	# ----
	local models=`cat /sys/block/*/device/model 2>/dev/null`
	if [ ! -z "$models" ];then
		echo $models; return
	fi
	echo "unknown"
}
#======================================
# setupInittab
#--------------------------------------
function setupInittab {
	# /.../
	# setup default runlevel according to /proc/cmdline
	# information. If textmode is set to 1 we will boot into
	# runlevel 3
	# ----
	local prefix=$1
	if cat /proc/cmdline | grep -qi "textmode=1";then
		sed -i -e s"@id:.*:initdefault:@id:3:initdefault:@" $prefix/etc/inittab
	fi
}
#======================================
# setupConfigFiles
#--------------------------------------
function setupConfigFiles {
	# /.../
	# all files created below /config inside the initrd are
	# now copied into the system image
	# ----
	local file
	local dir
	cd /config
	find . -type f | while read file;do
		dir=$(dirname $file)
		if ! canWrite /mnt/$dir;then
			Echo "Can't write to $dir, read-only filesystem... skipped"
			continue
		fi
		if [ ! -d /mnt/$dir ];then
			mkdir -p /mnt/$dir
		fi
		cp $file /mnt/$file
	done
	cd /
	rm -rf /config
}
#======================================
# activateImage
#--------------------------------------
function activateImage {
	# /.../
	# move the udev created nodes from the initrd into
	# the system root tree call the pre-init phase which
	# already runs in the new tree and finaly switch the
	# new tree to be the new root (/) 
	# ----
	#======================================
	# setup image name
	#--------------------------------------
	local name
	if [ ! -z "$stickSerial" ];then
		name="$stickSerial on -> $stickDevice"
	elif [ ! -z "$imageName" ];then
		name=$imageName
	elif [ ! -z "$imageRootName" ];then
		name=$imageRootName
	elif [ ! -z "$imageRootDevice" ];then
		name=$imageRootDevice
	elif [ ! -z "$imageDiskDevice" ];then
		name=$imageDiskDevice
	else
		name="unknown"
	fi
	#======================================
	# move union mount points to system
	#--------------------------------------
	local roDir=read-only
	local rwDir=read-write
	local xiDir=xino
	if [ -z "$NFSROOT" ];then
		if [ -d $roDir ];then
			mkdir -p /mnt/$roDir && mount --move /$roDir /mnt/$roDir
		fi
		if [ -d $rwDir ];then
			mkdir -p /mnt/$rwDir && mount --move /$rwDir /mnt/$rwDir
		fi
		if [ -d $xiDir ];then
			mkdir -p /mnt/$xiDir && mount --move /$xiDir /mnt/$xiDir
		fi
	fi
	#======================================
	# move live CD mount points to system
	#--------------------------------------
	local cdDir=/livecd
	if [ -d $cdDir ];then
		mkdir -p /mnt/$cdDir && mount --move /$cdDir /mnt/$cdDir
	fi
	#======================================
	# move device nodes
	#--------------------------------------
	Echo "Activating Image: [$name]"
	reopenKernelConsole
	udevPending
	mount --move /dev /mnt/dev
	udevKill
	#======================================
	# run preinit stage
	#--------------------------------------
	Echo "Preparing preinit phase..."
	if ! cp /iprocs /mnt;then
		systemException "Failed to copy iprocs code" "reboot"
	fi
	if ! cp /preinit /mnt;then
		systemException "Failed to copy preinit code" "reboot"
	fi
	if ! cp /include /mnt;then
		systemException "Failed to copy include code" "reboot"
	fi
	if [ ! -x /lib/mkinitrd/bin/run-init ];then
		systemException "Can't find run-init program" "reboot"
	fi
}
#======================================
# cleanImage
#--------------------------------------
function cleanImage {
	# /.../
	# remove preinit code from system image before real init
	# is called
	# ----
	#======================================
	# kill second utimer and tail
	#--------------------------------------
	. /iprocs
	kill $UTIMER_PID &>/dev/null
	kill $TAIL_PID   &>/dev/null
	#======================================
	# remove preinit code from system image
	#--------------------------------------
	rm -f /tmp/utimer
	rm -f /dev/utimer
	rm -f /utimer
	rm -f /iprocs
	rm -f /preinit
	rm -f /include
	rm -f /.kconfig
	rm -f /.profile
	rm -rf /image
	#======================================
	# return early for special types
	#--------------------------------------
	if \
		[ "$haveClicFS" = "yes" ] || \
		[ "$haveBtrFS"  = "yes" ] || \
		[ ! -z "$NFSROOT" ]       || \
		[ ! -z "$NBDROOT" ]       || \
		[ ! -z "$AOEROOT" ]       || \
		[ ! -z "$COMBINED_IMAGE" ]
	then
		export ROOTFS_FSCK="0"
		return
	fi
	#======================================
	# umount LVM root parts lazy
	#--------------------------------------
	if [ "$haveLVM" = "yes" ]; then
		for i in /dev/$VGROUP/LV*;do
			if [ ! -e $i ];then
				continue
			fi
			if \
				[ ! $i = "/dev/$VGROUP/LVRoot" ] && \
				[ ! $i = "/dev/$VGROUP/LVComp" ] && \
				[ ! $i = "/dev/$VGROUP/LVSwap" ]
			then
				umount -l $i &>/dev/null
			fi
		done
	fi
}
#======================================
# bootImage
#--------------------------------------
function bootImage {
	# /.../
	# call the system image init process and therefore
	# boot into the operating system
	# ----
	local reboot=no
	local option=$@
	#======================================
	# turn runlevel 4 to 5 if found
	#--------------------------------------
	option=$(echo $@ | sed -e s@4@5@)
	echo && Echo "Booting System: $option"
	export IFS=$IFS_ORIG
	#======================================
	# check for reboot request
	#--------------------------------------
	if [ "$LOCAL_BOOT" = "no" ];then
		if [ -z "$KIWI_RECOVERY" ];then
			if [ ! -z "$OEM_REBOOT" ] || [ ! -z "$REBOOT_IMAGE" ];then
				reboot=yes
			fi
		fi
	fi
	#======================================
	# kill initial tail and utimer
	#--------------------------------------
	. /iprocs
	kill $UTIMER_PID &>/dev/null
	kill $TAIL_PID   &>/dev/null
	#======================================
	# copy boot log file into system image
	#--------------------------------------
	mkdir -p /mnt/var/log
	rm -f /mnt/boot/grub/mbrid
	if [ -e /mnt/dev/shm/initrd.msg ];then
		cp -f /mnt/dev/shm/initrd.msg /mnt/var/log/boot.msg
	fi
	if [ -e /var/log/boot.kiwi ];then
		cp -f /var/log/boot.kiwi /mnt/var/log/boot.kiwi
	fi
	#======================================
	# directly boot/reboot
	#--------------------------------------
	umount proc &>/dev/null && \
	umount proc &>/dev/null
	if [ $reboot = "yes" ];then
		Echo "Reboot requested... rebooting after preinit"
		exec /lib/mkinitrd/bin/run-init -c /dev/console /mnt /bin/bash -c \
			"/preinit ; . /include ; cleanImage ; exec /sbin/reboot -f -i"
	else
		# FIXME: clicfs doesn't like run-init
		if [ ! "$haveClicFS" = "yes" ];then
			exec /lib/mkinitrd/bin/run-init -c /dev/console /mnt /bin/bash -c \
				"/preinit ; . /include ; cleanImage ; exec /sbin/init $option"
		else
			cd /mnt && exec chroot . /bin/bash -c \
				"/preinit ; . /include ; cleanImage ; exec /sbin/init $option"
		fi
	fi
}
#======================================
# setupUnionFS
#--------------------------------------
function setupUnionFS {
	# /.../
	# export the UNIONFS_CONFIG environment variable
	# which contains a three part coma separated list of the
	# following style: rwDevice,roDevice,unionType. The
	# devices are stores by disk ID if possible
	# ----
	local rwDevice=`getDiskID $1`
	local roDevice=`getDiskID $2`
	local unionFST=$3
	rwDeviceLuks=$(luksOpen $rwDevice luksReadWrite)
	roDeviceLuks=$(luksOpen $roDevice luksReadOnly)
	if [ ! $rwDeviceLuks = $rwDevice ];then
		rwDevice=$rwDeviceLuks
		export haveLuks="yes"
	fi
	if [ ! $roDeviceLuks = $roDevice ];then
		roDevice=$roDeviceLuks
		export haveLuks="yes"
	fi
	export UNIONFS_CONFIG="$rwDevice,$roDevice,$unionFST"
}
#======================================
# canWrite
#--------------------------------------
function canWrite {
	# /.../
	# check if we can write to the given location
	# If no location is given the function test
	# for write permissions in /mnt.
	# returns zero on success.
	# ---
	local dest=$1
	if [ -z "$dest" ];then
		dest=/mnt
	fi
	if [ ! -d $dest ];then
		return 1
	fi
	if touch $dest/can-write &>/dev/null;then
		rm $dest/can-write
		return 0
	fi
	return 1
}
#======================================
# xenServer
#--------------------------------------
function xenServer {
	# /.../
	# check if the given kernel is a xen kernel and if so
	# check if a dom0 or a domU setup was requested
	# ----
	local kname=$1
	local mountPrefix=$2
	local sysmap="$mountPrefix/boot/System.map-$kname"
	local isxen
	if [ ! -e $sysmap ]; then
		sysmap="$mountPrefix/boot/System.map"
	fi
	if [ ! -e $sysmap ]; then
		Echo "No system map for kernel $kname found"
		return 1
	fi
	isxen=$(grep -c "xen_base" $sysmap)
	if [ $isxen -eq 0 ]; then
		# not a xen kernel
		return 1
	fi
	if [ -z "$kiwi_xendomain" ];then
		# no xen domain set, assume domU
		return 1
	fi
	if [ $kiwi_xendomain = "dom0" ];then
		# xen dom0 requested
		return 0
	fi
	return 1
}
#======================================
# makeLabel
#--------------------------------------
function makeLabel {
	# /.../
	# create boot label and replace all spaces with
	# underscores. current bootloaders show the
	# underscore sign as as space in the boot menu
	# ---
	if [ -z "$loader" ];then
		loader="grub"
	fi
	if [ ! $loader = "syslinux" ] && [ ! $loader = "extlinux" ];then
		echo $1 | tr " " "_"
	else
		echo $1
	fi
}
#======================================
# waitForX
#--------------------------------------
function waitForX {
	# /.../
	# wait for the X-Server with PID $xserver_pid to
	# become read for client calls
	# ----
	local xserver_pid=$1
	local testx=/usr/sbin/testX
	local err=1
	while kill -0 $xserver_pid 2>/dev/null ; do
		sleep 1
		if test -e /tmp/.X11-unix/X0 && test -x $testx ; then
			$testx 16 2>/dev/null
			err=$?
			# exit code 1 -> XOpenDisplay failed...
			if test $err = 1;then
				Echo "TestX: XOpenDisplay failed"
				return 1
			fi
			# exit code 2 -> color or dimensions doesn't fit...
			if test $err = 2;then
				Echo "TestX: color or dimensions doesn't fit"
				kill $xserver_pid
				return 1
			fi
			# server is running, detach oom-killer from it
			echo -n '-17' > /proc/$xserver_pid/oom_adj
			return 0
		fi
	done
	return 1
}
#======================================
# startX
#--------------------------------------
function startX {
	# /.../
	# start X-Server and wait for it to become ready
	# ----
	export DISPLAY=:0
	local XServer=/usr/bin/Xorg
	if [ -x /usr/X11R6/bin/Xorg ];then
		XServer=/usr/X11R6/bin/Xorg
	fi
	$XServer -deferglyphs 16 vt07 &
	export XServerPID=$!
	if ! waitForX $XServerPID;then
		Echo "Failed to start X-Server"
		return 1
	fi
	return 0
}
#======================================
# stoppX
#--------------------------------------
function stoppX {
	if [ -z "$XServerPID" ];then
		return
	fi
	if kill -0 $XServerPID 2>/dev/null; then
		sleep 1 && kill $XServerPID
		while kill -0 $XServerPID 2>/dev/null; do
			sleep 1
		done
	fi
}
#======================================
# luksOpen
#--------------------------------------
function luksOpen {
	# /.../
	# check given device if it uses the LUKS extension
	# if yes open the device and return the new
	# /dev/mapper/ device name
	# ----
	local ldev=$1
	local name=$2
	local info
	if [ -z $name ];then
		name=luksroot
	fi
	if [ -e /dev/mapper/$name ];then
		echo /dev/mapper/$name; return
	fi
	if ! cryptsetup isLuks $ldev &>/dev/null;then
		echo $ldev; return
	fi
	while true;do
		if [ ! -e /tmp/luks ];then
			LUKS_OPEN=$(runInteractive \
				"--stdout --insecure --passwordbox "\"$TEXT_LUKS\"" 10 60"
			)
			echo $LUKS_OPEN > /tmp/luks
		fi
		info=$(cat /tmp/luks | cryptsetup luksOpen $ldev $name 2>&1)
		if [ $? = 0 ];then
			break
		fi
		rm -f /tmp/luks
		Dialog --stdout --timeout 10 --msgbox "\"Error: $info\"" 8 60
	done
	echo /dev/mapper/$name
}
#======================================
# luksResize
#--------------------------------------
function luksResize {
	# /.../
	# check given device if it is a mapped device name
	# and run cryptsetup resize on the mapper name
	# ----
	local ldev=$1
	if [ ! "$haveLuks" = "yes" ] || [ ! -e $ldev ];then
		return
	fi
	local name=$(basename $ldev)
	local dmap=$(dirname  $ldev)
	if [ ! "$dmap" = "/dev/mapper" ];then
		return
	fi
	cryptsetup resize $name
}
#======================================
# luksClose
#--------------------------------------
function luksClose {
	# /.../
	# close all open LUKS mappings
	# ----
	local name
	for i in /dev/mapper/luks*;do
		name=$(basename $i)
		cryptsetup luksClose $name
	done
}
#======================================
# selectLanguage
#--------------------------------------
function selectLanguage {
	# /.../
	# select language if not yet done. The value is
	# used for all dialog windows with i18n support
	# ----
	local title="\"Select Language\""
	local list="en_US \"[ English ]\" on"
	local list_orig=$list
	local zh_CN=Chinese
	local zh_TW=Taiwanese
	local ru_RU=Russian
	local de_DE=German
	local ar_AR=Arabic
	local cs_CZ=Czech
	local el_GR=Greek
	local es_ES=Spanish
	local fi_FI=Finnish
	local fr_FR=French
	local hu_HU=Hungarian
	local it_IT=Italian
	local ja_JP=Japanese
	local ko_KR=Korean
	local nl_NL=Dutch
	local pl_PL=Polish
	local pt_BR=Portuguese
	local sv_SE=Swedish
	local tr_TR=Turkish
	local nb_NO=Norwegian
	local da_DK=Danish
	local pt_PT=Portuguese
	local en_GB=English
	local code
	local lang
	if [ -f /.profile ];then
		importFile < /.profile
	fi
	if [ ! -z "$kiwi_oemunattended" ] && [ "$DIALOG_LANG" = "ask" ];then
		DIALOG_LANG=en_US
	fi
	if [ "$DIALOG_LANG" = "ask" ];then
		for code in $(echo $kiwi_language | tr "," " ");do
			if [ $code = "en_US" ];then
				continue
			fi
			eval lang=\$$code
			list="$list $code \"[ $lang ]\" off"
		done
		if [ "$list" = "$list_orig" ];then
			DIALOG_LANG=en_US
		else
			DIALOG_LANG=$(runInteractive \
				"--stdout --no-cancel --radiolist $title 20 40 10 $list"
			)
		fi
	fi
	#======================================
	# Exports (Texts)
	#--------------------------------------
	export TEXT_OK=$(
		getText "OK")
	export TEXT_CANCEL=$(
		getText "Cancel")
	export TEXT_YES=$(
		getText "Yes")
	export TEXT_NO=$(
		getText "No")
	export TEXT_EXIT=$(
		getText "Exit")
	export TEXT_LUKS=$(
		getText "Enter LUKS passphrase")
	export TEXT_LICENSE=$(
		getText "Do you accept the license agreement ?")
	export TEXT_RESTORE=$(
		getText "Do you want to start the System-Restore ?")
	export TEXT_REPAIR=$(
		getText "Do you want to start the System-Recovery ?")
	export TEXT_RECOVERYTITLE=$(
		getText "Restoring base operating system...")
	export TEXT_INSTALLTITLE=$(
		getText "Installation...")
	export TEXT_CDPULL=$(
		getText "Please remove the CD/DVD before reboot")
	export TEXT_USBPULL=$(
		getText "Please unplug the USB stick before reboot")
	export TEXT_SELECT=$(
		getText "Select disk for installation:")
}
#======================================
# getText
#--------------------------------------
function getText {
	# /.../
	# return translated text
	# ----
	export LANG=$DIALOG_LANG.utf8
	local text=$(gettext kiwi "$1")
	if [ ! -z "$2" ];then
		text=$(echo $text | sed -e s"@%1@$2@")
	fi
	if [ ! -z "$3" ];then
		text=$(echo $text | sed -e s"@%2@$3@")
	fi
	echo "$text"
}
#======================================
# displayEULA
#--------------------------------------
function displayEULA {
	# /.../
	# display in a dialog window the text part of the
	# selected language file or the default file 
	# /license.txt or /EULA.txt
	# ----
	local code=$(echo $DIALOG_LANG | cut -f1 -d_)
	#======================================
	# check license files
	#--------------------------------------
	local files=$(find /license.*txt 2>/dev/null)
	if [ -z "$files" ];then
		return
	fi
	#======================================
	# use selected file or default
	#--------------------------------------
	code=/license.$code.txt
	if [ ! -f $code ];then
		code=/license.txt
		if [ ! -f $code ];then
			code=/EULA.txt
		fi
	fi
	#======================================
	# check selected file and show it
	#--------------------------------------
	if [ ! -f $code ];then
		Echo "License file $code not found... skipped"
		return
	fi
	while true;do
		Dialog --textbox $code 20 70 \
			--and-widget --extra-button \
			--extra-label "$TEXT_NO" \
			--ok-label "$TEXT_YES" \
			--cancel-label "$TEXT_CANCEL" \
			--yesno "\"$TEXT_LICENSE\"" \
			5 45
		case $? in
			0 ) break
				;;
			1 ) continue
				;;
			* ) systemException \
					"License not accepted... reboot" \
				"reboot"
				;;
		esac
	done
}
#======================================
# ddn
#--------------------------------------
function ddn {
	# /.../
	# print disk device name (node name) according to the
	# linux device node specs: If the last character of the
	# device is a letter, attach the partition number. If the
	# last character is a number, attach a 'p' and then the
	# partition number.
	# ----
	local lastc=$(echo $1 | sed -e 's@\(^.*\)\(.$\)@\2@')
	if echo $lastc | grep -qP "^\d+$";then
		echo $1"p"$2
		return
	fi
	echo $1$2
}
#======================================
# dn
#--------------------------------------
function dn {
	# /.../
	# print disk name (device name) according to the
	# linux device node specs: If the device matches "p"
	# followed by a number remove pX else remove 
	# the last number
	# ----
	local part=$(getDiskDevice $1)
	local part_new=$(echo $part | sed -e 's@\(^.*\)\(p.*$\)@\1@')
	if [ $part = $part_new ];then
		part_new=$(echo $part | sed -e 's@\(^.*\)\([0-9].*$\)@\1@')
	fi
	echo $part_new
}
#======================================
# ndd
#--------------------------------------
function nd {
	# /.../
	# print the number of the disk device according to the
	# device node name. 
	# ----
	local part=$(getDiskDevice $1)
	local part_new=$(echo $part | sed -e 's@\(^.*\)p\(.*$\)@\2@')
	if [ $part = $part_new ];then
		part_new=$(echo $part | sed -e 's@\(^.*\)\([0-9].*$\)@\2@')
	fi
	echo $part_new
}
#======================================
# runInteractive
#--------------------------------------
function runInteractive {
	# /.../
	# run dialog in a bash inside an fbiterm or directly
	# on the running terminal. Make the terminal the controlling
	# tty first. The output of the dialog call is stored in
	# a file and printed as result to this function
	# ----
	local r=/tmp/rid
	local code
	echo "dialog $@ > /tmp/out" > $r
	echo "echo -n \$? > /tmp/out.exit" >> $r
	if [ -e /dev/fb0 ];then
		setctsid $ELOG_EXCEPTION fbiterm -m $UFONT -- bash -i $r
	else
		setctsid $ELOG_EXCEPTION bash -i $r
	fi
	code=$(cat /tmp/out.exit)
	if [ ! $code = 0 ];then
		return $code
	fi
	cat /tmp/out && rm -f /tmp/out* $r
	return 0
}
#======================================
# createHybridPersistent
#--------------------------------------
function createHybridPersistent {
	local dev=$1;
	local relativeDevName=`basename $dev`
	local input=/part.input
	local id=0
	for disknr in 2 3 4; do
		id=`partitionID $dev $disknr`
		if [ $id = $HYBRID_PERSISTENT_ID ]; then
			Echo "Existing persistent hybrid partition found $dev$disknr"
			return
		else
			Echo "Creating hybrid persistent partition for COW data: "
			Echo "$dev$disknr id=$HYBRID_PERSISTENT_ID fs=$HYBRID_PERSISTENT_FS"
			if [ $disknr -lt 4 ];then
				createPartitionerInput \
					n p $disknr . . t $disknr $HYBRID_PERSISTENT_ID w
			else
				createPartitionerInput \
					n p . . t 4 $HYBRID_PERSISTENT_ID w
			fi
			imageDiskDevice=$dev
			callPartitioner $input
			if ! waitForStorageDevice $dev$disknr;then
				Echo "Partition $dev$disknr doesn't appear... fatal !"
				Echo "Persistent writing deactivated"
				unset kiwi_hybridpersistent
			elif ! mkfs.$HYBRID_PERSISTENT_FS $dev$disknr;then
				Echo "Failed to create hybrid persistent filesystem"
				Echo "Persistent writing deactivated"
				unset kiwi_hybridpersistent
			fi
			return
		fi
	done
}
#======================================
# callPartitioner
#--------------------------------------
function callPartitioner {
	local input=$1
	if [ $PARTITIONER = "sfdisk" ];then
		Echo "Repartition the disk according to real geometry [ fdisk ]"
		echo "w" >> $input
		echo "q" >> $input
		fdisk $imageDiskDevice < $input 1>&2
		if test $? != 0; then
			systemException "Failed to create partition table" "reboot"
		fi
	else
		# /.../
		# nothing to do for parted here as we write
		# imediately with parted and don't create a
		# command input file as for fdisk but we re-read
		# the disk so that the new table will be used
		# ----
		udevPending
		blockdev --rereadpt $imageDiskDevice
	fi
}
#======================================
# createPartitionerInput
#--------------------------------------
function createPartitionerInput {
	if [ $PARTITIONER = "sfdisk" ];then
		createFDiskInput $@
	else
		Echo "Repartition the disk according to real geometry [ parted ]"
		partedInit $imageDiskDevice
		partedSectorInit $imageDiskDevice
		createPartedInput $imageDiskDevice $@
    fi
}
#======================================
# createFDiskInput
#--------------------------------------
function createFDiskInput {
	local input=/part.input
	for cmd in $*;do
		if [ $cmd = "." ];then
			echo >> $input
			continue
		fi
		echo $cmd >> $input
	done
}
#======================================
# partedInit
#--------------------------------------
function partedInit {
	# /.../
	# initialize current partition table output
	# as well as the number of cylinders and the
	# cyliner size in kB for this disk
	# ----
	local device=$1
	local IFS=""
	local parted=$(parted -m $device unit cyl print)
	local header=$(echo $parted | head -n 3 | tail -n 1)
	local ccount=$(echo $header | cut -f1 -d:)
	local cksize=$(echo $header | cut -f4 -d: | cut -f1 -dk)
	export partedOutput=$parted
	export partedCylCount=$ccount
	export partedCylKSize=$cksize
}
#======================================
# partedWrite
#--------------------------------------
function partedWrite {
	# /.../
	# call parted with current command queue.
	# This will immediately change the partition table
	# ----
	local device=$1
	local cmds=$2
	local opts
	if [ $PARTED_HAVE_ALIGN -eq 1 ];then
		opts="-a cyl"
	fi
	if ! parted $opts -m $device unit cyl $cmds;then
		systemException "Failed to create partition table" "reboot"
	fi
	partedInit $device
}
#======================================
# partedSectorInit
#--------------------------------------
function partedSectorInit {
	# /.../
	# return start/end sectors of current partitions.
	# ----
	IFS=$IFS_ORIG
	local disk=$1
	local s_start
	local s_stopp
	unset startSectors
	unset endSectors
	for i in $(
		parted -m $disk unit s print | grep ^[1-4]: | cut -f2-3 -d: | tr -d s
	);do
		s_start=$(echo $i | cut -f1 -d:)
		s_stopp=$(echo $i | cut -f2 -d:)
		if [ -z "$startSectors" ];then
			startSectors=${s_start}s
		else
			startSectors=${startSectors}:${s_start}s
		fi
		if [ -z "$endSectors" ];then
			endSectors=$((s_stopp + 1))s
		else
			endSectors=$endSectors:$((s_stopp + 1))s
		fi
	done
	if [ -z "$startSectors" ];then
		startSectors=1
	fi
}
#======================================
# partedEndCylinder
#--------------------------------------
function partedEndCylinder {
	# /.../
	# return end cylinder of given partition, next
	# partition must start at return value plus 1
	# ----
	local part=$(($1 + 3))
	local IFS=""
	local header=$(echo $partedOutput | head -n $part | tail -n 1)
	local ccount=$(echo $header | cut -f3 -d: | tr -d cyl)
	echo $ccount
}
#======================================
# partedMBToCylinder
#--------------------------------------
function partedMBToCylinder {
	# /.../
	# convert size given in MB to cylinder count
	# ----
	local sizeKB=$(($1 * 1048576))
	local cylreq=$(($sizeKB / ($partedCylKSize * 1000)))
	echo $cylreq
}
#======================================
# createPartedInput
#--------------------------------------
function createPartedInput {
	# /.../
	# evaluate partition instructions and turn them
	# into a parted command line queue. As soon as the
	# geometry data would be changed according to the
	# last partedInit() call the command queue is processed
	# and the partedInit() will be called afterwards
	# ----
	local disk=$1
	shift
	local index=0
	local pcmds
	local pcmds_fix
	local partid
	local pstart
	local pstopp
	local value
	local cmdq
	#======================================
	# create list of commands
	#--------------------------------------
	for cmd in $*;do
		pcmds[$index]=$cmd
		index=$(($index + 1))
	done
	index=0
	index_fix=0
	#======================================
	# fix list of commands
	#--------------------------------------
	while [ ! -z "${pcmds[$index]}" ];do
		cmd=${pcmds[$index]}
		pcmds_fix[$index_fix]=$cmd
		case $cmd in
			"d")
				partid=${pcmds[$index + 1]}
				if ! echo $partid | grep -q "^[0-4]$";then
					# make sure there is a ID set for the deletion
					index_fix=$(($index_fix + 1))
					pcmds_fix[$index_fix]=1
				fi
			;;
			"n")
				partid=${pcmds[$index + 2]}
				if ! echo $partid | grep -q "^[0-4]$";then
					# make sure there is a ID set for the creation
					index_fix=$(($index_fix + 1))
					pcmds_fix[$index_fix]=${pcmds[$index + 1]}
					index_fix=$(($index_fix + 1))
					pcmds_fix[$index_fix]=4
					index=$(($index + 1))
				fi
			;;
		esac
		index=$(($index + 1))
		index_fix=$(($index_fix + 1))
	done
	#======================================
	# use fixed list and print log info
	#--------------------------------------
	unset pcmds
	pcmds=(${pcmds_fix[*]})
	unset pcmds_fix
	index=0
	#======================================
	# process commands
	#--------------------------------------
	echo "createPartedInput: fixed input: ${pcmds[*]}" 1>&2
	for cmd in ${pcmds[*]};do
		case $cmd in
			#======================================
			# delete partition
			#--------------------------------------
			"d")
				partid=${pcmds[$index + 1]}
				partid=$(($partid / 1))
				cmdq="$cmdq rm $partid"
				partedWrite "$disk" "$cmdq"
				cmdq=""
				;;
			#======================================
			# create new partition
			#--------------------------------------
			"n")
				partid=${pcmds[$index + 2]}
				partid=$(($partid / 1))
				pstart=${pcmds[$index + 3]}
				if [ "$pstart" = "1" ];then
					pstart=$(echo $startSectors | cut -f $partid -d:)
				fi
				if [ $pstart = "." ];then
					# start is next sector according to previous partition
					pstart=$(($partid - 1))
					if [ $pstart -gt 0 ];then
						pstart=$(echo $endSectors | cut -f $pstart -d:)
					else
						pstart=$(echo $startSectors | cut -f $partid -d:)
					fi
				fi
				pstopp=${pcmds[$index + 4]}
				if [ $pstopp = "." ];then
					# use rest of the disk for partition end
					pstopp=$partedCylCount
				elif echo $pstopp | grep -qi M;then
					# calculate stopp cylinder from size
					pstopp=$(($partid - 1))
					if [ $pstopp -gt 0 ];then
						pstopp=$(partedEndCylinder $pstopp)
					fi
					value=$(echo ${pcmds[$index + 4]} | cut -f1 -dM | tr -d +)
					value=$(partedMBToCylinder $value)
					pstopp=$((1 + $pstopp + $value))
				fi
				cmdq="$cmdq mkpart primary $pstart $pstopp"
				partedWrite "$disk" "$cmdq"
				partedSectorInit $imageDiskDevice
				cmdq=""
				;;
			#======================================
			# change partition ID
			#--------------------------------------
			"t")
				ptypex=${pcmds[$index + 2]}
				partid=${pcmds[$index + 1]}
				cmdq="$cmdq set $partid type 0x$ptypex"
				partedWrite "$disk" "$cmdq"
				cmdq=""
				;;
		esac
		index=$(($index + 1))
	done
}
#======================================
# reloadKernel
#--------------------------------------
function reloadKernel {
	# /.../
	# reload the given kernel and initrd. This function
	# checks USB stick devices for a kernel and initrd
	# and shows them in a dialog window. The selected kernel
	# and initrd is loaded via kexec.
	# ----
	#======================================
	# check proc/cmdline
	#--------------------------------------
	ldconfig
	mountSystemFilesystems &>/dev/null
	if ! cat /proc/cmdline | grep -qi "hotfix=1";then
		umountSystemFilesystems
		return
	fi
	#======================================
	# check for kexec
	#--------------------------------------
	if [ ! -x /sbin/kexec ];then
		systemException "Can't find kexec" "reboot"
	fi
	#======================================
	# start udev
	#--------------------------------------
	touch /etc/modules.conf
	touch /lib/modules/*/modules.dep
	udevStart
	errorLogStart
	probeDevices
	#======================================
	# search hotfix stick
	#--------------------------------------
	USBStickDevice kexec
	if [ $stickFound = 0 ];then
		systemException "No hotfix USB stick found" "reboot"
	fi
	#======================================
	# mount stick
	#--------------------------------------
	if ! mount -o ro $stickDevice /mnt;then
		systemException "Failed to mount hotfix stick" "reboot"
	fi
	#======================================
	# load kernel
	#--------------------------------------
	kexec -l /mnt/linux.kexec --initrd=/mnt/initrd.kexec \
		--append="$(cat /proc/cmdline | sed -e s"@hotfix=1@@")"
	if [ ! $? = 0 ];then
		systemException "Failed to load hotfix kernel" "reboot"
	fi
	#======================================
	# go for gold
	#--------------------------------------
	exec kexec -e
}
#======================================
# resizeFilesystem
#--------------------------------------
function resizeFilesystem {
	local deviceResize=$1
	local callme=$2
	local ramdisk=0
	local resize_fs
	local resize_lucks
	local check
	if echo $deviceResize | grep -qi "/dev/ram";then
		ramdisk=1
	fi
	if [ -z "$FSTYPE" ];then
		probeFileSystem $deviceResize
	fi
	resize_lucks="luksResize $deviceResize"
	if [ "$FSTYPE" = "reiserfs" ];then
		Echo "Resize Reiser filesystem to full partition space..."
		resize_fs="resize_reiserfs -q $deviceResize"
		check="reiserfsck -y $deviceResize"
	elif [ "$FSTYPE" = "ext2" ];then
		Echo "Resize EXT2 filesystem to full partition space..."
		resize_fs="resize2fs -f -F -p $deviceResize"
		check="e2fsck -p $deviceResize"
		if [ $ramdisk -eq 1 ];then
			resize_fs="resize2fs -f $deviceResize"
		fi
	elif [ "$FSTYPE" = "ext3" ];then
		Echo "Resize EXT3 filesystem to full partition space..."
		resize_fs="resize2fs -f -F -p $deviceResize"
		check="e2fsck -p $deviceResize"
		if [ $ramdisk -eq 1 ];then
			resize_fs="resize2fs -f $deviceResize"
		fi
	elif [ "$FSTYPE" = "ext4" ];then
		Echo "Resize EXT4 filesystem to full partition space..."
		resize_fs="resize2fs -f -F -p $deviceResize"
		check="e2fsck -p $deviceResize"
		if [ $ramdisk -eq 1 ];then
			resize_fs="resize2fs -f $deviceResize"
		fi
	elif [ "$FSTYPE" = "btrfs" ];then
		Echo "Resize BTRFS filesystem to full partition space..."
		resize_fs="mount $deviceResize /mnt &&"
		resize_fs="$resize_fs btrfsctl -r max /mnt;umount /mnt"
		check="btrfsck $deviceResize"
	else
		# don't know how to resize this filesystem
		return
	fi
	if [ -z "$callme" ];then
		if [ $ramdisk -eq 0 ];then
			eval $resize_lucks
			eval $resize_fs
		else
			eval $resize_lucks
			eval $resize_fs
		fi
		if [ ! $? = 0 ];then
			systemException \
				"Failed to resize/check filesystem" \
			"reboot"
		fi
		if [ $ramdisk -eq 0 ];then
			$check
		fi
		INITRD_MODULES="$INITRD_MODULES $FSTYPE"
	else
		echo $resize_fs
	fi
}
#======================================
# resetMountCounter
#--------------------------------------
function resetMountCounter {
	local curtype=$FSTYPE
	local command
	for device in \
		$imageRootDevice $imageBootDevice \
		$imageRecoveryDevice $imageHomeDevice
	do
		if [ ! -e $device ];then
			continue
		fi
		probeFileSystem $device
		if [ "$FSTYPE" = "ext2" ];then
			command="tune2fs -c -1 -i 0"
		elif [ "$FSTYPE" = "ext3" ];then
			command="tune2fs -c -1 -i 0"
		elif [ "$FSTYPE" = "ext4" ];then
			command="tune2fs -c -1 -i 0"
		else
			# nothing to do here...
			continue
		fi
		eval $command $device 1>&2
	done
	FSTYPE=$curtype
}
#======================================
# createFilesystem
#--------------------------------------
function createFilesystem {
	local deviceCreate=$1
	local blocks=$2
	if [ "$FSTYPE" = "reiserfs" ];then
		mkreiserfs -f $deviceCreate $blocks 1>&2
	elif [ "$FSTYPE" = "ext2" ];then
		mke2fs -T ext2 -F $deviceCreate $blocks 1>&2
	elif [ "$FSTYPE" = "ext3" ];then
		mke2fs -T ext3 -j -F $deviceCreate $blocks 1>&2
	elif [ "$FSTYPE" = "ext4" ];then
		mke2fs -T ext4 -j -F $deviceCreate $blocks 1>&2
	elif [ "$FSTYPE" = "btrfs" ];then
		if [ ! -z "$blocks" ];then
			local bytes=$((blocks * 4096))
			mkfs.btrfs -b $bytes $deviceCreate
		else
			mkfs.btrfs $deviceCreate
		fi
	else
		# use ext3 by default
		mke2fs -T ext3 -j -F $deviceCreate $blocks 1>&2
	fi
	if [ ! $? = 0 ];then
		systemException \
			"Failed to create filesystem" \
		"reboot"
	fi
}
#======================================
# restoreLVMMetadata
#--------------------------------------
function restoreLVMPhysicalVolumes {
	# /.../
	# restore the pysical volumes by the given restore file
	# created from vgcfgbackup. It's important to create them
	# with the same uuid's compared to the restore file
	# ----
	local restorefile=$1
	cat $restorefile | grep -A2 -E 'pv[0-9] {' | while read line;do
		if [ -z "$uuid" ];then
			uuid=$(echo $line | grep 'id =' |\
				cut -f2 -d= | tr -d \")
		fi
		if [ -z "$pdev" ];then
			pdev=$(echo $line|grep 'device =' |\
				cut -f2 -d\" | cut -f1 -d\")
		fi
		if [ ! -z "$pdev" ];then
			pvcreate -u $uuid $pdev
			unset uuid
			unset pdev
		fi
	done
}
#======================================
# pxeSizeToMB
#--------------------------------------
function pxeSizeToMB {
	local size=$1
	if [ "$size" = "x" ];then
		echo . ; return
	fi
	local lastc=$(echo $size | sed -e 's@\(^.*\)\(.$\)@\2@')
	local value=$(echo $size | sed -e 's@\(^.*\)\(.$\)@\1@')
	if [ "$lastc" = "m" ] || [ "$lastc" = "M" ];then
		size=$value
	elif [ "$lastc" = "g" ] || [ "$lastc" = "G" ];then
		size=$(($value * 1024))
	fi
	echo +"$size"M
}
#======================================
# pxePartitionInput
#--------------------------------------
function pxePartitionInput {
	local field=0
	local count=0
	local IFS=","
	for i in $PART;do
		field=0
		count=$((count + 1))
		IFS=";" ; for n in $i;do
		case $field in
			0) partSize=$n   ; field=1 ;;
			1) partID=$n     ; field=2 ;;
			2) partMount=$n;
		esac
		done
		partSize=$(pxeSizeToMB $partSize)
		if [ $partID = "S" ];then
			partID=82
		fi
		if [ $partID = "L" ];then
			partID=83
		fi
		if [ $partID = "V" ];then
			partID=8e
		fi
		if [ $count -eq 1 ];then
			echo -n "n p $count 1 $partSize "
		else
			echo -n "n p $count . $partSize "
		fi
		if [ $partID = "82" ] || [ $partID = "8e" ];then
			echo -n "t $count $partID "
		fi
	done
	echo "w q"
}
#======================================
# pxeSwapDevice
#--------------------------------------
function pxeSwapDevice {
	local field=0
	local count=0
	local device
	local IFS=","
	for i in $PART;do
		field=0
		count=$((count + 1))
		IFS=";" ; for n in $i;do
		case $field in
			0) partSize=$n   ; field=1 ;;
			1) partID=$n     ; field=2 ;;
			2) partMount=$n;
		esac
		done
		if test $partID = "82" -o $partID = "S";then
			device=$(ddn $DISK $count)
			waitForStorageDevice $device
			echo $device
			return
		fi
	done
}
#======================================
# startUtimer
#--------------------------------------
function startUtimer {
	local utimer=/usr/bin/utimer
	if [ ! -x $utimer ];then
		utimer=/utimer
	fi
	if [ -x $utimer ];then
		if [ ! -e /tmp/utimer ];then
			ln -s $UTIMER_INFO /tmp/utimer
		fi
		$utimer
		export UTIMER=$(cat /var/run/utimer.pid)
		if [ -f /iprocs ];then
			cat /iprocs | grep -v UTIMER_PID > /iprocs
		fi
		echo UTIMER_PID=$UTIMER >> /iprocs
	fi
}
#======================================
# setupBootPartition
#--------------------------------------
function setupBootPartition {
	local pSearch=83
	local mpoint
	if [ "$haveLVM" = "yes" ];then
		#======================================
		# lvmboot
		#--------------------------------------
		test -z "$bootid" && export bootid=2
		mpoint=lvmboot
	elif [ "$haveBtrFS" = "yes" ];then
		#======================================
		# btrboot
		#--------------------------------------
		test -z "$bootid" && export bootid=2
		mpoint=btrboot
	elif [ "$haveClicFS" = "yes" ];then
		#======================================
		# clicboot
		#--------------------------------------
		test -z "$bootid" && export bootid=3
		mpoint=clicboot
	elif \
		[ "$loader" = "syslinux" ] || \
		[ "$loader" = "extlinux" ] || \
		[ "$haveLuks" = "yes" ]
	then
		#======================================
		# syslboot / luksboot
		#--------------------------------------
		if [ -z "$bootid" ];then
			test "$loader" = "syslinux" && pSearch=6
			for i in 4 3 2;do
				pType=$(partitionID $imageDiskDevice $i)
				if [ "$pType" = $pSearch ];then
					export bootid=$i
					break
				fi
			done
		fi
		if [ "$haveLuks" = "yes" ];then
			mpoint=luksboot
		else
			mpoint=syslboot
		fi
	else
		#======================================
		# no separate boot partition
		#--------------------------------------
		if [ -z "$bootid" ];then
			export bootid=1
		fi
		return
	fi
	if [ -z "$imageBootDevice" ];then
		export imageBootDevice=$(ddn $imageDiskDevice $bootid)
	fi
	mkdir -p /mnt/$mpoint
	mount $imageBootDevice /mnt/$mpoint
	rm -fr /mnt/$mpoint/*
	cp -a /mnt/boot /mnt/$mpoint
	if [ -e /boot.tgz ];then
		tar -xf /boot.tgz -C /mnt/$mpoint
	fi
	rm -rf /mnt/boot
	mkdir  /mnt/boot
	mount --bind \
		/mnt/$mpoint/boot /mnt/boot
}
#======================================
# setupConsole
#--------------------------------------
function setupConsole {
	# /.../
	# setup the xvc and/or hvc console if the device is present
	# ----
	local itab=/etc/inittab
	local stty=/etc/securetty
	if [ -e /sys/class/tty/xvc0 ];then
		echo "X0:12345:respawn:/sbin/mingetty --noclear xvc0 linux" >> $itab
		echo xvc0 >> $stty
	fi
	if [ -e /sys/class/tty/hvc0 ];then
		echo "H0:12345:respawn:/sbin/mingetty --noclear hvc0 linux" >> $itab
		echo hvc0 >> $stty
	fi
}
#======================================
# initialize
#--------------------------------------
function initialize {
	#======================================
	# Check for hotfix kernel
	#--------------------------------------
	reloadKernel
	#======================================
	# Prevent blank screen
	#--------------------------------------
	if [ -x /usr/bin/setterm ];then
		setterm -powersave off -blank 0
	fi
	#======================================
	# Start boot timer (first stage)
	#--------------------------------------
	startUtimer
}

# vim: set noexpandtab: