/contrib/dialog/samples/install/setup.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 371 lines · 310 code · 38 blank · 23 comment · 56 complexity · 306aad91b3e079b5138fb4370c3f3947 MD5 · raw file

  1. /* Copyright (C) 1995 Florian La Roche */
  2. /* Who wants to help coding? I don't like doing this... */
  3. /* You can just start setup as normal user and see how far it is coded
  4. right now. This will do a fake installation and won't actually chnage
  5. any data on your computer. */
  6. /* TODO: write a good package selection code
  7. change functions to return better error code
  8. */
  9. /* Show an extra text-box with the contents of all external commands,
  10. before they are executed. So you can abort the installation, if any
  11. wrong commands are to be executed. (So don't format wrong partition.) */
  12. #define VERBOSE 1
  13. /* If defined, don't actually execute any comands and don't actually modify
  14. any files. So you can test any possible installation without doing any
  15. damage to your computer.
  16. The file FDISK.TEST is used instead of real "fdisk -l" output, so that
  17. it can be started as normal user. */
  18. #define DEBUG_THIS 1
  19. #include <dialog.h>
  20. /* max length of a partition name like e.g. '/dev/hda1' */
  21. #define MAX_DEV_NAME 25
  22. /* max number of possible Linux/Swap/MsDos partitions */
  23. #define MAX_PARTS 20
  24. char *progname = NULL;
  25. static void
  26. error(const char *s)
  27. {
  28. fprintf(stderr, "%s: %s\n", progname, s);
  29. exit(1);
  30. }
  31. static int
  32. my_system(const char *s,...)
  33. {
  34. int ret, i;
  35. va_list ap;
  36. char sh[200];
  37. va_start(ap, s);
  38. vsprintf(sh, s, ap);
  39. va_end(ap);
  40. #ifdef VERBOSE
  41. i = dialog_msgbox("I will run the following command:", sh, 10, 65, 1);
  42. dialog_clear();
  43. #ifdef DEBUG_THIS
  44. return 0;
  45. #endif
  46. #endif
  47. ret = system(sh);
  48. if (!(ret >> 8))
  49. return 0;
  50. i = dialog_msgbox("Error-Exit on the following command:",
  51. sh, 12, 73, 1);
  52. dialog_clear();
  53. return 1;
  54. }
  55. /* We support to install from DOS/Linux-partitions. */
  56. enum partition_type {
  57. MsDos,
  58. Linux,
  59. Swap
  60. };
  61. struct partition {
  62. enum partition_type type;
  63. char name[MAX_DEV_NAME];
  64. int blocks;
  65. int flag;
  66. } partitions[MAX_PARTS];
  67. int num_partition = 0;
  68. int num_linux = 0;
  69. int num_swap = 0;
  70. int num_msdos = 0;
  71. static int
  72. get_line(char *line, int size, FILE * f)
  73. {
  74. char *ptr = line;
  75. int c;
  76. if (feof(f))
  77. return -1;
  78. while (size-- && ((c = getc(f)) != EOF) && (c != '\n'))
  79. *ptr++ = c;
  80. *ptr++ = '\0';
  81. return (int) (ptr - line);
  82. }
  83. static void
  84. read_partitions(void)
  85. {
  86. FILE *f;
  87. char line[200];
  88. int length;
  89. #ifndef DEBUG_THIS
  90. int ret = system("fdisk -l 2>/dev/null 1>/tmp/fdisk.output");
  91. if ((ret >> 8) != 0) {
  92. error("fdisk didn't run");
  93. }
  94. if ((f = fopen("/tmp/fdisk.output", "r")) == NULL)
  95. #else
  96. if ((f = fopen("FDISK.TEST", "r")) == NULL)
  97. #endif
  98. error("cannot read fdisk output");
  99. while (num_partition <= MAX_PARTS
  100. && (length = get_line(line, 200, f)) >= 0) {
  101. if (strncmp(line, "/dev/", 5) == 0) {
  102. int n = 0;
  103. char *s = line + 5;
  104. char *t = partitions[num_partition].name;
  105. strcpy(t, "/dev/");
  106. t += 5;
  107. while (n < MAX_DEV_NAME && *s != '\0'
  108. && !isspace((unsigned char) *s)) {
  109. *t++ = *s++;
  110. n++;
  111. }
  112. *t = '\0';
  113. /* Read the size of the partition. */
  114. t = line + 37;
  115. while (isspace((unsigned char) *t))
  116. t++;
  117. partitions[num_partition].blocks = atoi(t);
  118. if (strstr(line, "Linux native")) {
  119. partitions[num_partition].type = Linux;
  120. num_partition++;
  121. num_linux++;
  122. } else if (strstr(line, "Linux swap")) {
  123. partitions[num_partition].type = Swap;
  124. num_partition++;
  125. num_swap++;
  126. } else if (strstr(line, "DOS")) {
  127. partitions[num_partition].type = MsDos;
  128. num_partition++;
  129. num_msdos++;
  130. }
  131. }
  132. }
  133. fclose(f);
  134. #ifndef DEBUG_THIS
  135. unlink("/tmp/fdisk.output");
  136. #endif
  137. }
  138. static int
  139. select_partition(const char *title, const char *prompt, int y, int x)
  140. {
  141. int i, num, ret;
  142. char info[MAX_PARTS][40];
  143. char *items[MAX_PARTS * 2];
  144. int num_pa[MAX_PARTS];
  145. num = 0;
  146. for (i = 0; i < num_partition; i++) {
  147. if (partitions[i].type == Linux) {
  148. items[num * 2] = partitions[i].name;
  149. sprintf(info[num], "Linux partition with %d blocks",
  150. partitions[i].blocks);
  151. items[num * 2 + 1] = info[num];
  152. num_pa[num] = i;
  153. num++;
  154. }
  155. }
  156. ret = dialog_menu(title, prompt, y + num, x, num, num, items);
  157. dialog_clear();
  158. if (ret >= 0) /* item selected */
  159. ret = num_pa[ret];
  160. return ret;
  161. }
  162. static int
  163. select_install_partition(void)
  164. {
  165. return select_partition("Select Install Partition",
  166. "\\nWhere do you want to install Linux?\\n", 9, 60);
  167. }
  168. static int
  169. select_source_partition(void)
  170. {
  171. return select_partition("Select Source Partition",
  172. "\\nOn which partition is the source?\\n", 9, 60);
  173. }
  174. const char *null = ">/dev/null 2>/dev/null";
  175. const char *install_partition = NULL;
  176. static void
  177. extract_packages(const char *source_path)
  178. {
  179. #ifndef DEBUG_THIS
  180. FILE *f;
  181. #endif
  182. if (my_system("mkdir -p /install/var/installed/packages %s", null))
  183. return;
  184. if (my_system("cd /install; for i in /source%s/*.tgz; do "
  185. "tar xzplvvkf $i >> var/installed/packages/base "
  186. "2>>var/installed/packages/ERROR; done", source_path))
  187. return;
  188. #ifndef DEBUG_THIS
  189. if ((f = fopen("/install/etc/fstab", "w")) == NULL) {
  190. /* i = */ dialog_msgbox("Error", "Cannot write /etc/fstab",
  191. 12, 40, 1);
  192. return;
  193. }
  194. fprintf(f, "%s / ext2 defaults 1 1\n", install_partition);
  195. fprintf(f, "none /proc proc defaults 0 2\n");
  196. /* XXX write swap-partitions */
  197. fclose(f);
  198. #endif
  199. }
  200. static void
  201. install_premounted(void)
  202. {
  203. extract_packages("");
  204. }
  205. static void
  206. install_harddisk(void)
  207. {
  208. const char *name;
  209. int part, ret;
  210. if ((part = select_source_partition()) <= -1)
  211. return;
  212. name = partitions[part].name;
  213. if (my_system("mount -t ext2 %s /source %s", name, null))
  214. return;
  215. ret = dialog_inputbox("Path in partition",
  216. "Please enter the directory in which the "
  217. "source files are.", 13, 50, "", FALSE);
  218. dialog_clear();
  219. if (ret != 0)
  220. return;
  221. /* XXX strdup */
  222. extract_packages(strdup(dialog_input_result));
  223. if (my_system("umount /source %s", null))
  224. return;
  225. }
  226. static void
  227. install_nfs(void)
  228. {
  229. if (my_system("ifconfig eth0 134.96.81.36 netmask 255.255.255.224 "
  230. "broadcast 134.96.81.63 %s", null))
  231. return;
  232. if (my_system("route add -net 134.96.81.32 %s", null))
  233. return;
  234. if (my_system("mount -t nfs 134.96.81.38:"
  235. "/local/ftp/pub/linux/ELF.binary/tar /source %s", null))
  236. return;
  237. extract_packages("/base");
  238. if (my_system("umount /source %s", null))
  239. return;
  240. if (my_system("ifconfig eth0 down %s", null))
  241. return;
  242. }
  243. static void
  244. main_install(void)
  245. {
  246. int part, ret;
  247. const char *name;
  248. char *items1[] =
  249. {
  250. "1", "Harddisk Install",
  251. "2", "Network Install(NFS)",
  252. "3", "Premounted on /source"
  253. };
  254. if (num_linux == 0) {
  255. /* XXX */
  256. return;
  257. }
  258. if ((part = select_install_partition()) <= -1)
  259. return;
  260. install_partition = name = partitions[part].name;
  261. if (my_system("mke2fs %s %s", name, null))
  262. return;
  263. if (my_system("mount -t ext2 %s /install %s", name, null))
  264. return;
  265. ret = dialog_menu("Choose install medium",
  266. "\\nPlease say from where you want to install.\\n",
  267. 12, 62, 3, 3, items1);
  268. dialog_clear();
  269. switch (ret) {
  270. case 0:
  271. install_harddisk();
  272. break;
  273. case 1:
  274. install_nfs();
  275. break;
  276. case 2:
  277. install_premounted();
  278. break;
  279. case -2: /* cancel */
  280. case -1:
  281. break; /* esc */
  282. }
  283. if (my_system("umount /install %s", null))
  284. return;
  285. }
  286. int
  287. main(int argc, char **argv)
  288. {
  289. int stop = 0;
  290. int ret;
  291. char *items1[] =
  292. {
  293. "1", "Display a help text",
  294. "2", "Start an installation",
  295. "3", "Exit to the shell"
  296. };
  297. progname = argv[0];
  298. read_partitions();
  299. if (num_linux == 0) {
  300. printf("\n\nPlease start \"fdisk\" or \"cfdisk\" and create a"
  301. "\nnative Linux-partition to install Linux on.\n\n");
  302. exit(1);
  303. }
  304. init_dialog();
  305. while (!stop) {
  306. ret = dialog_menu("Linux Install Utility",
  307. "\\nCopyright (C) 1995 Florian La Roche\\n"
  308. "\\nPre-Alpha version, be careful, read the doc!!!"
  309. "\\nemail: florian@jurix.jura.uni-sb.de, "
  310. "flla@stud.uni-sb.de\\n",
  311. 15, 64, 3, 3, items1);
  312. dialog_clear();
  313. switch (ret) {
  314. case 0:
  315. ret = dialog_textbox("Help Text",
  316. "setup.help", 20, 70);
  317. dialog_clear();
  318. break;
  319. case 1:
  320. main_install();
  321. break;
  322. case 2:
  323. stop = 1;
  324. break;
  325. case -2: /* cancel */
  326. case -1:
  327. stop = 1; /* esc */
  328. }
  329. }
  330. end_dialog();
  331. printf("\nExecute \"reboot\" to restart your computer...\n");
  332. exit(0);
  333. }