Fixed a bug which caused docker to crash at startup while restoring existing containers

This commit is contained in:
Solomon Hykes 2013-03-09 19:49:09 -08:00
parent c59fff422f
commit 4474cd5677
3 changed files with 14 additions and 7 deletions

View File

@ -100,17 +100,25 @@ func createContainer(id string, root string, command string, args []string, imag
return container, nil return container, nil
} }
func loadContainer(containerPath string) (*Container, error) { func loadContainer(store *fs.Store, containerPath string) (*Container, error) {
data, err := ioutil.ReadFile(path.Join(containerPath, "config.json")) data, err := ioutil.ReadFile(path.Join(containerPath, "config.json"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
mountpoint, err := store.FetchMountpoint(
path.Join(containerPath, "rootfs"),
path.Join(containerPath, "rw"),
)
if err != nil {
return nil, err
}
container := &Container{ container := &Container{
stdout: newWriteBroadcaster(), stdout: newWriteBroadcaster(),
stderr: newWriteBroadcaster(), stderr: newWriteBroadcaster(),
stdoutLog: new(bytes.Buffer), stdoutLog: new(bytes.Buffer),
stderrLog: new(bytes.Buffer), stderrLog: new(bytes.Buffer),
lxcConfigPath: path.Join(containerPath, "config.lxc"), lxcConfigPath: path.Join(containerPath, "config.lxc"),
Mountpoint: mountpoint,
} }
if err := json.Unmarshal(data, container); err != nil { if err := json.Unmarshal(data, container); err != nil {
return nil, err return nil, err

View File

@ -92,12 +92,11 @@ func (docker *Docker) restore() error {
return err return err
} }
for _, v := range dir { for _, v := range dir {
container, err := loadContainer(path.Join(docker.repository, v.Name())) container, err := loadContainer(docker.Store, path.Join(docker.repository, v.Name()))
if err != nil { if err != nil {
log.Printf("Failed to load container %v: %v", v.Name(), err) log.Printf("Failed to load container %v: %v", v.Name(), err)
continue continue
} }
container.Mountpoint.Store = docker.Store
docker.containers.PushBack(container) docker.containers.PushBack(container)
} }
return nil return nil

View File

@ -227,7 +227,7 @@ func (image *Image) Mountpoints() ([]*Mountpoint, error) {
func (image *Image) Mount(root, rw string) (*Mountpoint, error) { func (image *Image) Mount(root, rw string) (*Mountpoint, error) {
var mountpoint *Mountpoint var mountpoint *Mountpoint
if mp, err := image.fetchMountpoint(root, rw); err != nil { if mp, err := image.store.FetchMountpoint(root, rw); err != nil {
return nil, err return nil, err
} else if mp == nil { } else if mp == nil {
mountpoint, err = image.Mountpoint(root, rw) mountpoint, err = image.Mountpoint(root, rw)
@ -345,8 +345,8 @@ func (mp *Mountpoint) Deregister() error {
return err return err
} }
func (image *Image) fetchMountpoint(root, rw string) (*Mountpoint, error) { func (store *Store) FetchMountpoint(root, rw string) (*Mountpoint, error) {
res, err := image.store.orm.Select(Mountpoint{}, "select * from mountpoints where Image=? and Root=? and Rw=?", image.Id, root, rw) res, err := store.orm.Select(Mountpoint{}, "select * from mountpoints where Root=? and Rw=?", root, rw)
if err != nil { if err != nil {
return nil, err return nil, err
} else if len(res) < 1 || res[0] == nil { } else if len(res) < 1 || res[0] == nil {
@ -354,7 +354,7 @@ func (image *Image) fetchMountpoint(root, rw string) (*Mountpoint, error) {
} }
mp := res[0].(*Mountpoint) mp := res[0].(*Mountpoint)
mp.Store = image.store mp.Store = store
return mp, nil return mp, nil
} }