Statistics
| Branch: | Revision:

root / common_linux.sh @ f59332af

History | View | Annotate | Download (6 kB)

1

    
2
get_os_type() {
3
    target=$1
4
    if [ -z "${target}" ] ; then
5
        log_error "target is not set in get_os_type"
6
        exit 1
7
    fi
8
    if [ -e ${target}/etc/redhat-release ] ; then
9
        OS_TYPE="redhat"
10
    elif [ -e ${target}/etc/debian_version ] ; then
11
        OS_TYPE="debian"
12
    elif [ -e ${target}/etc/gentoo-release ] ; then
13
        OS_TYPE="gentoo"
14
    elif [ -e ${target}/etc/SuSE-release ] ; then
15
        OS_TYPE="suse"
16
    fi
17
}
18

    
19
get_os() {
20
    target=$1
21
    if [ -z "${target}" ] ; then
22
        log_error "target is not set in get_os"
23
        exit 1
24
    fi
25
    if [ -e ${target}/etc/debian_version ] ; then
26
        if [ -e ${target}/etc/lsb-release ] ; then
27
            ID=$(grep ^DISTRIB_ID= ${target}/etc/lsb-release | cut -d= -f2)
28
        fi
29
        if [ "a$ID" = "aUbuntu" ] ; then
30
            OPERATING_SYSTEM="ubuntu"
31
        else
32
            OPERATING_SYSTEM="debian"
33
        fi
34
    elif [ -e ${target}/etc/gentoo-release ] ; then
35
        OPERATING_SYSTEM="gentoo"
36
    elif [ -e ${target}/etc/fedora-release ] ; then
37
        OPERATING_SYSTEM="fedora"
38
    elif [ -e ${target}/etc/redhat-release ] ; then
39
        if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
40
            OPERATING_SYSTEM="centos"
41
        else
42
            OPERATING_SYSTEM="redhat"
43
        fi
44
    fi
45
}
46

    
47
get_os_release() {
48
    target=$1
49
    if [ -z "${target}" ] ; then
50
        log_error "target is not set in get_os_release"
51
        exit 1
52
    fi
53
    if [ -e ${target}/etc/debian_version ] ; then
54
        OS_RELEASE="$(cat ${target}/etc/debian_version)"
55
    elif [ -e ${target}/etc/fedora-release ] ; then
56
        OS_RELEASE="$(cat ${target}/etc/fedora-release | awk '{print $3}')"
57
    elif [ -e ${$target}/etc/redhat-release ] ; then
58
        OS_RELEASE="$(cat ${target}/etc/redhat-release | awk '{print $3}')"
59
    fi
60
}
61

    
62
linux_format_disk0() {
63
    local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux --DOS $1"
64
    if [  "${SWAP}" = "yes" -a "${BOOT}" = "yes" ] ; then
65
        # Create three partitions:
66
        # 1 - 100MB /boot, bootable
67
        # 2 - Size of Memory, swap
68
        # 3 - Rest
69
        $sfdisk_cmd > /dev/null <<EOF
70
,100,L,*
71
,${SWAP_SIZE},S
72
,,L
73
EOF
74
    elif [  "${SWAP}" = "no" -a "${BOOT}" = "yes" ] ; then
75
        # Create two partitions:
76
        # 1 - 100MB /boot, bootable
77
        # 2 - Rest
78
        $sfdisk_cmd > /dev/null <<EOF
79
,100,L,*
80
,,L
81
EOF
82
    elif [  "${SWAP}" = "yes" -a "${BOOT}" = "no" ] ; then
83
        # Create two partitions:
84
        # 1 - Size of Memory, swap
85
        # 2 - Rest
86
        $sfdisk_cmd > /dev/null <<EOF
87
,$SWAP_SIZE,S
88
,,L
89
EOF
90
    elif [  "${SWAP}" = "no" -a "${BOOT}" = "no" ] ; then
91
        # Create two partitions:
92
        # 1 - Whole
93
        $sfdisk_cmd > /dev/null <<EOF
94
,,L
95
EOF
96
    fi
97
}
98

    
99
linux_mkfs_disk0() {
100
    local mkfs="mkfs.${FILESYSTEM}"
101
   # Format /
102
    $mkfs -Fq -L / $root_dev > /dev/null
103
    # Format /boot
104
    if [ -n "${boot_dev}" ] ; then
105
        $mkfs -Fq -L /boot $boot_dev > /dev/null
106
    fi
107
    # Format swap
108
    if [ -n "${swap_dev}" ] ; then
109
        # Format swap
110
        mkswap -f $swap_dev > /dev/null
111
    fi
112
    # During reinstalls, ext4 needs a little time after a mkfs so add it here
113
    # and also run a sync to be sure.
114
    sync
115
    sleep 2
116
}
117

    
118
linux_setup_fstab() {
119
    local target=$1 fs=${FILESYSTEM}
120
    get_os_type $target
121
    cat > $target/etc/fstab <<EOF
122
# /etc/fstab: static file system information.
123
#
124
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
125
UUID=$root_uuid   /               $fs     defaults        0       1
126
proc              /proc           proc    defaults        0       0
127
EOF
128

    
129
if [ -n "$boot_dev" -a -n "$boot_uuid" ] ; then
130
    cat >> $target/etc/fstab <<EOF
131
UUID=$boot_uuid   /boot           $fs     defaults        1       2
132
EOF
133
fi
134

    
135
if [ -n "$swap_dev" -a -n "$swap_uuid" ] ; then
136
    cat >> $target/etc/fstab <<EOF
137
UUID=$swap_uuid   swap            swap    defaults        0       0
138
EOF
139
fi
140

    
141
# OS Specific fstabs
142
if [ "$OS_TYPE" = "redhat" ] ; then
143
    cat >> $target/etc/fstab <<EOF
144
tmpfs             /dev/shm        tmpfs   defaults        0       0
145
devpts            /dev/pts        devpts  gid=5,mode=620  0       0
146
sysfs             /sys            sysfs   defaults        0       0
147
EOF
148
fi
149

    
150
if [ "$OS_TYPE" = "gentoo" ] ; then
151
    cat >> $target/etc/fstab <<EOF
152
shm               /dev/shm        tmpfs   nodev,nosuid,noexec 0   0
153
EOF
154
fi
155
}
156

    
157
linux_setup_console() {
158
    local target=$1
159
    if [ -z "$target" ] ; then
160
        log_error "target not set for setup_console"
161
        exit 1
162
    fi
163
    # Upstart is on this system, so do this instead
164
    if [ -e ${target}/etc/event.d/tty1 ] ; then
165
        cat ${target}/etc/event.d/tty1 | sed -re 's/tty1/ttyS0/' \
166
            > ${target}/etc/event.d/ttyS0
167
        return
168
    fi
169
    # upstart in karmic and newer
170
    if [ -e ${target}/etc/init/tty1.conf ] ; then
171
        cat ${target}/etc/init/tty1.conf | \
172
        sed -re 's/^exec.*/exec \/sbin\/getty -L 115200 ttyS0 vt102/' \
173
            > ${target}/etc/init/ttyS0.conf
174
        sed -ie 's/tty1/ttyS0/g' ${target}/etc/init/ttyS0.conf
175
        return
176
    fi
177
    get_os $target
178
    case $OPERATING_SYSTEM in
179
        gentoo)
180
            sed -i -e 's/.*ttyS0.*/s0:12345:respawn:\/sbin\/agetty 115200 ttyS0 vt100/' \
181
                ${target}/etc/inittab
182
            ;;
183
        centos)
184
            echo "s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100" >> \
185
                ${target}/etc/inittab
186
            ;;
187
        debian|ubuntu)
188
            sed -i -e 's/.*T0.*/T0:23:respawn:\/sbin\/getty -L ttyS0 115200 vt100/' \
189
                ${target}/etc/inittab
190
            ;;
191
        *)
192
            echo "No support for your OS in instance-image, skipping..."
193
            ;;
194
    esac
195
}
196

    
197
linux_epilogue() {
198
    local target=$1
199
    if [ -z "$target" ] ; then
200
        log_error "target not set"
201
        exit 1
202
    fi
203

    
204
    get_os_type $target
205

    
206
    if [ "${OS_TYPE}" = "redhat" ]; then
207
        # we have to force a filesystem relabeling for SELinux after messing
208
        # around with the filesystem in redhat derived OSs
209
        echo "Enforce an automatic relabeling in the initial boot process..."
210
        touch $target/.autorelabel
211
    fi
212
}
213