controller (1374B)
1 #!/usr/bin/env lexpect 2 3 local function read_file(name) 4 local f, err = io.open(name, 'r') 5 if not f then 6 error(err) 7 end 8 9 local contents = f:read('*a') 10 11 f:close() 12 13 return contents 14 end 15 16 local function file_exists(name) 17 assert(name, 'expected file name path') 18 local f = io.open(name, 'r') 19 20 if f then 21 f:close() 22 return true 23 end 24 25 return false 26 end 27 28 local args = { 29 '-m', 30 '512', 31 '-nic', 32 'user', 33 '-boot', 34 'd', 35 '-cdrom', 36 assert(file_exists(arg[1]), 'expected image path to exist') and arg[1], 37 '-hda', 38 'alpine.qcow2', 39 '-nographic', 40 '-virtfs', 41 'local,path=./tmp.share,mount_tag=host0,security_model=mapped,id=host0,multidevs=remap', 42 } 43 44 if file_exists('/dev/kvm') then 45 table.insert(args, '-enable-kvm') 46 end 47 48 --- 49 50 local lexpect = require('lexpect') 51 52 local session = lexpect.spawn_args('qemu-system-x86_64', args, { 53 passthrough = true 54 }) 55 56 local function command(cmd) 57 session:exp_string('# ') 58 session:send_line(cmd) 59 end 60 61 session:exp_string('login: ') 62 session:send_line('root') 63 64 command('echo \'' .. read_file('./initializer') .. '\' > initializer') 65 command('chmod +x ./initializer') 66 command('./initializer prep') 67 command('setup-alpine -f answers -e') 68 69 session:exp_string('[n] ') 70 session:send_line('y') 71 72 command('./initializer initramfs') 73 command('./initializer teardown') 74 75 session:exp_eof()