1#!/usr/bin/expect
2
3###############################################################################
4# fsremount
5#
6# A script for mounting a device's root filesystem read-only or read-write.
7#
8# Andrew Myrick
9# Copyright © 2012 Apple Inc. All rights reserved.
10###############################################################################
11
12proc usage {progname} {
13	send_user "Usage: $progname <hostname> <port> <root password> \[ro|rw\]\n";
14	exit 1
15}
16
17# Set core variables
18
19set timeout 10
20set progname $argv0
21set prompt ":~ root#"
22
23# Parse arguments
24
25if {$argc != 4} {
26	usage $progname
27}
28
29set hostname [lindex $argv 0]
30set port [lindex $argv 1]
31set pass [lindex $argv 2]
32set mountopt [lindex $argv 3]
33
34if {$mountopt == "ro"} {
35	set mountflag "-ur"
36} elseif  {$mountopt == "rw"} {
37	set mountflag "-uw"
38} else {
39	send_user "Error: invalid mount option\n";
40	usage $progname
41}
42
43# Log in to the device
44
45spawn telnet $hostname $port
46expect {
47	"telnet: Unable to connect to remote host" { exit 1 }
48	"login:"
49}
50send "root\r";
51expect "Password:"
52send "$pass\r";
53expect {
54	"Login incorrect" { exit 1 }
55	$prompt
56}
57
58# If we're going to mount read-write, check to see if the root filesystem is
59# already mounted read-write. If so, exit with a special code to notify our
60# parent.
61if {$mountopt == "rw"} {
62	send "mount | grep \"disk0s1s1.*read-only\"\r";
63	expect "$prompt"
64	send "echo $?\r";
65	expect {
66		"\r\n1\r\n" { exit 128 }
67		"\r\n0\r\n"
68	}
69}
70
71# Remount the filesystem as requested
72send "mount $mountflag /\r";
73expect "$prompt"
74send "echo $?\r"
75expect {
76	"\r\n1\r\n" { exit 1 }
77	"\r\n0\r\n"
78}
79
80# Exit cleanly
81expect "$prompt"
82send "exit\r"
83expect "Connection closed by foreign host."
84