未加星标

Ten years on, Chris Lightfoot looks more prescient than ever

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Ten years on, Chris Lightfoot looks more prescient than ever

(gif from imgur via gify)

(title shamelessly stolen from Tom Steinberg , MySociety founder from his tribute to Chris , Mythic Beasts founder who died ten years ago).

Lots of people have been excited recently about this script , which allows you to remotely reinstall a linux system with a different version of Linux by giving you a shell in a ramdisk and letting you reinstall the operating system from there.

Chris did this the hard way. Back in 2005 I remember being asked to code review ‘evil.c’, a script that allocated a lot of RAM (800MB!), compressed a filesystem into it, then uncompressed it back to the disk. On reboot it should come up with Debian instead of FreeBSD that it had earlier. It’s really very important not to swap during this process.

Amazingly it worked, and the first test was on a remote box and it saved us a data centre visit. Here’s the code in its full glory.

#include <sys/types.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <zlib.h> #include <sys/mman.h> #include <sys/reboot.h> #define SIZE ((size_t)896058269) #define E(...) fprintf(stderr, __VA_ARGS__) #define die() do { fprintf(stderr, "%s; aborted\n", strerror(errno)); exit(1); } while (0) #define zdie() do { fprintf(stderr, "%s; aborted\n", Z.msg); exit(1); } while (0) int main(void) { unsigned char *buf, *outbuf, *p; int fd; FILE *fp; z_stream Z = {0}; unsigned int nin = 0, nout = 0; E("size = %lu\n", (unsigned long)SIZE); E("open /dev/amrd0... "); if (-1 == (fd = open("/dev/amrd0", O_RDWR | O_DIRECT))) die(); E("done\n"); close(fd); E("allocate file buffer... "); if (!(buf = malloc(SIZE))) die(); E("done\n"); E("allocate write buffer... "); if (!(outbuf = malloc(1024 * 1024))) die(); E("done\n"); E("lock into memory... "); if (-1 == mlockall(MCL_CURRENT | MCL_FUTURE)) die(); E("done\n"); E("open file... "); if (!(fp = fopen("/usr/bitter-first-2100M-of-sda.gz", "rb"))) die(); E("done\n"); E("read file... "); p = buf; while (nin < SIZE) { size_t n; n = fread(p, 1, 262144, fp); if (n == 0) die(); nin += n; p += n; E("\rread file... %.2f%% ", 100 * (float)nin / (float)SIZE); } E("done\n"); fclose(fp); E("zlib version = \"%s\"\n", zlibVersion()); /* Now we need to walk through the buffer decompressing it into the * write buffer, then writing the results to the device. */ E("initialise inflate object... "); Z.next_in = buf; Z.avail_in = SIZE; if (Z_OK != inflateInit2(&Z, 15 + 32)) zdie(); E("done\n"); while (nout < 2100) { int i; size_t N; Z.next_out = outbuf; Z.avail_out = 1024 * 1024; i = inflate(&Z, 0); if (i != Z_OK && i != Z_STREAM_END) zdie(); if (Z.next_out != outbuf + 1024 * 1024) { fprintf(stderr, "\ndidn't get 1MB of output\n"); } /* this is where we'd write the data */ N = 0; p = outbuf; while (N < 1024 * 1024) { ssize_t n; do n = write(fd, p, 1024 * 1024 - N); while (n == -1 && errno == EINTR); if (n == -1) die(); N += n; p += n; } ++nout; fprintf(stderr, "\r%d / 2100 MB", nout); } fprintf(stderr, "\n"); /* this is where we reboot */ reboot(RB_NOSYNC); E("we should have rebooted by now -- probably best to assume we're completely\n" "screwed at this point\n"); return 0; }

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: LinuxDebianUTCUFU
分页:12
转载请注明
本文标题:Ten years on, Chris Lightfoot looks more prescient than ever
本站链接:http://www.codesec.net/view/533182.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(14)