3 # http://www.logicsupply.com/blog/2009/01/27/how-to-build-a-read-only-linux-system/
22 root_directory
="${rootmnt}"
23 mount
"${root_directory}" -o remount
,ro
25 CMDLINE
="$(cat /proc/cmdline)"
30 PARAM
="`echo $x | sed -e 's|^aufs=||g'`"
34 IP
=`ifconfig | awk '/dr:/{gsub(/.*:/,"",$2);print$2}'`
36 if [ "$PARAM" = "tmpfs" ]; then
37 ro_mount_point
="${root_directory%/}.ro"
38 rw_mount_point
="${root_directory%/}.rw"
40 # Create mount points for the read-only and read/write layers:
41 mkdir
"${ro_mount_point}" "${rw_mount_point}"
43 # Move the already-mounted root filesystem to the ro mount point:
44 mount
--move "${root_directory}" "${ro_mount_point}"
46 # Mount the read/write filesystem:
47 mount
-t tmpfs root.rw
"${rw_mount_point}"
50 mount
-t aufs
-o "dirs=${rw_mount_point}=rw:${ro_mount_point}=ro" root.union "${root_directory}"
53 mkdir
-p "${root_directory}/tmp"
54 mount tmpfs
-t tmpfs
"${root_directory}/tmp"
56 # Correct the permissions of /:
57 chmod 755 "${root_directory}"
59 # Set hostname according to IP
60 if test ! -z "$IP"; then
61 printf "ws-aula-%03d" $
(echo $IP | cut
-d .
-f 4) > "${root_directory}/etc/hostname"
64 # Make sure the individual ro and rw mounts are accessible from within the root
65 # once the union is assumed as /. This makes it possible to access the
66 # component filesystems individually.
67 mkdir
"${root_directory}/.ro" "${root_directory}/.rw"
68 mount
--bind "${ro_mount_point}" "${root_directory}/.ro"
69 mount
--bind "${rw_mount_point}" "${root_directory}/.rw"
74 if [ "$PARAM" = "tmpfs+nfs" ]; then
75 ro_mount_point
="${root_directory%/}.ro"
76 rw_mount_point
="${root_directory%/}.rw"
78 # Create mount points for the read-only and read/write layers:
79 mkdir
"${ro_mount_point}" "${rw_mount_point}"
81 # Move the already-mounted root filesystem to the ro mount point:
82 mount
--move "${root_directory}" "${ro_mount_point}"
84 # Mount the read/write filesystem:
85 mount
-t tmpfs root.rw
"${rw_mount_point}"
88 mount
-t aufs
-o "dirs=${rw_mount_point}=rw:${ro_mount_point}=ro" root.union "${root_directory}"
91 mkdir
-p "${root_directory}/tmp"
92 mount tmpfs
-t tmpfs
"${root_directory}/tmp"
94 # Correct the permissions of /:
95 chmod 755 "${root_directory}"
97 # Set hostname according to IP
98 if test ! -z "$IP"; then
99 printf "ws-aula-%03d" $
(echo $IP | cut
-d .
-f 4) > "${root_directory}/etc/hostname"
102 # Make sure the individual ro and rw mounts are accessible from within the root
103 # once the union is assumed as /. This makes it possible to access the
104 # component filesystems individually.
105 mkdir
"${root_directory}/.ro" "${root_directory}/.rw"
106 mount
--bind "${ro_mount_point}" "${root_directory}/.ro"
107 mount
--bind "${rw_mount_point}" "${root_directory}/.rw"
109 # Mount NFS partition
110 nfs_mount_point
="${root_directory%/}.nas"
111 mkdir
-p "${nfs_mount_point}"
112 chmod 755 "${nfs_mount_point}"
113 nfsmount
-o nolock
-o rw
-o retrans
=10 "${ROOTSERVER}:/data/workstations" "${nfs_mount_point}"
114 while test -z "$(mount | grep -E '/data/workstations on [^ ]* type nfs')"; do
116 echo "Trying to mount /data/workstations from NFS"
117 nfsmount
-o nolock
-o rw
-o retrans
=10 "${ROOTSERVER}:/data/workstations" "${nfs_mount_point}"
120 rootdir
="${root_directory}"
122 # Create private space for this computer in the NFS partition
123 MAC
=`ifconfig | grep eth | head -n 1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | sed -e 's/://g'`
124 nfs_mac_dir
="${nfs_mount_point}/${MAC}"
126 echo "Initializing disk space in the server"
127 if [ ! -z "${nfs_mac_dir}" -a -d "${nfs_mac_dir}" -a "${nfs_mac_dir}" != "/" ]; then
128 rm -rf "${nfs_mac_dir}"
130 mkdir
-p "${nfs_mac_dir}"
131 chmod 755 "${nfs_mac_dir}"
133 # Mount the binding to the read/write NFS filesystem:
134 mkdir
"${root_directory}/.nas"
135 mount
--bind "${nfs_mac_dir}" "${root_directory}/.nas"
137 # Create and link directories
139 mkdir
-p "${nfs_mac_dir}/var/log"
140 rm -rf "${root_directory}/var/log"
141 ln -s "/.nas/var/log" "${root_directory}/var/log"
143 mkdir
-p "${nfs_mac_dir}/var/tmp"
144 rm -rf "${root_directory}/var/tmp"
145 ln -s "/.nas/var/tmp" "${root_directory}/var/tmp"
147 mkdir
-p "${nfs_mac_dir}/var/lib/swapspace"
148 rm -rf "${root_directory}/var/lib/swapspace"
149 ln -s "/.nas/var/lib/swapspace" "${root_directory}/var/lib/swapspace"
151 #mkdir "${nfs_mac_dir}/var"
152 #mount -t aufs -o "xino=${root_directory}/tmp/.var.xino,dirs=${nfs_mac_dir}/var=rw:${ro_mount_point}/var=ro" root.union "${root_directory}/var"